DecodeSignal: Support unitsize > 1 for logic output
[pulseview.git] / pv / widgets / colorbutton.cpp
blob702fddeb92541eaf0b4c8b00657901c45d73b811
1 /*
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, see <http://www.gnu.org/licenses/>.
20 #include "colorbutton.hpp"
22 #include <cassert>
24 #include <QApplication>
25 #include <QColorDialog>
26 #include <QPainter>
28 namespace pv {
29 namespace widgets {
31 const int ColorButton::SwatchMargin = 7;
33 ColorButton::ColorButton(QWidget *parent) :
34 QPushButton("", parent),
35 popup_(nullptr)
37 connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
40 ColorButton::ColorButton(int rows, int cols, QWidget *parent) :
41 QPushButton("", parent),
42 popup_(new ColorPopup(rows, cols, this))
44 connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
45 connect(popup_, SIGNAL(selected(int, int)),
46 this, SLOT(on_selected(int, int)));
49 ColorPopup* ColorButton::popup()
51 return popup_;
54 const QColor& ColorButton::color() const
56 return cur_color_;
59 void ColorButton::set_color(QColor color)
61 cur_color_ = color;
63 if (popup_) {
64 const unsigned int rows = popup_->well_array().numRows();
65 const unsigned int cols = popup_->well_array().numCols();
67 for (unsigned int r = 0; r < rows; r++)
68 for (unsigned int c = 0; c < cols; c++)
69 if (popup_->well_array().cellBrush(r, c).color() == color) {
70 popup_->well_array().setSelected(r, c);
71 popup_->well_array().setCurrent(r, c);
72 return;
77 void ColorButton::set_palette(const QColor *const palette)
79 assert(palette);
80 assert(popup_);
82 const unsigned int rows = popup_->well_array().numRows();
83 const unsigned int cols = popup_->well_array().numCols();
85 for (unsigned int r = 0; r < rows; r++)
86 for (unsigned int c = 0; c < cols; c++)
87 popup_->well_array().setCellBrush(r, c, QBrush(palette[r * cols + c]));
90 void ColorButton::on_clicked(bool)
92 if (popup_) {
93 popup_->set_position(mapToGlobal(rect().center()), Popup::Bottom);
94 popup_->show();
95 } else {
96 QColorDialog dlg(this);
97 dlg.setOption(QColorDialog::ShowAlphaChannel);
98 dlg.setOption(QColorDialog::DontUseNativeDialog);
99 connect(&dlg, SIGNAL(colorSelected(const QColor)),
100 this, SLOT(on_color_selected(const QColor)));
101 dlg.setCurrentColor(cur_color_);
102 dlg.exec();
106 void ColorButton::on_selected(int row, int col)
108 assert(popup_);
110 cur_color_ = popup_->well_array().cellBrush(row, col).color();
111 selected(cur_color_);
114 void ColorButton::on_color_selected(const QColor& color)
116 cur_color_ = color;
117 selected(cur_color_);
120 void ColorButton::paintEvent(QPaintEvent *event)
122 QPushButton::paintEvent(event);
124 QPainter p(this);
126 const QRect r = rect().adjusted(SwatchMargin, SwatchMargin,
127 -SwatchMargin, -SwatchMargin);
128 p.setPen(QApplication::palette().color(QPalette::Dark));
129 p.setBrush(QBrush(cur_color_));
130 p.drawRect(r);
133 } // namespace widgets
134 } // namespace pv