OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmljsdebugger / editor / toolbarcolorbox.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "toolbarcolorbox.h"
34 #include "qmlobserverconstants.h"
35
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>
45
46 #include <QtCore/QMimeData>
47 #include <QtCore/QDebug>
48
49 namespace QmlJSDebugger {
50
51 ToolBarColorBox::ToolBarColorBox(QWidget *parent) :
52     QLabel(parent)
53 {
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);
58 }
59
60 void ToolBarColorBox::setColor(const QColor &color)
61 {
62     m_color = color;
63
64     QPixmap pix = createDragPixmap(width());
65     setPixmap(pix);
66     update();
67 }
68
69 void ToolBarColorBox::mousePressEvent(QMouseEvent *event)
70 {
71     m_dragBeginPoint = event->pos();
72     m_dragStarted = false;
73 }
74
75 void ToolBarColorBox::mouseMoveEvent(QMouseEvent *event)
76 {
77
78     if (event->buttons() & Qt::LeftButton
79             && (QPoint(event->pos() - m_dragBeginPoint).manhattanLength()
80                 > Constants::DragStartDistance)
81             && !m_dragStarted)
82     {
83         m_dragStarted = true;
84         QDrag *drag = new QDrag(this);
85         QMimeData *mimeData = new QMimeData;
86
87         mimeData->setText(m_color.name());
88         drag->setMimeData(mimeData);
89         drag->setPixmap(createDragPixmap());
90
91         drag->exec();
92     }
93 }
94
95 QPixmap ToolBarColorBox::createDragPixmap(int size) const
96 {
97     QPixmap pix(size, size);
98     QPainter p(&pix);
99
100     QColor borderColor1 = QColor(143, 143 ,143);
101     QColor borderColor2 = QColor(43, 43, 43);
102
103     p.setBrush(QBrush(m_color));
104     p.setPen(QPen(QBrush(borderColor2),1));
105
106     p.fillRect(0, 0, size, size, borderColor1);
107     p.drawRect(1,1, size - 3, size - 3);
108     return pix;
109 }
110
111 void ToolBarColorBox::contextMenuEvent(QContextMenuEvent *ev)
112 {
113     QMenu contextMenu;
114     contextMenu.addAction(m_copyHexColor);
115     contextMenu.exec(ev->globalPos());
116 }
117
118 void ToolBarColorBox::copyColorToClipboard()
119 {
120     QClipboard *clipboard = QApplication::clipboard();
121     clipboard->setText(m_color.name());
122 }
123
124 } // namespace QmlJSDebugger