OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / formeditor / selectionindicator.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "selectionindicator.h"
34
35 #include <QPen>
36 #include <cmath>
37 #include <QGraphicsScene>
38 #include <formeditorview.h>
39 #include <formeditorwidget.h>
40 #include <zoomaction.h>
41
42 namespace QmlDesigner {
43
44 SelectionIndicator::SelectionIndicator(LayerItem *layerItem)
45     : m_layerItem(layerItem)
46 {
47 }
48
49 SelectionIndicator::~SelectionIndicator()
50 {
51     clear();
52 }
53
54 void SelectionIndicator::show()
55 {
56     foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
57         item->show();
58 }
59
60 void SelectionIndicator::hide()
61 {
62     foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
63         item->hide();
64 }
65
66 void SelectionIndicator::clear()
67 {
68     if (m_layerItem) {
69         foreach (QGraphicsItem *item, m_indicatorShapeHash.values())
70             m_layerItem->scene()->removeItem(item);
71     }
72     m_indicatorShapeHash.clear();
73 }
74
75 //static void alignVertices(QPolygonF &polygon, double factor)
76 //{
77 //    QMutableVectorIterator<QPointF> iterator(polygon);
78 //    while (iterator.hasNext()) {
79 //        QPointF &vertex = iterator.next();
80 //        vertex.setX(std::floor(vertex.x()) + factor);
81 //        vertex.setY(std::floor(vertex.y()) + factor);
82 //    }
83 //
84 //}
85
86 void SelectionIndicator::setItems(const QList<FormEditorItem*> &itemList)
87 {
88     clear();
89
90     foreach (FormEditorItem *item, itemList) {
91         if (!item->qmlItemNode().isValid())
92             continue;
93
94         QGraphicsPolygonItem *newSelectionIndicatorGraphicsItem = new QGraphicsPolygonItem(m_layerItem.data());
95         m_indicatorShapeHash.insert(item, newSelectionIndicatorGraphicsItem);
96         QPolygonF boundingRectInSceneSpace(item->mapToScene(item->qmlItemNode().instanceBoundingRect()));
97         //        alignVertices(boundingRectInSceneSpace, 0.5 / item->formEditorView()->widget()->zoomAction()->zoomLevel());
98         QPolygonF boundingRectInLayerItemSpace = m_layerItem->mapFromScene(boundingRectInSceneSpace);
99         newSelectionIndicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);
100         newSelectionIndicatorGraphicsItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
101
102         QPen pen;
103         pen.setColor(QColor(108, 141, 221));
104         newSelectionIndicatorGraphicsItem->setPen(pen);
105     }
106 }
107
108
109
110 void SelectionIndicator::updateItems(const QList<FormEditorItem*> &itemList)
111 {
112     foreach (FormEditorItem *item, itemList) {
113         if (m_indicatorShapeHash.contains(item)) {
114             QGraphicsPolygonItem *indicatorGraphicsItem =  m_indicatorShapeHash.value(item);
115             QPolygonF boundingRectInSceneSpace(item->mapToScene(item->qmlItemNode().instanceBoundingRect()));
116 //            alignVertices(boundingRectInSceneSpace, 0.5 / item->formEditorView()->widget()->zoomAction()->zoomLevel());
117             QPolygonF boundingRectInLayerItemSpace = m_layerItem->mapFromScene(boundingRectInSceneSpace);
118             indicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);
119         }
120     }
121 }
122
123 }
124