snippetdialog.cpp 5.2 KB
Newer Older
1
2
3
4
5
/***************************************************************************
 *   snippet feature from kdevelop/plugins/snippet/                        *
 *                                                                         *
 *   Copyright (C) 2007 by Robert Gruber                                   *
 *   rgruber@users.sourceforge.net                                         *
Laurent Montel's avatar
Laurent Montel committed
6
 *   Copyright (c) 2019 Montel Laurent <montel@kde.org>                    *
7
8
9
10
11
12
13
14
 *   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 "snippetdialog.h"
Laurent Montel's avatar
Laurent Montel committed
15
#include "snippetwidget.h"
16

Laurent Montel's avatar
Laurent Montel committed
17
18
#include <MessageComposer/ConvertSnippetVariableMenu>
#include <KPIMTextEdit/PlainTextEditor>
19
#include <KActionCollection>
20
21
22
23
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include <QDialogButtonBox>
Laurent Montel's avatar
Laurent Montel committed
24
25
#include <KConfigGroup>
#include <KSharedConfig>
26
27

using namespace MailCommon;
Laurent Montel's avatar
Laurent Montel committed
28
29
30
namespace {
static const char myConfigGroupName[] = "SnippetDialog";
}
31
SnippetDialog::SnippetDialog(KActionCollection *actionCollection, bool inGroupMode, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
32
33
    : QDialog(parent)
    , mActionCollection(actionCollection)
34
    , mInGroupMode(inGroupMode)
35
{
Laurent Montel's avatar
Laurent Montel committed
36
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
37
38
39
40
    mainLayout->setObjectName(QStringLiteral("mainLayout"));
    mSnippetWidget = new SnippetWidget(this);

    mainLayout->addWidget(mSnippetWidget);
Laurent Montel's avatar
Laurent Montel committed
41
42

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
43
44
45
46
47
48
49
    mOkButton = buttonBox->button(QDialogButtonBox::Ok);
    mOkButton->setDefault(true);
    mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &SnippetDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &SnippetDialog::reject);
    mainLayout->addWidget(buttonBox);

Laurent Montel's avatar
Laurent Montel committed
50
51
    mSnippetWidget->setCheckActionCollections(QList<KActionCollection *>() << actionCollection);
    mOkButton->setEnabled(false);
52

Laurent Montel's avatar
Laurent Montel committed
53
54
    connect(mSnippetWidget, &MailCommon::SnippetWidget::textChanged, this, &SnippetDialog::slotTextChanged);
    connect(mSnippetWidget, &MailCommon::SnippetWidget::groupChanged, this, &SnippetDialog::slotGroupChanged);
55

56
57
58
59
    mSnippetWidget->setGroupSelected(mInGroupMode);
    if (!mInGroupMode) {
        readConfig();
    }
60
61
62
63
}

SnippetDialog::~SnippetDialog()
{
64
65
66
    if (!mInGroupMode) {
        writeConfig();
    }
67
68
}

Laurent Montel's avatar
Laurent Montel committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
void SnippetDialog::writeConfig()
{
    KConfigGroup group(KSharedConfig::openConfig(), myConfigGroupName);
    group.writeEntry("Size", size());
}

void SnippetDialog::readConfig()
{
    KConfigGroup group(KSharedConfig::openConfig(), myConfigGroupName);
    const QSize sizeDialog = group.readEntry("Size", QSize(300, 350));
    if (sizeDialog.isValid()) {
        resize(sizeDialog);
    }
}

84
85
86
87
88
89
90
void SnippetDialog::slotGroupChanged()
{
    mOkButton->setEnabled(snippetIsValid());
}

void SnippetDialog::setName(const QString &name)
{
Laurent Montel's avatar
Laurent Montel committed
91
    mSnippetWidget->setName(name);
92
93
94
95
}

QString SnippetDialog::name() const
{
Laurent Montel's avatar
Laurent Montel committed
96
    return mSnippetWidget->name();
97
98
99
100
}

void SnippetDialog::setText(const QString &text)
{
Laurent Montel's avatar
Laurent Montel committed
101
    mSnippetWidget->setText(text);
102
103
104
105
}

QString SnippetDialog::text() const
{
Laurent Montel's avatar
Laurent Montel committed
106
    return mSnippetWidget->text();
107
108
}

109
110
111
112
113
114
115
116
117
118
void SnippetDialog::setSubject(const QString &text)
{
    mSnippetWidget->setSubject(text);
}

QString SnippetDialog::subject() const
{
    return mSnippetWidget->subject();
}

119
120
void SnippetDialog::setKeySequence(const QKeySequence &sequence)
{
Laurent Montel's avatar
Laurent Montel committed
121
    mSnippetWidget->setKeySequence(sequence);
122
123
124
125
}

QKeySequence SnippetDialog::keySequence() const
{
Laurent Montel's avatar
Laurent Montel committed
126
    return mSnippetWidget->keySequence();
127
128
}

Laurent Montel's avatar
Laurent Montel committed
129
130
void SnippetDialog::setKeyword(const QString &keyword)
{
Laurent Montel's avatar
Laurent Montel committed
131
    mSnippetWidget->setKeyword(keyword);
Laurent Montel's avatar
Laurent Montel committed
132
133
134
135
}

QString SnippetDialog::keyword() const
{
Laurent Montel's avatar
Laurent Montel committed
136
    return mSnippetWidget->keyword();
Laurent Montel's avatar
Laurent Montel committed
137
138
}

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
void SnippetDialog::setTo(const QString &keyword)
{
    mSnippetWidget->setTo(keyword);
}

QString SnippetDialog::to() const
{
    return mSnippetWidget->to();
}

void SnippetDialog::setCc(const QString &keyword)
{
    mSnippetWidget->setCc(keyword);
}

QString SnippetDialog::cc() const
{
    return mSnippetWidget->cc();
}

void SnippetDialog::setBcc(const QString &keyword)
{
    mSnippetWidget->setBcc(keyword);
}

QString SnippetDialog::bcc() const
{
    return mSnippetWidget->bcc();
}

Laurent Montel's avatar
Laurent Montel committed
169
170
171
172
173
174
175
176
177
178
void SnippetDialog::setAttachment(const QString &keyword)
{
    mSnippetWidget->setAttachment(keyword);
}

QString SnippetDialog::attachment() const
{
    return mSnippetWidget->attachment();
}

179
180
void SnippetDialog::setGroupModel(QAbstractItemModel *model)
{
Laurent Montel's avatar
Laurent Montel committed
181
    mSnippetWidget->setGroupModel(model);
182
183
184
185
}

void SnippetDialog::setGroupIndex(const QModelIndex &index)
{
Laurent Montel's avatar
Laurent Montel committed
186
    mSnippetWidget->setGroupIndex(index);
187
188
189
190
}

QModelIndex SnippetDialog::groupIndex() const
{
Laurent Montel's avatar
Laurent Montel committed
191
    return mSnippetWidget->groupIndex();
192
193
194
195
196
197
198
199
200
}

void SnippetDialog::slotTextChanged()
{
    mOkButton->setEnabled(snippetIsValid());
}

bool SnippetDialog::snippetIsValid() const
{
Laurent Montel's avatar
Laurent Montel committed
201
    return mSnippetWidget->snippetIsValid();
202
}