Mailcommon

snippettreeview.cpp
1 /*
2  snippet feature from kdevelop/plugins/snippet/
3 
4  SPDX-FileCopyrightText: 2007 Robert Gruber <[email protected]>
5  SPDX-FileCopyrightText: 2019-2022 Laurent Montel <[email protected]>
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>
19 using namespace MailCommon;
20 SnippetTreeView::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 
41 SnippetTreeView::~SnippetTreeView() = default;
42 
43 void 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 
74 void SnippetTreeView::dropEvent(QDropEvent *event)
75 {
76  if (event->source() == this) {
77  event->setDropAction(Qt::MoveAction);
78  }
80 }
81 
82 MailCommon::SnippetsManager *SnippetTreeView::snippetsManager() const
83 {
84  return mSnippetsManager;
85 }
The SnippetsManager class.
QAction * deleteSnippetAction() const
Returns the action that handles deleting the currently selected snippet.
QAction * editSnippetAction() const
Returns the action that handles editing the currently selected snippet.
virtual void dropEvent(QDropEvent *event) override
QAction * addSeparator()
void setTitle(const QString &title)
bool hasSelection() const const
virtual bool event(QEvent *event) override
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QItemSelectionModel * selectionModel() const
Returns the selection model that is used by the manager to select the snippet or snippet group to wor...
void hide()
QAction * addSnippetGroupAction() const
Returns the action that handles adding new snippet groups.
QAction * addAction(const QString &text)
QAction * deleteSnippetGroupAction() const
Returns the action that handles deleting the currently selected snippet group.
void expandAll()
QString selectedName() const
Returns the name of the currently selected snippet or snippet group.
QAction * insertSnippetAction() const
Returns the action that handles inserting a snippet into the editor.
QString i18n(const char *text, const TYPE &arg...)
QAction * addSnippetAction() const
Returns the action that handles adding new snippets.
void activated(const QModelIndex &index)
void trigger()
MoveAction
bool snippetGroupSelected() const
Returns whether the currently selected item is a snippet group.
void rowsInserted(const QModelIndex &parent, int first, int last)
void rowsRemoved(const QModelIndex &parent, int first, int last)
QAction * exec()
QAction * editSnippetGroupAction() const
Returns the action that handles editing the currently selected snippet group.
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.