2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include "colourbutton.hpp"
25 #include <QApplication>
31 const int ColourButton::SwatchMargin = 7;
33 ColourButton::ColourButton(int rows, int cols, QWidget *parent) :
34 QPushButton("", parent),
35 popup_(rows, cols, this)
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)));
42 ColourPopup& ColourButton::popup()
47 const QColor& ColourButton::colour() const
52 void ColourButton::set_colour(QColor colour)
56 const unsigned int rows = popup_.well_array().numRows();
57 const unsigned int cols = popup_.well_array().numCols();
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() == colour) {
62 popup_.well_array().setSelected(r, c);
63 popup_.well_array().setCurrent(r, c);
68 void ColourButton::set_palette(const QColor *const palette)
72 const unsigned int rows = popup_.well_array().numRows();
73 const unsigned int cols = popup_.well_array().numCols();
75 for (unsigned int r = 0; r < rows; r++)
76 for (unsigned int c = 0; c < cols; c++)
77 popup_.well_array().setCellBrush(r, c,
78 QBrush(palette[r * cols + c]));
81 void ColourButton::on_clicked(bool)
83 popup_.set_position(mapToGlobal(rect().center()), Popup::Bottom);
87 void ColourButton::on_selected(int row, int col)
89 cur_colour_ = popup_.well_array().cellBrush(row, col).color();
90 selected(cur_colour_);
93 void ColourButton::paintEvent(QPaintEvent *event)
95 QPushButton::paintEvent(event);
99 const QRect r = rect().adjusted(SwatchMargin, SwatchMargin,
100 -SwatchMargin, -SwatchMargin);
101 p.setPen(QApplication::palette().color(QPalette::Dark));
102 p.setBrush(QBrush(cur_colour_));