OSDN Git Service

1b4b0a53f7a6e28f9054f399eebc1450b2e6254d
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / propertyeditor / originwidget.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
34 #include "originwidget.h"
35 #include <QList>
36 #include <QPainter>
37 #include <QMouseEvent>
38 #include <qdeclarative.h>
39
40 static QList<QPoint> positions;
41 static QStringList originsStringList;
42
43 namespace QmlDesigner {
44
45 OriginWidget::OriginWidget(QWidget *parent) : QWidget(parent), m_pressed(false), m_marked(false)
46 {
47     if (positions.isEmpty())
48         positions << QPoint(0,  0) << QPoint(18,  0) << QPoint(36,  0)
49             << QPoint(0,  18) << QPoint(18, 18) << QPoint(36, 18)
50             << QPoint(0,  36) << QPoint(18, 36) << QPoint(36, 36);
51
52     if (originsStringList.isEmpty())
53         originsStringList << "TopLeft" << "Top"
54                 << "TopRight" << "Left" << "Center" << "Right"
55                 << "BottomLeft" << "Bottom" << "BottomRight";
56
57     m_originString = "Center";
58     resize(50, 50);
59     setMinimumHeight(50);
60     m_index = 0;
61     setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
62 }
63
64 void OriginWidget::setOrigin(const QString& newOrigin)
65 {
66     if (!originsStringList.contains(newOrigin))
67         return;
68     if (newOrigin == m_originString)
69         return;
70
71     m_originString = newOrigin;
72     update();
73     emit originChanged();
74 }
75
76 void OriginWidget::registerDeclarativeType()
77 {
78     qmlRegisterType<QmlDesigner::OriginWidget>("Bauhaus",1,0,"OriginWidget");
79
80 }
81
82 void OriginWidget::paintEvent(QPaintEvent *event)
83 {
84     QWidget::paintEvent(event);
85
86     QPainter p(this);
87
88     foreach (const QPoint& position, positions)
89         p.fillRect(position.x(), position.y(), 14, 14, Qt::black);
90
91     int origin = originsStringList.indexOf(m_originString);
92
93     if (m_pressed)
94         p.fillRect(positions.at(m_index).x() + 4, positions.at(m_index).y() + 4, 6, 6, "#868686");
95
96     if (m_marked)
97         p.fillRect(positions.at(origin).x(), positions.at(origin).y(), 14, 14, "#9999ff");
98     else
99         p.fillRect(positions.at(origin).x(), positions.at(origin).y(), 14, 14, "#e6e6e6");
100     p.fillRect(positions.at(origin).x() + 2, positions.at(origin).y() + 2, 10, 10, "#666666");
101 }
102
103 void OriginWidget::mouseReleaseEvent(QMouseEvent *event)
104 {
105     if (event->button() == Qt::LeftButton) {
106         m_pressed = false;
107         for (int i = 0; i < positions.size(); i++)
108             if (QRect(positions.at(i), QSize(14, 14)).contains(event->pos()))
109                 setOrigin(originsStringList.at(i));
110     }
111     QWidget::mouseReleaseEvent(event);
112 }
113
114 void OriginWidget::mousePressEvent(QMouseEvent * event)
115 {    
116     if (event->button() == Qt::LeftButton) {
117         m_pressed = true;
118         for (int i = 0; i < positions.size(); i++)
119             if (QRect(positions.at(i), QSize(14, 14)).contains(event->pos())) {
120             m_index = i;
121             update();
122         }
123     }
124     QWidget::mousePressEvent(event);
125 }
126
127 static inline QString getToolTip(const QPoint &pos)
128 {
129     for (int i = 0; i < positions.size(); i++)
130         if (QRect(positions.at(i), QSize(14, 14)).contains(pos))
131             return originsStringList.at(i);
132
133     return QString();
134 }
135
136 bool OriginWidget::event(QEvent *event)
137 {
138     if (event->type() == QEvent::ToolTip)
139         setToolTip(getToolTip(static_cast<QHelpEvent*>(event)->pos()));
140
141     return QWidget::event(event);
142 }
143
144 } //QmlDesigner
145