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