formeditorgraphicsview.cpp 12.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 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 "formeditorgraphicsview.h"

#include <QWheelEvent>
33
34
#include <QApplication>
#include <QtDebug>
35

36
37
#include <qmlanchors.h>

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
namespace QmlDesigner {

FormEditorGraphicsView::FormEditorGraphicsView(QWidget *parent) :
    QGraphicsView(parent)
{
    setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    setResizeAnchor(QGraphicsView::AnchorViewCenter);
//    setCacheMode(QGraphicsView::CacheNone);
     setCacheMode(QGraphicsView::CacheBackground);
    setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
//    setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
    setRenderHint(QPainter::Antialiasing, false);

    setFrameShape(QFrame::NoFrame);

    setAutoFillBackground(true);
    setBackgroundRole(QPalette::Window);

    const int checkerbordSize= 20;
    QPixmap tilePixmap(checkerbordSize * 2, checkerbordSize * 2);
    tilePixmap.fill(Qt::white);
    QPainter tilePainter(&tilePixmap);
    QColor color(220, 220, 220);
    tilePainter.fillRect(0, 0, checkerbordSize, checkerbordSize, color);
    tilePainter.fillRect(checkerbordSize, checkerbordSize, checkerbordSize, checkerbordSize, color);
    tilePainter.end();

    setBackgroundBrush(tilePixmap);

    viewport()->setMouseTracking(true);
}

void FormEditorGraphicsView::wheelEvent(QWheelEvent *event)
{
    if (event->modifiers().testFlag(Qt::ControlModifier)) {
        event->ignore();
    } else {
        QGraphicsView::wheelEvent(event);
    }

}

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
void FormEditorGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    if (rect().contains(event->pos())) {
        QGraphicsView::mouseMoveEvent(event);
    } else {
        QPoint position = event->pos();
        QPoint topLeft = rect().topLeft();
        QPoint bottomRight = rect().bottomRight();
        position.rx() = qMax(topLeft.x(), qMin(position.x(), bottomRight.x()));
        position.ry() = qMax(topLeft.y(), qMin(position.y(), bottomRight.y()));
        QMouseEvent *mouseEvent = QMouseEvent::createExtendedMouseEvent(event->type(), position, mapToGlobal(position), event->button(), event->buttons(), event->modifiers());

        QGraphicsView::mouseMoveEvent(mouseEvent);
        delete mouseEvent;
    }
}


void FormEditorGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
    if (rect().contains(event->pos())) {
        QGraphicsView::mouseReleaseEvent(event);
    } else {
        QPoint position = event->pos();
        QPoint topLeft = rect().topLeft();
        QPoint bottomRight = rect().bottomRight();
        position.rx() = qMax(topLeft.x(), qMin(position.x(), bottomRight.x()));
        position.ry() = qMax(topLeft.y(), qMin(position.y(), bottomRight.y()));
        QMouseEvent *mouseEvent = QMouseEvent::createExtendedMouseEvent(event->type(), position, mapToGlobal(position), event->button(), event->buttons(), event->modifiers());

        QGraphicsView::mouseReleaseEvent(mouseEvent);
        delete mouseEvent;
    }
}

