File indexing completed on 2024-11-10 04:50:17
0001 /* 0002 snippet feature from kdevelop/plugins/snippet/ 0003 0004 SPDX-FileCopyrightText: 2007 Robert Gruber <rgruber@users.sourceforge.net> 0005 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include "snippettreeview.h" 0011 #include "snippetsmanager.h" 0012 0013 #include <KActionCollection> 0014 #include <KLocalizedString> 0015 0016 #include <QContextMenuEvent> 0017 #include <QHeaderView> 0018 #include <QMenu> 0019 using namespace MailCommon; 0020 SnippetTreeView::SnippetTreeView(KActionCollection *actionCollection, QWidget *parent) 0021 : QTreeView(parent) 0022 { 0023 header()->hide(); 0024 setAcceptDrops(true); 0025 setDragEnabled(true); 0026 setRootIsDecorated(true); 0027 setAlternatingRowColors(true); 0028 mSnippetsManager = new MailCommon::SnippetsManager(actionCollection, this, this); 0029 connect(mSnippetsManager, &MailCommon::SnippetsManager::insertSnippetInfo, this, &SnippetTreeView::insertSnippetInfo); 0030 0031 setModel(mSnippetsManager->model()); 0032 setSelectionModel(mSnippetsManager->selectionModel()); 0033 0034 connect(this, &QAbstractItemView::activated, mSnippetsManager->insertSnippetAction(), &QAction::trigger); 0035 connect(mSnippetsManager->model(), &QAbstractItemModel::rowsInserted, this, &QTreeView::expandAll); 0036 connect(mSnippetsManager->model(), &QAbstractItemModel::rowsRemoved, this, &QTreeView::expandAll); 0037 0038 expandAll(); 0039 } 0040 0041 SnippetTreeView::~SnippetTreeView() = default; 0042 0043 void SnippetTreeView::contextMenuEvent(QContextMenuEvent *event) 0044 { 0045 QMenu popup; 0046 0047 const bool itemSelected = mSnippetsManager->selectionModel()->hasSelection(); 0048 0049 bool canAddSnippet = true; 0050 if (itemSelected) { 0051 popup.setTitle(mSnippetsManager->selectedName()); 0052 if (mSnippetsManager->snippetGroupSelected()) { 0053 popup.addAction(mSnippetsManager->editSnippetGroupAction()); 0054 popup.addAction(mSnippetsManager->deleteSnippetGroupAction()); 0055 } else { 0056 canAddSnippet = false; // subsnippets are not permitted 0057 popup.addAction(mSnippetsManager->addSnippetAction()); 0058 popup.addAction(mSnippetsManager->editSnippetAction()); 0059 popup.addAction(mSnippetsManager->deleteSnippetAction()); 0060 popup.addAction(mSnippetsManager->insertSnippetAction()); 0061 } 0062 popup.addSeparator(); 0063 } else { 0064 popup.setTitle(i18n("Text Snippets")); 0065 } 0066 if (canAddSnippet) { 0067 popup.addAction(mSnippetsManager->addSnippetAction()); 0068 } 0069 popup.addAction(mSnippetsManager->addSnippetGroupAction()); 0070 0071 popup.exec(event->globalPos()); 0072 } 0073 0074 void SnippetTreeView::dropEvent(QDropEvent *event) 0075 { 0076 if (event->source() == this) { 0077 event->setDropAction(Qt::MoveAction); 0078 } 0079 QTreeView::dropEvent(event); 0080 } 0081 0082 MailCommon::SnippetsManager *SnippetTreeView::snippetsManager() const 0083 { 0084 return mSnippetsManager; 0085 } 0086 0087 #include "moc_snippettreeview.cpp"