Seamly2D
Code documentation
pen_toolbar.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file pen_toolbar.cpp
3  ** @author DS Caskey
4  ** @date Jan 16, 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 #include "../vmisc/logging.h"
30 #include "../vmisc/vabstractapplication.h"
31 
32 #include "pen_toolbar.h"
33 
34 Q_LOGGING_CATEGORY(penToolBar, "pentoolbar")
35 
36 /**
37  * Constructor.
38  */
39 PenToolBar::PenToolBar( const QString &title, QWidget *parent )
40  : QToolBar(title, parent)
41  , currentPen(Pen{})
42  , colorBox(new ColorComboBox{40, 14, this, "colorbox"})
43  , lineTypeBox(new LineTypeComboBox{40, 14, this, "lineTypebox"})
44  , lineWeightBox(new LineWeightComboBox{40, 14, this, "lineWeightbox"})
45 {
46  currentPen.color = qApp->Settings()->getDefaultLineColor();
47  currentPen.lineType = qApp->Settings()->getDefaultLineType();
48  currentPen.lineWeight = qApp->Settings()->getDefaultLineWeight();
49 
50  int index = colorBox->findData(currentPen.color);
51  if (index != -1)
52  {
53  colorBox->setCurrentIndex(index);
54  }
55  index = lineTypeBox->findData(currentPen.lineType);
56  if (index != -1)
57  {
58  lineTypeBox->setCurrentIndex(index);
59  }
60  index = lineWeightBox->findData(currentPen.lineWeight);
61  if (index != -1)
62  {
63  lineWeightBox->setCurrentIndex(index);
64  }
65 
66  addWidget(colorBox);
67  colorBox->setToolTip(tr("Current line color"));
69 
70  addWidget(lineTypeBox);
71  lineTypeBox->setToolTip(tr("Current line type"));
73 
74  addWidget(lineWeightBox);
75  lineWeightBox->setToolTip(tr("Current line weight"));
77 
78  QToolButton *resetButton = new QToolButton;
79  resetButton->setIcon(QIcon(":/icons/win.icon.theme/24x24/actions/edit-undo.png"));
80  resetButton->setToolTip ("Reset current pen to defaults");
81  addWidget(resetButton);
82  connect(resetButton, &QToolButton::clicked, this, &PenToolBar::penReset);
83 
84  QToolButton *savePresetButton = new QToolButton;
85  savePresetButton->setIcon(QIcon(":/icons/win.icon.theme/24x24/actions/document-save-as.png"));
86  savePresetButton->setToolTip ("Save current pen preset");
87  addWidget(savePresetButton);
88  connect(savePresetButton, &QToolButton::clicked, this, &PenToolBar::savePreset);
89 }
90 
91 
92 /**
93  * Destructor
94  */
95 PenToolBar::~PenToolBar() = default;
96 
98 {
99  return currentPen;
100 }
101 
102 /**
103  * Called when the linetype was changed by the user.
104  */
105 void PenToolBar::lineTypeChanged(const QString &type)
106 {
107  qCDebug(penToolBar, "PenToolBar::lineTypeChanged - Pen type changed\n");
108  currentPen.lineType = type;
109 
110  emit penChanged(currentPen);
111 }
112 
113 /**
114  * Called when the color was changed by the user.
115  */
116 void PenToolBar::colorChanged(const QString &color)
117 {
118  qCDebug(penToolBar, "PenToolBar::colorChanged - Pen color changed\n");
119  currentPen.color = color;
120 
121  emit penChanged(currentPen);
122 }
123 
124 /**
125  * Called when the width was changed by the user.
126  */
127 void PenToolBar::lineWeightChanged(const qreal &weight)
128 {
129  qCDebug(penToolBar, "PenToolBar::lineWeightChanged - Pen width changed\n");
130  currentPen.lineWeight = weight;
131 
132  emit penChanged(currentPen);
133 }
134 
135 /**
136  * @brief penReset resets the current pen to the preferred defaults.
137  */
139 {
140  qCDebug(penToolBar, "Pen Reset");
141  blockSignals(true);
142 
143  int index = colorBox->findData(qApp->Settings()->getDefaultLineColor());
144  if (index != -1)
145  {
146  colorBox->setCurrentIndex(index);
147  currentPen.color = colorBox->getColor();
148  }
149  index = lineTypeBox->findData(qApp->Settings()->getDefaultLineType());
150  if (index != -1)
151  {
152  lineTypeBox->setCurrentIndex(index);
153  currentPen.lineType = lineTypeBox->getLineType();
154  }
155  index = lineWeightBox->findData(qApp->Settings()->getDefaultLineWeight());
156  if (index != -1)
157  {
158  lineWeightBox->setCurrentIndex(index);
159  currentPen.lineWeight = lineWeightBox->getLineWeight();
160  }
161 
162  blockSignals(false);
163  emit penChanged(currentPen);
164 }
165 
166 /**
167  * savePreset() Save the current pen to the preferences.
168  */
170 {
171  qApp->Settings()->setDefaultLineColor(currentPen.color);
172  qApp->Settings()->setDefaultLineType(currentPen.lineType);
173  qApp->Settings()->setDefaultLineWeight(currentPen.lineWeight);
174 }
void colorChangedSignal(const QString &color)
void lineTypeChanged(const QString &type)
void lineWeightChanged(const qreal &weight)
void penReset()
penReset resets the current pen to the preferred defaults.
Pen getPen() const
Definition: pen_toolbar.cpp:97
QPointer< LineWeightComboBox > lineWeightBox
Definition: pen_toolbar.h:69
void savePreset()
QPointer< LineTypeComboBox > lineTypeBox
Definition: pen_toolbar.h:68
void penChanged(Pen pen)
Pen currentPen
Definition: pen_toolbar.h:66
QPointer< ColorComboBox > colorBox
Definition: pen_toolbar.h:67
virtual ~PenToolBar()
void colorChanged(const QString &color)
void lineTypeChanged(const QString &type)
void lineWeightChanged(const qreal &weight)
Definition: pen.h:37
QString color
Definition: pen_toolbar.h:51
qreal lineWeight
Definition: pen_toolbar.h:52
QString lineType
Definition: pen_toolbar.h:53
#define qApp
Definition: vapplication.h:67