abstractformeditortool.cpp 5.92 KB
Newer Older
1
2
3
4
5
6
7
8
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15
16
17
18
19
20
21
22
23
24
**
** 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.
**
con's avatar
con committed
25
26
27
28
29
30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
31
32
33
34
**
**************************************************************************/

#include "abstractformeditortool.h"
35
#include "qdeclarativeviewobserver.h"
36
#include "../qdeclarativeviewobserver_p.h"
37
38
39
40
41
42
43

#include <QDeclarativeEngine>

#include <QtDebug>
#include <QGraphicsItem>
#include <QDeclarativeItem>

44
namespace QmlJSDebugger {
45

46
47
AbstractFormEditorTool::AbstractFormEditorTool(QDeclarativeViewObserver *editorView)
    : QObject(editorView), m_observer(editorView)
48
49
50
51
52
53
54
55
56
{
}


AbstractFormEditorTool::~AbstractFormEditorTool()
{

}

57
QDeclarativeViewObserver *AbstractFormEditorTool::observer() const
58
{
59
60
61
62
63
64
    return m_observer;
}

QDeclarativeView *AbstractFormEditorTool::view() const
{
    return m_observer->declarativeView();
65
66
67
68
69
70
71
}

QGraphicsScene* AbstractFormEditorTool::scene() const
{
    return view()->scene();
}

72
void AbstractFormEditorTool::updateSelectedItems()
73
{
74
    selectedItemsChanged(items());
75
76
77
78
}

QList<QGraphicsItem*> AbstractFormEditorTool::items() const
{
79
    return observer()->selectedItems();
80
81
}

82
83
void AbstractFormEditorTool::enterContext(QGraphicsItem *itemToEnter)
{
84
    observer()->data->enterContext(itemToEnter);
85
86
}

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
bool AbstractFormEditorTool::topItemIsMovable(const QList<QGraphicsItem*> & itemList)
{
    QGraphicsItem *firstSelectableItem = topMovableGraphicsItem(itemList);
    if (firstSelectableItem == 0)
        return false;

    QDeclarativeItem *declarativeItem = dynamic_cast<QDeclarativeItem*>(firstSelectableItem->toGraphicsObject());

    if (declarativeItem != 0)
        return true;

    return false;

}

bool AbstractFormEditorTool::topSelectedItemIsMovable(const QList<QGraphicsItem*> &itemList)
{
104
    QList<QGraphicsItem*> selectedItems = observer()->selectedItems();
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

    foreach (QGraphicsItem *item, itemList) {
        QDeclarativeItem *declarativeItem = toQDeclarativeItem(item);
        if (declarativeItem
            && selectedItems.contains(declarativeItem)
            /*&& (declarativeItem->qmlItemNode().hasShowContent() || selectNonContentItems)*/)
            return true;
    }

    return false;

}

bool AbstractFormEditorTool::topItemIsResizeHandle(const QList<QGraphicsItem*> &/*itemList*/)
{
    return false;
}

QDeclarativeItem *AbstractFormEditorTool::toQDeclarativeItem(QGraphicsItem *item)
{
    return dynamic_cast<QDeclarativeItem*>(item->toGraphicsObject());
}

QGraphicsItem *AbstractFormEditorTool::topMovableGraphicsItem(const QList<QGraphicsItem*> &itemList)
{
    foreach (QGraphicsItem *item, itemList) {
        if (item->flags().testFlag(QGraphicsItem::ItemIsMovable))
            return item;
    }
    return 0;
}

QDeclarativeItem *AbstractFormEditorTool::topMovableDeclarativeItem(const QList<QGraphicsItem*> &itemList)
{
    foreach (QGraphicsItem *item, itemList) {
        QDeclarativeItem *declarativeItem = toQDeclarativeItem(item);
        if (declarativeItem /*&& (declarativeItem->qmlItemNode().hasShowContent())*/)
            return declarativeItem;
    }

    return 0;
}

QList<QGraphicsObject*> AbstractFormEditorTool::toGraphicsObjectList(const QList<QGraphicsItem*> &itemList)
{
    QList<QGraphicsObject*> gfxObjects;
    foreach(QGraphicsItem *item, itemList) {
        QGraphicsObject *obj = item->toGraphicsObject();
        if (obj)
            gfxObjects << obj;
    }

    return gfxObjects;
}

QList<QObject*> AbstractFormEditorTool::toObjectList(const QList<QGraphicsItem*> &itemList)
{
    QList<QObject*> objects;
    foreach(QGraphicsItem *item, itemList) {
        QObject *obj = item->toGraphicsObject();
        if (obj)
            objects << obj;
    }

    return objects;
}

172
QString AbstractFormEditorTool::titleForItem(QGraphicsItem *item)
173
174
175
176
{
    QString className("QGraphicsItem");
    QString objectStringId;

177
178
    QString constructedName;

179
    QGraphicsObject *gfxObject = item->toGraphicsObject();
180
181
    if (gfxObject) {
        className = gfxObject->metaObject()->className();
182

183
        className.replace(QRegExp("_QMLTYPE_\\d+"), "");
184
185
186
        className.replace(QRegExp("_QML_\\d+"), "");
        if (className.startsWith(QLatin1String("QDeclarative")))
            className = className.replace(QLatin1String("QDeclarative"), "");
187

188
        QDeclarativeItem *declarativeItem = qobject_cast<QDeclarativeItem*>(gfxObject);
189
        if (declarativeItem) {
190
            objectStringId = QDeclarativeViewObserver::idStringForObject(declarativeItem);
191
192
        }

193
194
195
        if (!objectStringId.isEmpty()) {
            constructedName = objectStringId + " (" + className + ")";
        } else {
196
            if (!gfxObject->objectName().isEmpty()) {
197
                constructedName = gfxObject->objectName() + " (" + className + ")";
198
199
200
            } else {
                constructedName = className;
            }
201
        }
202
203
    }

204
    return constructedName;
205
206
207
208
}


}