Debian Codegraph @

avogadro/qtgui/colorbutton.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 // Adapted from Avogadro 1.0 by Geoffrey Hutchison
7 // Contributed to Avogadro 2.0 by Geoffrey Hutchison
8
9 #include "colorbutton.h"
10
11 #include <QColorDialog>
12 #include <QPainter>
13
14 namespace Avogadro::QtGui {
15
16 ColorButton::ColorButton(QWidget* parent)
17 : QAbstractButton(parent), m_color(Qt::white), m_title("")
18 {
19 setMinimumSize(35, 20);
20
21 connect(this, SIGNAL(clicked()), this, SLOT(changeColor()));
22 }
23
24 ColorButton::ColorButton(const QColor& initial, QWidget* parent)
25 : QAbstractButton(parent), m_color(initial)
26 {
27 setMinimumSize(35, 20);
28
29 connect(this, SIGNAL(clicked()), this, SLOT(changeColor()));
30 }
31
32 void ColorButton::changeColor()
33 {
34 // This could be an ifdef for KColorDialog if KDE is present
35 QColor currentColor = m_color;
36
37 if (m_title == "")
38 m_color = QColorDialog::getColor(m_color, this);
39 else
40 m_color = QColorDialog::getColor(m_color, this, m_title);
41
42 if (currentColor == m_color || !m_color.isValid()) {
43 m_color = currentColor; // e.g., invalid color
44 return;
45 }
46
47 update();
48
49 emit colorChanged(m_color);
50 }
51
52 void ColorButton::setColor(const QColor& color)
53 {
54 m_color = color;
55 update();
56
57 emit colorChanged(m_color);
58 }
59
60 void ColorButton::setDialogTitle(const QString title)
61 {
62 m_title = title;
63 }
64
65 QColor ColorButton::color() const
66 {
67 return m_color;
68 }
69
70 void ColorButton::paintEvent(QPaintEvent*)
71 {
72 // TODO: If we go to RGBA colors, we should really show two pieces
73 // e.g. -----------
74 // | /|
75 // | non / |
76 // | alpha/ |
77 // | / |
78 // | /alpha
79 // | / |
80 // -----------
81
82 QPainter painter(this);
83
84 // outer border
85 painter.drawRect(0, 0, width(), height());
86 // inner color
87 painter.setBrush(m_color);
88 painter.drawRect(4, 4, width() - 8, height() - 8);
89 }
90
91 bool ColorButton::event(QEvent* e)
92 {
93 return QAbstractButton::event(e);
94 }
95
96 } // namespace Avogadro::QtGui