Changeset View
Changeset View
Standalone View
Standalone View
src/KGraphEditorElementTreeWidget.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 "KGraphEditorElementTreeWidget.h" | 19 | #include "KGraphEditorElementTreeWidget.h" | ||
20 | #include "kgrapheditor_debug.h" | ||||
22 | 21 | | |||
23 | #include <QDebug> | | |||
24 | #include <QAction> | 22 | #include <QAction> | ||
25 | #include <QMenu> | | |||
26 | #include <QContextMenuEvent> | 23 | #include <QContextMenuEvent> | ||
24 | #include <QDebug> | ||||
25 | #include <QMenu> | ||||
27 | #include <klocalizedstring.h> | 26 | #include <klocalizedstring.h> | ||
28 | 27 | | |||
29 | KGraphEditorElementTreeWidget::KGraphEditorElementTreeWidget(QWidget* parent) : | 28 | KGraphEditorElementTreeWidget::KGraphEditorElementTreeWidget(QWidget *parent) | ||
30 | QTreeWidget(parent), | 29 | : QTreeWidget(parent), m_popup(nullptr), m_item(nullptr) {} | ||
31 | m_popup(nullptr), | | |||
32 | m_item(nullptr) | | |||
33 | { | | |||
34 | } | | |||
35 | 30 | | |||
36 | KGraphEditorElementTreeWidget::~KGraphEditorElementTreeWidget() | 31 | KGraphEditorElementTreeWidget::~KGraphEditorElementTreeWidget() {} | ||
37 | { | | |||
38 | } | | |||
39 | 32 | | |||
40 | void KGraphEditorElementTreeWidget::setupPopup(const QPoint& point) | 33 | void KGraphEditorElementTreeWidget::setupPopup(const QPoint &point) { | ||
41 | { | | |||
42 | qCDebug(KGRAPHEDITOR_LOG) << point; | 34 | qCDebug(KGRAPHEDITOR_LOG) << point; | ||
43 | 35 | | |||
44 | if (m_popup) | 36 | if (m_popup) { | ||
45 | { | | |||
46 | delete m_popup; | 37 | delete m_popup; | ||
47 | } | 38 | } | ||
48 | m_popup = new QMenu(); | 39 | m_popup = new QMenu(); | ||
49 | 40 | | |||
50 | m_item = itemAt(point); | 41 | m_item = itemAt(point); | ||
51 | QAction* aaa = new QAction(i18n("Add a new attribute"), this); | 42 | QAction *aaa = new QAction(i18n("Add a new attribute"), this); | ||
52 | connect(aaa, &QAction::triggered, | 43 | connect(aaa, &QAction::triggered, this, | ||
53 | this, &KGraphEditorElementTreeWidget::slotAddAttribute); | 44 | &KGraphEditorElementTreeWidget::slotAddAttribute); | ||
54 | m_popup->addAction(aaa); | 45 | m_popup->addAction(aaa); | ||
55 | 46 | | |||
56 | if (m_item) // attribute item | 47 | if (m_item) // attribute item | ||
57 | { | 48 | { | ||
58 | QAction* raa = new QAction(i18n("Remove this attribute"), this); | 49 | QAction *raa = new QAction(i18n("Remove this attribute"), this); | ||
59 | connect(raa, &QAction::triggered, | 50 | connect(raa, &QAction::triggered, this, | ||
60 | this, &KGraphEditorElementTreeWidget::slotRemoveAttribute); | 51 | &KGraphEditorElementTreeWidget::slotRemoveAttribute); | ||
61 | m_popup->addAction(raa); | 52 | m_popup->addAction(raa); | ||
62 | } | 53 | } | ||
63 | } | 54 | } | ||
64 | 55 | | |||
65 | void KGraphEditorElementTreeWidget::slotAddAttribute() | 56 | void KGraphEditorElementTreeWidget::slotAddAttribute() { | ||
66 | { | | |||
67 | QString nodeName = "NewAttribute"; | 57 | QString nodeName = "NewAttribute"; | ||
68 | nodeName += QString::number(topLevelItemCount()); | 58 | nodeName += QString::number(topLevelItemCount()); | ||
69 | emit addAttribute(nodeName); | 59 | emit addAttribute(nodeName); | ||
70 | QTreeWidgetItem* item = new QTreeWidgetItem(this, QStringList(nodeName)); | 60 | QTreeWidgetItem *item = new QTreeWidgetItem(this, QStringList(nodeName)); | ||
71 | item->setFlags(item->flags() | Qt::ItemIsEditable); | 61 | item->setFlags(item->flags() | Qt::ItemIsEditable); | ||
72 | } | 62 | } | ||
73 | 63 | | |||
74 | void KGraphEditorElementTreeWidget::slotRemoveAttribute() | 64 | void KGraphEditorElementTreeWidget::slotRemoveAttribute() { | ||
75 | { | | |||
76 | qCDebug(KGRAPHEDITOR_LOG) << "Remove Attribute"; | 65 | qCDebug(KGRAPHEDITOR_LOG) << "Remove Attribute"; | ||
77 | if (m_item == nullptr) // should not happen | 66 | if (m_item == nullptr) // should not happen | ||
78 | { | 67 | { | ||
79 | qCWarning(KGRAPHEDITOR_LOG) << "null item ; should not happen" << endl; | 68 | qCWarning(KGRAPHEDITOR_LOG) << "null item ; should not happen" << endl; | ||
80 | return; | 69 | return; | ||
81 | } | 70 | } | ||
82 | emit removeAttribute(m_item->text(0)); | 71 | emit removeAttribute(m_item->text(0)); | ||
83 | delete takeTopLevelItem (indexOfTopLevelItem(m_item)); | 72 | delete takeTopLevelItem(indexOfTopLevelItem(m_item)); | ||
84 | m_item = nullptr; | 73 | m_item = nullptr; | ||
85 | } | 74 | } | ||
86 | 75 | | |||
87 | 76 | void KGraphEditorElementTreeWidget::contextMenuEvent(QContextMenuEvent *e) { | |||
88 | | ||||
89 | void KGraphEditorElementTreeWidget::contextMenuEvent ( QContextMenuEvent * e ) | | |||
90 | { | | |||
91 | setupPopup(e->pos()); | 77 | setupPopup(e->pos()); | ||
92 | m_popup->exec(e->globalPos()); | 78 | m_popup->exec(e->globalPos()); | ||
93 | } | 79 | } |