modelnodecontextmenu_helper.h 8.84 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
33
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#ifndef MODELNODECONTEXTMENU_HELPER_H
#define MODELNODECONTEXTMENU_HELPER_H

#include "modelnodecontextmenu.h"
Thomas Hartmann's avatar
Thomas Hartmann committed
34
#include "modelnodeoperations.h"
35
#include "designeractionmanager.h"
hjk's avatar
hjk committed
36

37
#include <QAction>
hjk's avatar
hjk committed
38
#include <QMenu>
39
40
41

namespace QmlDesigner {

Thomas Hartmann's avatar
Thomas Hartmann committed
42
typedef  bool (*SelectionContextFunction)(const SelectionContext &);
hjk's avatar
hjk committed
43

Thomas Hartmann's avatar
Thomas Hartmann committed
44
namespace SelectionContextFunctors {
hjk's avatar
hjk committed
45

Thomas Hartmann's avatar
Thomas Hartmann committed
46
47
48
49
inline bool always(const SelectionContext &)
{
    return true;
}
hjk's avatar
hjk committed
50

Thomas Hartmann's avatar
Thomas Hartmann committed
51
52
53
54
inline bool inBaseState(const SelectionContext &selectionState)
{
    return selectionState.isInBaseState();
}
hjk's avatar
hjk committed
55

Thomas Hartmann's avatar
Thomas Hartmann committed
56
57
58
59
inline bool singleSelection(const SelectionContext &selectionState)
{
    return selectionState.singleSelected();
}
hjk's avatar
hjk committed
60

Thomas Hartmann's avatar
Thomas Hartmann committed
61
62
63
64
inline bool selectionEnabled(const SelectionContext &selectionState)
{
    return selectionState.showSelectionTools();
}
hjk's avatar
hjk committed
65

Thomas Hartmann's avatar
Thomas Hartmann committed
66
67
68
69
inline bool selectionNotEmpty(const SelectionContext &selectionState)
{
    return !selectionState.selectedModelNodes().isEmpty();
}
hjk's avatar
hjk committed
70

Thomas Hartmann's avatar
Thomas Hartmann committed
71
72
73
74
75
inline bool singleSelectionNotRoot(const SelectionContext &selectionState)
{
    return selectionState.singleSelected()
        && !selectionState.currentSingleSelectedNode().isRootNode();
}
hjk's avatar
hjk committed
76

Thomas Hartmann's avatar
Thomas Hartmann committed
77
78
79
80
81
82
83
inline bool selectionHasProperty(const SelectionContext &selectionState, const char *property)
{
    foreach (const ModelNode &modelNode, selectionState.selectedModelNodes())
        if (modelNode.hasProperty(QLatin1String(property)))
            return true;
    return false;
}
hjk's avatar
hjk committed
84

Thomas Hartmann's avatar
Thomas Hartmann committed
85
86
87
88
89
inline bool singleSelectedItem(const SelectionContext &selectionState)
{
    QmlItemNode itemNode(selectionState.currentSingleSelectedNode());
    return itemNode.isValid();
}
hjk's avatar
hjk committed
90

Thomas Hartmann's avatar
Thomas Hartmann committed
91
92
93
94
95
bool selectionHasSameParent(const SelectionContext &selectionState);
bool selectionIsComponent(const SelectionContext &selectionState);
bool selectionIsComponent(const SelectionContext &selectionState);
bool singleSelectionItemIsAnchored(const SelectionContext &selectionState);
bool singleSelectionItemIsNotAnchored(const SelectionContext &selectionState);
hjk's avatar
hjk committed
96

Thomas Hartmann's avatar
Thomas Hartmann committed
97
} // namespace SelectionStateFunctors
hjk's avatar
hjk committed
98

99

Thomas Hartmann's avatar
Thomas Hartmann committed
100
101
102
namespace ComponentUtils {
    void goIntoComponent(const ModelNode &modelNode);
}
103

Thomas Hartmann's avatar
Thomas Hartmann committed
104
105
class DefaultAction : public QAction
{
106
107
108
109
110
111
112
113
114
    Q_OBJECT

public:
    DefaultAction(const QString &description) : QAction(description, 0)
    {
        connect(this, SIGNAL(triggered(bool)), this, SLOT(actionTriggered(bool)));
    }

public slots: //virtual method instead of slot
Thomas Hartmann's avatar
Thomas Hartmann committed
115
    virtual void actionTriggered(bool)
116
117
118
119
120
121
122
123
124
125
126
    { }

    void setSelectionContext(const SelectionContext &selectionContext)
    {
        m_selectionContext = selectionContext;
    }

protected:
    SelectionContext m_selectionContext;
};

Thomas Hartmann's avatar
Thomas Hartmann committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class ActionTemplate : public DefaultAction
{

public:
    ActionTemplate(const QString &description, ModelNodeOperations::SelectionAction action)
        : DefaultAction(description), m_action(action)
    { }

public /*slots*/:
    virtual void actionTriggered(bool b)
    {
        m_selectionContext.setToggled(b);
        return m_action(m_selectionContext);
    }
    ModelNodeOperations::SelectionAction m_action;
};


145
146
147
class DefaultDesignerAction : public AbstractDesignerAction
{
public:
Thomas Hartmann's avatar
Thomas Hartmann committed
148
    DefaultDesignerAction() : m_action(new DefaultAction(QString()))
149
150
151
152
153
    {}

