Mailcommon

snippettreeview.cpp
1/*
2 snippet feature from kdevelop/plugins/snippet/
3
4 SPDX-FileCopyrightText: 2007 Robert Gruber <rgruber@users.sourceforge.net>
5 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include "snippettreeview.h"
11#include "snippetsmanager.h"
12
13#include <KActionCollection>
14#include <KLocalizedString>
15
16#include <QContextMenuEvent>
17#include <QHeaderView>
18#include <QMenu>
19using namespace MailCommon;
20SnippetTreeView::SnippetTreeView(KActionCollection *actionCollection, QWidget *parent)
21 : QTreeView(parent)
22{
23 header()->hide();
24 setAcceptDrops(true);
25 setDragEnabled(true);
26 setRootIsDecorated(true);
27 setAlternatingRowColors(true);
28 mSnippetsManager = new MailCommon::SnippetsManager(actionCollection, this, this);
29 connect(mSnippetsManager, &MailCommon::SnippetsManager::insertSnippetInfo, this, &SnippetTreeView::insertSnippetInfo);
30
31 setModel(mSnippetsManager->model());
32 setSelectionModel(mSnippetsManager->selectionModel());
33
34 connect(this, &QAbstractItemView::activated, mSnippetsManager->insertSnippetAction(), &QAction::trigger);
35 connect(mSnippetsManager->model(), &QAbstractItemModel::rowsInserted, this, &QTreeView::expandAll);
36 connect(mSnippetsManager->model(), &QAbstractItemModel::rowsRemoved, this, &QTreeView::expandAll);
37
38 expandAll();
39}
40
41SnippetTreeView::~SnippetTreeView() = default;
42
43void SnippetTreeView::contextMenuEvent(QContextMenuEvent *event)
44{
45 QMenu popup;
46
47 const bool itemSelected = mSnippetsManager->selectionModel()->hasSelection();
48
49 bool canAddSnippet = true;
50 if (itemSelected) {
51 popup.setTitle(mSnippetsManager->selectedName());
52 if (mSnippetsManager->snippetGroupSelected()) {
53 popup.addAction(mSnippetsManager->editSnippetGroupAction());
54 popup.addAction(mSnippetsManager->deleteSnippetGroupAction());
55 } else {
56 canAddSnippet = false; // subsnippets are not permitted
57 popup.addAction(mSnippetsManager->addSnippetAction());
58 popup.addAction(mSnippetsManager->editSnippetAction());
59 popup.addAction(mSnippetsManager->deleteSnippetAction());
60 popup.addAction(mSnippetsManager->insertSnippetAction());
61 }
62 popup.addSeparator();
63 } else {
64 popup.setTitle(i18n("Text Snippets"));
65 }
66 if (canAddSnippet) {
67 popup.addAction(mSnippetsManager->addSnippetAction());
68 }
69 popup.addAction(mSnippetsManager->addSnippetGroupAction());
70
71 popup.exec(event->globalPos());
72}
73
74void SnippetTreeView::dropEvent(QDropEvent *event)
75{
76 if (event->source() == this) {
77 event->setDropAction(Qt::MoveAction);
78 }
80}
81
82MailCommon::SnippetsManager *SnippetTreeView::snippetsManager() const
83{
84 return mSnippetsManager;
85}
86
87#include "moc_snippettreeview.cpp"
The SnippetsManager class.
QString selectedName() const
Returns the name of the currently selected snippet or snippet group.
QAction * editSnippetAction() const
Returns the action that handles editing the currently selected snippet.
bool snippetGroupSelected() const
Returns whether the currently selected item is a snippet group.
QAction * deleteSnippetAction() const
Returns the action that handles deleting the currently selected snippet.
QAction * addSnippetAction() const
Returns the action that handles adding new snippets.
QAction * addSnippetGroupAction() const
Returns the action that handles adding new snippet groups.
QAction * insertSnippetAction() const
Returns the action that handles inserting a snippet into the editor.
QAction * deleteSnippetGroupAction() const
Returns the action that handles deleting the currently selected snippet group.
QItemSelectionModel * selectionModel() const
Returns the selection model that is used by the manager to select the snippet or snippet group to wor...
QAction * editSnippetGroupAction() const
Returns the action that handles editing the currently selected snippet group.
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
void rowsInserted(const QModelIndex &parent, int first, int last)
void rowsRemoved(const QModelIndex &parent, int first, int last)
void activated(const QModelIndex &index)
virtual void dropEvent(QDropEvent *event) override
virtual bool event(QEvent *event) override
void trigger()
bool hasSelection() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * addSeparator()
QAction * exec()
void setTitle(const QString &title)
MoveAction
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void expandAll()
void hide()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:01 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.