]> sigrok.org Git - pulseview.git/blob - pv/widgets/colorbutton.cpp
Implement "fill logic signal high areas" feature
[pulseview.git] / pv / widgets / colorbutton.cpp
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
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "colorbutton.hpp"
21
22 #include <cassert>
23
24 #include <QApplication>
25 #include <QColorDialog>
26 #include <QPainter>
27
28 namespace pv {
29 namespace widgets {
30
31 const int ColorButton::SwatchMargin = 7;
32
33 ColorButton::ColorButton(QWidget *parent) :
34         QPushButton("", parent),
35         popup_(nullptr)
36 {
37         connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
38 }
39
40 ColorButton::ColorButton(int rows, int cols, QWidget *parent) :
41         QPushButton("", parent),
42         popup_(new ColorPopup(rows, cols, this))
43 {
44         connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
45         connect(popup_, SIGNAL(selected(int, int)),
46                 this, SLOT(on_selected(int, int)));
47 }
48
49 ColorPopup* ColorButton::popup()
50 {
51         return popup_;
52 }
53
54 const QColor& ColorButton::color() const
55 {
56         return cur_color_;
57 }
58
59 void ColorButton::set_color(QColor color)
60 {
61         cur_color_ = color;
62
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         }
75 }
76
77 void ColorButton::set_palette(const QColor *const palette)
78 {
79         assert(palette);
80         assert(popup_);
81
82         const unsigned int rows = popup_->well_array().numRows();
83         const unsigned int cols = popup_->well_array().numCols();
84
85         for (unsigned int r = 0; r < rows; r++)
86                 for (unsigned int c = 0; c < cols; c++)
87                         popup_->well_array().setCellBrush(r, c, QBrush(palette[r * cols + c]));
88 }
89
90 void ColorButton::on_clicked(bool)
91 {
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         }
104 }
105
106 void ColorButton::on_selected(int row, int col)
107 {
108         assert(popup_);
109
110         cur_color_ = popup_->well_array().cellBrush(row, col).color();
111         selected(cur_color_);
112 }
113
114 void ColorButton::on_color_selected(const QColor& color)
115 {
116         cur_color_ = color;
117         selected(cur_color_);
118 }
119
120 void ColorButton::paintEvent(QPaintEvent *event)
121 {
122         QPushButton::paintEvent(event);
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));
129         p.setBrush(QBrush(cur_color_));
130         p.drawRect(r);
131 }
132
133 }  // namespace widgets
134 }  // namespace pv