QGIS API Documentation  2.99.0-Master (c558d51)
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* vl, int fieldIdx, QWidget* editor, QWidget* parent )
22  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
23  , mColorButton( nullptr )
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->setMargin( 0 );
51  layout->setContentsMargins( 0, 0, 0, 0 );
52  QgsColorButton* button = new QgsColorButton();
53  button->setContext( QStringLiteral( "editor" ) );
54  layout->addWidget( button );
55  layout->addStretch();
56  container->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
57  return container;
58 }
59 
60 void QgsColorWidgetWrapper::initWidget( QWidget* editor )
61 {
62  mColorButton = qobject_cast<QgsColorButton*>( editor );
63  if ( !mColorButton )
64  {
65  mColorButton = editor->findChild<QgsColorButton*>();
66  }
67 
68  mColorButton->setShowNull( true );
69  connect( mColorButton, SIGNAL( colorChanged( QColor ) ), this, SLOT( valueChanged() ) );
70 }
71 
73 {
74  return mColorButton;
75 }
76 
77 void QgsColorWidgetWrapper::setValue( const QVariant& value )
78 {
79  if ( mColorButton )
80  mColorButton->setColor( !value.isNull() ? QColor( value.toString() ) : QColor() );
81 }
82 
83 void QgsColorWidgetWrapper::updateConstraintWidgetStatus( ConstraintResult /*constraintValid*/ )
84 {
85  // nothing
86 }
void valueChanged()
Will call the value() method to determine the emitted value.
Manages an editor widget Widget and wrapper share the same parent.
A cross platform button subclass for selecting colors.
void setContext(const QString &context)
Sets the context string for the color button.
void setValue(const QVariant &value) override
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:176
ConstraintResult
Result of constraint checks.
QVariant value() const override
Will be used to access the widget&#39;s value.
void setShowNull(bool showNull)
Sets whether a set to null (clear) option is shown in the button&#39;s drop down menu.
void setColor(const QColor &color)
Sets the current color for the button.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
bool valid() const override
Return true if the widget has been properly initialized.
Represents a vector layer which manages a vector based data sets.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QgsColorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)