Seamly2D
Code documentation
linetype_combobox.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file linetypecombobox.cpp
3  ** @author DS Caskey
4  ** @date April 6, 2021
5  **
6  ** @brief
7  ** @copyright
8  **
9  ** Seamly2D 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 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D 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
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  *****************************************************************************/
23 
24 #include "linetype_combobox.h"
25 
26 #include "../ifc/ifcdef.h"
27 
28 #include <QStandardItemModel>
29 #include <QAbstractItemView>
30 #include <QPixmap>
31 #include <QComboBox>
32 #include <QPainter>
33 
34 /*
35  * Default Constructor.
36  */
38  : QComboBox(parent)
39  , m_currentLineType(LineTypeSolidLine)
40  , m_iconWidth(40)
41  , m_iconHeight(14)
42 {
43  setEditable (false);
44  init();
45 }
46 
47 /**
48  * Constructor that provides a width & height for the icon, and a name for the combobox.
49  */
50 LineTypeComboBox::LineTypeComboBox(int width, int height, QWidget *parent, const char *name)
51  : QComboBox(parent)
52  , m_currentLineType(LineTypeSolidLine)
53  , m_iconWidth(width)
54  , m_iconHeight(height)
55 {
56  setObjectName(name);
57  init();
58 }
59 
60 /**
61  * Destructor
62  */
64 
65 
66 /**
67  * Initialisation called from constructor or manually but only once.
68  */
70 {
71  blockSignals(true);
72 
73 #if defined(Q_OS_MAC)
74  setIconSize(QSize(m_iconWidth-= 2 ,m_iconHeight-= 2)); // On Mac pixmap should be little bit smaller.
75 #else // Windows
76  setIconSize(QSize(m_iconWidth,m_iconHeight));
77 #endif
78 
79  view()->setTextElideMode(Qt::ElideNone);
80  setSizeAdjustPolicy(QComboBox::AdjustToContents);
81 
82  addItem(createIcon(LineTypeNone), tr("No Pen"), LineTypeNone);
83  addItem(createIcon(LineTypeSolidLine), tr("Solidline"), LineTypeSolidLine);
84  addItem(createIcon(LineTypeDashLine), tr("Dash"), LineTypeDashLine);
85  addItem(createIcon(LineTypeDotLine), tr("Dot"), LineTypeDotLine);
86  addItem(createIcon(LineTypeDashDotLine), tr("Dash Dot"), LineTypeDashDotLine);
87  addItem(createIcon(LineTypeDashDotDotLine), tr("Dash Dot Dot"), LineTypeDashDotDotLine);
88 
89  const int index = findData(QVariant(LineTypeSolidLine));
90  if (index != -1)
91  {
92  setCurrentIndex(index);
93  }
94  else
95  {
96  setCurrentIndex(1);
97  }
98 
99  blockSignals(false);
100  connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &LineTypeComboBox::updateLineType);
101 
102 
103  updateLineType(currentIndex());
104 }
105 
106 /**
107  * Sets the currently selected linetype item to the given linetype.
108  */
109 void LineTypeComboBox::setLineType(const QString &type)
110 {
111  m_currentLineType = type;
112 
113  setCurrentIndex(findData(type));
114 
115  if (currentIndex()!= count() -1 )
116  {
117  updateLineType(currentIndex());
118  }
119 }
120 
121 
122 /* Called when the linetype has changed. This method sets the current linetype
123  * to the value chosen or even offers a dialog to the user that allows one to
124  * choose an individual linetype.
125  */
127 {
128  QVariant type = itemData(index);
129  if(type != QVariant::Invalid )
130  {
131  m_currentLineType = QVariant(type).toString();
132  }
133 
135 }
136 
137 QIcon LineTypeComboBox::createIcon(const QString &type)
138 {
139  const Qt::PenStyle style = lineTypeToPenStyle(type);
140  QPixmap pixmap(m_iconWidth, m_iconHeight);
141  pixmap.fill(Qt::black);
142 
143  QBrush brush(Qt::black);
144  QPen pen(brush, 2.5, style);
145 
146  QPainter painter(&pixmap);
147  painter.fillRect(QRect(1, 1, m_iconWidth-2, m_iconHeight-2), QColor(Qt::white));
148  if (style != Qt::NoPen)
149  {
150  painter.setPen(pen);
151  painter.drawLine(0, m_iconHeight/2, m_iconWidth, m_iconHeight/2);
152  }
153 
154  return QIcon(pixmap);
155 }
void setLineType(const QString &type)
void lineTypeChanged(const QString &type)
void updateLineType(int index)
QIcon createIcon(const QString &type)
LineTypeComboBox(QWidget *parent=nullptr)
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
const QString LineTypeDashDotLine
Definition: ifcdef.cpp:162
const QString LineTypeDashDotDotLine
Definition: ifcdef.cpp:163
const QString LineTypeDashLine
Definition: ifcdef.cpp:160
const QString LineTypeNone
Definition: ifcdef.cpp:158
Qt::PenStyle lineTypeToPenStyle(const QString &lineType)
LineStyle return pen style for current line style.
Definition: ifcdef.cpp:183
const QString LineTypeDotLine
Definition: ifcdef.cpp:161