Seamly2D
Code documentation
fill_combobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ** @file fill_combobox.cpp
3  ** @author Douglas S Caskey
4  ** @date Dec 28, 2022
5  **
6  ** @copyright
7  ** Copyright (C) 2017 - 2022 Seamly, LLC
8  ** https://github.com/fashionfreedom/seamly2d
9  **
10  ** @brief
11  ** Seamly2D is free software: you can redistribute it and/or modify
12  ** it under the terms of the GNU General Public License as published by
13  ** the Free Software Foundation, either version 3 of the License, or
14  ** (at your option) any later version.
15  **
16  ** Seamly2D is distributed in the hope that it will be useful,
17  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  ** GNU General Public License for more details.
20  **
21  ** You should have received a copy of the GNU General Public License
22  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
23  **************************************************************************/
24 
25 #include "fill_combobox.h"
26 
27 #include <QAbstractItemView>
28 #include <Qt>
29 #include <QPainter>
30 #include <QPixmap>
31 
32 #include "../vtools/tools/vabstracttool.h"
33 #include "../vmisc/logging.h"
34 
35 Q_LOGGING_CATEGORY(fillComboBox, "fill_combobox")
36 
37 /*
38  * Default Constructor. You must call init manually if you choose
39  * to use this constructor.
40  */
41 PieceFillComboBox::PieceFillComboBox(QWidget *parent, const char *name)
42  : QComboBox(parent)
43  , m_currentFill(FillNone)
44  , m_iconWidth(40)
45  , m_iconHeight(14)
46 {
47  setObjectName(name);
48  setEditable (false);
49  init();
50 }
51 
52 /*
53  * Constructor that calls init and provides a fully functional
54  * comboBox for choosing fills.
55  */
56 PieceFillComboBox::PieceFillComboBox(int width, int height, QWidget *parent, const char *name)
57  : QComboBox(parent)
58  , m_currentFill(FillNone)
59  , m_iconWidth(width)
60  , m_iconHeight(height)
61 {
62  setObjectName(name);
63  setEditable (false);
64  init();
65 }
66 
67 /**
68  * Destructor
69  */
71 
72 /*
73  * Initialisation called from constructor or manually but only once.
74  */
76 {
77  qCDebug(fillComboBox, "PieceFillComboBox::init");
78  this->blockSignals(true);
79 
80 #if defined(Q_OS_MAC)
81  // Mac pixmap should be little bit smaller
82  setIconSize(QSize(m_iconWidth-= 2 ,m_iconHeight-= 2));
83 #else
84  // Windows
85  setIconSize(QSize(m_iconWidth, m_iconHeight));
86 #endif
87 
88  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::NoBrush)), tr("No Fill"), FillNone);
89  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::SolidPattern)), tr("Solid"), FillSolid);
90  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::Dense1Pattern)), tr("Density 1"), FillDense1);
91  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::Dense2Pattern)), tr("Density 2"), FillDense2);
92  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::Dense3Pattern)), tr("Density 3"), FillDense3);
93  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::Dense4Pattern)), tr("Density 4"), FillDense4);
94  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::Dense5Pattern)), tr("Density 5"), FillDense5);
95  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::Dense6Pattern)), tr("Density 6"), FillDense6);
96  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::Dense7Pattern)), tr("Density 7"), FillDense7);
97  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::HorPattern)), tr("Horizontal Line"), FillHorizLines);
98  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::VerPattern)), tr("Vertical Line"), FillVertLines);
99  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::CrossPattern)), tr("Cross"), FillCross);
100  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::BDiagPattern)), tr("Backward Diagonal"),FillBackwardDiagonal);
101  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::FDiagPattern)), tr("Forward Diagonal"), FillForwardDiagonal);
102  addItem(QIcon(createFillIcon(m_iconWidth, m_iconHeight, Qt::DiagCrossPattern)), tr("Diagonal Cross"), FilldDiagonalCross);
103 
104  setMaxVisibleItems(15);
105  this->model()->sort(1, Qt::AscendingOrder);
106  setCurrentIndex(0);
107 
108  connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &PieceFillComboBox::fillChanged);
109 
110  fillChanged(currentIndex());
111  this->blockSignals(false);
112 }
113 
114 /*
115  * Sets the fill shown in the combobox to the given fill.
116  */
117 void PieceFillComboBox::setFill(const QString &fill)
118 {
119  m_currentFill = fill;
120 
121  setCurrentIndex(findData(fill));
122 
123  if (currentIndex()!= count() -1 )
124  {
125  fillChanged(currentIndex());
126  }
127 }
128 
130 {
131  return m_currentFill;
132 }
133 
134 //* generate icon from fill style, then add to fill box
135 QPixmap PieceFillComboBox::createFillIcon(const int w, const int h, Qt::BrushStyle style)
136 {
137  QPixmap pixmap(w, h);
138  pixmap.fill(QColor(Qt::black));
139  QPainter painter(&pixmap);
140  painter.fillRect(1, 1, w-2, h-2, QColor(Qt::white));
141  painter.fillRect(1, 1, w-2, h-2, style);
142 
143  return pixmap;
144 }
145 
146 /*
147  * Called when the fill has changed. This method sets the current fill to the
148  * value chosen or even offers a dialog to the user that allows one to
149  * choose an individual fill.
150  */
152 {
153  QVariant fill = itemData(index);
154  if(fill != QVariant::Invalid )
155  {
156  m_currentFill = QVariant(fill).toString();
157  }
158 
160 }
161 
163 {
164  return m_iconWidth;
165 }
166 
168 {
169  return m_iconHeight;
170 }
void fillChangedSignal(const QString &fill)
QString getFill() const
void setFill(const QString &fill)
virtual ~PieceFillComboBox()
void fillChanged(int index)
QPixmap createFillIcon(const int w, const int h, Qt::BrushStyle style)
PieceFillComboBox(QWidget *parent=nullptr, const char *name=nullptr)
const QString FillDense6
Definition: ifcdef.cpp:402
const QString FilldDiagonalCross
Definition: ifcdef.cpp:409
const QString FillBackwardDiagonal
Definition: ifcdef.cpp:407
const QString FillDense4
Definition: ifcdef.cpp:400
const QString FillDense3
Definition: ifcdef.cpp:399
const QString FillHorizLines
Definition: ifcdef.cpp:404
const QString FillNone
Definition: ifcdef.cpp:395
const QString FillCross
Definition: ifcdef.cpp:406
const QString FillDense2
Definition: ifcdef.cpp:398
const QString FillDense5
Definition: ifcdef.cpp:401
const QString FillSolid
Definition: ifcdef.cpp:396
const QString FillDense1
Definition: ifcdef.cpp:397
const QString FillVertLines
Definition: ifcdef.cpp:405
const QString FillDense7
Definition: ifcdef.cpp:403
const QString FillForwardDiagonal
Definition: ifcdef.cpp:408