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 **************************************************************************/
34 #include "originwidget.h"
37 #include <QMouseEvent>
38 #include <qdeclarative.h>
40 static QList<QPoint> positions;
41 static QStringList originsStringList;
43 namespace QmlDesigner {
45 OriginWidget::OriginWidget(QWidget *parent) : QWidget(parent), m_pressed(false), m_marked(false)
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);
52 if (originsStringList.isEmpty())
53 originsStringList << "TopLeft" << "Top"
54 << "TopRight" << "Left" << "Center" << "Right"
55 << "BottomLeft" << "Bottom" << "BottomRight";
57 m_originString = "Center";
61 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
64 void OriginWidget::setOrigin(const QString& newOrigin)
66 if (!originsStringList.contains(newOrigin))
68 if (newOrigin == m_originString)
71 m_originString = newOrigin;
76 void OriginWidget::registerDeclarativeType()
78 qmlRegisterType<QmlDesigner::OriginWidget>("Bauhaus",1,0,"OriginWidget");
82 void OriginWidget::paintEvent(QPaintEvent *event)
84 QWidget::paintEvent(event);
88 foreach (const QPoint& position, positions)
89 p.fillRect(position.x(), position.y(), 14, 14, Qt::black);
91 int origin = originsStringList.indexOf(m_originString);
94 p.fillRect(positions.at(m_index).x() + 4, positions.at(m_index).y() + 4, 6, 6, "#868686");
97 p.fillRect(positions.at(origin).x(), positions.at(origin).y(), 14, 14, "#9999ff");
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");
103 void OriginWidget::mouseReleaseEvent(QMouseEvent *event)
105 if (event->button() == Qt::LeftButton) {
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));
111 QWidget::mouseReleaseEvent(event);
114 void OriginWidget::mousePressEvent(QMouseEvent * event)
116 if (event->button() == Qt::LeftButton) {
118 for (int i = 0; i < positions.size(); i++)
119 if (QRect(positions.at(i), QSize(14, 14)).contains(event->pos())) {
124 QWidget::mousePressEvent(event);
127 static inline QString getToolTip(const QPoint &pos)
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);
136 bool OriginWidget::event(QEvent *event)
138 if (event->type() == QEvent::ToolTip)
139 setToolTip(getToolTip(static_cast<QHelpEvent*>(event)->pos()));
141 return QWidget::event(event);