OSDN Git Service

f83aa71f4d619c95861a052ac0b3773e04f3d037
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljsinspector / qmljstoolbarcolorbox.cpp
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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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, 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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33 #include "qmljstoolbarcolorbox.h"
34
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>
44
45 #include <QtCore/QMimeData>
46 #include <QtCore/QDebug>
47
48 namespace QmlJSInspector {
49
50 ToolBarColorBox::ToolBarColorBox(QWidget *parent) :
51     QLabel(parent)
52 {
53     m_color = Qt::white;
54     m_borderColorOuter = Qt::white;
55     m_borderColorInner = QColor(143, 143 ,143);
56
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);
60 }
61
62 void ToolBarColorBox::setColor(const QColor &color)
63 {
64     m_color = color;
65
66     QPixmap pix = createDragPixmap(width());
67     setPixmap(pix);
68     update();
69 }
70
71 void ToolBarColorBox::setInnerBorderColor(const QColor &color)
72 {
73     m_borderColorInner = color;
74     setColor(m_color);
75 }
76
77 void ToolBarColorBox::setOuterBorderColor(const QColor &color)
78  {
79      m_borderColorOuter = color;
80      setColor(m_color);
81  }
82
83 void ToolBarColorBox::mousePressEvent(QMouseEvent *event)
84 {
85     m_dragBeginPoint = event->pos();
86     m_dragStarted = false;
87 }
88
89 void ToolBarColorBox::mouseMoveEvent(QMouseEvent *event)
90 {
91     if (event->buttons() & Qt::LeftButton
92         && QPoint(event->pos() - m_dragBeginPoint).manhattanLength() > QApplication::startDragDistance()
93         && !m_dragStarted)
94     {
95         m_dragStarted = true;
96         QDrag *drag = new QDrag(this);
97         QMimeData *mimeData = new QMimeData;
98
99         mimeData->setText(m_color.name());
100         drag->setMimeData(mimeData);
101         drag->setPixmap(createDragPixmap());
102
103         drag->exec();
104     }
105 }
106
107 QPixmap ToolBarColorBox::createDragPixmap(int size) const
108 {
109     QPixmap pix(size, size);
110     QPainter p(&pix);
111
112     p.setBrush(QBrush(m_color));
113     p.setPen(QPen(QBrush(m_borderColorInner),1));
114
115     p.fillRect(0, 0, size, size, m_borderColorOuter);
116     p.drawRect(1,1, size - 3, size - 3);
117     return pix;
118 }
119
120 void ToolBarColorBox::contextMenuEvent(QContextMenuEvent *ev)
121 {
122     QMenu contextMenu;
123     contextMenu.addAction(m_copyHexColorAction);
124     contextMenu.exec(ev->globalPos());
125 }
126
127 void ToolBarColorBox::copyColorToClipboard()
128 {
129     QClipboard *clipboard = QApplication::clipboard();
130     clipboard->setText(m_color.name());
131 }
132
133 } // namespace QmlJSInspector