Seamly2D
Code documentation
vlinetypeproperty.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file vlinetypeproperty.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 /************************************************************************
29  **
30  ** @file vlinetypeproperty.cpp
31  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
32  ** @date 29 1, 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 "vlinetypeproperty.h"
57 
58 #include <QBrush>
59 #include <QComboBox>
60 #include <QCoreApplication>
61 #include <QLocale>
62 #include <QPainter>
63 #include <QPen>
64 #include <QPixmap>
65 #include <QSize>
66 #include <QStaticStringData>
67 #include <QStringData>
68 #include <QStringDataPtr>
69 #include <QWidget>
70 
71 #include "../ifc/ifcdef.h"
72 #include "../vproperty_p.h"
73 
75  : VProperty(name, QVariant::Int)
76  , m_lineTypes()
77  , m_indexList()
78  , m_iconWidth(40)
79  , m_iconHeight(14)
80 {
82  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
83 }
84 
85 QVariant VPE::LineTypeProperty::data(int column, int role) const
86 {
87  if (m_lineTypes.empty())
88  {
89  return QVariant();
90  }
91 
92  int tmpIndex = VProperty::d_ptr->VariantValue.toInt();
93 
94  if (tmpIndex < 0 || tmpIndex >= m_indexList.count())
95  {
96  tmpIndex = 0;
97  }
98 
99  if (column == DPC_Data && Qt::DisplayRole == role)
100  {
101  return m_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::LineTypeProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options,
114  const QAbstractItemDelegate *delegate)
115 {
116  Q_UNUSED(options)
117  Q_UNUSED(delegate)
118  QComboBox *lineTypeEditor = new QComboBox(parent);
119 
120 #if defined(Q_OS_MAC)
121  // Mac pixmap should be little bit smaller
122  lineTypeEditor->setIconSize(QSize(m_iconWidth-= 2 ,m_iconHeight-= 2));
123 #else
124  // Windows
125  lineTypeEditor->setIconSize(QSize(m_iconWidth, m_iconHeight));
126 #endif
127  lineTypeEditor->clear();
128  lineTypeEditor->setMinimumWidth(140);
129  lineTypeEditor->setLocale(parent->locale());
130 
131  QMap<QString, QString>::const_iterator i = m_lineTypes.constBegin();
132  while (i != m_lineTypes.constEnd())
133  {
134  const QString lineType = QVariant(i.key()).toString();
135  lineTypeEditor->addItem(createIcon(lineType), i.value(), QVariant(i.key()));
136  ++i;
137  }
138  lineTypeEditor->setCurrentIndex(VProperty::d_ptr->VariantValue.toInt());
139  connect(lineTypeEditor, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
141 
142  VProperty::d_ptr->editor = lineTypeEditor;
143  return VProperty::d_ptr->editor;
144 }
145 
146 QVariant VPE::LineTypeProperty::getEditorData(const QWidget *editor) const
147 {
148  const QComboBox *lineTypeEditor = qobject_cast<const QComboBox*>(editor);
149  if (lineTypeEditor)
150  {
151  return lineTypeEditor->currentIndex();
152  }
153 
154  return QVariant(0);
155 }
156 
158 {
159  this->m_lineTypes = lineTypes;
160  m_indexList.clear();
161  QMap<QString, QString>::const_iterator i = m_lineTypes.constBegin();
162  while (i != lineTypes.constEnd())
163  {
164  m_indexList.append(i.key());
165  ++i;
166  }
167 }
168 
169 // cppcheck-suppress unusedFunction
171 {
172  return m_lineTypes;
173 }
174 
175 void VPE::LineTypeProperty::setValue(const QVariant &value)
176 {
177  int tmpIndex = value.toInt();
178 
179  if (tmpIndex < 0 || tmpIndex >= m_indexList.count())
180  {
181  tmpIndex = 0;
182  }
183 
184  VProperty::d_ptr->VariantValue = tmpIndex;
185  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
186 
187  if (VProperty::d_ptr->editor != nullptr)
188  {
189  setEditorData(VProperty::d_ptr->editor);
190  }
191 }
192 
194 {
195  return QStringLiteral("lineType");
196 }
197 
198 VPE::VProperty *VPE::LineTypeProperty::clone(bool include_children, VProperty *container) const
199 {
200  return VProperty::clone(include_children, container ? container : new LineTypeProperty(getName()));
201 }
202 
203 int VPE::LineTypeProperty::indexOfLineType(const QMap<QString, QString> &lineTypes, const QString &lineType)
204 {
205  QVector<QString> m_indexList;
206  QMap<QString, QString>::const_iterator i = lineTypes.constBegin();
207  while (i != lineTypes.constEnd())
208  {
209  m_indexList.append(i.key());
210  ++i;
211  }
212  return m_indexList.indexOf(lineType);
213 }
214 
216 {
217  Q_UNUSED(index)
218  UserChangeEvent *event = new UserChangeEvent();
219  QCoreApplication::postEvent (VProperty::d_ptr->editor, event );
220 }
221 
222 QIcon VPE::LineTypeProperty::createIcon(const QString &type)
223 {
224  const Qt::PenStyle style = lineTypeToPenStyle(type);
225  QPixmap pixmap(m_iconWidth, m_iconHeight);
226  pixmap.fill(Qt::black);
227 
228  QBrush brush(Qt::black);
229  QPen pen(brush, 2.5, style);
230 
231  QPainter painter(&pixmap);
232  painter.fillRect(QRect(1, 1, m_iconWidth-2, m_iconHeight-2), QColor(Qt::white));
233  if (style != Qt::NoPen)
234  {
235  painter.setPen(pen);
236  painter.drawLine(0, m_iconHeight/2, m_iconWidth, m_iconHeight/2);
237  }
238 
239  return QIcon(pixmap);
240 }
static int indexOfLineType(const QMap< QString, QString > &styles, const QString &style)
virtual QMap< QString, QString > getLineTypes() const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
virtual void setValue(const QVariant &value) Q_DECL_OVERRIDE
Sets the value of the property.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
Clones this property.
void currentIndexChanged(int index)
QIcon createIcon(const QString &type)
virtual QString type() const Q_DECL_OVERRIDE
Returns a string containing the type of the property.
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
Gets the data from the widget.
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const Q_DECL_OVERRIDE
Get the data how it should be displayed.
LineTypeProperty(const QString &name)
Constructor.
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 void setLineTypes(const QMap< QString, QString > &styles)
Sets the line styles.
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
Qt::PenStyle lineTypeToPenStyle(const QString &lineType)
LineStyle return pen style for current line style.
Definition: ifcdef.cpp:183