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#include "brushtool.h"
30#include <QtWidgets/QPushButton>
31#include <QtWidgets/QFormLayout>
32#include <QtWidgets/QComboBox>
33#include <QtWidgets/QColorDialog>
34
35BrushTool::BrushTool(QString title, QWidget *parent)
36 : QWidget(parent)
37{
38 setWindowTitle(title);
39 setWindowFlags(Qt::Tool);
40
41 m_colorButton = new QPushButton();
42 m_styleCombo = new QComboBox();
43 m_styleCombo->addItem(atext: "Nobrush", auserData: (int) Qt::NoBrush);
44 m_styleCombo->addItem(atext: "Solidpattern", auserData: (int) Qt::SolidPattern);
45 m_styleCombo->addItem(atext: "Dense1pattern", auserData: (int) Qt::Dense1Pattern);
46 m_styleCombo->addItem(atext: "Dense2attern", auserData: (int) Qt::Dense2Pattern);
47 m_styleCombo->addItem(atext: "Dense3Pattern", auserData: (int) Qt::Dense3Pattern);
48 m_styleCombo->addItem(atext: "Dense4Pattern", auserData: (int) Qt::Dense4Pattern);
49 m_styleCombo->addItem(atext: "Dense5Pattern", auserData: (int) Qt::Dense5Pattern);
50 m_styleCombo->addItem(atext: "Dense6Pattern", auserData: (int) Qt::Dense6Pattern);
51 m_styleCombo->addItem(atext: "Dense7Pattern", auserData: (int) Qt::Dense7Pattern);
52 m_styleCombo->addItem(atext: "HorPattern", auserData: (int) Qt::HorPattern);
53 m_styleCombo->addItem(atext: "VerPattern", auserData: (int) Qt::VerPattern);
54 m_styleCombo->addItem(atext: "CrossPattern", auserData: (int) Qt::CrossPattern);
55 m_styleCombo->addItem(atext: "BDiagPattern", auserData: (int) Qt::BDiagPattern);
56 m_styleCombo->addItem(atext: "FDiagPattern", auserData: (int) Qt::FDiagPattern);
57 m_styleCombo->addItem(atext: "DiagCrossPattern", auserData: (int) Qt::DiagCrossPattern);
58
59 QFormLayout *layout = new QFormLayout();
60 layout->addRow(labelText: "Color", field: m_colorButton);
61 layout->addRow(labelText: "Style", field: m_styleCombo);
62 setLayout(layout);
63
64 connect(sender: m_colorButton, signal: &QPushButton::clicked, receiver: this, slot: &BrushTool::showColorDialog);
65 connect(sender: m_styleCombo, signal: static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
66 receiver: this, slot: &BrushTool::updateStyle);
67}
68
69void BrushTool::setBrush(QBrush brush)
70{
71 m_brush = brush;
72 m_colorButton->setText(m_brush.color().name());
73 m_styleCombo->setCurrentIndex(m_brush.style()); // index matches the enum
74}
75
76QBrush BrushTool::brush() const
77{
78 return m_brush;
79}
80
81QString BrushTool::name()
82{
83 return name(brush: m_brush);
84}
85
86QString BrushTool::name(const QBrush &brush)
87{
88 return brush.color().name();
89}
90
91void BrushTool::showColorDialog()
92{
93 QColorDialog dialog(m_brush.color());
94 dialog.show();
95 dialog.exec();
96 m_brush.setColor(dialog.selectedColor());
97 m_colorButton->setText(m_brush.color().name());
98 emit changed();
99}
100
101void BrushTool::updateStyle()
102{
103 Qt::BrushStyle style = (Qt::BrushStyle) m_styleCombo->itemData(index: m_styleCombo->currentIndex()).toInt();
104 if (m_brush.style() != style) {
105 m_brush.setStyle(style);
106 emit changed();
107 }
108}
109
110#include "moc_brushtool.cpp"
111

source code of qtcharts/examples/charts/piechartcustomization/brushtool.cpp