QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
16 #include "qgscolorwidgetwrapper.h"
17 #include "qgscolorbutton.h"
18 #include <QLayout>
19 
20 
21 QgsColorWidgetWrapper::QgsColorWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
22  : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
23 
24 {
25 }
26 
27 
29 {
30  QColor c;
31  if ( mColorButton )
32  c = mColorButton->color();
33 
34  return c.isValid() ? QVariant( c ) : QVariant( QVariant::Color );
35 }
36 
38 {
39  if ( mColorButton )
40  {
41  whileBlocking( mColorButton )->setColor( QColor() );
42  }
43 }
44 
45 QWidget *QgsColorWidgetWrapper::createWidget( QWidget *parent )
46 {
47  QWidget *container = new QWidget( parent );
48  QHBoxLayout *layout = new QHBoxLayout();
49  container->setLayout( layout );
50  layout->setContentsMargins( 0, 0, 0, 0 );
51  QgsColorButton *button = new QgsColorButton();
52  button->setContext( QStringLiteral( "editor" ) );
53  layout->addWidget( button );
54  layout->addStretch();
55  container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
56  return container;
57 }
58 
59 void QgsColorWidgetWrapper::initWidget( QWidget *editor )
60 {
61  mColorButton = qobject_cast<QgsColorButton *>( editor );
62  if ( !mColorButton )
63  {
64  mColorButton = editor->findChild<QgsColorButton *>();
65  }
66 
67  mColorButton->setShowNull( true );
68  connect( mColorButton, &QgsColorButton::colorChanged, this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
69 }
70 
72 {
73  return mColorButton;
74 }
75 
76 void QgsColorWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
77 {
78  if ( mColorButton )
79  mColorButton->setColor( !value.isNull() ? QColor( value.toString() ) : QColor() );
80 }
81 
82 void QgsColorWidgetWrapper::updateConstraintWidgetStatus()
83 {
84  // nothing
85 }
qgscolorbutton.h
QgsColorWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgscolorwidgetwrapper.cpp:59
QgsColorButton::setColor
void setColor(const QColor &color)
Sets the current color for the button.
Definition: qgscolorbutton.cpp:646
QgsEditorWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgseditorwidgetwrapper.h:48
QgsColorButton::colorChanged
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:262
QgsColorWidgetWrapper::QgsColorWidgetWrapper
QgsColorWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsColorWidgetWrapper.
Definition: qgscolorwidgetwrapper.cpp:21
QgsColorWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgscolorwidgetwrapper.cpp:71
QgsColorButton::color
QColor color
Definition: qgscolorbutton.h:50
qgscolorwidgetwrapper.h
QgsColorWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgscolorwidgetwrapper.cpp:45
QgsEditorWidgetWrapper::emitValueChanged
void emitValueChanged()
Will call the value() method to determine the emitted value.
Definition: qgseditorwidgetwrapper.cpp:91
QgsColorButton::setShowNull
void setShowNull(bool showNull, const QString &nullString=QString())
Sets whether a set to null (clear) option is shown in the button's drop-down menu.
Definition: qgscolorbutton.cpp:828
c
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
Definition: porting_processing.dox:1
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsColorButton
A cross platform button subclass for selecting colors.
Definition: qgscolorbutton.h:36
QgsColorWidgetWrapper::showIndeterminateState
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
Definition: qgscolorwidgetwrapper.cpp:37
QgsColorWidgetWrapper::value
QVariant value() const override
Will be used to access the widget's value.
Definition: qgscolorwidgetwrapper.cpp:28
QgsColorButton::setContext
void setContext(const QString &context)
Sets the context string for the color button.
Definition: qgscolorbutton.h:245