115
116
117
118
119
120
void FormEditorGraphicsView::drawForeground(QPainter *painter, const QRectF &/*rect*/ )
{
    if (!m_feedbackNode.isValid())
        return;

    painter->save();
121
122
123
124
125

    painter->translate(mapToScene(QPoint(3, 3)));

    QColor changeColor(QColor(Qt::green).lighter(170));

126
    QFont font;
Marco Bubke's avatar
Marco Bubke committed
127
128
    font.setFamily("Helvetica");
    font.setPixelSize(11);
129
130
    painter->setFont(font);

131
132
133
134
135
136
137
138
139
140
141
    painter->save();
    painter->setOpacity(0.7);

    QLinearGradient gradient(QPoint(0, 0), QPoint(100, 0));
    gradient.setColorAt(0.0, Qt::darkGray);
    gradient.setColorAt(1.0, Qt::gray);
    painter->setBrush(gradient);

    painter->setPen(Qt::black);
    int height = 40;
    if (m_feedbackNode.instanceHasAnchors())
142
        height += 36;
143
144
145
    painter->drawRoundedRect(QRect(-1, -1, 100, height), 5, 5);
    painter->restore();

146

147
148
149
150
151
    if (m_beginXHasExpression) {
        if(m_feedbackNode.hasBindingProperty("x"))
            painter->setPen(Qt::blue);
        else
            painter->setPen(Qt::red);
152
153
154
155
156
157
    } else {
        if (m_beginX != m_feedbackNode.instanceValue("x"))
            painter->setPen(changeColor);
        else
            painter->setPen(Qt::black);
    }
158

159
160
    painter->drawText(QPoint(2.0, 12.0), QString("x:"));
    painter->drawText(QPoint(14.0, 12.0), m_feedbackNode.instanceValue("x").toString());
161
162


163
164
165
166
167
    if (m_beginYHasExpression) {
        if(m_feedbackNode.hasBindingProperty("y"))
            painter->setPen(Qt::blue);
        else
            painter->setPen(Qt::red);
168
169
170
171
172
173
    } else {
        if (m_beginY != m_feedbackNode.instanceValue("y"))
            painter->setPen(changeColor);
        else
            painter->setPen(Qt::black);
    }
174

175
176
    painter->drawText(QPoint(50.0, 12.0), QString("y:"));
    painter->drawText(QPoint(60.0, 12.0), m_feedbackNode.instanceValue("y").toString());
177
178


179
180
181
182
183
    if (m_beginWidthHasExpression) {
        if(m_feedbackNode.hasBindingProperty("width"))
            painter->setPen(Qt::blue);
        else
            painter->setPen(Qt::red);
184
185
186
187
188
189
    } else {
        if (m_beginWidth != m_feedbackNode.instanceValue("width"))
            painter->setPen(changeColor);
        else
            painter->setPen(Qt::black);
    }
190

191
192
    painter->drawText(QPoint(2.0, 24.0), QString("w:"));
    painter->drawText(QPoint(14.0, 24.0), m_feedbackNode.instanceValue("width").toString());
193
194


195
196
197
198
199
    if (m_beginHeightHasExpression) {
        if(m_feedbackNode.hasBindingProperty("height"))
            painter->setPen(Qt::blue);
        else
            painter->setPen(Qt::red);
200
201
202
203
204
205
    } else {
        if (m_beginHeight != m_feedbackNode.instanceValue("height"))
            painter->setPen(changeColor);
        else
            painter->setPen(Qt::black);
    }
206

207
208
    painter->drawText(QPoint(50.0, 24.0), QString("h:"));
    painter->drawText(QPoint(60.0, 24.0),m_feedbackNode.instanceValue("height").toString());
209
210
211

    if (m_parentNode == m_feedbackNode.instanceParent()) {

212
        if (!m_feedbackNode.canReparent()) {
Marco Bubke's avatar
Marco Bubke committed
213
            painter->setPen(Qt::blue);
214
            painter->drawText(QPoint(2.0, 36.0), QString("Cannot reparent"));
215
216
217
        }
    } else {
        painter->setPen(Qt::yellow);
218
219
        painter->drawText(QPoint(2.0, 36.0), QString("Parent changed"));

220
221
222
    }


223
224
    if (m_feedbackNode.instanceHasAnchors()) {
        if (m_beginTopMargin != m_feedbackNode.instanceValue("anchors.leftMargin"))
225
            painter->setPen(changeColor);
226
227
        else if (m_feedbackNode.anchors().instanceHasAnchor(AnchorLine::Left))
            painter->setPen(Qt::yellow);
228
229
        else
            painter->setPen(Qt::black);
230

231
232
        painter->drawText(QPoint(2.0, 48.0), QString("l:"));
        painter->drawText(QPoint(14.0, 48.0), m_feedbackNode.instanceValue("anchors.leftMargin").toString());
233
234


235
        if (m_beginRightMargin != m_feedbackNode.instanceValue("anchors.rightMargin"))
236
            painter->setPen(changeColor);
237
238
        else if (m_feedbackNode.anchors().instanceHasAnchor(AnchorLine::Right))
            painter->setPen(Qt::yellow);
239
240
        else
            painter->setPen(Qt::black);
241

242
243
        painter->drawText(QPoint(50.0, 48.0), QString("r:"));
        painter->drawText(QPoint(60.0, 48.0), m_feedbackNode.instanceValue("anchors.rightMargin").toString());
244
245


246
        if (m_beginTopMargin != m_feedbackNode.instanceValue("anchors.topMargin"))
247
            painter->setPen(changeColor);
248
249
        else if (m_feedbackNode.anchors().instanceHasAnchor(AnchorLine::Top))
            painter->setPen(Qt::yellow);
250
251
        else
            painter->setPen(Qt::black);
252

253
254
        painter->drawText(QPoint(2.0, 60.0), QString("t:"));
        painter->drawText(QPoint(14.0, 60.0), m_feedbackNode.instanceValue("anchors.topMargin").toString());
255
256


257
        if (m_beginBottomMargin != m_feedbackNode.instanceValue("anchors.bottomMargin"))
258
            painter->setPen(changeColor);
259
260
        else if (m_feedbackNode.anchors().instanceHasAnchor(AnchorLine::Bottom))
            painter->setPen(Qt::yellow);
261
262
        else
            painter->setPen(Qt::black);
263

Marco Bubke's avatar
Marco Bubke committed
264
        painter->drawText(QPoint(50.0, 60.0), QString("b:"));
265
        painter->drawText(QPoint(60.0, 60.0), m_feedbackNode.instanceValue("anchors.bottomMargin").toString());
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

        if (m_beginTopMargin != m_feedbackNode.instanceValue("anchors.horizontalCenterOffset"))
            painter->setPen(changeColor);
        else if (m_feedbackNode.anchors().instanceHasAnchor(AnchorLine::HorizontalCenter))
            painter->setPen(Qt::yellow);
        else
            painter->setPen(Qt::black);
        painter->drawText(QPoint(2.0, 72.0), QString("h:"));
        painter->drawText(QPoint(14.0, 72.0), m_feedbackNode.instanceValue("anchors.horizontalCenterOffset").toString());


        if (m_beginBottomMargin != m_feedbackNode.instanceValue("anchors.verticalCenterOffset"))
            painter->setPen(changeColor);
        else if (m_feedbackNode.anchors().instanceHasAnchor(AnchorLine::VerticalCenter))
            painter->setPen(Qt::yellow);
        else
            painter->setPen(Qt::black);

        painter->drawText(QPoint(50.0, 72.0), QString("v:"));
        painter->drawText(QPoint(60.0, 72.0), m_feedbackNode.instanceValue("anchors.verticalCenterOffset").toString());
286
    }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

    painter->restore();
}

