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"
24 #include <QApplication>
25 #include <QColorDialog>
31 const int ColorButton::SwatchMargin
= 7;
33 ColorButton::ColorButton(QWidget
*parent
) :
34 QPushButton("", parent
),
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()
54 const QColor
& ColorButton::color() const
59 void ColorButton::set_color(QColor color
)
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
);
77 void ColorButton::set_palette(const QColor
*const palette
)
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)
93 popup_
->set_position(mapToGlobal(rect().center()), Popup::Bottom
);
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_
);
106 void ColorButton::on_selected(int row
, int col
)
110 cur_color_
= popup_
->well_array().cellBrush(row
, col
).color();
111 selected(cur_color_
);
114 void ColorButton::on_color_selected(const QColor
& color
)
117 selected(cur_color_
);
120 void ColorButton::paintEvent(QPaintEvent
*event
)
122 QPushButton::paintEvent(event
);
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_
));
133 } // namespace widgets