Qt 4.8
toolbarcolorbox.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "toolbarcolorbox.h"
43 
44 #include "../qmlinspectorconstants.h"
45 
46 #include <QtGui/QPixmap>
47 #include <QtGui/QPainter>
48 #include <QtGui/QMenu>
49 #include <QtGui/QAction>
50 #include <QtGui/QContextMenuEvent>
51 #include <QtGui/QClipboard>
52 #include <QtGui/QApplication>
53 #include <QtGui/QColorDialog>
54 #include <QtGui/QDrag>
55 
56 #include <QtCore/QMimeData>
57 #include <QtCore/QDebug>
58 
59 namespace QmlJSDebugger {
60 
62  QLabel(parent)
63 {
64  m_copyHexColor = new QAction(QIcon(QLatin1String(":/qml/images/color-picker-hicontrast.png")),
65  tr("Copy Color"), this);
67  setScaledContents(false);
68 }
69 
71 {
72  m_color = color;
73 
75  setPixmap(pix);
76  update();
77 }
78 
80 {
81  m_dragBeginPoint = event->pos();
82  m_dragStarted = false;
83 }
84 
86 {
87 
88  if (event->buttons() & Qt::LeftButton
89  && (QPoint(event->pos() - m_dragBeginPoint).manhattanLength()
91  && !m_dragStarted)
92  {
93  m_dragStarted = true;
94  QDrag *drag = new QDrag(this);
95  QMimeData *mimeData = new QMimeData;
96 
97  mimeData->setText(m_color.name());
98  drag->setMimeData(mimeData);
99  drag->setPixmap(createDragPixmap());
100 
101  drag->exec();
102  }
103 }
104 
106 {
107  QPixmap pix(size, size);
108  QPainter p(&pix);
109 
110  QColor borderColor1 = QColor(143, 143 ,143);
111  QColor borderColor2 = QColor(43, 43, 43);
112 
113  p.setBrush(QBrush(m_color));
114  p.setPen(QPen(QBrush(borderColor2),1));
115 
116  p.fillRect(0, 0, size, size, borderColor1);
117  p.drawRect(1,1, size - 3, size - 3);
118  return pix;
119 }
120 
122 {
123  QMenu contextMenu;
124  contextMenu.addAction(m_copyHexColor);
125  contextMenu.exec(ev->globalPos());
126 }
127 
129 {
131  clipboard->setText(m_color.name());
132 }
133 
134 } // namespace QmlJSDebugger
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
static QWaylandClipboard * clipboard
bool event(QEvent *e)
em>Reimplemented Function
Definition: qlabel.cpp:1085
void setPixmap(const QPixmap &)
Sets pixmap as the pixmap used to represent the data in a drag and drop operation.
Definition: qdrag.cpp:166
The QDrag class provides support for MIME-based drag and drop data transfer.
Definition: qdrag.h:61
QSize size() const
void setMimeData(QMimeData *data)
Sets the data to be sent to the given MIME data.
Definition: qdrag.cpp:142
The QContextMenuEvent class contains parameters that describe a context menu event.
Definition: qevent.h:396
ToolBarColorBox(QWidget *parent=0)
#define SLOT(a)
Definition: qobjectdefs.h:226
void contextMenuEvent(QContextMenuEvent *ev)
This event handler, for event event, can be reimplemented in a subclass to receive widget context men...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Qt::DropAction exec(Qt::DropActions supportedActions=Qt::MoveAction)
Starts the drag and drop operation and returns a value indicating the requested drop action when it i...
Definition: qdrag.cpp:243
void setText(const QString &, Mode mode=Clipboard)
Copies text into the clipboard as plain text.
Definition: qclipboard.cpp:375
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:64
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
void setPixmap(const QPixmap &)
Definition: qlabel.cpp:439
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
#define SIGNAL(a)
Definition: qobjectdefs.h:227
static const int DragStartDistance
static QClipboard * clipboard()
Returns a pointer to the application global clipboard.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
void setScaledContents(bool)
Definition: qlabel.cpp:1549
int width() const
The QClipboard class provides access to the window system clipboard.
Definition: qclipboard.h:62
void setColor(const QColor &color)
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
void mousePressEvent(QMouseEvent *ev)
em>Reimplemented Function
QAction * exec()
Executes this menu synchronously.
Definition: qmenu.cpp:2101
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
Definition: qevent.h:102
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void setBrush(const QBrush &brush)
Sets the painter&#39;s brush to the given brush.
Definition: qpainter.cpp:4171
void setPen(const QColor &color)
Sets the painter&#39;s pen to have style Qt::SolidLine, width 0 and the specified color.
Definition: qpainter.cpp:4047
The QLabel widget provides a text or image display.
Definition: qlabel.h:55
void drawRect(const QRectF &rect)
Draws the current rectangle with the current pen and brush.
Definition: qpainter.h:650
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
void setText(const QString &text)
Sets text as the plain text (MIME type text/plain) used to represent the data.
Definition: qmimedata.cpp:377
const QPoint & globalPos() const
Returns the global position of the mouse pointer at the time of the event.
Definition: qevent.h:413
QString name() const
Returns the name of the color in the format "#RRGGBB"; i.e.
Definition: qcolor.cpp:529
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
QPixmap createDragPixmap(int size=24) const
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
Definition: qpainter.cpp:7420
void mouseMoveEvent(QMouseEvent *ev)
em>Reimplemented Function
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60