void FormEditorGraphicsView::setFeedbackNode(const QmlItemNode &node)
{
    if (node == m_feedbackNode)
        return;

    m_feedbackNode = node;

    if (m_feedbackNode.isValid()) {
        m_beginX = m_feedbackNode.instanceValue("x");
        m_beginY = m_feedbackNode.instanceValue("y");
        m_beginWidth = m_feedbackNode.instanceValue("width");
        m_beginHeight = m_feedbackNode.instanceValue("height");
        m_parentNode = m_feedbackNode.instanceParent();
        m_beginLeftMargin = m_feedbackNode.instanceValue("anchors.leftMargin");
        m_beginRightMargin = m_feedbackNode.instanceValue("anchors.rightMargin");
        m_beginTopMargin = m_feedbackNode.instanceValue("anchors.topMargin");
        m_beginBottomMargin = m_feedbackNode.instanceValue("anchors.bottomMargin");
308
309
310
311
        m_beginXHasExpression = m_feedbackNode.hasBindingProperty("x");
        m_beginYHasExpression = m_feedbackNode.hasBindingProperty("y");
        m_beginWidthHasExpression = m_feedbackNode.hasBindingProperty("width");
        m_beginHeightHasExpression = m_feedbackNode.hasBindingProperty("height");
312
313
314
315
316
317
318
319
320
321
    } else {
        m_beginX = QVariant();
        m_beginY = QVariant();
        m_beginWidth = QVariant();
        m_beginHeight = QVariant();
        m_parentNode = QmlObjectNode();
        m_beginLeftMargin = QVariant();
        m_beginRightMargin = QVariant();
        m_beginTopMargin = QVariant();
        m_beginBottomMargin = QVariant();
322
323
324
325
        m_beginXHasExpression = false;
        m_beginYHasExpression = false;
        m_beginWidthHasExpression = false;
        m_beginHeightHasExpression = false;
326
327
328
    }
}

329
330
331
332
333
334
void FormEditorGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
{
    painter->save();
    painter->setBrushOrigin(0, 0);
    painter->fillRect(rect.intersected(sceneRect()), backgroundBrush());
    // paint rect around editable area
Marco Bubke's avatar
Marco Bubke committed
335
336
    painter->setPen(Qt::black);
    QRectF frameRect = sceneRect().adjusted(0, 0, 0, 0);
337
338
339
340
341
    painter->drawRect(frameRect);
    painter->restore();
}

} // namespace QmlDesigner