Seamly2D
Code documentation
lineweight_property.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file lineweight_property.cpp
3  ** @author DS Caskey
4  ** @date Feb 7, 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 #include "lineweight_property.h"
29 
30 #include <QBrush>
31 #include <QComboBox>
32 #include <QCoreApplication>
33 #include <QLocale>
34 #include <QPainter>
35 #include <QPen>
36 #include <QPixmap>
37 #include <QSize>
38 #include <QStaticStringData>
39 #include <QStringData>
40 #include <QStringDataPtr>
41 #include <QWidget>
42 
43 #include "../ifc/ifcdef.h"
44 #include "../vproperty_p.h"
45 
47  : VProperty(name, QVariant::Int)
48  , m_lineWeights()
49  , m_indexList()
50  , m_iconWidth(40)
51  , m_iconHeight(14)
52 {
54  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
55 }
56 
57 QVariant VPE::LineWeightProperty::data(int column, int role) const
58 {
59  if (m_lineWeights.empty())
60  {
61  return QVariant();
62  }
63 
64  int tmpIndex = VProperty::d_ptr->VariantValue.toInt();
65 
66  if (tmpIndex < 0 || tmpIndex >= m_indexList.count())
67  {
68  tmpIndex = 0;
69  }
70 
71  if (column == DPC_Data && Qt::DisplayRole == role)
72  {
73  return m_indexList.at(tmpIndex);
74  }
75  else if (column == DPC_Data && Qt::EditRole == role)
76  {
77  return tmpIndex;
78  }
79  else
80  {
81  return VProperty::data(column, role);
82  }
83 }
84 
85 QWidget *VPE::LineWeightProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options,
86  const QAbstractItemDelegate *delegate)
87 {
88  Q_UNUSED(options)
89  Q_UNUSED(delegate)
90  QComboBox* lineWeightEditor = new QComboBox(parent);
91 
92 #if defined(Q_OS_MAC)
93  // Mac pixmap should be little bit smaller
94  lineWeightEditor->setIconSize(QSize(m_iconWidth-= 2 ,m_iconHeight-= 2));
95 #else
96  // Windows
97  lineWeightEditor->setIconSize(QSize(m_iconWidth, m_iconHeight));
98 #endif
99  lineWeightEditor->clear();
100  lineWeightEditor->setMinimumWidth(140);
101  lineWeightEditor->setLocale(parent->locale());
102 
103  QMap<QString, QString>::const_iterator i = m_lineWeights.constBegin();
104  while (i != m_lineWeights.constEnd())
105  {
106  const qreal lineweight = QVariant(i.key()).toDouble();
107  lineWeightEditor->addItem(createIcon(lineweight), i.value(), QVariant(i.key()));
108  ++i;
109  }
110  lineWeightEditor->setCurrentIndex(VProperty::d_ptr->VariantValue.toInt());
111  connect(lineWeightEditor, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
113 
114  VProperty::d_ptr->editor = lineWeightEditor;
115  return VProperty::d_ptr->editor;
116 }
117 
118 QVariant VPE::LineWeightProperty::getEditorData(const QWidget *editor) const
119 {
120  const QComboBox* lineWeightEditor = qobject_cast<const QComboBox*>(editor);
121  if (lineWeightEditor)
122  {
123  return lineWeightEditor->currentIndex();
124  }
125 
126  return QVariant(0);
127 }
128 
130 {
131  this->m_lineWeights = lineWeights;
132  m_indexList.clear();
133  QMap<QString, QString>::const_iterator i = m_lineWeights.constBegin();
134  while (i != lineWeights.constEnd())
135  {
136  m_indexList.append(i.key());
137  ++i;
138  }
139 }
140 
141 // cppcheck-suppress unusedFunction
143 {
144  return m_lineWeights;
145 }
146 
147 void VPE::LineWeightProperty::setValue(const QVariant &value)
148 {
149  int tmpIndex = value.toInt();
150 
151  if (tmpIndex < 0 || tmpIndex >= m_indexList.count())
152  {
153  tmpIndex = 0;
154  }
155 
156  VProperty::d_ptr->VariantValue = tmpIndex;
157  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
158 
159  if (VProperty::d_ptr->editor != nullptr)
160  {
161  setEditorData(VProperty::d_ptr->editor);
162  }
163 }
164 
166 {
167  return QStringLiteral("lineWeight");
168 }
169 
170 VPE::VProperty *VPE::LineWeightProperty::clone(bool include_children, VProperty *container) const
171 {
172  return VProperty::clone(include_children, container ? container : new LineWeightProperty(getName()));
173 }
174 
175 int VPE::LineWeightProperty::indexOfLineWeight(const QMap<QString, QString> &lineWeights, const QString &weight)
176 {
177  QVector<QString> m_indexList;
178  QMap<QString, QString>::const_iterator i = lineWeights.constBegin();
179  while (i != lineWeights.constEnd())
180  {
181  m_indexList.append(i.key());
182  ++i;
183  }
184  return m_indexList.indexOf(weight);
185 }
186 
188 {
189  Q_UNUSED(index)
190  UserChangeEvent *event = new UserChangeEvent();
191  QCoreApplication::postEvent (VProperty::d_ptr->editor, event);
192 }
193 
194 QIcon VPE::LineWeightProperty::createIcon(const qreal &width)
195 {
196  QPixmap pixmap(m_iconWidth, m_iconHeight);
197  pixmap.fill(Qt::black);
198 
199  QPen pen(Qt::black, ToPixel(width, Unit::Mm), Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
200 
201  QPainter painter(&pixmap);
202  painter.fillRect(QRect(1, 1, m_iconWidth-2, m_iconHeight-2), QColor(Qt::white));
203  painter.setPen(pen);
204  painter.drawLine(0, m_iconHeight/2, m_iconWidth, m_iconHeight/2);
205 
206  return QIcon(pixmap);
207 }
LineWeightProperty(const QString &name)
Constructor.
virtual void setValue(const QVariant &value) Q_DECL_OVERRIDE
Sets the value of the property.
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
Gets the data from the widget.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
Clones this property.
virtual void setLineWeights(const QMap< QString, QString > &weights)
Sets the line styles.
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const Q_DECL_OVERRIDE
Get the data how it should be displayed.
static int indexOfLineWeight(const QMap< QString, QString > &lineWeights, const QString &weight)
QIcon createIcon(const qreal &width)
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.
virtual QString type() const Q_DECL_OVERRIDE
Returns a string containing the type of the property.
virtual QMap< QString, QString > getLineWeights() const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
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
double ToPixel(double val, const Unit &unit)
Definition: def.cpp:231