]> sigrok.org Git - pulseview.git/blame - pv/widgets/colorbutton.cpp
DecodeSignal: When lacking input, retry only when data is available
[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>
25#include <QPainter>
26
a52fcc76
JH
27namespace pv {
28namespace widgets {
29
641574bc 30const int ColorButton::SwatchMargin = 7;
a52fcc76 31
641574bc 32ColorButton::ColorButton(int rows, int cols, QWidget *parent) :
a52fcc76 33 QPushButton("", parent),
8dbbc7f0 34 popup_(rows, cols, this)
a52fcc76
JH
35{
36 connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
8dbbc7f0 37 connect(&popup_, SIGNAL(selected(int, int)),
a52fcc76
JH
38 this, SLOT(on_selected(int, int)));
39}
40
641574bc 41ColorPopup& ColorButton::popup()
a52fcc76 42{
8dbbc7f0 43 return popup_;
a52fcc76
JH
44}
45
641574bc 46const QColor& ColorButton::color() const
a52fcc76 47{
641574bc 48 return cur_color_;
a52fcc76
JH
49}
50
641574bc 51void ColorButton::set_color(QColor color)
a52fcc76 52{
641574bc 53 cur_color_ = color;
a52fcc76 54
8dbbc7f0
JH
55 const unsigned int rows = popup_.well_array().numRows();
56 const unsigned int cols = popup_.well_array().numCols();
a52fcc76
JH
57
58 for (unsigned int r = 0; r < rows; r++)
59 for (unsigned int c = 0; c < cols; c++)
641574bc 60 if (popup_.well_array().cellBrush(r, c).color() == color) {
8dbbc7f0
JH
61 popup_.well_array().setSelected(r, c);
62 popup_.well_array().setCurrent(r, c);
a52fcc76 63 return;
bb19aac4 64 }
a52fcc76
JH
65}
66
641574bc 67void ColorButton::set_palette(const QColor *const palette)
a52fcc76
JH
68{
69 assert(palette);
70
8dbbc7f0
JH
71 const unsigned int rows = popup_.well_array().numRows();
72 const unsigned int cols = popup_.well_array().numCols();
a52fcc76
JH
73
74 for (unsigned int r = 0; r < rows; r++)
75 for (unsigned int c = 0; c < cols; c++)
8dbbc7f0 76 popup_.well_array().setCellBrush(r, c,
a52fcc76
JH
77 QBrush(palette[r * cols + c]));
78}
79
641574bc 80void ColorButton::on_clicked(bool)
a52fcc76 81{
8dbbc7f0
JH
82 popup_.set_position(mapToGlobal(rect().center()), Popup::Bottom);
83 popup_.show();
a52fcc76
JH
84}
85
641574bc 86void ColorButton::on_selected(int row, int col)
a52fcc76 87{
641574bc
SA
88 cur_color_ = popup_.well_array().cellBrush(row, col).color();
89 selected(cur_color_);
a52fcc76
JH
90}
91
641574bc 92void ColorButton::paintEvent(QPaintEvent *event)
a52fcc76 93{
d9ea9628 94 QPushButton::paintEvent(event);
a52fcc76
JH
95
96 QPainter p(this);
97
98 const QRect r = rect().adjusted(SwatchMargin, SwatchMargin,
99 -SwatchMargin, -SwatchMargin);
100 p.setPen(QApplication::palette().color(QPalette::Dark));
641574bc 101 p.setBrush(QBrush(cur_color_));
a52fcc76
JH
102 p.drawRect(r);
103}
104
870ea3db
UH
105} // namespace widgets
106} // namespace pv