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
37PenTool::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 connect(sender: m_colorButton, SIGNAL(clicked()), receiver: this, SLOT(showColorDialog()));
75 connect(sender: m_widthSpinBox, SIGNAL(valueChanged(double)), receiver: this, SLOT(updateWidth(double)));
76 connect(sender: m_styleCombo, SIGNAL(currentIndexChanged(int)), receiver: this, SLOT(updateStyle(int)));
77 connect(sender: m_capStyleCombo, SIGNAL(currentIndexChanged(int)), receiver: this, SLOT(updateCapStyle(int)));
78 connect(sender: m_joinStyleCombo, SIGNAL(currentIndexChanged(int)), receiver: this, SLOT(updateJoinStyle(int)));
79}
80
81void PenTool::setPen(const QPen &pen)
82{
83 m_pen = pen;
84 m_colorButton->setText(m_pen.color().name());
85 m_widthSpinBox->setValue(m_pen.widthF());
86 m_styleCombo->setCurrentIndex(m_pen.style()); // index matches the enum
87 m_capStyleCombo->setCurrentIndex(m_capStyleCombo->findData(data: m_pen.capStyle()));
88 m_joinStyleCombo->setCurrentIndex(m_joinStyleCombo->findData(data: m_pen.joinStyle()));
89}
90
91QPen PenTool::pen() const
92{
93 return m_pen;
94}
95
96QString PenTool::name()
97{
98 return name(pen: m_pen);
99}
100
101QString PenTool::name(const QPen &pen)
102{
103 return pen.color().name() + ":" + QString::number(pen.widthF());
104}
105
106void PenTool::showColorDialog()
107{
108 QColorDialog dialog(m_pen.color());
109 dialog.show();
110 dialog.exec();
111 m_pen.setColor(dialog.selectedColor());
112 m_colorButton->setText(m_pen.color().name());
113 emit changed();
114}
115
116void PenTool::updateWidth(double width)
117{
118 if (!qFuzzyCompare(p1: (qreal) width, p2: m_pen.widthF())) {
119 m_pen.setWidthF(width);
120 emit changed();
121 }
122}
123
124void PenTool::updateStyle(int style)
125{
126 if (m_pen.style() != style) {
127 m_pen.setStyle((Qt::PenStyle) style);
128 emit changed();
129 }
130}
131
132void PenTool::updateCapStyle(int index)
133{
134 Qt::PenCapStyle capStyle = (Qt::PenCapStyle) m_capStyleCombo->itemData(index).toInt();
135 if (m_pen.capStyle() != capStyle) {
136 m_pen.setCapStyle(capStyle);
137 emit changed();
138 }
139}
140
141void PenTool::updateJoinStyle(int index)
142{
143 Qt::PenJoinStyle joinStyle = (Qt::PenJoinStyle) m_joinStyleCombo->itemData(index).toInt();
144 if (m_pen.joinStyle() != joinStyle) {
145 m_pen.setJoinStyle(joinStyle);
146 emit changed();
147 }
148}
149
150#include "moc_pentool.cpp"
151

source code of qtcharts/tests/manual/boxplottester/pentool.cpp