Imported Upstream version 1.0.1
[gammaray-debian.git] / qmldebugcontrol / qmljsdebugger / editor / toolbarcolorbox.cpp
blob2f589fb8e66586cc7b217bd278395725271e6a82
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
23 ** Other Usage
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "toolbarcolorbox.h"
34 #include "qmlinspectorconstants.h"
36 #include <QtGui/QPixmap>
37 #include <QtGui/QPainter>
38 #include <QtGui/QMenu>
39 #include <QtGui/QAction>
40 #include <QtGui/QContextMenuEvent>
41 #include <QtGui/QClipboard>
42 #include <QtGui/QApplication>
43 #include <QtGui/QColorDialog>
44 #include <QtGui/QDrag>
46 #include <QtCore/QMimeData>
47 #include <QtCore/QDebug>
49 namespace QmlJSDebugger {
51 ToolBarColorBox::ToolBarColorBox(QWidget *parent) :
52 QLabel(parent)
54 m_copyHexColor = new QAction(QIcon(QLatin1String(":/qml/images/color-picker-hicontrast.png")),
55 tr("Copy Color"), this);
56 connect(m_copyHexColor, SIGNAL(triggered()), SLOT(copyColorToClipboard()));
57 setScaledContents(false);
60 void ToolBarColorBox::setColor(const QColor &color)
62 m_color = color;
64 QPixmap pix = createDragPixmap(width());
65 setPixmap(pix);
66 update();
69 void ToolBarColorBox::mousePressEvent(QMouseEvent *event)
71 m_dragBeginPoint = event->pos();
72 m_dragStarted = false;
75 void ToolBarColorBox::mouseMoveEvent(QMouseEvent *event)
78 if (event->buttons() & Qt::LeftButton
79 && (QPoint(event->pos() - m_dragBeginPoint).manhattanLength()
80 > Constants::DragStartDistance)
81 && !m_dragStarted)
83 m_dragStarted = true;
84 QDrag *drag = new QDrag(this);
85 QMimeData *mimeData = new QMimeData;
87 mimeData->setText(m_color.name());
88 drag->setMimeData(mimeData);
89 drag->setPixmap(createDragPixmap());
91 drag->exec();
95 QPixmap ToolBarColorBox::createDragPixmap(int size) const
97 QPixmap pix(size, size);
98 QPainter p(&pix);
100 QColor borderColor1 = QColor(143, 143 ,143);
101 QColor borderColor2 = QColor(43, 43, 43);
103 p.setBrush(QBrush(m_color));
104 p.setPen(QPen(QBrush(borderColor2),1));
106 p.fillRect(0, 0, size, size, borderColor1);
107 p.drawRect(1,1, size - 3, size - 3);
108 return pix;
111 void ToolBarColorBox::contextMenuEvent(QContextMenuEvent *ev)
113 QMenu contextMenu;
114 contextMenu.addAction(m_copyHexColor);
115 contextMenu.exec(ev->globalPos());
118 void ToolBarColorBox::copyColorToClipboard()
120 QClipboard *clipboard = QApplication::clipboard();
121 clipboard->setText(m_color.name());
124 } // namespace QmlJSDebugger
126 #include "toolbarcolorbox.moc"