parent
78bec0a870
commit
bcab6b2cca
9 changed files with 360 additions and 153 deletions
@ -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…
Reference in new issue