PulseView  unreleased development snapshot
A Qt-based sigrok GUI
colorbutton.cpp
Go to the documentation of this file.
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 "colorbutton.hpp"
21 
22 #include <cassert>
23 
24 #include <QApplication>
25 #include <QColorDialog>
26 #include <QPainter>
27 
28 namespace pv {
29 namespace widgets {
30 
31 const int ColorButton::SwatchMargin = 7;
32 
33 ColorButton::ColorButton(QWidget *parent) :
34  QPushButton("", parent),
35  popup_(nullptr)
36 {
37  connect(this, SIGNAL(clicked(bool)), this, SLOT(on_clicked(bool)));
38 }
39 
40 ColorButton::ColorButton(int rows, int cols, QWidget *parent) :
41  QPushButton("", parent),
42  popup_(new ColorPopup(rows, cols, this))
43 {
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)));
47 }
48 
50 {
51  return popup_;
52 }
53 
54 const QColor& ColorButton::color() const
55 {
56  return cur_color_;
57 }
58 
60 {
61  cur_color_ = color;
62 
63  if (popup_) {
64  const unsigned int rows = popup_->well_array().numRows();
65  const unsigned int cols = popup_->well_array().numCols();
66 
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;
73  }
74  }
75 }
76 
77 void ColorButton::set_palette(const QColor *const palette)
78 {
79  assert(palette);
80  assert(popup_);
81 
82  const unsigned int rows = popup_->well_array().numRows();
83  const unsigned int cols = popup_->well_array().numCols();
84 
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]));
88 }
89 
91 {
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();
103  }
104 }
105 
106 void ColorButton::on_selected(int row, int col)
107 {
108  assert(popup_);
109 
110  cur_color_ = popup_->well_array().cellBrush(row, col).color();
112 }
113 
115 {
116  cur_color_ = color;
118 }
119 
120 void ColorButton::paintEvent(QPaintEvent *event)
121 {
122  QPushButton::paintEvent(event);
123 
124  QPainter p(this);
125 
126  const QRect r = rect().adjusted(SwatchMargin, SwatchMargin,
128  p.setPen(QApplication::palette().color(QPalette::Dark));
129  p.setBrush(QBrush(cur_color_));
130  p.drawRect(r);
131 }
132 
133 } // namespace widgets
134 } // namespace pv
void paintEvent(QPaintEvent *event)
virtual void setSelected(int row, int col)
Definition: wellarray.cpp:209
void set_position(const QPoint point, Position pos)
Definition: popup.cpp:90
WellArray & well_array()
Definition: colorpopup.cpp:39
int numCols() const
Definition: wellarray.hpp:94
QBrush cellBrush(int row, int col)
Definition: wellarray.cpp:247
static const int SwatchMargin
Definition: colorbutton.hpp:32
void selected(const QColor &color)
void on_selected(int row, int col)
virtual void setCellBrush(int row, int col, const QBrush &)
Definition: wellarray.cpp:231
void on_color_selected(const QColor &color)
int numRows() const
Definition: wellarray.hpp:91
virtual void show()
Definition: popup.cpp:119
void set_palette(const QColor *const palette)
Definition: colorbutton.cpp:77
it will come up with a session that has the demo device selected That you can get to know the program even when you don t have any hardware to use it you see a list of devices PulseView has recognized If the device you want to use is you can just select it here image::device_selector_dropdown png[] If it s not you ll need to scan for it first Since most serial port and Ethernet devices can t be auto this is usually required for those To do either choose the Connect to Device option from the list or click on the button itself You will see the following you ll need to pick a driver that you want to use In order to do this
Definition: acquisition.txt:14
ColorButton(QWidget *parent)
Definition: colorbutton.cpp:33
void set_color(QColor color)
Definition: colorbutton.cpp:59
const QColor & color() const
Definition: colorbutton.cpp:54
virtual void setCurrent(int row, int col)
Definition: wellarray.cpp:185