]> sigrok.org Git - pulseview.git/blame - pv/widgets/colourbutton.cpp
Made Probes popup reusable
[pulseview.git] / pv / widgets / colourbutton.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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "colourbutton.h"
22
23namespace pv {
24namespace widgets {
25
26const int ColourButton::SwatchMargin = 7;
27
28ColourButton::ColourButton(int rows, int cols, QWidget *parent) :
29 QPushButton("", parent),
30 _popup(rows, cols, this)
31{
32 connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
33 connect(&_popup, SIGNAL(selected(int, int)),
34 this, SLOT(on_selected(int, int)));
35}
36
37ColourPopup& ColourButton::popup()
38{
39 return _popup;
40}
41
42const QColor& ColourButton::colour() const
43{
44 return _cur_colour;
45}
46
47void ColourButton::set_colour(QColor colour)
48{
49 _cur_colour = colour;
50
51 const unsigned int rows = _popup.well_array().numRows();
52 const unsigned int cols = _popup.well_array().numCols();
53
54 for (unsigned int r = 0; r < rows; r++)
55 for (unsigned int c = 0; c < cols; c++)
56 if (_popup.well_array().cellBrush(r, c).color() ==
57 colour)
58 {
59 _popup.well_array().setSelected(r, c);
60 _popup.well_array().setCurrent(r, c);
61 return;
62 }
63}
64
65void ColourButton::set_palette(const QColor *const palette)
66{
67 assert(palette);
68
69 const unsigned int rows = _popup.well_array().numRows();
70 const unsigned int cols = _popup.well_array().numCols();
71
72 for (unsigned int r = 0; r < rows; r++)
73 for (unsigned int c = 0; c < cols; c++)
74 _popup.well_array().setCellBrush(r, c,
75 QBrush(palette[r * cols + c]));
76}
77
78void ColourButton::on_clicked(bool)
79{
80 _popup.set_position(mapToGlobal(rect().center()), Popup::Bottom);
81 _popup.show();
82}
83
84void ColourButton::on_selected(int row, int col)
85{
86 _cur_colour = _popup.well_array().cellBrush(row, col).color();
87 selected(_cur_colour);
88}
89
90void ColourButton::paintEvent(QPaintEvent *e)
91{
92 QPushButton::paintEvent(e);
93
94 QPainter p(this);
95
96 const QRect r = rect().adjusted(SwatchMargin, SwatchMargin,
97 -SwatchMargin, -SwatchMargin);
98 p.setPen(QApplication::palette().color(QPalette::Dark));
99 p.setBrush(QBrush(_cur_colour));
100 p.drawRect(r);
101}
102
103} // widgets
104} // pv