1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "gradientlineqmladaptor.h"
35 #include <qdeclarative.h>
36 #include <QMessageBox>
38 #include <nodeproperty.h>
39 #include <nodelistproperty.h>
40 #include <nodemetainfo.h>
41 #include <abstractview.h>
42 #include <rewritingexception.h>
43 #include <variantproperty.h>
45 namespace QmlDesigner {
47 GradientLineQmlAdaptor::GradientLineQmlAdaptor(QWidget *parent) :
48 QmlEditorWidgets::GradientLine(parent)
51 connect(this, SIGNAL(gradientChanged()), this, SLOT(writeGradient()));
54 void GradientLineQmlAdaptor::setItemNode(const QVariant &itemNode)
57 if (!itemNode.value<ModelNode>().isValid())
59 m_itemNode = itemNode.value<ModelNode>();
60 emit itemNodeChanged();
63 static inline QColor normalizeColor(const QColor &color)
65 QColor newColor = QColor(color.name());
66 newColor.setAlpha(color.alpha());
70 static inline qreal roundReal(qreal real)
73 return qreal(i) / 100;
76 void GradientLineQmlAdaptor::setupGradient()
81 ModelNode modelNode = m_itemNode.modelNode();
82 QLinearGradient newGradient;
83 QVector<QGradientStop> stops;
85 if (!modelNode.isValid())
88 if (modelNode.hasProperty(gradientName())) { //gradient exists
90 ModelNode gradientNode = modelNode.nodeProperty(gradientName()).modelNode();
91 QList<ModelNode> stopList = gradientNode.nodeListProperty("stops").toModelNodeList();
93 foreach (const ModelNode &stopNode, stopList) {
94 QmlObjectNode stopObjectNode = stopNode;
95 if (stopObjectNode.isValid()) {
96 qreal position = stopObjectNode.modelValue("position").toReal();
97 QColor color = stopObjectNode.modelValue("color").value<QColor>();
98 stops.append( QPair<qreal, QColor>(position, color));
102 stops.append( QPair<qreal, QColor>(0, activeColor()));
103 stops.append( QPair<qreal, QColor>(1, QColor(Qt::black)));
106 newGradient.setStops(stops);
107 setGradient(newGradient);
110 void GradientLineQmlAdaptor::writeGradient()
115 if (!m_itemNode.isValid())
118 if (!m_itemNode.modelNode().metaInfo().hasProperty(gradientName()))
121 ModelNode modelNode = m_itemNode.modelNode();
124 QVector<QGradientStop> stops = gradient().stops();
125 if (m_itemNode.isInBaseState()) {
126 if (modelNode.hasProperty(gradientName())) {
127 oldId = modelNode.nodeProperty(gradientName()).modelNode().id();
128 modelNode.removeProperty(gradientName());
131 ModelNode gradientNode = modelNode.view()->createModelNode("QtQuick.Gradient", 1, 0);
132 modelNode.nodeProperty(gradientName()).reparentHere(gradientNode);
134 RewriterTransaction transaction = m_itemNode.modelNode().view()->beginRewriterTransaction();
137 gradientNode.setId(oldId);
139 for (int i = 0;i < stops.size(); i++) {
140 ModelNode gradientStopNode = modelNode.view()->createModelNode("QtQuick.GradientStop", 1, 0);
141 gradientStopNode.variantProperty("position") = roundReal(stops.at(i).first);
142 gradientStopNode.variantProperty("color") = normalizeColor(stops.at(i).second);
143 gradientNode.nodeListProperty("stops").reparentHere(gradientStopNode);
146 if (!modelNode.hasProperty(gradientName())) {
147 qWarning(" GradientLine::updateGradient: no gradient in state");
150 ModelNode gradientNode = modelNode.nodeProperty(gradientName()).modelNode();
151 QList<ModelNode> stopList = gradientNode.nodeListProperty("stops").toModelNodeList();
152 for (int i = 0;i < stops.size(); i++) {
153 QmlObjectNode stopObjectNode = stopList.at(i);
154 stopObjectNode.setVariantProperty("position", roundReal(stops.at(i).first));
155 stopObjectNode.setVariantProperty("color", normalizeColor(stops.at(i).second));
159 catch (RewritingException &e) {
160 QMessageBox::warning(0, "Error", e.description());
164 void GradientLineQmlAdaptor::deleteGradient()
166 if (!m_itemNode.isValid())
169 if (!m_itemNode.modelNode().metaInfo().hasProperty(gradientName()))
172 ModelNode modelNode = m_itemNode.modelNode();
174 if (m_itemNode.isInBaseState()) {
175 if (modelNode.hasProperty(gradientName())) {
176 RewriterTransaction transaction = m_itemNode.modelNode().view()->beginRewriterTransaction();
177 ModelNode gradientNode = modelNode.nodeProperty(gradientName()).modelNode();
178 if (QmlObjectNode(gradientNode).isValid())
179 QmlObjectNode(gradientNode).destroy();
184 void GradientLineQmlAdaptor::registerDeclarativeType() {
185 qmlRegisterType<QmlDesigner::GradientLineQmlAdaptor>("Bauhaus",1,0,"GradientLine");