Seamly2D
Code documentation
lineweight_combobox.cpp
Go to the documentation of this file.
1  /******************************************************************************
2  * @file lineweight_combobox.cpp
3  ** @author DS Caskey
4  ** @date Nov 2, 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 "lineweight_combobox.h"
25 
26 #include <QAbstractItemView>
27 #include <QPen>
28 #include <Qt>
29 #include <QPainter>
30 #include <QPixmap>
31 
32 /**
33  * Constructor with name.
34  */
35 LineWeightComboBox::LineWeightComboBox(QWidget *parent, const char *name)
36  : QComboBox(parent)
37  , m_currentWeight(0.35)
38  , m_iconWidth(40)
39  , m_iconHeight(12)
40 {
41  setObjectName(name);
42  setEditable ( false );
43  init();
44 }
45 
46 /**
47  * Constructor that provides a width and height for the icon.
48  */
49 LineWeightComboBox::LineWeightComboBox(int width, int height, QWidget *parent, const char *name)
50  : QComboBox(parent)
51  , m_currentWeight(0.35)
52  , m_iconWidth(width)
53  , m_iconHeight(height)
54 {
55  setObjectName(name);
56  setEditable ( false );
57  init();
58 }
59 
60 /**
61  * Destructor
62  */
64 
65 /**
66  * Initialisation called from constructor or manually but only once.
67  */
69 {
70  this->blockSignals(true);
71 
72 #if defined(Q_OS_MAC)
73  setIconSize(QSize(m_iconWidth-= 2 ,m_iconHeight-= 2)); // On Mac pixmap should be little bit smaller.
74 #else // Windows
75  setIconSize(QSize(m_iconWidth, m_iconHeight));
76 #endif
77 
78  this->view()->setTextElideMode(Qt::ElideNone);
79 
80  addItem(createIcon(0.00), "0.00mm", 0.00);
81  addItem(createIcon(1.00), "0.05mm", 0.05);
82  addItem(createIcon(1.00), "0.09mm", 0.09);
83  addItem(createIcon(2.00), "0.13mm (ISO)", 0.13);
84  addItem(createIcon(2.00), "0.15mm", 0.15);
85  addItem(createIcon(2.00), "0.18mm (ISO)", 0.18);
86  addItem(createIcon(3.00), "0.20mm", 0.20);
87  addItem(createIcon(3.00), "0.25mm (ISO)", 0.25);
88  addItem(createIcon(3.00), "0.30mm", 0.30);
89  addItem(createIcon(3.00), "0.35mm (ISO)", 0.35);
90  addItem(createIcon(3.00), "0.40mm", 0.40);
91  addItem(createIcon(4.00), "0.50mm (ISO)", 0.50);
92  addItem(createIcon(4.00), "0.53mm", 0.53);
93  addItem(createIcon(4.00), "0.60mm", 0.60);
94  addItem(createIcon(4.00), "0.70mm (ISO)", 0.70);
95  addItem(createIcon(5.00), "0.80mm", 0.80);
96  addItem(createIcon(5.00), "0.90mm", 0.90);
97  addItem(createIcon(6.00), "1.00mm (ISO)", 1.00);
98  addItem(createIcon(6.00), "1.06mm", 1.06);
99  addItem(createIcon(6.00), "1.20mm", 1.20);
100  addItem(createIcon(7.00), "1.40mm (ISO)", 1.40);
101  addItem(createIcon(7.00), "1.58mm", 1.58);
102  addItem(createIcon(8.00), "2.00mm (ISO)", 2.00);
103  addItem(createIcon(8.00), "2.11mm", 2.11);
104  setMaxVisibleItems(24);
105 
106  this->blockSignals(false);
107  connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &LineWeightComboBox::updateLineWeight);
108 
109  setCurrentIndex(0);
110  updateLineWeight(currentIndex());
111 }
112 
114 {
115  return m_currentWeight;
116 }
117 
118 /**
119  * Sets the currently selected weight item to the given weight.
120  */
121 void LineWeightComboBox::setLineWeight(const qreal &weight)
122 {
123  m_currentWeight = weight;
124 
125  setCurrentIndex(findData(weight));
126 
127  if (currentIndex()!= count() -1 )
128  {
129  updateLineWeight(currentIndex());
130  }
131 }
132 
133 /**
134  * Called when the width has changed. This method sets the current width to the value chosen or even
135  * offers a dialog to the user that allows him/ her to choose an individual width.
136  */
138 {
139  QVariant weight = itemData(index);
140  if(weight != QVariant::Invalid )
141  {
142  m_currentWeight = QVariant(weight).toReal();
143  }
144 
146 }
147 
148 QIcon LineWeightComboBox::createIcon(const qreal &width)
149 {
150  QPixmap pixmap(m_iconWidth, m_iconHeight);
151  pixmap.fill(Qt::black);
152 
153  QPen pen(Qt::black, width, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
154 
155  QPainter painter(&pixmap);
156  painter.fillRect(QRect(1, 1, m_iconWidth-2, m_iconHeight-2), QColor(Qt::white));
157  painter.setPen(pen);
158  painter.drawLine(0, m_iconHeight/2, m_iconWidth, m_iconHeight/2);
159 
160  return QIcon(pixmap);
161 }
void updateLineWeight(int index)
QIcon createIcon(const qreal &width)
LineWeightComboBox(QWidget *parent=nullptr, const char *name=nullptr)
void setLineWeight(const qreal &weight)
void lineWeightChanged(const qreal &weight)