QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 }
A cross platform button subclass for selecting colors.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
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.
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.
Manages an editor widget Widget and wrapper share the same parent.
void emitValueChanged()
Will call the value() method to determine the emitted value.
Represents a vector layer which manages a vector based data sets.
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:537