]> sigrok.org Git - pulseview.git/blame - pv/widgets/colourbutton.cpp
InputFile: Don't try to create device twice
[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
2acdb232 21#include "colourbutton.hpp"
a52fcc76 22
b213ef09
JH
23#include <assert.h>
24
25#include <QApplication>
26#include <QPainter>
27
a52fcc76
JH
28namespace pv {
29namespace widgets {
30
31const int ColourButton::SwatchMargin = 7;
32
33ColourButton::ColourButton(int rows, int cols, QWidget *parent) :
34 QPushButton("", parent),
8dbbc7f0 35 popup_(rows, cols, this)
a52fcc76
JH
36{
37 connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
8dbbc7f0 38 connect(&popup_, SIGNAL(selected(int, int)),
a52fcc76
JH
39 this, SLOT(on_selected(int, int)));
40}
41
42ColourPopup& ColourButton::popup()
43{
8dbbc7f0 44 return popup_;
a52fcc76
JH
45}
46
47const QColor& ColourButton::colour() const
48{
8dbbc7f0 49 return cur_colour_;
a52fcc76
JH
50}
51
52void ColourButton::set_colour(QColor colour)
53{
8dbbc7f0 54 cur_colour_ = colour;
a52fcc76 55
8dbbc7f0
JH
56 const unsigned int rows = popup_.well_array().numRows();
57 const unsigned int cols = popup_.well_array().numCols();
a52fcc76
JH
58
59 for (unsigned int r = 0; r < rows; r++)
60 for (unsigned int c = 0; c < cols; c++)
2ad82c2e 61 if (popup_.well_array().cellBrush(r, c).color() == colour) {
8dbbc7f0
JH
62 popup_.well_array().setSelected(r, c);
63 popup_.well_array().setCurrent(r, c);
a52fcc76
JH
64 return;
65 }
66}
67
68void ColourButton::set_palette(const QColor *const palette)
69{
70 assert(palette);
71
8dbbc7f0
JH
72 const unsigned int rows = popup_.well_array().numRows();
73 const unsigned int cols = popup_.well_array().numCols();
a52fcc76
JH
74
75 for (unsigned int r = 0; r < rows; r++)
76 for (unsigned int c = 0; c < cols; c++)
8dbbc7f0 77 popup_.well_array().setCellBrush(r, c,
a52fcc76
JH
78 QBrush(palette[r * cols + c]));
79}
80
81void ColourButton::on_clicked(bool)
82{
8dbbc7f0
JH
83 popup_.set_position(mapToGlobal(rect().center()), Popup::Bottom);
84 popup_.show();
a52fcc76
JH
85}
86
87void ColourButton::on_selected(int row, int col)
88{
8dbbc7f0
JH
89 cur_colour_ = popup_.well_array().cellBrush(row, col).color();
90 selected(cur_colour_);
a52fcc76
JH
91}
92
d9ea9628 93void ColourButton::paintEvent(QPaintEvent *event)
a52fcc76 94{
d9ea9628 95 QPushButton::paintEvent(event);
a52fcc76
JH
96
97 QPainter p(this);
98
99 const QRect r = rect().adjusted(SwatchMargin, SwatchMargin,
100 -SwatchMargin, -SwatchMargin);
101 p.setPen(QApplication::palette().color(QPalette::Dark));
8dbbc7f0 102 p.setBrush(QBrush(cur_colour_));
a52fcc76
JH
103 p.drawRect(r);
104}
105
106} // widgets
107} // pv