]> git.sven.stormbind.net Git - sven/vym.git/blob - src/treeeditor.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / treeeditor.cpp
1 #include "treeeditor.h"
2
3 #include <QAction>
4 #include <QMenu>
5 #include <QRegExp>
6
7 #include "mainwindow.h"
8 #include "vymmodel.h"
9
10 extern Main *mainWindow;
11 extern QString editorFocusStyle;
12
13 extern QMenu *branchContextMenu;
14 extern QMenu *canvasContextMenu;
15 extern QMenu *floatimageContextMenu;
16
17 ///////////////////////////////////////////////////////////////////////
18 ///////////////////////////////////////////////////////////////////////
19 TreeEditor::TreeEditor(VymModel *m)
20 {
21     model = m;
22     if (model)
23         setModel(model);
24     init();
25 }
26
27 void TreeEditor::init()
28 {
29     setSelectionMode(QAbstractItemView::ExtendedSelection);
30     header()->hide();
31
32     QAction *a;
33     // Shortcuts for navigating with cursor:
34     a = new QAction(tr("Select upper object", "Tree Editor"), this);
35     a->setShortcut(Qt::Key_Up);
36     a->setShortcutContext(Qt::WidgetShortcut);
37     addAction(a);
38     connect(a, SIGNAL(triggered()), this, SLOT(cursorUp()));
39
40     a = new QAction(tr("Select lower object", "Tree Editor"), this);
41     a->setShortcut(Qt::Key_Down);
42     a->setShortcutContext(Qt::WidgetShortcut);
43     addAction(a);
44     connect(a, SIGNAL(triggered()), this, SLOT(cursorDown()));
45
46     a = new QAction(this);
47     a->setShortcut(Qt::Key_PageUp);
48     a->setShortcutContext(Qt::WidgetShortcut);
49     addAction(a);
50     connect(a, SIGNAL(triggered()), mainWindow, SLOT(editMoveUp()));
51
52     a = new QAction(this);
53     a->setShortcut(Qt::Key_PageDown);
54     a->setShortcutContext(Qt::WidgetShortcut);
55     addAction(a);
56     connect(a, SIGNAL(triggered()), mainWindow, SLOT(editMoveDown()));
57
58     a = new QAction(this);
59     a->setShortcut(Qt::Key_Return);
60     a->setShortcutContext(Qt::WidgetShortcut);
61     addAction(a);
62     connect(a, SIGNAL(triggered()), this, SLOT(startEdit()));
63
64     // Clone actions defined in MainWindow
65     foreach (QAction *qa, mainWindow->mapEditorActions) {
66         a = new QAction(this);
67         a->setShortcut(qa->shortcut());
68         a->setShortcutContext(qa->shortcutContext());
69         connect(a, SIGNAL(triggered()), qa, SLOT(trigger()));
70         addAction(a);
71     }
72
73     setStyleSheet("QTreeView:focus {" + editorFocusStyle + "}");
74 }
75
76 TreeEditor::~TreeEditor()
77 {
78     // qDebug()<<"Destructor TreeEditor for "<<model->getMapName();
79 }
80
81 QModelIndex TreeEditor::getSelectedIndex()
82 {
83     QModelIndexList list = selectionModel()->selectedIndexes();
84     if (list.isEmpty())
85         return QModelIndex();
86     else
87         return list.first();
88 }
89
90 void TreeEditor::contextMenuEvent(QContextMenuEvent *e) {
91     if (model->getSelectedBranch())
92         branchContextMenu->popup(e->globalPos());
93     else if (model->getSelectedImage())
94         floatimageContextMenu->popup(e->globalPos());
95     else if (model->getSelectedXLink())
96         model->editXLink();
97     else
98         canvasContextMenu->exec(e->globalPos());
99
100     e->accept();
101 }
102
103 void TreeEditor::cursorUp()
104 {
105     QModelIndex ix = getSelectedIndex();
106     ix = indexAbove(ix);
107     if (ix.isValid())
108         model->select(ix);
109 }
110
111 void TreeEditor::cursorDown()
112 {
113     QModelIndex ix = getSelectedIndex();
114     ix = indexBelow(ix);
115     if (ix.isValid())
116         model->select(ix);
117 }
118
119 void TreeEditor::startEdit()
120 {
121
122     QModelIndex ix = getSelectedIndex();
123     if (ix.isValid())
124         edit(ix);
125 }