QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgscolorwidgetwrapper.cpp"
18#include "qgscolorbutton.h"
19#include <QLayout>
20
21
22QgsColorWidgetWrapper::QgsColorWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
23 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
24
25{
26}
27
28
30{
31 QColor c;
32 if ( mColorButton )
33 c = mColorButton->color();
34
35 return c.isValid() ? QVariant( c ) : QgsVariantUtils::createNullVariant( QMetaType::Type::QColor );
36}
37
39{
40 if ( mColorButton )
41 {
42 whileBlocking( mColorButton )->setColor( QColor() );
43 }
44}
45
46QWidget *QgsColorWidgetWrapper::createWidget( QWidget *parent )
47{
48 QWidget *container = new QWidget( parent );
49 QHBoxLayout *layout = new QHBoxLayout();
50 container->setLayout( layout );
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
60void 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, &QgsColorButton::colorChanged, this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
70}
71
73{
74 return mColorButton;
75}
76
77void QgsColorWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
78{
79 if ( mColorButton )
80 mColorButton->setColor( !QgsVariantUtils::isNull( value ) ? QColor( value.toString() ) : QColor() );
81}
82
83void QgsColorWidgetWrapper::updateConstraintWidgetStatus()
84{
85 // nothing
86}
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.
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 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:5862