Changeset View
Changeset View
Standalone View
Standalone View
src/KGraphEditorNodesTreeWidget.cpp
Show All 10 Lines | 1 | /* This file is part of KGraphViewer. | |||
---|---|---|---|---|---|
11 | General Public License for more details. | 11 | General Public License for more details. | ||
12 | 12 | | |||
13 | You should have received a copy of the GNU General Public License | 13 | You should have received a copy of the GNU General Public License | ||
14 | along with this program; if not, write to the Free Software | 14 | along with this program; if not, write to the Free Software | ||
15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
16 | 02110-1301, USA | 16 | 02110-1301, USA | ||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | | ||||
20 | #include "kgrapheditor_debug.h" | | |||
21 | #include "KGraphEditorNodesTreeWidget.h" | 19 | #include "KGraphEditorNodesTreeWidget.h" | ||
20 | #include "kgrapheditor_debug.h" | ||||
22 | 21 | | |||
23 | #include <QDebug> | | |||
24 | #include <QAction> | 22 | #include <QAction> | ||
23 | #include <QDebug> | ||||
25 | 24 | | |||
26 | #include <QMenu> | | |||
27 | #include <QContextMenuEvent> | 25 | #include <QContextMenuEvent> | ||
26 | #include <QMenu> | ||||
28 | #include <klocalizedstring.h> | 27 | #include <klocalizedstring.h> | ||
29 | 28 | | |||
30 | KGraphEditorNodesTreeWidget::KGraphEditorNodesTreeWidget(QWidget* parent) : | 29 | KGraphEditorNodesTreeWidget::KGraphEditorNodesTreeWidget(QWidget *parent) | ||
31 | QTreeWidget(parent), | 30 | : QTreeWidget(parent), m_popup(nullptr), m_item(nullptr) {} | ||
32 | m_popup(nullptr), | | |||
33 | m_item(nullptr) | | |||
34 | { | | |||
35 | } | | |||
36 | 31 | | |||
37 | KGraphEditorNodesTreeWidget::~KGraphEditorNodesTreeWidget() | 32 | KGraphEditorNodesTreeWidget::~KGraphEditorNodesTreeWidget() {} | ||
38 | { | | |||
39 | } | | |||
40 | 33 | | |||
41 | void KGraphEditorNodesTreeWidget::setupPopup(const QPoint& point) | 34 | void KGraphEditorNodesTreeWidget::setupPopup(const QPoint &point) { | ||
42 | { | | |||
43 | qCDebug(KGRAPHEDITOR_LOG) << point; | 35 | qCDebug(KGRAPHEDITOR_LOG) << point; | ||
44 | 36 | | |||
45 | if (m_popup) | 37 | if (m_popup) { | ||
46 | { | | |||
47 | delete m_popup; | 38 | delete m_popup; | ||
48 | } | 39 | } | ||
49 | m_popup = new QMenu(); | 40 | m_popup = new QMenu(); | ||
50 | 41 | | |||
51 | m_item = itemAt(point); | 42 | m_item = itemAt(point); | ||
52 | if (m_item == nullptr) | 43 | if (m_item == nullptr) { | ||
53 | { | | |||
54 | qCDebug(KGRAPHEDITOR_LOG) << "no item at" << point; | 44 | qCDebug(KGRAPHEDITOR_LOG) << "no item at" << point; | ||
55 | return; | 45 | return; | ||
56 | } | 46 | } | ||
57 | QAction* aaa = new QAction(i18n("Add a new attribute"), this); | 47 | QAction *aaa = new QAction(i18n("Add a new attribute"), this); | ||
58 | connect(aaa, &QAction::triggered, | 48 | connect(aaa, &QAction::triggered, this, | ||
59 | this, &KGraphEditorNodesTreeWidget::slotAddAttribute); | 49 | &KGraphEditorNodesTreeWidget::slotAddAttribute); | ||
60 | m_popup->addAction(aaa); | 50 | m_popup->addAction(aaa); | ||
61 | 51 | | |||
62 | if (m_item->parent()) // attribute item | 52 | if (m_item->parent()) // attribute item | ||
63 | { | 53 | { | ||
64 | QAction* raa = new QAction(i18n("Remove this attribute"), this); | 54 | QAction *raa = new QAction(i18n("Remove this attribute"), this); | ||
65 | connect(raa, &QAction::triggered, | 55 | connect(raa, &QAction::triggered, this, | ||
66 | this, &KGraphEditorNodesTreeWidget::slotRemoveAttribute); | 56 | &KGraphEditorNodesTreeWidget::slotRemoveAttribute); | ||
67 | m_popup->addAction(raa); | 57 | m_popup->addAction(raa); | ||
68 | } | 58 | } | ||
69 | m_popup->addSeparator(); | 59 | m_popup->addSeparator(); | ||
70 | QAction* rna = new QAction(i18n("Remove this node"), this); | 60 | QAction *rna = new QAction(i18n("Remove this node"), this); | ||
71 | connect(rna, &QAction::triggered, | 61 | connect(rna, &QAction::triggered, this, | ||
72 | this, &KGraphEditorNodesTreeWidget::slotRemoveNode); | 62 | &KGraphEditorNodesTreeWidget::slotRemoveNode); | ||
73 | m_popup->addAction(rna); | 63 | m_popup->addAction(rna); | ||
74 | } | 64 | } | ||
75 | 65 | | |||
76 | void KGraphEditorNodesTreeWidget::slotRemoveNode() | 66 | void KGraphEditorNodesTreeWidget::slotRemoveNode() { | ||
77 | { | | |||
78 | emit removeNode(m_item->text(0)); | 67 | emit removeNode(m_item->text(0)); | ||
79 | delete takeTopLevelItem (indexOfTopLevelItem(m_item)); | 68 | delete takeTopLevelItem(indexOfTopLevelItem(m_item)); | ||
80 | m_item = nullptr; | 69 | m_item = nullptr; | ||
81 | } | 70 | } | ||
82 | 71 | | |||
83 | void KGraphEditorNodesTreeWidget::slotRemoveElement(const QString& id) | 72 | void KGraphEditorNodesTreeWidget::slotRemoveElement(const QString &id) { | ||
84 | { | | |||
85 | qCDebug(KGRAPHEDITOR_LOG) << id; | 73 | qCDebug(KGRAPHEDITOR_LOG) << id; | ||
86 | QList<QTreeWidgetItem*> items = findItems(id,Qt::MatchExactly,0); | 74 | QList<QTreeWidgetItem *> items = findItems(id, Qt::MatchExactly, 0); | ||
87 | foreach (QTreeWidgetItem* item, items) | 75 | foreach (QTreeWidgetItem *item, items) { | ||
88 | { | | |||
89 | delete takeTopLevelItem (indexOfTopLevelItem(item)); | 76 | delete takeTopLevelItem(indexOfTopLevelItem(item)); | ||
90 | } | 77 | } | ||
91 | } | 78 | } | ||
92 | 79 | | |||
93 | void KGraphEditorNodesTreeWidget::slotAddAttribute() | 80 | void KGraphEditorNodesTreeWidget::slotAddAttribute() { | ||
94 | { | | |||
95 | qCDebug(KGRAPHEDITOR_LOG) << "Add Attribute"; | 81 | qCDebug(KGRAPHEDITOR_LOG) << "Add Attribute"; | ||
96 | QString nodeName = "NewAttribute"; | 82 | QString nodeName = "NewAttribute"; | ||
97 | emit addAttribute(m_item->text(0)); | 83 | emit addAttribute(m_item->text(0)); | ||
98 | if (m_item->parent() == nullptr) | 84 | if (m_item->parent() == nullptr) { | ||
99 | { | | |||
100 | nodeName += QString::number(m_item->childCount()); | 85 | nodeName += QString::number(m_item->childCount()); | ||
101 | QTreeWidgetItem* item = new QTreeWidgetItem(m_item, QStringList(nodeName)); | 86 | QTreeWidgetItem *item = | ||
87 | new QTreeWidgetItem(m_item, QStringList(nodeName)); | ||||
102 | item->setFlags(item->flags() | Qt::ItemIsEditable); | 88 | item->setFlags(item->flags() | Qt::ItemIsEditable); | ||
103 | } | 89 | } else { | ||
104 | else | | |||
105 | { | | |||
106 | nodeName += QString::number(m_item->parent()->childCount()); | 90 | nodeName += QString::number(m_item->parent()->childCount()); | ||
107 | QTreeWidgetItem* item = new QTreeWidgetItem(m_item->parent(), QStringList(nodeName)); | 91 | QTreeWidgetItem *item = | ||
92 | new QTreeWidgetItem(m_item->parent(), QStringList(nodeName)); | ||||
108 | item->setFlags(item->flags() | Qt::ItemIsEditable); | 93 | item->setFlags(item->flags() | Qt::ItemIsEditable); | ||
109 | } | 94 | } | ||
110 | } | 95 | } | ||
111 | 96 | | |||
112 | void KGraphEditorNodesTreeWidget::slotRemoveAttribute() | 97 | void KGraphEditorNodesTreeWidget::slotRemoveAttribute() { | ||
113 | { | | |||
114 | qCDebug(KGRAPHEDITOR_LOG) << "Remove Attribute"; | 98 | qCDebug(KGRAPHEDITOR_LOG) << "Remove Attribute"; | ||
115 | emit removeAttribute(m_item->parent()->text(0), m_item->text(0)); | 99 | emit removeAttribute(m_item->parent()->text(0), m_item->text(0)); | ||
116 | m_item->parent()->removeChild(m_item); | 100 | m_item->parent()->removeChild(m_item); | ||
117 | m_item = nullptr; | 101 | m_item = nullptr; | ||
118 | } | 102 | } | ||
119 | 103 | | |||
120 | 104 | void KGraphEditorNodesTreeWidget::contextMenuEvent(QContextMenuEvent *e) { | |||
121 | | ||||
122 | void KGraphEditorNodesTreeWidget::contextMenuEvent ( QContextMenuEvent * e ) | | |||
123 | { | | |||
124 | setupPopup(e->pos()); | 105 | setupPopup(e->pos()); | ||
125 | m_popup->exec(e->globalPos()); | 106 | m_popup->exec(e->globalPos()); | ||
126 | } | 107 | } |