modelnodecontextmenu_helper.h 7.59 KB
Newer Older
1
2
/****************************************************************************
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
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
** 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"
36
#include "defaultdesigneraction.h"
hjk's avatar
hjk committed
37

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

namespace QmlDesigner {

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

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

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

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

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

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

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

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

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

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

Thomas Hartmann's avatar
Thomas Hartmann committed
92
93
94
95
96
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
97

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

100

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

Thomas Hartmann's avatar
Thomas Hartmann committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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;
};

122
123
124
class MenuDesignerAction : public AbstractDesignerAction
{
public:
Thomas Hartmann's avatar
Thomas Hartmann committed
125
126
127
    MenuDesignerAction(const QString &displayName, const QString &menuId, int priority,
            SelectionContextFunction enabled = &SelectionContextFunctors::always,
            SelectionContextFunction visibility = &SelectionContextFunctors::always) :
128
129
130
        m_displayName(displayName),
        m_menuId(menuId),
        m_priority(priority),
Thomas Hartmann's avatar
Thomas Hartmann committed
131
132
133
        m_menu(new QMenu),
        m_enabled(enabled),
        m_visibility(visibility)
134
135
136
137
138
    {
        m_menu->setTitle(displayName);
        m_action = m_menu->menuAction();
    }

Thomas Hartmann's avatar
Thomas Hartmann committed
139
140
141
142
143
144
145
    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; }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

    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
168
169
    SelectionContextFunction m_enabled;
    SelectionContextFunction m_visibility;
170
171
172
173
174
175
};

class SeperatorDesignerAction : public DefaultDesignerAction
{
public:
    SeperatorDesignerAction(const QString &category, int priority) :
Thomas Hartmann's avatar
Thomas Hartmann committed
176
177
178
        m_category(category),
        m_priority(priority),
        m_visibility(&SelectionContextFunctors::always)
179
    { defaultAction()->setSeparator(true); }
180

Thomas Hartmann's avatar
Thomas Hartmann committed
181
182
183
184
185
186
187
    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 &) {}
188
189
190
191

private:
    const QString m_category;
    const int m_priority;
Thomas Hartmann's avatar
Thomas Hartmann committed
192
    SelectionContextFunction m_visibility;
193
194
};

Thomas Hartmann's avatar
Thomas Hartmann committed
195
class ModelNodeAction : public DefaultDesignerAction
196
197
{
public:
Thomas Hartmann's avatar
Thomas Hartmann committed
198
199
200
201
202
    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)),
203
        m_category(category),
Thomas Hartmann's avatar
Thomas Hartmann committed
204
205
206
        m_priority(priority),
        m_enabled(enabled),
        m_visibility(visibility)
207
208
    {}

Thomas Hartmann's avatar
Thomas Hartmann committed
209
210
211
212
213
214
    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; }
215
216

private:
Thomas Hartmann's avatar
Thomas Hartmann committed
217
218
219
220
    const QString m_category;
    const int m_priority;
    const SelectionContextFunction m_enabled;
    const SelectionContextFunction m_visibility;
221
222
223
224
225
226
};


} //QmlDesigner

#endif // MODELNODECONTEXTMENU_HELPER_H