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