1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #include "pentool.h"
31 #include <QtWidgets/QPushButton>
32 #include <QtWidgets/QDoubleSpinBox>
33 #include <QtWidgets/QComboBox>
34 #include <QtWidgets/QFormLayout>
35 #include <QtWidgets/QColorDialog>
36 
PenTool(QString title,QWidget * parent)37 PenTool::PenTool(QString title, QWidget *parent)
38     : QWidget(parent)
39 {
40     setWindowTitle(title);
41     setWindowFlags(Qt::Tool);
42 
43     m_colorButton = new QPushButton();
44 
45     m_widthSpinBox = new QDoubleSpinBox();
46 
47     m_styleCombo = new QComboBox();
48     m_styleCombo->addItem("NoPen");
49     m_styleCombo->addItem("SolidLine");
50     m_styleCombo->addItem("DashLine");
51     m_styleCombo->addItem("DotLine");
52     m_styleCombo->addItem("DashDotLine");
53     m_styleCombo->addItem("DashDotDotLine");
54 
55     m_capStyleCombo = new QComboBox();
56     m_capStyleCombo->addItem("FlatCap", Qt::FlatCap);
57     m_capStyleCombo->addItem("SquareCap", Qt::SquareCap);
58     m_capStyleCombo->addItem("RoundCap", Qt::RoundCap);
59 
60     m_joinStyleCombo = new QComboBox();
61     m_joinStyleCombo->addItem("MiterJoin", Qt::MiterJoin);
62     m_joinStyleCombo->addItem("BevelJoin", Qt::BevelJoin);
63     m_joinStyleCombo->addItem("RoundJoin", Qt::RoundJoin);
64     m_joinStyleCombo->addItem("SvgMiterJoin", Qt::SvgMiterJoin);
65 
66     QFormLayout *layout = new QFormLayout();
67     layout->addRow("Color", m_colorButton);
68     layout->addRow("Width", m_widthSpinBox);
69     layout->addRow("Style", m_styleCombo);
70     layout->addRow("Cap style", m_capStyleCombo);
71     layout->addRow("Join style", m_joinStyleCombo);
72     setLayout(layout);
73 
74     // Use old style connect on some signals because the signal is overloaded
75     connect(m_colorButton, &QPushButton::clicked, this, &PenTool::showColorDialog);
76     connect(m_widthSpinBox,
77             static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
78             this, &PenTool::updateWidth);
79     connect(m_styleCombo,
80             static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
81             this, &PenTool::updateStyle);
82     connect(m_capStyleCombo,
83             static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
84             this, &PenTool::updateCapStyle);
85     connect(m_joinStyleCombo,
86             static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
87             this, &PenTool::updateJoinStyle);
88 }
89 
setPen(const QPen & pen)90 void PenTool::setPen(const QPen &pen)
91 {
92     m_pen = pen;
93     m_colorButton->setText(m_pen.color().name());
94     m_widthSpinBox->setValue(m_pen.widthF());
95     m_styleCombo->setCurrentIndex(m_pen.style()); // index matches the enum
96     m_capStyleCombo->setCurrentIndex(m_capStyleCombo->findData(m_pen.capStyle()));
97     m_joinStyleCombo->setCurrentIndex(m_joinStyleCombo->findData(m_pen.joinStyle()));
98 }
99 
pen() const100 QPen PenTool::pen() const
101 {
102     return m_pen;
103 }
104 
name()105 QString PenTool::name()
106 {
107     return name(m_pen);
108 }
109 
name(const QPen & pen)110 QString PenTool::name(const QPen &pen)
111 {
112     return pen.color().name() + ":" + QString::number(pen.widthF());
113 }
114 
showColorDialog()115 void PenTool::showColorDialog()
116 {
117     QColorDialog dialog(m_pen.color());
118     dialog.show();
119     dialog.exec();
120     m_pen.setColor(dialog.selectedColor());
121     m_colorButton->setText(m_pen.color().name());
122     emit changed();
123 }
124 
updateWidth(double width)125 void PenTool::updateWidth(double width)
126 {
127     if (!qFuzzyCompare((qreal) width, m_pen.widthF())) {
128         m_pen.setWidthF(width);
129         emit changed();
130     }
131 }
132 
updateStyle(int style)133 void PenTool::updateStyle(int style)
134 {
135     if (m_pen.style() != style) {
136         m_pen.setStyle((Qt::PenStyle) style);
137         emit changed();
138     }
139 }
140 
updateCapStyle(int index)141 void PenTool::updateCapStyle(int index)
142 {
143     Qt::PenCapStyle capStyle = (Qt::PenCapStyle) m_capStyleCombo->itemData(index).toInt();
144     if (m_pen.capStyle() != capStyle) {
145         m_pen.setCapStyle(capStyle);
146         emit changed();
147     }
148 }
149 
updateJoinStyle(int index)150 void PenTool::updateJoinStyle(int index)
151 {
152     Qt::PenJoinStyle joinStyle = (Qt::PenJoinStyle) m_joinStyleCombo->itemData(index).toInt();
153     if (m_pen.joinStyle() != joinStyle) {
154         m_pen.setJoinStyle(joinStyle);
155         emit changed();
156     }
157 }
158 
159 #include "moc_pentool.cpp"
160