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 "KGraphEditorElementTreeWidget.h" 0020 #include "kgrapheditor_debug.h" 0021 0022 #include <QAction> 0023 #include <QContextMenuEvent> 0024 #include <QDebug> 0025 #include <QMenu> 0026 #include <klocalizedstring.h> 0027 0028 KGraphEditorElementTreeWidget::KGraphEditorElementTreeWidget(QWidget *parent) 0029 : QTreeWidget(parent) 0030 , m_popup(nullptr) 0031 , m_item(nullptr) 0032 { 0033 } 0034 0035 KGraphEditorElementTreeWidget::~KGraphEditorElementTreeWidget() 0036 { 0037 } 0038 0039 void KGraphEditorElementTreeWidget::setupPopup(const QPoint &point) 0040 { 0041 qCDebug(KGRAPHEDITOR_LOG) << point; 0042 0043 if (m_popup) { 0044 delete m_popup; 0045 } 0046 m_popup = new QMenu(); 0047 0048 m_item = itemAt(point); 0049 QAction *aaa = new QAction(i18n("Add a new attribute"), this); 0050 connect(aaa, &QAction::triggered, this, &KGraphEditorElementTreeWidget::slotAddAttribute); 0051 m_popup->addAction(aaa); 0052 0053 if (m_item) // attribute item 0054 { 0055 QAction *raa = new QAction(i18n("Remove this attribute"), this); 0056 connect(raa, &QAction::triggered, this, &KGraphEditorElementTreeWidget::slotRemoveAttribute); 0057 m_popup->addAction(raa); 0058 } 0059 } 0060 0061 void KGraphEditorElementTreeWidget::slotAddAttribute() 0062 { 0063 QString nodeName = "NewAttribute"; 0064 nodeName += QString::number(topLevelItemCount()); 0065 emit addAttribute(nodeName); 0066 QTreeWidgetItem *item = new QTreeWidgetItem(this, QStringList(nodeName)); 0067 item->setFlags(item->flags() | Qt::ItemIsEditable); 0068 } 0069 0070 void KGraphEditorElementTreeWidget::slotRemoveAttribute() 0071 { 0072 qCDebug(KGRAPHEDITOR_LOG) << "Remove Attribute"; 0073 if (m_item == nullptr) // should not happen 0074 { 0075 qCWarning(KGRAPHEDITOR_LOG) << "null item ; should not happen" << Qt::endl; 0076 return; 0077 } 0078 emit removeAttribute(m_item->text(0)); 0079 delete takeTopLevelItem(indexOfTopLevelItem(m_item)); 0080 m_item = nullptr; 0081 } 0082 0083 void KGraphEditorElementTreeWidget::contextMenuEvent(QContextMenuEvent *e) 0084 { 0085 setupPopup(e->pos()); 0086 m_popup->exec(e->globalPos()); 0087 } 0088 0089 #include "moc_KGraphEditorElementTreeWidget.cpp"