~ubuntu-branches/debian/sid/kexi/sid

« back to all changes in this revision

Viewing changes to src/formeditor/richtextdialog.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2017-06-24 20:10:10 UTC
  • Revision ID: package-import@ubuntu.com-20170624201010-5lrzd5r2vwthwifp
Tags: upstream-3.0.1.1
ImportĀ upstreamĀ versionĀ 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "richtextdialog.h"
 
21
 
 
22
#include <KexiIcon.h>
 
23
 
 
24
#include <QAction>
 
25
#include <QVBoxLayout>
 
26
#include <QDialogButtonBox>
 
27
#include <QPushButton>
 
28
 
 
29
#include <KToolBar>
 
30
#include <KFontRequester>
 
31
#include <KColorCombo>
 
32
#include <KTextEdit>
 
33
#include <KLocalizedString>
 
34
 
 
35
using namespace KFormDesigner;
 
36
 
 
37
//////////////////////////////////////////////////////////////////////////////////
 
38
//////////////// A simple dialog to edit rich text   ////////////////////////////
 
39
//////////////////////////////////////////////////////////////////////////////////
 
40
 
 
41
class RichTextDialog::Private
 
42
{
 
43
public:
 
44
    Private();
 
45
    ~Private();
 
46
 
 
47
    QAction *fontComboAction, *colorComboAction, *boldTextAction,
 
48
    *italicTextAction, *underlineTextAction,
 
49
    *subscriptTextAction, *superscriptTextAction,
 
50
    *alignLeftAction, *alignRightAction, *alignCenterAction, *alignJustifyAction;
 
51
    QActionGroup* alignActionGroup;
 
52
    KToolBar  *toolbar;
 
53
    KTextEdit  *edit;
 
54
    KFontRequester  *fontCombo;
 
55
    KColorCombo  *colorCombo;
 
56
};
 
57
 
 
58
RichTextDialog::Private::Private()
 
59
{
 
60
 
 
61
}
 
62
 
 
63
RichTextDialog::Private::~Private()
 
64
{
 
65
 
 
66
}
 
67
 
 
68
RichTextDialog::RichTextDialog(QWidget *parent, const QString &text)
 
69
    : QDialog(parent), d(new Private())
 
70
{
 
71
    setObjectName("richtext_dialog");
 
72
    setModal(true);
 
73
    setWindowTitle(xi18nc("@title:window", "Edit Rich Text"));
 
74
 
 
75
    QVBoxLayout *mainLayout = new QVBoxLayout;
 
76
    setLayout(mainLayout);
 
77
 
 
78
    d->toolbar = new KToolBar(this);
 
79
    mainLayout->addWidget(d->toolbar);
 
80
 
 
81
    d->fontCombo = new KFontRequester(d->toolbar);
 
82
    d->fontComboAction = d->toolbar->addWidget(d->fontCombo);
 
83
    connect(d->fontCombo, SIGNAL(fontSelected(QFont)),
 
84
            this, SLOT(changeFont(QFont)));
 
85
 
 
86
    d->toolbar->addSeparator();
 
87
 
 
88
    d->colorCombo = new KColorCombo(d->toolbar);
 
89
    d->colorComboAction = d->toolbar->addWidget(d->colorCombo);
 
90
    connect(d->colorCombo, SIGNAL(activated(QColor)), this, SLOT(changeColor(QColor)));
 
91
 
 
92
    d->boldTextAction = d->toolbar->addAction(koIcon("format-text-bold"), xi18n("Bold"));
 
93
    d->boldTextAction->setCheckable(true);
 
94
    d->italicTextAction = d->toolbar->addAction(koIcon("format-text-italic"), xi18n("Italic"));
 
95
    d->italicTextAction->setCheckable(true);
 
96
    d->underlineTextAction = d->toolbar->addAction(koIcon("format-text-underline"), xi18n("Underline"));
 
97
    d->underlineTextAction->setCheckable(true);
 
98
    d->toolbar->addSeparator();
 
99
 
 
100
    d->superscriptTextAction = d->toolbar->addAction(koIcon("format-text-superscript"), xi18n("Superscript"));
 
101
    d->superscriptTextAction->setCheckable(true);
 
102
    d->subscriptTextAction = d->toolbar->addAction(koIcon("format-text-subscript"), xi18n("Subscript"));
 
103
    d->subscriptTextAction->setCheckable(true);
 
104
    d->toolbar->addSeparator();
 
105
 
 
106
    d->alignActionGroup = new QActionGroup(this);
 
107
    d->alignLeftAction = d->toolbar->addAction(koIcon("format-justify-left"), xi18n("Left Align"));
 
108
    d->alignLeftAction->setCheckable(true);
 
109
    d->alignActionGroup->addAction(d->alignLeftAction);
 
110
    d->alignCenterAction = d->toolbar->addAction(koIcon("format-justify-center"), xi18n("Centered"));
 
111
    d->alignCenterAction->setCheckable(true);
 
112
    d->alignActionGroup->addAction(d->alignCenterAction);
 
113
    d->alignRightAction = d->toolbar->addAction(koIcon("format-justify-right"), xi18n("Right Align"));
 
114
    d->alignRightAction->setCheckable(true);
 
115
    d->alignActionGroup->addAction(d->alignRightAction);
 
116
    d->alignJustifyAction = d->toolbar->addAction(koIcon("format-justify-fill"), xi18n("Justified"));
 
117
    d->alignJustifyAction->setCheckable(true);
 
118
    d->alignActionGroup->addAction(d->alignJustifyAction);
 
119
 
 
120
    connect(d->toolbar, SIGNAL(actionTriggered(QAction*)),
 
121
            this, SLOT(slotActionTriggered(QAction*)));
 
122
 
 
123
    d->edit = new KTextEdit(text);
 
124
    d->edit->setAcceptRichText(true);
 
125
    mainLayout->addWidget(d->edit);
 
126
 
 
127
    connect(d->edit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
 
128
            this, SLOT(slotCurrentCharFormatChanged(QTextCharFormat)));
 
129
 
 
130
    d->edit->moveCursor(QTextCursor::End);
 
131
    slotCurrentCharFormatChanged(d->edit->currentCharFormat());
 
132
    d->edit->setFocus();
 
133
 
 
134
    // buttons
 
135
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
 
136
    buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
 
137
    QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
 
138
    okButton->setDefault(true);
 
139
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
 
140
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
 
141
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
 
142
    mainLayout->addWidget(buttonBox);
 
143
}
 
