• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

kcachegrind

  • sources
  • kde-4.14
  • kdesdk
  • kcachegrind
  • qcachegrind
qtcolorbutton.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copied from Qt 4.6.0 sources, directory tools/shared/qtgradienteditor
4 ** with small modifications to allow compilation with Qt 4.4 and Qt 5.0
5 **
6 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
7 ** All rights reserved.
8 ** Contact: Nokia Corporation (qt-info@nokia.com)
9 **
10 ** This file is part of the tools applications of the Qt Toolkit.
11 **
12 ** No Commercial Usage
13 ** This file contains pre-release code and may not be distributed.
14 ** You may use this file in accordance with the terms and conditions
15 ** contained in the Technology Preview License Agreement accompanying
16 ** this package.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 2.1 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 2.1 requirements
24 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** In addition, as a special exception, Nokia gives you certain additional
27 ** rights. These rights are described in the Nokia Qt LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** If you have questions regarding the use of this file, please contact
31 ** Nokia at qt-info@nokia.com.
32 **
33 **
34 ****************************************************************************/
35 
36 #include "qtcolorbutton.h"
37 
38 #include <QColorDialog>
39 #include <QPainter>
40 #include <QMimeData>
41 #include <QDrag>
42 #include <QDragEnterEvent>
43 #include <QApplication>
44 
45 QT_BEGIN_NAMESPACE
46 
47 class QtColorButtonPrivate
48 {
49  QtColorButton *q_ptr;
50  Q_DECLARE_PUBLIC(QtColorButton)
51 public:
52  QColor m_color;
53 #ifndef QT_NO_DRAGANDDROP
54  QColor m_dragColor;
55  QPoint m_dragStart;
56  bool m_dragging;
57 #endif
58  bool m_backgroundCheckered;
59 
60  void slotEditColor();
61  QColor shownColor() const;
62  QPixmap generatePixmap() const;
63 };
64 
65 void QtColorButtonPrivate::slotEditColor()
66 {
67  const QColor newColor = QColorDialog::getColor(m_color, q_ptr);
68  if (!newColor.isValid() || newColor == q_ptr->color())
69  return;
70  q_ptr->setColor(newColor);
71  emit q_ptr->colorChanged(m_color);
72 }
73 
74 QColor QtColorButtonPrivate::shownColor() const
75 {
76 #ifndef QT_NO_DRAGANDDROP
77  if (m_dragging)
78  return m_dragColor;
79 #endif
80  return m_color;
81 }
82 
83 QPixmap QtColorButtonPrivate::generatePixmap() const
84 {
85  QPixmap pix(24, 24);
86 
87  int pixSize = 20;
88  QBrush br(shownColor());
89 
90  QPixmap pm(2 * pixSize, 2 * pixSize);
91  QPainter pmp(&pm);
92  pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
93  pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
94  pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
95  pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);
96  pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, shownColor());
97  br = QBrush(pm);
98 
99  QPainter p(&pix);
100  int corr = 1;
101  QRect r = pix.rect().adjusted(corr, corr, -corr, -corr);
102  p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
103  p.fillRect(r, br);
104 
105  p.fillRect(r.width() / 4 + corr, r.height() / 4 + corr,
106  r.width() / 2, r.height() / 2,
107  QColor(shownColor().rgb()));
108  p.drawRect(pix.rect().adjusted(0, 0, -1, -1));
109 
110  return pix;
111 }
112 
114 
115 QtColorButton::QtColorButton(QWidget *parent)
116  : QToolButton(parent), d_ptr(new QtColorButtonPrivate)
117 {
118  d_ptr->q_ptr = this;
119  d_ptr->m_dragging = false;
120  d_ptr->m_backgroundCheckered = true;
121 
122  setAcceptDrops(true);
123 
124  connect(this, SIGNAL(clicked()), this, SLOT(slotEditColor()));
125  setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
126 }
127 
128 QtColorButton::~QtColorButton()
129 {
130  delete d_ptr;
131 }
132 
133 void QtColorButton::setColor(const QColor &color)
134 {
135  if (d_ptr->m_color == color)
136  return;
137  d_ptr->m_color = color;
138  update();
139 }
140 
141 QColor QtColorButton::color() const
142 {
143  return d_ptr->m_color;
144 }
145 
146 void QtColorButton::setBackgroundCheckered(bool checkered)
147 {
148  if (d_ptr->m_backgroundCheckered == checkered)
149  return;
150  d_ptr->m_backgroundCheckered = checkered;
151  update();
152 }
153 
154 bool QtColorButton::isBackgroundCheckered() const
155 {
156  return d_ptr->m_backgroundCheckered;
157 }
158 
159 void QtColorButton::paintEvent(QPaintEvent *event)
160 {
161  QToolButton::paintEvent(event);
162  if (!isEnabled())
163  return;
164 
165  const int pixSize = 10;
166  QBrush br(d_ptr->shownColor());
167  if (d_ptr->m_backgroundCheckered) {
168  QPixmap pm(2 * pixSize, 2 * pixSize);
169  QPainter pmp(&pm);
170  pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
171  pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
172  pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
173  pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
174  pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, d_ptr->shownColor());
175  br = QBrush(pm);
176  }
177 
178  QPainter p(this);
179  const int corr = 4;
180  QRect r = rect().adjusted(corr, corr, -corr, -corr);
181  p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
182  p.fillRect(r, br);
183 
184  //const int adjX = qRound(r.width() / 4.0);
185  //const int adjY = qRound(r.height() / 4.0);
186  //p.fillRect(r.adjusted(adjX, adjY, -adjX, -adjY),
187  // QColor(d_ptr->shownColor().rgb()));
188  /*
189  p.fillRect(r.adjusted(0, r.height() * 3 / 4, 0, 0),
190  QColor(d_ptr->shownColor().rgb()));
191  p.fillRect(r.adjusted(0, 0, 0, -r.height() * 3 / 4),
192  QColor(d_ptr->shownColor().rgb()));
193  */
194  /*
195  const QColor frameColor0(0, 0, 0, qRound(0.2 * (0xFF - d_ptr->shownColor().alpha())));
196  p.setPen(frameColor0);
197  p.drawRect(r.adjusted(adjX, adjY, -adjX - 1, -adjY - 1));
198  */
199 
200  const QColor frameColor1(0, 0, 0, 26);
201  p.setPen(frameColor1);
202  p.drawRect(r.adjusted(1, 1, -2, -2));
203  const QColor frameColor2(0, 0, 0, 51);
204  p.setPen(frameColor2);
205  p.drawRect(r.adjusted(0, 0, -1, -1));
206 }
207 
208 void QtColorButton::mousePressEvent(QMouseEvent *event)
209 {
210 #ifndef QT_NO_DRAGANDDROP
211  if (event->button() == Qt::LeftButton)
212  d_ptr->m_dragStart = event->pos();
213 #endif
214  QToolButton::mousePressEvent(event);
215 }
216 
217 void QtColorButton::mouseMoveEvent(QMouseEvent *event)
218 {
219 #ifndef QT_NO_DRAGANDDROP
220  if (event->buttons() & Qt::LeftButton &&
221  (d_ptr->m_dragStart - event->pos()).manhattanLength() > QApplication::startDragDistance()) {
222  QMimeData *mime = new QMimeData;
223  mime->setColorData(color());
224  QDrag *drg = new QDrag(this);
225  drg->setMimeData(mime);
226  drg->setPixmap(d_ptr->generatePixmap());
227  setDown(false);
228  event->accept();
229  drg->start();
230  return;
231  }
232 #endif
233  QToolButton::mouseMoveEvent(event);
234 }
235 
236 #ifndef QT_NO_DRAGANDDROP
237 void QtColorButton::dragEnterEvent(QDragEnterEvent *event)
238 {
239  const QMimeData *mime = event->mimeData();
240  if (!mime->hasColor())
241  return;
242 
243  event->accept();
244  d_ptr->m_dragColor = qvariant_cast<QColor>(mime->colorData());
245  d_ptr->m_dragging = true;
246  update();
247 }
248 
249 void QtColorButton::dragLeaveEvent(QDragLeaveEvent *event)
250 {
251  event->accept();
252  d_ptr->m_dragging = false;
253  update();
254 }
255 
256 void QtColorButton::dropEvent(QDropEvent *event)
257 {
258  event->accept();
259  d_ptr->m_dragging = false;
260  if (d_ptr->m_dragColor == color())
261  return;
262  setColor(d_ptr->m_dragColor);
263  emit colorChanged(color());
264 }
265 #endif
266 
267 QT_END_NAMESPACE
268 
269 #include "qtcolorbutton.moc"
QAbstractButton::setDown
void setDown(bool)
QWidget
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
QDrag::setMimeData
void setMimeData(QMimeData *data)
QDrag::start
Qt::DropAction start(QFlags< Qt::DropAction > request)
QMimeData::setColorData
void setColorData(const QVariant &color)
QDrag::setPixmap
void setPixmap(const QPixmap &pixmap)
QtColorButton::mousePressEvent
void mousePressEvent(QMouseEvent *event)
Definition: qtcolorbutton.cpp:208
QSizePolicy
QAbstractButton::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *e)
QtColorButton
Definition: qtcolorbutton.h:42
QRect::height
int height() const
QBrush
QPoint
QMouseEvent
QtColorButton::QtColorButton
QtColorButton(QWidget *parent=0)
Definition: qtcolorbutton.cpp:115
QtColorButton::~QtColorButton
~QtColorButton()
Definition: qtcolorbutton.cpp:128
QMouseEvent::buttons
Qt::MouseButtons buttons() const
QMimeData
QWidget::update
void update()
QPainter::setBrushOrigin
void setBrushOrigin(int x, int y)
QtColorButton::dragEnterEvent
void dragEnterEvent(QDragEnterEvent *event)
Definition: qtcolorbutton.cpp:237
QPainter::drawRect
void drawRect(const QRectF &rectangle)
QRect
QWidget::isEnabled
bool isEnabled() const
QToolButton::mousePressEvent
virtual void mousePressEvent(QMouseEvent *e)
QPainter::setPen
void setPen(const QColor &color)
QMouseEvent::button
Qt::MouseButton button() const
QDropEvent
QToolButton::paintEvent
virtual void paintEvent(QPaintEvent *event)
QPainter
QDrag
QAbstractButton::clicked
void clicked(bool checked)
QtColorButton::setColor
void setColor(const QColor &color)
Definition: qtcolorbutton.cpp:133
QColor
QWidget::setSizePolicy
void setSizePolicy(QSizePolicy)
QMimeData::colorData
QVariant colorData() const
QtColorButton::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *event)
Definition: qtcolorbutton.cpp:217
QtColorButton::colorChanged
void colorChanged(const QColor &color)
QWidget::rect
QRect rect() const
QPixmap
QRect::adjusted
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const
QWidget::setAcceptDrops
void setAcceptDrops(bool on)
QtColorButton::setBackgroundCheckered
void setBackgroundCheckered(bool checkered)
Definition: qtcolorbutton.cpp:146
QToolButton
QColorDialog::getColor
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, QFlags< QColorDialog::ColorDialogOption > options)
QDragLeaveEvent
QtColorButton::dropEvent
void dropEvent(QDropEvent *event)
Definition: qtcolorbutton.cpp:256
QtColorButton::dragLeaveEvent
void dragLeaveEvent(QDragLeaveEvent *event)
Definition: qtcolorbutton.cpp:249
QRect::width
int width() const
QDragEnterEvent
QtColorButton::color
QColor color() const
Definition: qtcolorbutton.cpp:141
QMouseEvent::pos
const QPoint & pos() const
QPaintEvent
QtColorButton::paintEvent
void paintEvent(QPaintEvent *event)
Definition: qtcolorbutton.cpp:159
qtcolorbutton.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QtColorButton::isBackgroundCheckered
bool isBackgroundCheckered() const
Definition: qtcolorbutton.cpp:154
QApplication::startDragDistance
int startDragDistance()
QColor::isValid
bool isValid() const
QMimeData::hasColor
bool hasColor() const
QRect::rect
void rect(int *x, int *y, int *width, int *height) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:39:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kcachegrind

Skip menu "kcachegrind"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal