1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the designer application of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include <QtCore/QPointer>
30
#include <QtCore/QMap>
32
#include <QtGui/QToolBar>
33
#include <QtGui/QComboBox>
34
#include <QtGui/QAction>
35
#include <QtGui/QTextCursor>
36
#include <QtGui/QPainter>
37
#include <QtGui/QIcon>
38
#include <QtGui/QMoveEvent>
39
#include <QtGui/QTextDocument>
40
#include <QtGui/QTextBlock>
41
#include <QtGui/QVBoxLayout>
42
#include <QtGui/QHBoxLayout>
43
#include <QtGui/QPushButton>
45
#include <QtCore/qdebug.h>
47
#include "iconloader_p.h"
48
#include "richtexteditor_p.h"
50
static bool operator < (const QColor &c1, const QColor &c2)
52
if (c1.red() != c2.red())
53
return c1.red() < c2.red();
54
if (c1.green() != c2.green())
55
return c1.green() < c2.green();
56
return c1.blue() < c2.blue();
59
class RichTextEditorToolBar : public QToolBar
63
RichTextEditorToolBar(RichTextEditor *editor, QWidget *parent = 0);
69
void sizeInputActivated(const QString &size);
70
void colorInputActivated(const QString &color);
73
QAction *m_bold_action;
74
QAction *m_italic_action;
75
QAction *m_underline_action;
76
QComboBox *m_font_size_input;
77
QComboBox *m_color_input;
79
QPointer<RichTextEditor> m_editor;
81
typedef QMap<QColor, QString> ColorMap;
85
static QAction *createCheckableAction(const QIcon &icon, const QString &text,
86
QObject *receiver, const char *slot,
89
QAction *result = new QAction(parent);
90
result->setIcon(icon);
91
result->setText(text);
92
result->setCheckable(true);
93
result->setChecked(false);
94
QObject::connect(result, SIGNAL(triggered(bool)), receiver, slot);
98
static QIcon iconForColor(const QColor &color)
100
QPixmap result(12, 12);
101
QPainter painter(&result);
102
painter.setPen(Qt::black);
103
painter.setBrush(color);
104
painter.drawRect(0, 0, result.width() - 1, result.height() - 1);
106
return QIcon(result);
109
RichTextEditorToolBar::RichTextEditorToolBar(RichTextEditor *editor,
115
m_bold_action = createCheckableAction(createIconSet(QLatin1String("textbold.png")),
116
tr("Bold"), editor, SLOT(setFontBold(bool)), this);
117
m_bold_action->setShortcut(tr("CTRL+b"));
118
addAction(m_bold_action);
120
m_italic_action = createCheckableAction(createIconSet(QLatin1String("textitalic.png")),
121
tr("Italic"), editor, SLOT(setFontItalic(bool)), this);
122
m_italic_action->setShortcut(tr("CTRL+I"));
124
addAction(m_italic_action);
125
m_underline_action = createCheckableAction(createIconSet(QLatin1String("textunder.png")),
126
tr("Underline"), editor, SLOT(setFontUnderline(bool)), this);
127
m_underline_action->setShortcut(tr("CTRL+U"));
128
addAction(m_underline_action);
130
m_font_size_input = new QComboBox(this);
131
m_font_size_input->setEditable(false);
132
for (int i = 4; i < 30; ++i)
133
m_font_size_input->addItem(QString::number(i));
134
connect(m_font_size_input, SIGNAL(activated(QString)),
135
this, SLOT(sizeInputActivated(QString)));
136
addWidget(m_font_size_input);
138
QStringList color_names = QColor::colorNames();
139
color_names.removeAll(QLatin1String("transparent"));
140
foreach (QString color, color_names)
141
m_color_map.insert(QColor(color), color);
143
m_color_input = new QComboBox(this);
144
foreach (QString color, color_names)
145
m_color_input->addItem(iconForColor(color), color);
146
connect(m_color_input, SIGNAL(activated(QString)),
147
this, SLOT(colorInputActivated(QString)));
148
addWidget(m_color_input);
150
connect(editor, SIGNAL(textChanged()), this, SLOT(updateActions()));
155
void RichTextEditorToolBar::colorInputActivated(const QString &s)
158
if (!color.isValid())
161
m_editor->setTextColor(color);
162
m_editor->setFocus();
165
void RichTextEditorToolBar::sizeInputActivated(const QString &size)
171
int i = size.toInt(&ok);
175
m_editor->setFontPointSize(i);
176
m_editor->setFocus();
179
void RichTextEditorToolBar::updateActions()
186
QTextCursor cursor = m_editor->textCursor();
188
QTextCharFormat char_format = cursor.charFormat();
190
m_bold_action->setChecked(char_format.fontWeight() == QFont::Bold);
191
m_italic_action->setChecked(char_format.fontItalic());
192
m_underline_action->setChecked(char_format.fontUnderline());
194
int size = (int) char_format.fontPointSize();
195
if (size == 0) // workaround for a bug in QTextEdit
196
size = (int) m_editor->document()->defaultFont().pointSize();
197
int idx = m_font_size_input->findText(QString::number(size));
199
m_font_size_input->setCurrentIndex(idx);
201
QString color = m_color_map.value(m_editor->textColor());
202
idx = m_color_input->findText(color);
203
m_color_input->setCurrentIndex(idx);
206
RichTextEditor::RichTextEditor(QWidget *parent)
209
connect(this, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
210
this, SIGNAL(textChanged()));
213
QToolBar *RichTextEditor::createToolBar(QWidget *parent)
215
return new RichTextEditorToolBar(this, parent);
218
void RichTextEditor::setFontBold(bool b)
221
setFontWeight(QFont::Bold);
223
setFontWeight(QFont::Normal);
226
void RichTextEditor::setFontPointSize(double d)
228
QTextEdit::setFontPointSize(qreal(d));
231
void RichTextEditor::setText(const QString &text)
236
void RichTextEditor::setDefaultFont(const QFont &font)
238
document()->setDefaultFont(font);
239
setFontPointSize(font.pointSize());
243
Qt::TextFormat RichTextEditor::detectFormat() const
245
Qt::TextFormat result = Qt::PlainText;
247
QFont default_font = document()->defaultFont();
248
QTextCursor cursor(document()->begin());
249
cursor.movePosition(QTextCursor::End);
250
while (!cursor.atStart()) {
251
QFont font = cursor.charFormat().font();
252
if (font != default_font) {
253
result = Qt::RichText;
256
cursor.movePosition(QTextCursor::Left);
262
QString RichTextEditor::text(Qt::TextFormat format) const
264
bool richtext = true;
266
if (format == Qt::PlainText)
268
else if (format != Qt::RichText)
269
richtext = detectFormat() == Qt::RichText;
274
return toPlainText();
277
RichTextEditorDialog::RichTextEditorDialog(QWidget *parent)
280
setWindowTitle(tr("Edit text"));
282
QVBoxLayout *layout = new QVBoxLayout(this);
283
layout->setMargin(1);
284
m_editor = new RichTextEditor(this);
285
QToolBar *tool_bar = m_editor->createToolBar(this);
286
tool_bar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
287
layout->addWidget(tool_bar);
288
layout->addWidget(m_editor);
290
QHBoxLayout *layout2 = new QHBoxLayout;
291
layout->addLayout(layout2);
293
layout2->addStretch();
294
QPushButton *cancel_button = new QPushButton(tr("&Cancel"), this);
295
connect(cancel_button, SIGNAL(clicked()), this, SLOT(reject()));
296
QPushButton *ok_button = new QPushButton(tr("&OK"), this);
297
connect(ok_button, SIGNAL(clicked()), this, SLOT(accept()));
298
ok_button->setDefault(true);
299
layout2->addWidget(ok_button);
300
layout2->addWidget(cancel_button);
303
RichTextEditor *RichTextEditorDialog::editor()
308
#include "richtexteditor.moc"