QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgscolorwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscolorwidgetwrapper.cpp
3 --------------------------------------
4 Date : 5.1.2014
5 Copyright : (C) 2014 Matthias Kuhn
6 Email : matthias at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgscolorbutton.h"
19
20#include <QLayout>
21
22#include "moc_qgscolorwidgetwrapper.cpp"
23
25 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
26
27{
28}
29
30
32{
33 QColor c;
34 if ( mColorButton )
35 c = mColorButton->color();
36
37 return c.isValid() ? QVariant( c ) : QgsVariantUtils::createNullVariant( QMetaType::Type::QColor );
38}
39
41{
42 if ( mColorButton )
43 {
44 whileBlocking( mColorButton )->setColor( QColor() );
45 }
46}
47
48QWidget *QgsColorWidgetWrapper::createWidget( QWidget *parent )
49{
50 QWidget *container = new QWidget( parent );
51 QHBoxLayout *layout = new QHBoxLayout();
52 container->setLayout( layout );
53 layout->setContentsMargins( 0, 0, 0, 0 );
54 QgsColorButton *button = new QgsColorButton();
55 button->setContext( QStringLiteral( "editor" ) );
56 layout->addWidget( button );
57 layout->addStretch();
58 container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
59 return container;
60}
61
62void QgsColorWidgetWrapper::initWidget( QWidget *editor )
63{
64 mColorButton = qobject_cast<QgsColorButton *>( editor );
65 if ( !mColorButton )
66 {
67 mColorButton = editor->findChild<QgsColorButton *>();
68 }
69
70 mColorButton->setShowNull( true );
71 connect( mColorButton, &QgsColorButton::colorChanged, this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
72}
73
75{
76 return mColorButton;
77}
78
79void QgsColorWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
80{
81 if ( mColorButton )
82 mColorButton->setColor( !QgsVariantUtils::isNull( value ) ? QColor( value.toString() ) : QColor() );
83}
84
86{
87 // nothing
88}
A cross platform button subclass for selecting colors.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
void setContext(const QString &context)
Sets the context string for the color button.
void setColor(const QColor &color)
Sets the current color for the button.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
bool valid() const override
Returns true if the widget has been properly initialized.
QgsColorWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsColorWidgetWrapper.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QVariant value() const override
Will be used to access the widget's value.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
virtual void updateConstraintWidgetStatus()
This should update the widget with a visual cue if a constraint status changed.
int fieldIdx() const
Access the field index.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
void emitValueChanged()
Will call the value() method to determine the emitted value.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based dataset.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6511