~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/designer/src/lib/shared/richtexteditor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the designer application of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtCore/QPointer>
 
30
#include <QtCore/QMap>
 
31
 
 
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>
 
44
 
 
45
#include <QtCore/qdebug.h>
 
46
 
 
47
#include "iconloader_p.h"
 
48
#include "richtexteditor_p.h"
 
49
 
 
50
static bool operator < (const QColor &c1, const QColor &c2)
 
51
{
 
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();
 
57
}
 
58
 
 
59
class RichTextEditorToolBar : public QToolBar
 
60
{
 
61
    Q_OBJECT
 
62
public:
 
63
    RichTextEditorToolBar(RichTextEditor *editor, QWidget *parent = 0);
 
64
 
 
65
public slots:
 
66
    void updateActions();
 
67
 
 
68
private slots:
 
69
    void sizeInputActivated(const QString &size);
 
70
    void colorInputActivated(const QString &color);
 
71
 
 
72
private:
 
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;
 
78
 
 
79
    QPointer<RichTextEditor> m_editor;
 
80
 
 
81
    typedef QMap<QColor, QString> ColorMap;
 
82
    ColorMap m_color_map;
 
83
};
 
84
 
 
85
static QAction *createCheckableAction(const QIcon &icon, const QString &text,
 
86
                                    QObject *receiver, const char *slot,
 
87
                                    QObject *parent = 0)
 
88
{
 
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);
 
95
    return result;
 
96
}
 
97
 
 
98
static QIcon iconForColor(const QColor &color)
 
99
{
 
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);
 
105
    painter.end();
 
106
    return QIcon(result);
 
107
}
 
108
 
 
109
RichTextEditorToolBar::RichTextEditorToolBar(RichTextEditor *editor,
 
110
                                                QWidget *parent)
 
111
    : QToolBar(parent)
 
112
{
 
113
    m_editor = editor;
 
114
 
 
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);
 
119
 
 
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"));
 
123
 
 
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);
 
129
 
 
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);
 
137
 
 
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);
 
142
 
 
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);
 
149
 
 
150
    connect(editor, SIGNAL(textChanged()), this, SLOT(updateActions()));
 
151
 
 
152
    updateActions();
 
153
}
 
154
 
 
155
void RichTextEditorToolBar::colorInputActivated(const QString &s)
 
156
{
 
157
    QColor color(s);
 
158
    if (!color.isValid())
 
159
        return;
 
160
 
 
161
    m_editor->setTextColor(color);
 
162
    m_editor->setFocus();
 
163
}
 
164
 
 
165
void RichTextEditorToolBar::sizeInputActivated(const QString &size)
 
166
{
 
167
    if (m_editor == 0)
 
168
        return;
 
169
 
 
170
    bool ok;
 
171
    int i = size.toInt(&ok);
 
172
    if (!ok)
 
173
        return;
 
174
 
 
175
    m_editor->setFontPointSize(i);
 
176
    m_editor->setFocus();
 
177
}
 
178
 
 
179
void RichTextEditorToolBar::updateActions()
 
180
{
 
181
    if (m_editor == 0) {
 
182
        setEnabled(false);
 
183
        return;
 
184
    }
 
185
 
 
186
    QTextCursor cursor = m_editor->textCursor();
 
187
 
 
188
    QTextCharFormat char_format = cursor.charFormat();
 
189
 
 
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());
 
193
 
 
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));
 
198
    if (idx != -1)
 
199
        m_font_size_input->setCurrentIndex(idx);
 
200
 
 
201
    QString color = m_color_map.value(m_editor->textColor());
 
202
    idx = m_color_input->findText(color);
 
203
    m_color_input->setCurrentIndex(idx);
 
204
}
 
205
 
 
206
RichTextEditor::RichTextEditor(QWidget *parent)
 
207
    : QTextEdit(parent)
 
208
{
 
209
    connect(this, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
 
210
                this, SIGNAL(textChanged()));
 
211
}
 
212
 
 
213
QToolBar *RichTextEditor::createToolBar(QWidget *parent)
 
214
{
 
215
    return new RichTextEditorToolBar(this, parent);
 
216
}
 
217
 
 
218
void RichTextEditor::setFontBold(bool b)
 
219
{
 
220
    if (b)
 
221
        setFontWeight(QFont::Bold);
 
222
    else
 
223
        setFontWeight(QFont::Normal);
 
224
}
 
225
 
 
226
void RichTextEditor::setFontPointSize(double d)
 
227
{
 
228
    QTextEdit::setFontPointSize(qreal(d));
 
229
}
 
230
 
 
231
void RichTextEditor::setText(const QString &text)
 
232
{
 
233
    setHtml(text);
 
234
}
 
235
 
 
236
void RichTextEditor::setDefaultFont(const QFont &font)
 
237
{
 
238
    document()->setDefaultFont(font);
 
239
    setFontPointSize(font.pointSize());
 
240
    emit textChanged();
 
241
}
 
242
 
 
243
Qt::TextFormat RichTextEditor::detectFormat() const
 
244
{
 
245
    Qt::TextFormat result = Qt::PlainText;
 
246
 
 
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;
 
254
            break;
 
255
        }
 
256
        cursor.movePosition(QTextCursor::Left);
 
257
    }
 
258
 
 
259
    return result;
 
260
};
 
261
 
 
262
QString RichTextEditor::text(Qt::TextFormat format) const
 
263
{
 
264
    bool richtext = true;
 
265
 
 
266
    if (format == Qt::PlainText)
 
267
        richtext = false;
 
268
    else if (format != Qt::RichText)
 
269
        richtext = detectFormat() == Qt::RichText;
 
270
 
 
271
    if (richtext)
 
272
        return toHtml();
 
273
    else
 
274
        return toPlainText();
 
275
}
 
276
 
 
277
RichTextEditorDialog::RichTextEditorDialog(QWidget *parent)
 
278
    : QDialog(parent)
 
279
{
 
280
    setWindowTitle(tr("Edit text"));
 
281
 
 
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);
 
289
 
 
290
    QHBoxLayout *layout2 = new QHBoxLayout;
 
291
    layout->addLayout(layout2);
 
292
 
 
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);
 
301
}
 
302
 
 
303
RichTextEditor *RichTextEditorDialog::editor()
 
304
{
 
305
    return m_editor;
 
306
}
 
307
 
 
308
#include "richtexteditor.moc"