diff --git a/src/plugins/qmldesigner/components/propertyeditor/originwidget.cpp b/src/plugins/qmldesigner/components/propertyeditor/originwidget.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6994232e19097cbe61e7f14c78a2d46ddddb43f --- /dev/null +++ b/src/plugins/qmldesigner/components/propertyeditor/originwidget.cpp @@ -0,0 +1,140 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include "originwidget.h" +#include <QList> +#include <QPainter> +#include <QMouseEvent> +#include <qdeclarative.h> + +static QList<QPoint> positions; +static QStringList originsStringList; + +namespace QmlDesigner { + +OriginWidget::OriginWidget(QWidget *parent) : QWidget(parent), m_pressed(false), m_marked(false) +{ + if (positions.isEmpty()) + positions << QPoint(0, 0) << QPoint(18, 0) << QPoint(36, 0) + << QPoint(0, 18) << QPoint(18, 18) << QPoint(36, 18) + << QPoint(0, 36) << QPoint(18, 36) << QPoint(36, 36); + + if (originsStringList.isEmpty()) + originsStringList << "TopLeft" << "Top" + << "TopRight" << "Left" << "Center" << "Right" + << "BottomLeft" << "Bottom" << "BottomRight"; + + m_originString = "Center"; + resize(50, 50); + setMinimumHeight(50); + setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); +} + +void OriginWidget::setOrigin(const QString& newOrigin) +{ + if (!originsStringList.contains(newOrigin)) + return; + if (newOrigin == m_originString) + return; + + m_originString = newOrigin; + update(); + emit originChanged(); +} + +void OriginWidget::registerDeclarativeType() +{ + qmlRegisterType<QmlDesigner::OriginWidget>("Bauhaus",1,0,"OriginWidget"); + +} + +void OriginWidget::paintEvent(QPaintEvent *event) +{ + QWidget::paintEvent(event); + + QPainter p(this); + + foreach (const QPoint& position, positions) + p.fillRect(position.x(), position.y(), 14, 14, Qt::black); + + int origin = originsStringList.indexOf(m_originString); + + if (m_pressed) + p.fillRect(positions.at(m_index).x() + 4, positions.at(m_index).y() + 4, 6, 6, "#868686"); + + if (m_marked) + p.fillRect(positions.at(origin).x(), positions.at(origin).y(), 14, 14, "#9999ff"); + else + p.fillRect(positions.at(origin).x(), positions.at(origin).y(), 14, 14, "#e6e6e6"); + p.fillRect(positions.at(origin).x() + 2, positions.at(origin).y() + 2, 10, 10, "#666666"); +} + +void OriginWidget::mouseReleaseEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + m_pressed = false; + for (int i = 0; i < positions.size(); i++) + if (QRect(positions.at(i), QSize(14, 14)).contains(event->pos())) + setOrigin(originsStringList.at(i)); + } + QWidget::mouseReleaseEvent(event); +} + +void OriginWidget::mousePressEvent(QMouseEvent * event) +{ + if (event->button() == Qt::LeftButton) { + m_pressed = true; + for (int i = 0; i < positions.size(); i++) + if (QRect(positions.at(i), QSize(14, 14)).contains(event->pos())) { + m_index = i; + update(); + } + } + QWidget::mousePressEvent(event); +} + +static inline QString getToolTip(const QPoint &pos) +{ + for (int i = 0; i < positions.size(); i++) + if (QRect(positions.at(i), QSize(14, 14)).contains(pos)) + return originsStringList.at(i); + + return QString(); +} + +bool OriginWidget::event(QEvent *event) +{ + if (event->type() == QEvent::ToolTip) + setToolTip(getToolTip(static_cast<QHelpEvent*>(event)->pos())); + + return QWidget::event(event); +} + +} //QmlDesigner + diff --git a/src/plugins/qmldesigner/components/propertyeditor/originwidget.h b/src/plugins/qmldesigner/components/propertyeditor/originwidget.h new file mode 100644 index 0000000000000000000000000000000000000000..14a9175f02430b8f65a8a21cd276725ded6b58d7 --- /dev/null +++ b/src/plugins/qmldesigner/components/propertyeditor/originwidget.h @@ -0,0 +1,78 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#ifndef ORIGINWIDGET_H +#define ORIGINWIDGET_H + +#include <QtGui/QWidget> + +namespace QmlDesigner { + +class OriginWidget: public QWidget { + +Q_OBJECT + +Q_PROPERTY(QString origin READ origin WRITE setOrigin NOTIFY originChanged) +Q_PROPERTY(bool marked READ marked WRITE setMarked) + +public: + OriginWidget(QWidget *parent = 0); + + QString origin() const + { return m_originString; } + + void setOrigin(const QString& newOrigin); + + bool marked() const + { return m_marked; } + + void setMarked(bool newMarked) + { m_marked = newMarked; } + + static void registerDeclarativeType(); + +signals: + void originChanged(); + +protected: + void paintEvent(QPaintEvent *event); + void mouseReleaseEvent(QMouseEvent * event); + void mousePressEvent(QMouseEvent * event); + bool event(QEvent *event); +private: + QString m_originString; + bool m_pressed; + int m_index; + bool m_marked; +}; + +} // QmlDesigner + + +#endif //ORIGINWIDGET_H diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.pri b/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.pri index 32449a97599e8adc65f161341479799576bc4606..3c4c9a9bf4be46b189e61fc171e254537a8e91ed 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.pri +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.pri @@ -15,6 +15,7 @@ SOURCES += propertyeditor.cpp \ filewidget.cpp \ propertyeditorvalue.cpp \ fontwidget.cpp \ + originwidget.cpp \ siblingcombobox.cpp \ propertyeditortransaction.cpp HEADERS += propertyeditor.h \ @@ -31,6 +32,7 @@ HEADERS += propertyeditor.h \ filewidget.h \ propertyeditorvalue.h \ fontwidget.h \ + originwidget.h \ siblingcombobox.h \ propertyeditortransaction.h QT += declarative