1
/* This file is part of the KDE project
2
Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
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.
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.
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.
20
#include "richtextdialog.h"
25
#include <QVBoxLayout>
26
#include <QDialogButtonBox>
27
#include <QPushButton>
30
#include <KFontRequester>
31
#include <KColorCombo>
33
#include <KLocalizedString>
35
using namespace KFormDesigner;
37
//////////////////////////////////////////////////////////////////////////////////
38
//////////////// A simple dialog to edit rich text ////////////////////////////
39
//////////////////////////////////////////////////////////////////////////////////
41
class RichTextDialog::Private
47
QAction *fontComboAction, *colorComboAction, *boldTextAction,
48
*italicTextAction, *underlineTextAction,
49
*subscriptTextAction, *superscriptTextAction,
50
*alignLeftAction, *alignRightAction, *alignCenterAction, *alignJustifyAction;
51
QActionGroup* alignActionGroup;
54
KFontRequester *fontCombo;
55
KColorCombo *colorCombo;
58
RichTextDialog::Private::Private()
63
RichTextDialog::Private::~Private()
68
RichTextDialog::RichTextDialog(QWidget *parent, const QString &text)
69
: QDialog(parent), d(new Private())
71
setObjectName("richtext_dialog");
73
setWindowTitle(xi18nc("@title:window", "Edit Rich Text"));
75
QVBoxLayout *mainLayout = new QVBoxLayout;
76
setLayout(mainLayout);
78
d->toolbar = new KToolBar(this);
79
mainLayout->addWidget(d->toolbar);
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)));
86
d->toolbar->addSeparator();
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)));
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();
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();
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);
120
connect(d->toolbar, SIGNAL(actionTriggered(QAction*)),
121
this, SLOT(slotActionTriggered(QAction*)));
123
d->edit = new KTextEdit(text);
124
d->edit->setAcceptRichText(true);
125
mainLayout->addWidget(d->edit);
127
connect(d->edit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
128
this, SLOT(slotCurrentCharFormatChanged(QTextCharFormat)));
130
d->edit->moveCursor(QTextCursor::End);
131
slotCurrentCharFormatChanged(d->edit->currentCharFormat());
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);
145
RichTextDialog::~RichTextDialog()
151
RichTextDialog::text() const
153
return d->edit->toHtml();
157
RichTextDialog::changeFont(const QFont &font)
159
d->edit->setCurrentFont(font);
163
RichTextDialog::changeColor(const QColor &color)
165
d->edit->setTextColor(color);
169
RichTextDialog::slotActionTriggered(QAction* action)
171
const bool isChecked = action->isChecked();
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);
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);
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) {
197
d->edit->setAlignment(Qt::AlignLeft);
198
} else if (action == d->alignCenterAction) {
200
d->edit->setAlignment(Qt::AlignCenter);
201
} else if (action == d->alignRightAction) {
203
d->edit->setAlignment(Qt::AlignRight);
204
} else if (action == d->alignJustifyAction) {
206
d->edit->setAlignment(Qt::AlignJustify);
211
RichTextDialog::slotCurrentCharFormatChanged(const QTextCharFormat& f)
213
d->superscriptTextAction->setChecked(f.verticalAlignment() == QTextCharFormat::AlignSuperScript);
214
d->subscriptTextAction->setChecked(f.verticalAlignment() == QTextCharFormat::AlignSubScript);
216
switch (d->edit->alignment()) {
218
d->alignLeftAction->setChecked(true);
220
case Qt::AlignCenter:
221
d->alignCenterAction->setChecked(true);
224
d->alignRightAction->setChecked(true);
226
case Qt::AlignJustify:
227
d->alignJustifyAction->setChecked(true);
231
//! @todo add more formatting options (buttons)