Extract snippetwidget

wilder
Laurent Montel 7 years ago
parent 78bec0a870
commit bcab6b2cca
  1. 2
      CMakeLists.txt
  2. 6
      src/CMakeLists.txt
  3. 59
      src/snippets/snippetdialog.cpp
  4. 7
      src/snippets/snippetdialog.h
  5. 94
      src/snippets/snippettreeview.cpp
  6. 49
      src/snippets/snippettreeview.h
  7. 200
      src/snippets/snippetwidget.cpp
  8. 92
      src/snippets/snippetwidget.h
  9. 4
      src/snippets/ui/snippetwidget.ui

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.5)
set(PIM_VERSION "5.12.45")
set(PIM_VERSION "5.12.46")
project(mailcommon VERSION ${PIM_VERSION})

@ -145,6 +145,7 @@ set(libmailcommon_snippets_SRCS
snippets/snippetsmanager.cpp
snippets/snippetsmodel.cpp
snippets/snippetvariabledialog.cpp
snippets/snippettreeview.cpp
snippets/snippetwidget.cpp
)
@ -199,7 +200,7 @@ qt5_add_dbus_interfaces(libmailcommon_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/dbusinterfaces/org.freedesktop.Akonadi.MailFilterAgent.xml
)
ki18n_wrap_ui(libmailcommon_SRCS filter/ui/filterconfigwidget.ui snippets/ui/snippetdialog.ui filter/ui/selectthunderbirdfilterfileswidget.ui)
ki18n_wrap_ui(libmailcommon_SRCS filter/ui/filterconfigwidget.ui snippets/ui/snippetwidget.ui filter/ui/selectthunderbirdfilterfileswidget.ui)
set(libmailcommon_SRCS ${libmailcommon_SRCS} filter/soundtestwidget.cpp)
@ -409,8 +410,9 @@ ecm_generate_headers(MailCommon_Camelcasefilterimporter_HEADERS
ecm_generate_headers(MailCommon_Camelcasesnippets_HEADERS
HEADER_NAMES
SnippetsManager
SnippetWidget
SnippetTreeView
SnippetsModel
SnippetWidget
REQUIRED_HEADERS MailCommon_snippets_HEADERS
PREFIX MailCommon
RELATIVE snippets

@ -12,8 +12,8 @@
***************************************************************************/
#include "snippetdialog.h"
#include "snippetwidget.h"
#include "ui_snippetdialog.h"
#include <MessageComposer/ConvertSnippetVariableMenu>
#include <KPIMTextEdit/PlainTextEditor>
#include <kactioncollection.h>
@ -32,10 +32,11 @@ SnippetDialog::SnippetDialog(KActionCollection *actionCollection, bool inGroupMo
: QDialog(parent)
, mActionCollection(actionCollection)
{
mUi = new Ui::SnippetDialog;
QWidget *mainWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(mainWidget);
mainLayout->setObjectName(QStringLiteral("mainLayout"));
mSnippetWidget = new SnippetWidget(this);
mainLayout->addWidget(mSnippetWidget);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
mOkButton = buttonBox->button(QDialogButtonBox::Ok);
@ -44,30 +45,21 @@ SnippetDialog::SnippetDialog(KActionCollection *actionCollection, bool inGroupMo
connect(buttonBox, &QDialogButtonBox::accepted, this, &SnippetDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &SnippetDialog::reject);
mainLayout->addWidget(buttonBox);
mUi->setupUi(mainWidget);
mUi->keyWidget->setCheckActionCollections(QList<KActionCollection *>() << actionCollection);
mOkButton->setEnabled(false);
connect(mUi->nameEdit, &KLineEdit::textChanged, this, &SnippetDialog::slotTextChanged);
connect(mUi->groupBox, QOverload<int>::of(&KComboBox::currentIndexChanged), this, &SnippetDialog::slotGroupChanged);
mSnippetWidget->setCheckActionCollections(QList<KActionCollection *>() << actionCollection);
mOkButton->setEnabled(false);
mUi->snippetText->setMinimumSize(500, 300);
connect(mSnippetWidget, &MailCommon::SnippetWidget::textChanged, this, &SnippetDialog::slotTextChanged);
connect(mSnippetWidget, &MailCommon::SnippetWidget::groupChanged, this, &SnippetDialog::slotGroupChanged);
mUi->groupWidget->setVisible(!inGroupMode);
mUi->nameEdit->setFocus();
MessageComposer::ConvertSnippetVariableMenu *variableMenu = new MessageComposer::ConvertSnippetVariableMenu(this, this);
mUi->pushButtonVariables->setMenu(variableMenu->menu());
connect(variableMenu, &MessageComposer::ConvertSnippetVariableMenu::insertVariable, this, [this](MessageComposer::ConvertSnippetVariablesUtil::VariableType type) {
mUi->snippetText->editor()->insertPlainText(MessageComposer::ConvertSnippetVariablesUtil::snippetVariableFromEnum(type));
});
mSnippetWidget->setGroupSelected(inGroupMode);
readConfig();
}
SnippetDialog::~SnippetDialog()
{
writeConfig();
delete mUi;
}
void SnippetDialog::writeConfig()
@ -92,57 +84,57 @@ void SnippetDialog::slotGroupChanged()
void SnippetDialog::setName(const QString &name)
{
mUi->nameEdit->setText(name);
mSnippetWidget->setName(name);
}
QString SnippetDialog::name() const
{
return mUi->nameEdit->text();
return mSnippetWidget->name();
}
void SnippetDialog::setText(const QString &text)
{
mUi->snippetText->setPlainText(text);
mSnippetWidget->setText(text);
}
QString SnippetDialog::text() const
{
return mUi->snippetText->toPlainText();
return mSnippetWidget->text();
}
void SnippetDialog::setKeySequence(const QKeySequence &sequence)
{
mUi->keyWidget->setKeySequence(sequence);
mSnippetWidget->setKeySequence(sequence);
}
QKeySequence SnippetDialog::keySequence() const
{
return mUi->keyWidget->keySequence();
return mSnippetWidget->keySequence();
}
void SnippetDialog::setKeyword(const QString &keyword)
{
mUi->keyword->setText(keyword);
mSnippetWidget->setKeyword(keyword);
}
QString SnippetDialog::keyword() const
{
return mUi->keyword->text();
return mSnippetWidget->keyword();
}
void SnippetDialog::setGroupModel(QAbstractItemModel *model)
{
mUi->groupBox->setModel(model);
mSnippetWidget->setGroupModel(model);
}
void SnippetDialog::setGroupIndex(const QModelIndex &index)
{
mUi->groupBox->setCurrentIndex(index.row());
mSnippetWidget->setGroupIndex(index);
}
QModelIndex SnippetDialog::groupIndex() const
{
return mUi->groupBox->model()->index(mUi->groupBox->currentIndex(), 0);
return mSnippetWidget->groupIndex();
}
void SnippetDialog::slotTextChanged()
@ -152,12 +144,5 @@ void SnippetDialog::slotTextChanged()
bool SnippetDialog::snippetIsValid() const
{
if (mUi->nameEdit->text().trimmed().isEmpty()) {
return false;
} else {
if (mUi->groupWidget->isVisible()) {
return !mUi->groupBox->currentText().trimmed().isEmpty();
}
}
return true;
return mSnippetWidget->snippetIsValid();
}

@ -16,10 +16,6 @@
#include <QDialog>
namespace Ui {
class SnippetDialog;
}
class KActionCollection;
class QAbstractItemModel;
@ -27,6 +23,7 @@ class QModelIndex;
class QPushButton;
namespace MailCommon {
class SnippetWidget;
class SnippetDialog : public QDialog
{
Q_OBJECT
@ -62,7 +59,7 @@ private:
void readConfig();
KActionCollection *mActionCollection = nullptr;
Ui::SnippetDialog *mUi = nullptr;
SnippetWidget *mSnippetWidget = nullptr;
QPushButton *mOkButton = nullptr;
};
}

@ -0,0 +1,94 @@
/***************************************************************************
* 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 "snippettreeview.h"
#include "snippetsmanager.h"
#include <kactioncollection.h>
#include <KLocalizedString>
#include <QMenu>
#include <QContextMenuEvent>
#include <QHeaderView>
using namespace MailCommon;
SnippetTreeView::SnippetTreeView(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, &SnippetTreeView::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();
}
SnippetTreeView::~SnippetTreeView()
{
}
void SnippetTreeView::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 SnippetTreeView::dropEvent(QDropEvent *event)
{
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
}
QTreeView::dropEvent(event);
}
MailCommon::SnippetsManager *SnippetTreeView::snippetsManager() const
{
return mSnippetsManager;
}

@ -0,0 +1,49 @@
/***************************************************************************
* 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_SNIPPETVIEW_H
#define MAILCOMMON_SNIPPETVIEW_H
#include <QTreeView>
#include "mailcommon_export.h"
class KActionCollection;
class QContextMenuEvent;
namespace MailCommon {
class SnippetsManager;
/**
* @author Robert Gruber
*/
class MAILCOMMON_EXPORT SnippetTreeView : public QTreeView
{
Q_OBJECT
public:
explicit SnippetTreeView(KActionCollection *actionCollection, QWidget *parent = nullptr);
~SnippetTreeView() override;
MailCommon::SnippetsManager *snippetsManager() const;
protected:
void contextMenuEvent(QContextMenuEvent *) override;
void dropEvent(QDropEvent *) override;
Q_SIGNALS:
void insertSnippetText(const QString &str);
private:
MailCommon::SnippetsManager *mSnippetsManager = nullptr;
};
}
#endif

@ -1,94 +1,160 @@
/***************************************************************************
* 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. *
* *
***************************************************************************/
/*
Copyright (c) 2019 Montel Laurent <montel@kde.org>
#include "snippetwidget.h"
#include "snippetsmanager.h"
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published
by the Free Software Foundation; either version 2 of the License or
( at your option ) version 3 or, at the discretion of KDE e.V.
( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <kactioncollection.h>
#include <KLocalizedString>
#include "snippetwidget.h"
#include "ui_snippetwidget.h"
#include <MessageComposer/ConvertSnippetVariableMenu>
#include <KPIMTextEdit/PlainTextEditor>
#include <QMenu>
#include <QContextMenuEvent>
#include <QHeaderView>
#include <KActionCollection>
#include <KComboBox>
#include <KLineEdit>
#include <QAbstractListModel>
#include <QVBoxLayout>
using namespace MailCommon;
SnippetWidget::SnippetWidget(KActionCollection *actionCollection, QWidget *parent)
: QTreeView(parent)
class SnippetWidgetPrivate
{
public:
Ui::SnippetWidget mUi;
QWidget *wdg = nullptr;
bool isSelectedGroup = false;
};
SnippetWidget::SnippetWidget(QWidget *parent)
: QWidget(parent),
d(new SnippetWidgetPrivate)
{
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);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setObjectName(QStringLiteral("mainlayout"));
layout->setContentsMargins(0, 0, 0, 0);
d->wdg = new QWidget(this);
d->mUi.setupUi(d->wdg);
layout->addWidget(d->wdg);
setModel(mSnippetsManager->model());
setSelectionModel(mSnippetsManager->selectionModel());
MessageComposer::ConvertSnippetVariableMenu *variableMenu = new MessageComposer::ConvertSnippetVariableMenu(this, this);
d->mUi.pushButtonVariables->setMenu(variableMenu->menu());
connect(variableMenu, &MessageComposer::ConvertSnippetVariableMenu::insertVariable, this, [this](MessageComposer::ConvertSnippetVariablesUtil::VariableType type) {
d->mUi.snippetText->editor()->insertPlainText(MessageComposer::ConvertSnippetVariablesUtil::snippetVariableFromEnum(type));
});
connect(this, &QAbstractItemView::activated,
mSnippetsManager->insertSnippetAction(), &QAction::trigger);
connect(mSnippetsManager->model(), &QAbstractItemModel::rowsInserted,
this, &QTreeView::expandAll);
connect(mSnippetsManager->model(), &QAbstractItemModel::rowsRemoved,
this, &QTreeView::expandAll);
connect(d->mUi.nameEdit, &KLineEdit::textChanged, this, &SnippetWidget::textChanged);
connect(d->mUi.groupBox, QOverload<int>::of(&KComboBox::currentIndexChanged), this, &SnippetWidget::groupChanged);
expandAll();
d->mUi.nameEdit->setFocus();
d->mUi.snippetText->setMinimumSize(500, 300);
}
SnippetWidget::~SnippetWidget()
{
delete d;
}
void SnippetWidget::contextMenuEvent(QContextMenuEvent *event)
void SnippetWidget::setName(const QString &name)
{
QMenu popup;
d->mUi.nameEdit->setText(name);
}
const bool itemSelected = mSnippetsManager->selectionModel()->hasSelection();
QString SnippetWidget::name() const
{
return d->mUi.nameEdit->text();
}
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();
void SnippetWidget::setText(const QString &text)
{
d->mUi.snippetText->setPlainText(text);
}
QString SnippetWidget::text() const
{
return d->mUi.snippetText->toPlainText();
}
void SnippetWidget::setKeySequence(const QKeySequence &sequence)
{
d->mUi.keyWidget->setKeySequence(sequence);
}
QKeySequence SnippetWidget::keySequence() const
{
return d->mUi.keyWidget->keySequence();
}
void SnippetWidget::setKeyword(const QString &keyword)
{
d->mUi.keyword->setText(keyword);
}
QString SnippetWidget::keyword() const
{
return d->mUi.keyword->text();
}
void SnippetWidget::setGroupModel(QAbstractItemModel *model)
{
d->mUi.groupBox->setModel(model);
}
void SnippetWidget::setGroupIndex(const QModelIndex &index)
{
d->mUi.groupBox->setCurrentIndex(index.row());
}
QModelIndex SnippetWidget::groupIndex() const
{
return d->mUi.groupBox->model()->index(d->mUi.groupBox->currentIndex(), 0);
}
bool SnippetWidget::snippetIsValid() const
{
if (d->mUi.nameEdit->text().trimmed().isEmpty()) {
return false;
} else {
popup.setTitle(i18n("Text Snippets"));
}
if (canAddSnippet) {
popup.addAction(mSnippetsManager->addSnippetAction());
if (d->mUi.groupWidget->isVisible()) {
return !d->mUi.groupBox->currentText().trimmed().isEmpty();
}
}
popup.addAction(mSnippetsManager->addSnippetGroupAction());
return true;
}
popup.exec(event->globalPos());
void SnippetWidget::setCheckActionCollections(const QList<KActionCollection *> &lst)
{
d->mUi.keyWidget->setCheckActionCollections(lst);
}
void SnippetWidget::dropEvent(QDropEvent *event)
void SnippetWidget::setGroupSelected(bool inGroupMode)
{
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
}
QTreeView::dropEvent(event);
d->isSelectedGroup = inGroupMode;
d->mUi.groupWidget->setVisible(!inGroupMode);
}
bool SnippetWidget::isGroupSelected() const
{
return d->isSelectedGroup;
}
MailCommon::SnippetsManager *SnippetWidget::snippetsManager() const
void SnippetWidget::clear()
{
return mSnippetsManager;
d->mUi.nameEdit->clear();
d->mUi.keyword->clear();
d->mUi.snippetText->clear();
d->mUi.keyWidget->setKeySequence({});
}

@ -1,49 +1,63 @@
/***************************************************************************
* 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 <QTreeView>
/*
Copyright (c) 2019 Montel Laurent <montel@kde.org>
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published
by the Free Software Foundation; either version 2 of the License or
( at your option ) version 3 or, at the discretion of KDE e.V.
( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef SNIPPETWIDGET_H
#define SNIPPETWIDGET_H
#include <QWidget>
#include <QAbstractItemModel>
#include "mailcommon_export.h"
class KActionCollection;
class QContextMenuEvent;
class SnippetWidgetPrivate;
namespace MailCommon {
class SnippetsManager;
/**
* @author Robert Gruber
*/
class MAILCOMMON_EXPORT SnippetWidget : public QTreeView
class MAILCOMMON_EXPORT SnippetWidget : public QWidget
{
Q_OBJECT
public:
explicit SnippetWidget(KActionCollection *actionCollection, QWidget *parent = nullptr);
~SnippetWidget() override;
MailCommon::SnippetsManager *snippetsManager() const;
protected:
void contextMenuEvent(QContextMenuEvent *) override;
void dropEvent(QDropEvent *) override;
explicit SnippetWidget(QWidget *parent = nullptr);
~SnippetWidget();
void setName(const QString &name);
Q_REQUIRED_RESULT QString name() const;
void setText(const QString &text);
Q_REQUIRED_RESULT QString text() const;
void setKeySequence(const QKeySequence &sequence);
Q_REQUIRED_RESULT QKeySequence keySequence() const;
void setKeyword(const QString &keyword);
Q_REQUIRED_RESULT QString keyword() const;
void setGroupModel(QAbstractItemModel *model);
void setGroupIndex(const QModelIndex &index);
Q_REQUIRED_RESULT QModelIndex groupIndex() const;
Q_REQUIRED_RESULT bool snippetIsValid() const;
void setCheckActionCollections(const QList<KActionCollection *> &lst);
void setGroupSelected(bool b);
Q_REQUIRED_RESULT bool isGroupSelected() const;
void clear();
Q_SIGNALS:
void insertSnippetText(const QString &str);
void textChanged(const QString &str);
void groupChanged(int index);
private:
MailCommon::SnippetsManager *mSnippetsManager = nullptr;
SnippetWidgetPrivate *const d;
};
}
#endif
#endif // SNIPPETWIDGET_H

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SnippetDialog</class>
<widget class="QWidget" name="SnippetDialog">
<class>SnippetWidget</class>
<widget class="QWidget" name="SnippetWidget">
<property name="geometry">
<rect>
<x>0</x>
Loading…
Cancel
Save