    DefaultDesignerAction(DefaultAction *action) : m_action(action)
    {}

Thomas Hartmann's avatar
Thomas Hartmann committed
154
    QAction *action() const { return m_action; }
155

Thomas Hartmann's avatar
Thomas Hartmann committed
156
    void setCurrentContext(const SelectionContext &selectionContext)
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    {
        m_selectionContext = selectionContext;
        updateContext();
    }

    virtual void updateContext()
    {
        m_action->setSelectionContext(m_selectionContext);
        if (m_selectionContext.isValid()) {
            m_action->setEnabled(isEnabled(m_selectionContext));
            m_action->setVisible(isVisible(m_selectionContext));
        }
    }

protected:
    DefaultAction *m_action;
    SelectionContext m_selectionContext;
};

class MenuDesignerAction : public AbstractDesignerAction
{
public:
Thomas Hartmann's avatar
Thomas Hartmann committed
179
180
181
    MenuDesignerAction(const QString &displayName, const QString &menuId, int priority,
            SelectionContextFunction enabled = &SelectionContextFunctors::always,
            SelectionContextFunction visibility = &SelectionContextFunctors::always) :
182
183
184
        m_displayName(displayName),
        m_menuId(menuId),
        m_priority(priority),
Thomas Hartmann's avatar
Thomas Hartmann committed
185
186
187
        m_menu(new QMenu),
        m_enabled(enabled),
        m_visibility(visibility)
188
189
190
191
192
    {
        m_menu->setTitle(displayName);
        m_action = m_menu->menuAction();
    }

Thomas Hartmann's avatar
Thomas Hartmann committed
193
194
195
196
197
198
199
    bool isVisible(const SelectionContext &m_selectionState) const { return m_visibility(m_selectionState); }
    bool isEnabled(const SelectionContext &m_selectionState) const { return m_enabled(m_selectionState); }
    QString category() const { return QString(); }
    QString menuId() const { return m_menuId; }
    int priority() const { return m_priority; }
    AbstractDesignerAction::Type type() const { return AbstractDesignerAction::Menu; }
    QAction *action() const { return m_action; }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

    virtual void setCurrentContext(const SelectionContext &selectionContext)
    {
        m_selectionContext = selectionContext;
        updateContext();
    }

    virtual void updateContext()
    {
        if (m_selectionContext.isValid()) {
            m_action->setEnabled(isEnabled(m_selectionContext));
            m_action->setVisible(isVisible(m_selectionContext));
        }
    }

protected:
    const QString m_displayName;
    const QString m_menuId;
    const int m_priority;
    SelectionContext m_selectionContext;
    QScopedPointer<QMenu> m_menu;
    QAction *m_action;
Thomas Hartmann's avatar
Thomas Hartmann committed
222
223
    SelectionContextFunction m_enabled;
    SelectionContextFunction m_visibility;
224
225
226
227
228
229
};

class SeperatorDesignerAction : public DefaultDesignerAction
{
public:
    SeperatorDesignerAction(const QString &category, int priority) :
Thomas Hartmann's avatar
Thomas Hartmann committed
230
231
232
        m_category(category),
        m_priority(priority),
        m_visibility(&SelectionContextFunctors::always)
233
234
    { m_action->setSeparator(true); }

Thomas Hartmann's avatar
Thomas Hartmann committed
235
236
237
238
239
240
241
    bool isVisible(const SelectionContext &m_selectionState) const { return m_visibility(m_selectionState); }
    bool isEnabled(const SelectionContext &) const { return true; }
    QString category() const { return m_category; }
    QString menuId() const { return QString(); }
    int priority() const { return m_priority; }
    Type type() const { return Action; }
    void setCurrentContext(const SelectionContext &) {}
242
243
244
245

private:
    const QString m_category;
    const int m_priority;
Thomas Hartmann's avatar
Thomas Hartmann committed
246
    SelectionContextFunction m_visibility;
247
248
};

Thomas Hartmann's avatar
Thomas Hartmann committed
249
class ModelNodeAction : public DefaultDesignerAction
250
251
{
public:
Thomas Hartmann's avatar
Thomas Hartmann committed
252
253
254
255
256
    ModelNodeAction(const QString &description,  const QString &category, int priority,
            ModelNodeOperations::SelectionAction selectionAction,
            SelectionContextFunction enabled = &SelectionContextFunctors::always,
            SelectionContextFunction visibility = &SelectionContextFunctors::always) :
        DefaultDesignerAction(new ActionTemplate(description, selectionAction)),
257
        m_category(category),
Thomas Hartmann's avatar
Thomas Hartmann committed
258
259
260
        m_priority(priority),
        m_enabled(enabled),
        m_visibility(visibility)
261
262
    {}

Thomas Hartmann's avatar
Thomas Hartmann committed
263
264
265
266
267
268
    bool isVisible(const SelectionContext &selectionState) const { return m_visibility(selectionState); }
    bool isEnabled(const SelectionContext &selectionState) const { return m_enabled(selectionState); }
    QString category() const { return m_category; }
    QString menuId() const { return QString(); }
    int priority() const { return m_priority; }
    Type type() const { return Action; }
269
270

private:
Thomas Hartmann's avatar
Thomas Hartmann committed
271
272
273
274
    const QString m_category;
    const int m_priority;
    const SelectionContextFunction m_enabled;
    const SelectionContextFunction m_visibility;
275
276
277
278
279
280
};


} //QmlDesigner

#endif // MODELNODECONTEXTMENU_HELPER_H