OSDN Git Service

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