From 9c49a7a0bdb930b137f9fe0aeff618fa6294d5a5 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Wed, 25 Sep 2019 08:00:18 +0200 Subject: [PATCH] Move widget here --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 2 + src/snippets/snippetwidget.cpp | 89 ++++++++++++++++++++++++++++++++++ src/snippets/snippetwidget.h | 47 ++++++++++++++++++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/snippets/snippetwidget.cpp create mode 100644 src/snippets/snippetwidget.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 530cb04..2a01816 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.5) -set(PIM_VERSION "5.12.43") +set(PIM_VERSION "5.12.44") project(mailcommon VERSION ${PIM_VERSION}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 19ce14f..8db5556 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -145,6 +145,7 @@ set(libmailcommon_snippets_SRCS snippets/snippetsmanager.cpp snippets/snippetsmodel.cpp snippets/snippetvariabledialog.cpp + snippets/snippetwidget.cpp ) set(libmailcommon_tag_SRCS @@ -408,6 +409,7 @@ ecm_generate_headers(MailCommon_Camelcasefilterimporter_HEADERS ecm_generate_headers(MailCommon_Camelcasesnippets_HEADERS HEADER_NAMES SnippetsManager + SnippetWidget REQUIRED_HEADERS MailCommon_snippets_HEADERS PREFIX MailCommon RELATIVE snippets diff --git a/src/snippets/snippetwidget.cpp b/src/snippets/snippetwidget.cpp new file mode 100644 index 0000000..d086b6e --- /dev/null +++ b/src/snippets/snippetwidget.cpp @@ -0,0 +1,89 @@ +/*************************************************************************** + * snippet feature from kdevelop/plugins/snippet/ * + * * + * Copyright (C) 2007 by Robert Gruber * + * rgruber@users.sourceforge.net * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "snippetwidget.h" +#include "snippetsmanager.h" + +#include +#include + +#include +#include +#include +using namespace MailCommon; +SnippetWidget::SnippetWidget(KActionCollection *actionCollection, QWidget *parent) + : QTreeView(parent) +{ + header()->hide(); + setAcceptDrops(true); + setDragEnabled(true); + setRootIsDecorated(true); + setAlternatingRowColors(true); + mSnippetsManager = new MailCommon::SnippetsManager(actionCollection, this, this); + connect(mSnippetsManager, &MailCommon::SnippetsManager::insertPlainText, this, &SnippetWidget::insertSnippetText); + + setModel(mSnippetsManager->model()); + setSelectionModel(mSnippetsManager->selectionModel()); + + connect(this, &QAbstractItemView::activated, + mSnippetsManager->insertSnippetAction(), &QAction::trigger); + connect(mSnippetsManager->model(), &QAbstractItemModel::rowsInserted, + this, &QTreeView::expandAll); + connect(mSnippetsManager->model(), &QAbstractItemModel::rowsRemoved, + this, &QTreeView::expandAll); + + expandAll(); +} + +SnippetWidget::~SnippetWidget() +{ +} + +void SnippetWidget::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu popup; + + const bool itemSelected = mSnippetsManager->selectionModel()->hasSelection(); + + bool canAddSnippet = true; + if (itemSelected) { + popup.setTitle(mSnippetsManager->selectedName()); + if (mSnippetsManager->snippetGroupSelected()) { + popup.addAction(mSnippetsManager->editSnippetGroupAction()); + popup.addAction(mSnippetsManager->deleteSnippetGroupAction()); + } else { + canAddSnippet = false; // subsnippets are not permitted + popup.addAction(mSnippetsManager->addSnippetAction()); + popup.addAction(mSnippetsManager->editSnippetAction()); + popup.addAction(mSnippetsManager->deleteSnippetAction()); + popup.addAction(mSnippetsManager->insertSnippetAction()); + } + popup.addSeparator(); + } else { + popup.setTitle(i18n("Text Snippets")); + } + if (canAddSnippet) { + popup.addAction(mSnippetsManager->addSnippetAction()); + } + popup.addAction(mSnippetsManager->addSnippetGroupAction()); + + popup.exec(event->globalPos()); +} + +void SnippetWidget::dropEvent(QDropEvent *event) +{ + if (event->source() == this) { + event->setDropAction(Qt::MoveAction); + } + QTreeView::dropEvent(event); +} diff --git a/src/snippets/snippetwidget.h b/src/snippets/snippetwidget.h new file mode 100644 index 0000000..d925258 --- /dev/null +++ b/src/snippets/snippetwidget.h @@ -0,0 +1,47 @@ +/*************************************************************************** + * snippet feature from kdevelop/plugins/snippet/ * + * * + * Copyright (C) 2007 by Robert Gruber * + * rgruber@users.sourceforge.net * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef MAILCOMMON_SNIPPETWIDGET_H +#define MAILCOMMON_SNIPPETWIDGET_H + +#include +#include "mailcommon_export.h" + +class KActionCollection; + +class QContextMenuEvent; + +namespace MailCommon { +class SnippetsManager; +/** + * @author Robert Gruber + */ +class MAILCOMMON_EXPORT SnippetWidget : public QTreeView +{ + Q_OBJECT +public: + explicit SnippetWidget(KActionCollection *actionCollection, QWidget *parent = nullptr); + ~SnippetWidget() override; + +protected: + void contextMenuEvent(QContextMenuEvent *) override; + void dropEvent(QDropEvent *) override; + +Q_SIGNALS: + void insertSnippetText(const QString &str); + +private: + MailCommon::SnippetsManager *mSnippetsManager = nullptr; +}; +} +#endif