144
 
 
145
RichTextDialog::~RichTextDialog()
 
146
{
 
147
    delete d;
 
148
}
 
149
 
 
150
QString
 
151
RichTextDialog::text() const
 
152
{
 
153
    return d->edit->toHtml();
 
154
}
 
155
 
 
156
void
 
157
RichTextDialog::changeFont(const QFont &font)
 
158
{
 
159
    d->edit->setCurrentFont(font);
 
160
}
 
161
 
 
162
void
 
163
RichTextDialog::changeColor(const QColor &color)
 
164
{
 
165
    d->edit->setTextColor(color);
 
166
}
 
167
 
 
168
void
 
169
RichTextDialog::slotActionTriggered(QAction* action)
 
170
{
 
171
    const bool isChecked = action->isChecked();
 
172
 
 
173
    if (action == d->boldTextAction)
 
174
        d->edit->setFontWeight(isChecked ? QFont::Bold : QFont::Normal);
 
175
    else if (action == d->italicTextAction)
 
176
        d->edit->setFontItalic(isChecked);
 
177
    else if (action == d->underlineTextAction)
 
178
        d->edit->setFontUnderline(isChecked);
 
179
    else if (action == d->superscriptTextAction) {
 
180
        if (isChecked && d->subscriptTextAction->isChecked()) {
 
181
            d->subscriptTextAction->setChecked(false);
 
182
        }
 
183
        QTextCharFormat currentCharFormat = d->edit->currentCharFormat();
 
184
        currentCharFormat.setVerticalAlignment(
 
185
            isChecked ? QTextCharFormat::AlignSuperScript : QTextCharFormat::AlignNormal);
 
186
        d->edit->setCurrentCharFormat(currentCharFormat);
 
187
    } else if (action == d->subscriptTextAction) {
 
188
        if (isChecked && d->superscriptTextAction->isChecked()) {
 
189
            d->superscriptTextAction->setChecked(false);
 
190
        }
 
191
        QTextCharFormat currentCharFormat = d->edit->currentCharFormat();
 
192
        currentCharFormat.setVerticalAlignment(
 
193
            isChecked ? QTextCharFormat::AlignSubScript : QTextCharFormat::AlignNormal);
 
194
        d->edit->setCurrentCharFormat(currentCharFormat);
 
195
    } else if (action == d->alignLeftAction) {
 
196
        if (isChecked)
 
197
            d->edit->setAlignment(Qt::AlignLeft);
 
198
    } else if (action == d->alignCenterAction) {
 
199
        if (isChecked)
 
200
            d->edit->setAlignment(Qt::AlignCenter);
 
201
    } else if (action == d->alignRightAction) {
 
202
        if (isChecked)
 
203
            d->edit->setAlignment(Qt::AlignRight);
 
204
    } else if (action == d->alignJustifyAction) {
 
205
        if (isChecked)
 
206
            d->edit->setAlignment(Qt::AlignJustify);
 
207
    }
 
208
}
 
209
 
 
210
void
 
211
RichTextDialog::slotCurrentCharFormatChanged(const QTextCharFormat& f)
 
212
{
 
213
    d->superscriptTextAction->setChecked(f.verticalAlignment() == QTextCharFormat::AlignSuperScript);
 
214
    d->subscriptTextAction->setChecked(f.verticalAlignment() == QTextCharFormat::AlignSubScript);
 
215
 
 
216
    switch (d->edit->alignment()) {
 
217
    case Qt::AlignLeft:
 
218
        d->alignLeftAction->setChecked(true);
 
219
        break;
 
220
    case Qt::AlignCenter:
 
221
        d->alignCenterAction->setChecked(true);
 
222
        break;
 
223
    case Qt::AlignRight:
 
224
        d->alignRightAction->setChecked(true);
 
225
        break;
 
226
    case Qt::AlignJustify:
 
227
        d->alignJustifyAction->setChecked(true);
 
228
        break;
 
229
    }
 
230
 
 
231
//! @todo add more formatting options (buttons)
 
232
}
 
233