File indexing completed on 2024-05-05 16:27:22
0001 /* This file is part of KGraphViewer. 0002 Copyright (C) 2007 Gael de Chalendar <kleag@free.fr> 0003 0004 KGraphViewer is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU General Public 0006 License as published by the Free Software Foundation, version 2. 0007 0008 This program is distributed in the hope that it will be useful, 0009 but WITHOUT ANY WARRANTY; without even the implied warranty of 0010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0011 General Public License for more details. 0012 0013 You should have received a copy of the GNU General Public License 0014 along with this program; if not, write to the Free Software 0015 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0016 02110-1301, USA 0017 */ 0018 0019 #include "KGraphEditorNodesTreeWidget.h" 0020 #include "kgrapheditor_debug.h" 0021 0022 #include <QAction> 0023 #include <QDebug> 0024 0025 #include <QContextMenuEvent> 0026 #include <QMenu> 0027 #include <klocalizedstring.h> 0028 0029 KGraphEditorNodesTreeWidget::KGraphEditorNodesTreeWidget(QWidget *parent) 0030 : QTreeWidget(parent) 0031 , m_popup(nullptr) 0032 , m_item(nullptr) 0033 { 0034 } 0035 0036 KGraphEditorNodesTreeWidget::~KGraphEditorNodesTreeWidget() 0037 { 0038 } 0039 0040 void KGraphEditorNodesTreeWidget::setupPopup(const QPoint &point) 0041 { 0042 qCDebug(KGRAPHEDITOR_LOG) << point; 0043 0044 if (m_popup) { 0045 delete m_popup; 0046 } 0047 m_popup = new QMenu(); 0048 0049 m_item = itemAt(point); 0050 if (m_item == nullptr) { 0051 qCDebug(KGRAPHEDITOR_LOG) << "no item at" << point; 0052 return; 0053 } 0054 QAction *aaa = new QAction(i18n("Add a new attribute"), this); 0055 connect(aaa, &QAction::triggered, this, &KGraphEditorNodesTreeWidget::slotAddAttribute); 0056 m_popup->addAction(aaa); 0057 0058 if (m_item->parent()) // attribute item 0059 { 0060 QAction *raa = new QAction(i18n("Remove this attribute"), this); 0061 connect(raa, &QAction::triggered, this, &KGraphEditorNodesTreeWidget::slotRemoveAttribute); 0062 m_popup->addAction(raa); 0063 } 0064 m_popup->addSeparator(); 0065 QAction *rna = new QAction(i18n("Remove this node"), this); 0066 connect(rna, &QAction::triggered, this, &KGraphEditorNodesTreeWidget::slotRemoveNode); 0067 m_popup->addAction(rna); 0068 } 0069 0070 void KGraphEditorNodesTreeWidget::slotRemoveNode() 0071 { 0072 emit removeNode(m_item->text(0)); 0073 delete takeTopLevelItem(indexOfTopLevelItem(m_item)); 0074 m_item = nullptr; 0075 } 0076 0077 void KGraphEditorNodesTreeWidget::slotRemoveElement(const QString &id) 0078 { 0079 qCDebug(KGRAPHEDITOR_LOG) << id; 0080 QList<QTreeWidgetItem *> items = findItems(id, Qt::MatchExactly, 0); 0081 for (QTreeWidgetItem *item : items) { 0082 delete takeTopLevelItem(indexOfTopLevelItem(item)); 0083 } 0084 } 0085 0086 void KGraphEditorNodesTreeWidget::slotAddAttribute() 0087 { 0088 qCDebug(KGRAPHEDITOR_LOG) << "Add Attribute"; 0089 QString nodeName = "NewAttribute"; 0090 emit addAttribute(m_item->text(0)); 0091 if (m_item->parent() == nullptr) { 0092 nodeName += QString::number(m_item->childCount()); 0093 QTreeWidgetItem *item = new QTreeWidgetItem(m_item, QStringList(nodeName)); 0094 item->setFlags(item->flags() | Qt::ItemIsEditable); 0095 } else { 0096 nodeName += QString::number(m_item->parent()->childCount()); 0097 QTreeWidgetItem *item = new QTreeWidgetItem(m_item->parent(), QStringList(nodeName)); 0098 item->setFlags(item->flags() | Qt::ItemIsEditable); 0099 } 0100 } 0101 0102 void KGraphEditorNodesTreeWidget::slotRemoveAttribute() 0103 { 0104 qCDebug(KGRAPHEDITOR_LOG) << "Remove Attribute"; 0105 emit removeAttribute(m_item->parent()->text(0), m_item->text(0)); 0106 m_item->parent()->removeChild(m_item); 0107 m_item = nullptr; 0108 } 0109 0110 void KGraphEditorNodesTreeWidget::contextMenuEvent(QContextMenuEvent *e) 0111 { 0112 setupPopup(e->pos()); 0113 m_popup->exec(e->globalPos()); 0114 } 0115 0116 #include "moc_KGraphEditorNodesTreeWidget.cpp"