Seamly2D
Code documentation
vlinecolorproperty.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file vlinecolorproperty.cpp
3  ** @author DS Caskey
4  ** @date Feb 2, 2023
5  **
6  ** @brief
7  ** @copyright
8  ** This source code is part of the Seamly2D project, a pattern making
9  ** program, whose allow create and modeling patterns of clothing.
10  ** Copyright (C) 2017-2023 Seamly2D project
11  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
12  **
13  ** Seamly2D is free software: you can redistribute it and/or modify
14  ** it under the terms of the GNU General Public License as published by
15  ** the Free Software Foundation, either version 3 of the License, or
16  ** (at your option) any later version.
17  **
18  ** Seamly2D is distributed in the hope that it will be useful,
19  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  ** GNU General Public License for more details.
22  **
23  ** You should have received a copy of the GNU General Public License
24  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
25  **
26  *************************************************************************/
27 
28 /************************************************************************
29  **
30  ** @file vlinecolorproperty.cpp
31  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
32  ** @date 7 2, 2015
33  **
34  ** @brief
35  ** @copyright
36  ** This source code is part of the Valentina project, a pattern making
37  ** program, whose allow create and modeling patterns of clothing.
38  ** Copyright (C) 2013-2015 Valentina project
39  ** <https://github.com/fashionfreedom/Valentina> All Rights Reserved.
40  **
41  ** Valentina is free software: you can redistribute it and/or modify
42  ** it under the terms of the GNU General Public License as published by
43  ** the Free Software Foundation, either version 3 of the License, or
44  ** (at your option) any later version.
45  **
46  ** Valentina is distributed in the hope that it will be useful,
47  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
48  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49  ** GNU General Public License for more details.
50  **
51  ** You should have received a copy of the GNU General Public License
52  ** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
53  **
54  *************************************************************************/
55 
56 #include "vlinecolorproperty.h"
57 
58 #include <QColor>
59 #include <QComboBox>
60 #include <QCoreApplication>
61 #include <QIcon>
62 #include <QLocale>
63 #include <QPixmap>
64 #include <QSize>
65 #include <QStaticStringData>
66 #include <QStringData>
67 #include <QStringDataPtr>
68 #include <QWidget>
69 
70 #include "../ifc/ifcdef.h"
71 #include "../vtools/tools/vabstracttool.h"
72 #include "../vproperty_p.h"
73 
75  : VProperty(name, QVariant::Int)
76  , colors()
77  , indexList()
78  , m_iconWidth(40)
79  , m_iconHeight(14)
80 {
82  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
83 }
84 
85 QVariant VPE::VLineColorProperty::data(int column, int role) const
86 {
87  if (colors.empty())
88  {
89  return QVariant();
90  }
91 
92  int tmpIndex = VProperty::d_ptr->VariantValue.toInt();
93 
94  if (tmpIndex < 0 || tmpIndex >= indexList.count())
95  {
96  tmpIndex = 0;
97  }
98 
99  if (column == DPC_Data && Qt::DisplayRole == role)
100  {
101  return indexList.at(tmpIndex);
102  }
103  else if (column == DPC_Data && Qt::EditRole == role)
104  {
105  return tmpIndex;
106  }
107  else
108  {
109  return VProperty::data(column, role);
110  }
111 }
112 
113 QWidget *VPE::VLineColorProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options,
114  const QAbstractItemDelegate *delegate)
115 {
116  Q_UNUSED(options)
117  Q_UNUSED(delegate)
118  QComboBox *colorEditor = new QComboBox(parent);
119  colorEditor->clear();
120 
121 #if defined(Q_OS_MAC)
122  // Mac pixmap should be little bit smaller
123  colorEditor->setIconSize(QSize(m_iconWidth-= 2 ,m_iconHeight-= 2));
124 #else
125  // Windows
126  colorEditor->setIconSize(QSize(m_iconWidth, m_iconHeight));
127 #endif
128 
129  QMap<QString, QString>::const_iterator i = colors.constBegin();
130  while (i != colors.constEnd())
131  {
132  QPixmap pixmap = VAbstractTool::createColorIcon(m_iconWidth, m_iconHeight, i.key());
133  colorEditor->addItem(QIcon(pixmap), i.value(), QVariant(i.key()));
134  ++i;
135  }
136 
137  colorEditor->setMinimumWidth(140);
138  colorEditor->setLocale(parent->locale());
139  colorEditor->setCurrentIndex(VProperty::d_ptr->VariantValue.toInt());
140  connect(colorEditor, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
142 
143  VProperty::d_ptr->editor = colorEditor;
144  return VProperty::d_ptr->editor;
145 }
146 
147 QVariant VPE::VLineColorProperty::getEditorData(const QWidget *editor) const
148 {
149  const QComboBox *colorEditor = qobject_cast<const QComboBox*>(editor);
150  if (colorEditor)
151  {
152  return colorEditor->currentIndex();
153  }
154 
155  return QVariant(0);
156 }
157 
159 {
160  this->colors = colors;
161  indexList.clear();
162  QMap<QString, QString>::const_iterator i = colors.constBegin();
163  while (i != colors.constEnd())
164  {
165  indexList.append(i.key());
166  ++i;
167  }
168 }
169 
170 // cppcheck-suppress unusedFunction
172 {
173  return colors;
174 }
175 
176 void VPE::VLineColorProperty::setValue(const QVariant &value)
177 {
178  int tmpIndex = value.toInt();
179 
180  if (tmpIndex < 0 || tmpIndex >= indexList.count())
181  {
182  tmpIndex = 0;
183  }
184 
185  VProperty::d_ptr->VariantValue = tmpIndex;
186  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
187 
188  if (VProperty::d_ptr->editor != nullptr)
189  {
190  setEditorData(VProperty::d_ptr->editor);
191  }
192 }
193 
195 {
196  return QStringLiteral("lineColor");
197 }
198 
199 VPE::VProperty *VPE::VLineColorProperty::clone(bool include_children, VProperty *container) const
200 {
201  return VProperty::clone(include_children, container ? container : new VLineColorProperty(getName()));
202 }
203 
204 int VPE::VLineColorProperty::indexOfColor(const QMap<QString, QString> &colors, const QString &color)
205 {
206  QVector<QString> indexList;
207  QMap<QString, QString>::const_iterator i = colors.constBegin();
208  while (i != colors.constEnd())
209  {
210  indexList.append(i.key());
211  ++i;
212  }
213  return indexList.indexOf(color);
214 }
215 
217 {
218  Q_UNUSED(index)
219  UserChangeEvent *event = new UserChangeEvent();
220  QCoreApplication::postEvent (VProperty::d_ptr->editor, event );
221 }
static QPixmap createColorIcon(const int w, const int h, const QString &color)
virtual void setValue(const QVariant &value) Q_DECL_OVERRIDE
Sets the value of the property.
virtual QString type() const Q_DECL_OVERRIDE
Returns a string containing the type of the property.
VLineColorProperty(const QString &name)
Constructor.
virtual QMap< QString, QString > getColors() const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const Q_DECL_OVERRIDE
Get the data how it should be displayed.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
Clones this property.
static int indexOfColor(const QMap< QString, QString > &colors, const QString &color)
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
Gets the data from the widget.
virtual void setColors(const QMap< QString, QString > &colors)
Sets the colors.
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &options, const QAbstractItemDelegate *delegate) Q_DECL_OVERRIDE
Returns an editor widget, or NULL if it doesn't supply one.
void currentIndexChanged(int index)
QVariant VariantValue
The property's value. This does not have to be used by subclasses, but it makes sense in cases where ...
Definition: vproperty_p.h:40
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const
Clones this property.
Definition: vproperty.cpp:372
VPropertyPrivate * d_ptr
The protected structure holding the member variables (to assure binary compatibility)
Definition: vproperty.h:214
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const
Get the data how it should be displayed.
Definition: vproperty.cpp:68