modelnodecontextmenu.cpp 4.95 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Thomas Murach's avatar
Thomas Murach committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Thomas Murach's avatar
Thomas Murach committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Thomas Murach's avatar
Thomas Murach committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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.
Thomas Murach's avatar
Thomas Murach committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** 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
Thomas Murach's avatar
Thomas Murach committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Thomas Murach's avatar
Thomas Murach committed
29

30
#include "modelnodecontextmenu.h"
31
32
33
34
#include "modelnodecontextmenu_helper.h"
#include "designeractionmanager.h"

#include <cmath>
35
#include <QApplication>
36
#include <QMessageBox>
37
38
39
40
41
42
43
#include <coreplugin/editormanager/editormanager.h>
#include <nodeabstractproperty.h>
#include <nodelistproperty.h>
#include <nodemetainfo.h>
#include <modelnode.h>
#include <qmlitemnode.h>
#include <variantproperty.h>
44
#include <bindingproperty.h>
45
#include <nodeproperty.h>
46
#include <rewritingexception.h>
47
#include <rewritertransaction.h>
48
49
#include <designmodewidget.h>
#include <qmlanchors.h>
50

51
#include <QSet>
52
53
54

namespace QmlDesigner {

55
56
ModelNodeContextMenu::ModelNodeContextMenu(QmlModelView *view) :
    m_selectionContext(view)
57
58
59
{
}

60
static bool sortFunction(AbstractDesignerAction * abstractDesignerAction01, AbstractDesignerAction *abstractDesignerAction02)
61
{
62
    return abstractDesignerAction01->priority() > abstractDesignerAction02->priority();
63
64
}

65
66
static QSet<AbstractDesignerAction* > findMembers(QSet<AbstractDesignerAction* > designerActionSet,
                                                          const QString &category)
67
{
68
    QSet<AbstractDesignerAction* > ret;
69

70
71
72
73
74
     foreach (AbstractDesignerAction* factory, designerActionSet) {
         if (factory->category() == category)
             ret.insert(factory);
     }
     return ret;
75
76
77
}


78
79
80
81
void populateMenu(QSet<AbstractDesignerAction* > &abstractDesignerActions,
                  const QString &category,
                  QMenu* menu,
                  const SelectionContext &selectionContext)
82
{
83
    QSet<AbstractDesignerAction* > matchingFactories = findMembers(abstractDesignerActions, category);
84

85
    abstractDesignerActions.subtract(matchingFactories);
86

87
88
    QList<AbstractDesignerAction* > matchingFactoriesList = matchingFactories.toList();
    qSort(matchingFactoriesList.begin(), matchingFactoriesList.end(), &sortFunction);
89

90
91
92
93
94
    foreach (AbstractDesignerAction* designerAction, matchingFactoriesList) {
       if (designerAction->type() == AbstractDesignerAction::Menu) {
           designerAction->setCurrentContext(selectionContext);
           QMenu *newMenu = designerAction->action()->menu();
           menu->addMenu(newMenu);
95

96
           //recurse
97

98
99
100
101
102
103
           populateMenu(abstractDesignerActions, designerAction->menuId(), newMenu, selectionContext);
       } else if (designerAction->type() == AbstractDesignerAction::Action) {
           QAction* action = designerAction->action();
           designerAction->setCurrentContext(selectionContext);
           menu->addAction(action);
       }
104
105
106
    }
}

107
void ModelNodeContextMenu::execute(const QPoint &position, bool selectionMenuBool)
108
{
109
    QMenu* mainMenu = new QMenu();
110

111
112
    m_selectionContext.setShowSelectionTools(selectionMenuBool);
    m_selectionContext.setScenePos(m_scenePos);
113
114


115
116
     QSet<AbstractDesignerAction* > factories =
             QSet<AbstractDesignerAction* >::fromList(DesignerActionManager::designerActions());
117

118
     populateMenu(factories, QString(""), mainMenu, m_selectionContext);
119

120
121
     mainMenu->exec(position);
     mainMenu->deleteLater();
122
123
}

124
void ModelNodeContextMenu::setScenePos(const QPoint &position)
125
{
126
    m_scenePos = position;
127
128
}

129
130
131
132
void ModelNodeContextMenu::showContextMenu(QmlModelView *view,
                                           const QPoint &globalPosition,
                                           const QPoint &scenePosition,
                                           bool showSelection)
133
134
135
136
137
{
    ModelNodeContextMenu contextMenu(view);
    contextMenu.setScenePos(scenePosition);
    contextMenu.execute(globalPosition, showSelection);
}
138

139
} //QmlDesigner