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;
20
21SnippetTreeView::SnippetTreeView(KActionCollection *actionCollection, QWidget *parent)
22 : QTreeView(parent)
23{
24 header()->hide();
25 setDragDropMode(DragDrop);
26 setRootIsDecorated(true);
27 setAlternatingRowColors(true);
28 setDefaultDropAction(Qt::MoveAction); // allow to reorder without having to hold Shift
29 mSnippetsManager = new MailCommon::SnippetsManager(actionCollection, this, this);
30 connect(mSnippetsManager, &MailCommon::SnippetsManager::insertSnippetInfo, this, &SnippetTreeView::insertSnippetInfo);
31
32 setModel(mSnippetsManager->model());
33 setSelectionModel(mSnippetsManager->selectionModel());
34
35 connect(this, &QAbstractItemView::activated, mSnippetsManager->insertSnippetAction(), &QAction::trigger);
36 connect(mSnippetsManager->model(), &QAbstractItemModel::rowsInserted, this, &QTreeView::expandAll);
37 connect(mSnippetsManager->model(), &QAbstractItemModel::rowsRemoved, this, &QTreeView::expandAll);
38
39 expandAll();
40}
41
42SnippetTreeView::~SnippetTreeView() = default;
43
44void SnippetTreeView::contextMenuEvent(QContextMenuEvent *event)
45{
46 QMenu popup;
47
48 const bool itemSelected = mSnippetsManager->selectionModel()->hasSelection();
49
50 bool canAddSnippet = true;
51 if (itemSelected) {
52 popup.setTitle(mSnippetsManager->selectedName());
53 if (mSnippetsManager->snippetGroupSelected()) {
54 popup.addAction(mSnippetsManager->editSnippetGroupAction());
55 popup.addAction(mSnippetsManager->deleteSnippetGroupAction());
56 } else {
57 canAddSnippet = false; // subsnippets are not permitted
58 popup.addAction(mSnippetsManager->addSnippetAction());
59 popup.addAction(mSnippetsManager->editSnippetAction());
60 popup.addAction(mSnippetsManager->deleteSnippetAction());
61 popup.addAction(mSnippetsManager->insertSnippetAction());
62 }
63 popup.addSeparator();
64 } else {
65 popup.setTitle(i18n("Text Snippets"));
66 }
67 if (canAddSnippet) {
68 popup.addAction(mSnippetsManager->addSnippetAction());
69 }
70 popup.addAction(mSnippetsManager->addSnippetGroupAction());
71
72 popup.exec(event->globalPos());
73}
74
75MailCommon::SnippetsManager *SnippetTreeView::snippetsManager() const
76{
77 return mSnippetsManager;
78}
79
80#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 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 Fri Oct 11 2024 12:18:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.