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 | |
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(atext: "NoPen" ); |
49 | m_styleCombo->addItem(atext: "SolidLine" ); |
50 | m_styleCombo->addItem(atext: "DashLine" ); |
51 | m_styleCombo->addItem(atext: "DotLine" ); |
52 | m_styleCombo->addItem(atext: "DashDotLine" ); |
53 | m_styleCombo->addItem(atext: "DashDotDotLine" ); |
54 | |
55 | m_capStyleCombo = new QComboBox(); |
56 | m_capStyleCombo->addItem(atext: "FlatCap" , auserData: Qt::FlatCap); |
57 | m_capStyleCombo->addItem(atext: "SquareCap" , auserData: Qt::SquareCap); |
58 | m_capStyleCombo->addItem(atext: "RoundCap" , auserData: Qt::RoundCap); |
59 | |
60 | m_joinStyleCombo = new QComboBox(); |
61 | m_joinStyleCombo->addItem(atext: "MiterJoin" , auserData: Qt::MiterJoin); |
62 | m_joinStyleCombo->addItem(atext: "BevelJoin" , auserData: Qt::BevelJoin); |
63 | m_joinStyleCombo->addItem(atext: "RoundJoin" , auserData: Qt::RoundJoin); |
64 | m_joinStyleCombo->addItem(atext: "SvgMiterJoin" , auserData: Qt::SvgMiterJoin); |
65 | |
66 | QFormLayout *layout = new QFormLayout(); |
67 | layout->addRow(labelText: "Color" , field: m_colorButton); |
68 | layout->addRow(labelText: "Width" , field: m_widthSpinBox); |
69 | layout->addRow(labelText: "Style" , field: m_styleCombo); |
70 | layout->addRow(labelText: "Cap style" , field: m_capStyleCombo); |
71 | layout->addRow(labelText: "Join style" , field: m_joinStyleCombo); |
72 | setLayout(layout); |
73 | |
74 | // Use old style connect on some signals because the signal is overloaded |
75 | connect(sender: m_colorButton, signal: &QPushButton::clicked, receiver: this, slot: &PenTool::showColorDialog); |
76 | connect(sender: m_widthSpinBox, |
77 | signal: static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), |
78 | receiver: this, slot: &PenTool::updateWidth); |
79 | connect(sender: m_styleCombo, |
80 | signal: static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), |
81 | receiver: this, slot: &PenTool::updateStyle); |
82 | connect(sender: m_capStyleCombo, |
83 | signal: static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), |
84 | receiver: this, slot: &PenTool::updateCapStyle); |
85 | connect(sender: m_joinStyleCombo, |
86 | signal: static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), |
87 | receiver: this, slot: &PenTool::updateJoinStyle); |
88 | } |
89 | |
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(data: m_pen.capStyle())); |
97 | m_joinStyleCombo->setCurrentIndex(m_joinStyleCombo->findData(data: m_pen.joinStyle())); |
98 | } |
99 | |
100 | QPen PenTool::pen() const |
101 | { |
102 | return m_pen; |
103 | } |
104 | |
105 | QString PenTool::name() |
106 | { |
107 | return name(pen: m_pen); |
108 | } |
109 | |
110 | QString PenTool::name(const QPen &pen) |
111 | { |
112 | return pen.color().name() + ":" + QString::number(pen.widthF()); |
113 | } |
114 | |
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 | |
125 | void PenTool::updateWidth(double width) |
126 | { |
127 | if (!qFuzzyCompare(p1: (qreal) width, p2: m_pen.widthF())) { |
128 | m_pen.setWidthF(width); |
129 | emit changed(); |
130 | } |
131 | } |
132 | |
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 | |
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 | |
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 | |