]> sigrok.org Git - pulseview.git/blame - pv/widgets/colorbutton.cpp
GlobalSettings: Always use Fusion style on Windows for dark themes
[pulseview.git] / pv / widgets / colorbutton.cpp
CommitLineData
a52fcc76
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
a52fcc76
JH
18 */
19
641574bc 20#include "colorbutton.hpp"
a52fcc76 21
eb8269e3 22#include <cassert>
b213ef09
JH
23
24#include <QApplication>
4521022b 25#include <QColorDialog>
b213ef09
JH
26#include <QPainter>
27
a52fcc76
JH
28namespace pv {
29namespace widgets {
30
641574bc 31const int ColorButton::SwatchMargin = 7;
a52fcc76 32
4521022b
SA
33ColorButton::ColorButton(QWidget *parent) :
34 QPushButton("", parent),
35 popup_(nullptr)
36{
37 connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
38}
39
641574bc 40ColorButton::ColorButton(int rows, int cols, QWidget *parent) :
a52fcc76 41 QPushButton("", parent),
4521022b 42 popup_(new ColorPopup(rows, cols, this))
a52fcc76
JH
43{
44 connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
4521022b 45 connect(popup_, SIGNAL(selected(int, int)),
a52fcc76
JH
46 this, SLOT(on_selected(int, int)));
47}
48
4521022b 49ColorPopup* ColorButton::popup()
a52fcc76 50{
8dbbc7f0 51 return popup_;
a52fcc76
JH
52}
53
641574bc 54const QColor& ColorButton::color() const
a52fcc76 55{
641574bc 56 return cur_color_;
a52fcc76
JH
57}
58
641574bc 59void ColorButton::set_color(QColor color)
a52fcc76 60{
641574bc 61 cur_color_ = color;
a52fcc76 62
4521022b
SA
63 if (popup_) {
64 const unsigned int rows = popup_->well_array().numRows();
65 const unsigned int cols = popup_->well_array().numCols();
66
67 for (unsigned int r = 0; r < rows; r++)
68 for (unsigned int c = 0; c < cols; c++)
69 if (popup_->well_array().cellBrush(r, c).color() == color) {
70 popup_->well_array().setSelected(r, c);
71 popup_->well_array().setCurrent(r, c);
72 return;
73 }
74 }
a52fcc76
JH
75}
76
641574bc 77void ColorButton::set_palette(const QColor *const palette)
a52fcc76
JH
78{
79 assert(palette);
4521022b 80 assert(popup_);
a52fcc76 81
4521022b
SA
82 const unsigned int rows = popup_->well_array().numRows();
83 const unsigned int cols = popup_->well_array().numCols();
a52fcc76
JH
84
85 for (unsigned int r = 0; r < rows; r++)
86 for (unsigned int c = 0; c < cols; c++)
4521022b 87 popup_->well_array().setCellBrush(r, c, QBrush(palette[r * cols + c]));
a52fcc76
JH
88}
89
641574bc 90void ColorButton::on_clicked(bool)
a52fcc76 91{
4521022b
SA
92 if (popup_) {
93 popup_->set_position(mapToGlobal(rect().center()), Popup::Bottom);
94 popup_->show();
95 } else {
96 QColorDialog dlg(this);
97 dlg.setOption(QColorDialog::ShowAlphaChannel);
98 dlg.setOption(QColorDialog::DontUseNativeDialog);
99 connect(&dlg, SIGNAL(colorSelected(const QColor)),
100 this, SLOT(on_color_selected(const QColor)));
101 dlg.setCurrentColor(cur_color_);
102 dlg.exec();
103 }
a52fcc76
JH
104}
105
641574bc 106void ColorButton::on_selected(int row, int col)
a52fcc76 107{
4521022b
SA
108 assert(popup_);
109
110 cur_color_ = popup_->well_array().cellBrush(row, col).color();
111 selected(cur_color_);
112}
113
114void ColorButton::on_color_selected(const QColor& color)
115{
116 cur_color_ = color;
641574bc 117 selected(cur_color_);
a52fcc76
JH
118}
119
641574bc 120void ColorButton::paintEvent(QPaintEvent *event)
a52fcc76 121{
d9ea9628 122 QPushButton::paintEvent(event);
a52fcc76
JH
123
124 QPainter p(this);
125
126 const QRect r = rect().adjusted(SwatchMargin, SwatchMargin,
127 -SwatchMargin, -SwatchMargin);
128 p.setPen(QApplication::palette().color(QPalette::Dark));
641574bc 129 p.setBrush(QBrush(cur_color_));
a52fcc76
JH
130 p.drawRect(r);
131}
132
870ea3db
UH
133} // namespace widgets
134} // namespace pv