Seamly2D
Code documentation
vcolorpropertyeditor.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vcolorpropertyeditor.cpp
4  ** @author hedgeware <internal(at)hedgeware.net>
5  ** @date
6  **
7  ** @brief
8  ** @copyright
9  ** All rights reserved. This program and the accompanying materials
10  ** are made available under the terms of the GNU Lesser General Public License
11  ** (LGPL) version 2.1 which accompanies this distribution, and is available at
12  ** http://www.gnu.org/licenses/lgpl-2.1.html
13  **
14  ** This library is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  ** Lesser General Public License for more details.
18  **
19  *************************************************************************/
20 
21 #include "vcolorpropertyeditor.h"
22 
23 #include <QColorDialog>
24 #include <QCoreApplication>
25 #include <QEvent>
26 #include <QHBoxLayout>
27 #include <QImage>
28 #include <QLabel>
29 #include <QRgb>
30 #include <QSizePolicy>
31 #include <QSpacerItem>
32 #include <QToolButton>
33 
34 #include "../vproperty.h"
35 
37  : QWidget(parent), Color(), ToolButton(nullptr), TextLabel(nullptr), ColorLabel(nullptr), Spacer(nullptr)
38 {
39  setAutoFillBackground(true);
40 
41  // Create the tool button
42  ToolButton = new QToolButton(this);
43  ToolButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
44  ToolButton->setText("...");
45  ToolButton->setFixedWidth(20);
46  ToolButton->installEventFilter(this);
47  setFocusProxy(ToolButton); // Make the ToolButton the focus proxy
48  setFocusPolicy(ToolButton->focusPolicy());
49  connect(ToolButton, &QToolButton::clicked, this, &VColorPropertyEditor::onToolButtonClicked);
50 
51  // Create the text label
52  TextLabel = new QLabel(this);
53  TextLabel->setText(GetColorString(Color));
54 
55  // Create the label for the pixmap
56  ColorLabel = new QLabel(this);
57  ColorLabel->setPixmap(GetColorPixmap(Color));
58 
59  // Spacer (this is needed for proper display of the label and button)
60  Spacer = new QSpacerItem(1, 0, QSizePolicy::Expanding, QSizePolicy::Ignored);
61 
62  // The layout (a horizontal layout)
63  QHBoxLayout *layout = new QHBoxLayout(this);
64  layout->setSpacing(3);
65  layout->setMargin(0);
66  layout->addWidget(ColorLabel);
67  layout->addWidget(TextLabel);
68  layout->addItem(Spacer);
69  layout->addWidget(ToolButton);
70  //TextLabel->hide();
71  //ColorLabel->hide(); // for now, we just use the standard display and only add the button
72 }
73 
74 void VPE::VColorPropertyEditor::setLineColor(const QColor &color_)
75 {
76  if (Color != color_)
77  {
78  Color = color_;
79  ColorLabel->setPixmap(GetColorPixmap(Color));
80  TextLabel->setText(GetColorString(Color));
81  }
82 }
83 
84 QPixmap VPE::VColorPropertyEditor::GetColorPixmap(const QColor &color, quint32 size)
85 {
86  QImage tmpImgage(static_cast<int>(size), static_cast<int>(size), QImage::Format_ARGB32_Premultiplied);
87  tmpImgage.fill(static_cast<quint32>(color.rgb()));
88  return QPixmap::fromImage(tmpImgage);
89  // todo: support alpha channel
90 }
91 
92 QString VPE::VColorPropertyEditor::GetColorString(const QColor &color)
93 {
94  return QString("[%1, %2, %3] (%4)").arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha());
95 }
96 
98 {
99  const QColor newColor = QColorDialog::getColor(Color, this, QString(), QColorDialog::ShowAlphaChannel);
100  if (newColor.isValid() && newColor != Color)
101  {
102  setLineColor(newColor);
103  emit dataChangedByUser(Color, this);
104  UserChangeEvent *event = new UserChangeEvent();
105  QCoreApplication::postEvent ( this, event );
106  }
107 }
108 
109 bool VPE::VColorPropertyEditor::eventFilter(QObject *obj, QEvent *ev)
110 {
111  if (obj == ToolButton && ev->type() == QEvent::KeyPress)
112  {
113  // Ignore the event, so that eventually the delegate gets the event.
114  ev->ignore();
115  return true;
116  }
117 
118  return QWidget::eventFilter(obj, ev);
119 }
120 
121 
123 {
124  //
125 }
126 
128 {
129  return Color;
130 }
static QString GetColorString(const QColor &color)
A helper function to convert a color into a string.
VColorPropertyEditor(QWidget *parent)
Constructor taking a widget as parent.
static QPixmap GetColorPixmap(const QColor &color, quint32 size=16)
A little helper function generating an image to represent a color.
virtual bool eventFilter(QObject *obj, QEvent *ev) Q_DECL_OVERRIDE
Needed for proper event handling.
QColor getLineColor() const
Returns the color currently set.
virtual ~VColorPropertyEditor() Q_DECL_OVERRIDE
Destructor.
void setLineColor(const QColor &color_)
Sets the color of the widget.