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