1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** 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.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
33 #include "qmljstoolbarcolorbox.h"
35 #include <QtGui/QPixmap>
36 #include <QtGui/QPainter>
37 #include <QtGui/QMenu>
38 #include <QtGui/QAction>
39 #include <QtGui/QContextMenuEvent>
40 #include <QtGui/QClipboard>
41 #include <QtGui/QApplication>
42 #include <QtGui/QColorDialog>
43 #include <QtGui/QDrag>
45 #include <QtCore/QMimeData>
46 #include <QtCore/QDebug>
48 namespace QmlJSInspector {
50 ToolBarColorBox::ToolBarColorBox(QWidget *parent) :
54 m_borderColorOuter = Qt::white;
55 m_borderColorInner = QColor(143, 143 ,143);
57 m_copyHexColorAction = new QAction(QIcon(QLatin1String(":/qml/images/color-picker-small-hicontrast.png")), tr("Copy Color"), this);
58 connect(m_copyHexColorAction, SIGNAL(triggered()), SLOT(copyColorToClipboard()));
59 setScaledContents(false);
62 void ToolBarColorBox::setColor(const QColor &color)
66 QPixmap pix = createDragPixmap(width());
71 void ToolBarColorBox::setInnerBorderColor(const QColor &color)
73 m_borderColorInner = color;
77 void ToolBarColorBox::setOuterBorderColor(const QColor &color)
79 m_borderColorOuter = color;
83 void ToolBarColorBox::mousePressEvent(QMouseEvent *event)
85 m_dragBeginPoint = event->pos();
86 m_dragStarted = false;
89 void ToolBarColorBox::mouseMoveEvent(QMouseEvent *event)
91 if (event->buttons() & Qt::LeftButton
92 && QPoint(event->pos() - m_dragBeginPoint).manhattanLength() > QApplication::startDragDistance()
96 QDrag *drag = new QDrag(this);
97 QMimeData *mimeData = new QMimeData;
99 mimeData->setText(m_color.name());
100 drag->setMimeData(mimeData);
101 drag->setPixmap(createDragPixmap());
107 QPixmap ToolBarColorBox::createDragPixmap(int size) const
109 QPixmap pix(size, size);
112 p.setBrush(QBrush(m_color));
113 p.setPen(QPen(QBrush(m_borderColorInner),1));
115 p.fillRect(0, 0, size, size, m_borderColorOuter);
116 p.drawRect(1,1, size - 3, size - 3);
120 void ToolBarColorBox::contextMenuEvent(QContextMenuEvent *ev)
123 contextMenu.addAction(m_copyHexColorAction);
124 contextMenu.exec(ev->globalPos());
127 void ToolBarColorBox::copyColorToClipboard()
129 QClipboard *clipboard = QApplication::clipboard();
130 clipboard->setText(m_color.name());
133 } // namespace QmlJSInspector