LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / libs / utils / qtcolorbutton.cpp
blob222e204c10d3d68a7b19c0bba81400cd15ec9e6d
1 /**
2 ******************************************************************************
4 * @file qtcolorbutton.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "qtcolorbutton.h"
31 #include <QtCore/QMimeData>
32 #include <QtWidgets/QApplication>
33 #include <QColorDialog>
34 #include <QDragEnterEvent>
35 #include <QDrag>
36 #include <QPainter>
38 namespace Utils {
39 class QtColorButtonPrivate {
40 QtColorButton *q_ptr;
41 Q_DECLARE_PUBLIC(QtColorButton)
42 public:
43 QColor m_color;
44 #ifndef QT_NO_DRAGANDDROP
45 QColor m_dragColor;
46 QPoint m_dragStart;
47 bool m_dragging;
48 #endif
49 bool m_backgroundCheckered;
50 bool m_alphaAllowed;
52 void slotEditColor();
53 QColor shownColor() const;
54 QPixmap generatePixmap() const;
57 void QtColorButtonPrivate::slotEditColor()
59 QColor newColor;
61 if (m_alphaAllowed) {
62 bool ok;
63 const QRgb rgba = QColorDialog::getRgba(m_color.rgba(), &ok, q_ptr);
64 if (!ok) {
65 return;
67 newColor = QColor::fromRgba(rgba);
68 } else {
69 newColor = QColorDialog::getColor(m_color, q_ptr);
70 if (!newColor.isValid()) {
71 return;
74 if (newColor == q_ptr->color()) {
75 return;
77 q_ptr->setColor(newColor);
78 emit q_ptr->colorChanged(m_color);
81 QColor QtColorButtonPrivate::shownColor() const
83 #ifndef QT_NO_DRAGANDDROP
84 if (m_dragging) {
85 return m_dragColor;
87 #endif
88 return m_color;
91 QPixmap QtColorButtonPrivate::generatePixmap() const
93 QPixmap pix(24, 24);
95 int pixSize = 20;
96 QBrush br(shownColor());
98 QPixmap pm(2 * pixSize, 2 * pixSize);
99 QPainter pmp(&pm);
101 pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
102 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
103 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
104 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);
105 pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, shownColor());
106 br = QBrush(pm);
108 QPainter p(&pix);
109 int corr = 1;
110 QRect r = pix.rect().adjusted(corr, corr, -corr, -corr);
111 p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
112 p.fillRect(r, br);
114 p.fillRect(r.width() / 4 + corr, r.height() / 4 + corr,
115 r.width() / 2, r.height() / 2,
116 QColor(shownColor().rgb()));
117 p.drawRect(pix.rect().adjusted(0, 0, -1, -1));
119 return pix;
122 ///////////////
124 QtColorButton::QtColorButton(QWidget *parent)
125 : QToolButton(parent)
127 d_ptr = new QtColorButtonPrivate;
128 d_ptr->q_ptr = this;
129 d_ptr->m_dragging = false;
130 d_ptr->m_backgroundCheckered = true;
131 d_ptr->m_alphaAllowed = true;
133 setAcceptDrops(true);
135 connect(this, SIGNAL(clicked()), this, SLOT(slotEditColor()));
136 setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
139 QtColorButton::~QtColorButton()
141 delete d_ptr;
144 void QtColorButton::setColor(const QColor &color)
146 if (d_ptr->m_color == color) {
147 return;
149 d_ptr->m_color = color;
150 update();
153 QColor QtColorButton::color() const
155 return d_ptr->m_color;
158 void QtColorButton::setBackgroundCheckered(bool checkered)
160 if (d_ptr->m_backgroundCheckered == checkered) {
161 return;
163 d_ptr->m_backgroundCheckered = checkered;
164 update();
167 bool QtColorButton::isBackgroundCheckered() const
169 return d_ptr->m_backgroundCheckered;
172 void QtColorButton::setAlphaAllowed(bool allowed)
174 d_ptr->m_alphaAllowed = allowed;
177 bool QtColorButton::isAlphaAllowed() const
179 return d_ptr->m_alphaAllowed;
182 void QtColorButton::paintEvent(QPaintEvent *event)
184 QToolButton::paintEvent(event);
186 if (!isEnabled()) {
187 return;
190 const int pixSize = 10;
191 QBrush br(d_ptr->shownColor());
192 if (d_ptr->m_backgroundCheckered) {
193 QPixmap pm(2 * pixSize, 2 * pixSize);
194 QPainter pmp(&pm);
195 pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
196 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
197 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
198 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
199 pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, d_ptr->shownColor());
200 br = QBrush(pm);
203 QPainter p(this);
204 const int corr = 5;
205 QRect r = rect().adjusted(corr, corr, -corr, -corr);
206 p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
207 p.fillRect(r, br);
209 // const int adjX = qRound(r.width() / 4.0);
210 // const int adjY = qRound(r.height() / 4.0);
211 // p.fillRect(r.adjusted(adjX, adjY, -adjX, -adjY),
212 // QColor(d_ptr->shownColor().rgb()));
214 p.fillRect(r.adjusted(0, r.height() * 3 / 4, 0, 0),
215 QColor(d_ptr->shownColor().rgb()));
216 p.fillRect(r.adjusted(0, 0, 0, -r.height() * 3 / 4),
217 QColor(d_ptr->shownColor().rgb()));
220 const QColor frameColor0(0, 0, 0, qRound(0.2 * (0xFF - d_ptr->shownColor().alpha())));
221 p.setPen(frameColor0);
222 p.drawRect(r.adjusted(adjX, adjY, -adjX - 1, -adjY - 1));
225 const QColor frameColor1(0, 0, 0, 26);
226 p.setPen(frameColor1);
227 p.drawRect(r.adjusted(1, 1, -2, -2));
228 const QColor frameColor2(0, 0, 0, 51);
229 p.setPen(frameColor2);
230 p.drawRect(r.adjusted(0, 0, -1, -1));
233 void QtColorButton::mousePressEvent(QMouseEvent *event)
235 #ifndef QT_NO_DRAGANDDROP
236 if (event->button() == Qt::LeftButton) {
237 d_ptr->m_dragStart = event->pos();
239 #endif
240 QToolButton::mousePressEvent(event);
243 void QtColorButton::mouseMoveEvent(QMouseEvent *event)
245 #ifndef QT_NO_DRAGANDDROP
246 if (event->buttons() & Qt::LeftButton &&
247 (d_ptr->m_dragStart - event->pos()).manhattanLength() > QApplication::startDragDistance()) {
248 QMimeData *mime = new QMimeData;
249 mime->setColorData(color());
250 QDrag *drg = new QDrag(this);
251 drg->setMimeData(mime);
252 drg->setPixmap(d_ptr->generatePixmap());
253 setDown(false);
254 event->accept();
255 drg->start();
256 return;
258 #endif
259 QToolButton::mouseMoveEvent(event);
262 #ifndef QT_NO_DRAGANDDROP
263 void QtColorButton::dragEnterEvent(QDragEnterEvent *event)
265 const QMimeData *mime = event->mimeData();
267 if (!mime->hasColor()) {
268 return;
271 event->accept();
272 d_ptr->m_dragColor = qvariant_cast<QColor>(mime->colorData());
273 d_ptr->m_dragging = true;
274 update();
277 void QtColorButton::dragLeaveEvent(QDragLeaveEvent *event)
279 event->accept();
280 d_ptr->m_dragging = false;
281 update();
284 void QtColorButton::dropEvent(QDropEvent *event)
286 event->accept();
287 d_ptr->m_dragging = false;
288 if (d_ptr->m_dragColor == color()) {
289 return;
291 setColor(d_ptr->m_dragColor);
292 emit colorChanged(color());
294 #endif // ifndef QT_NO_DRAGANDDROP
295 } // namespace Utils
297 #include "moc_qtcolorbutton.cpp"