1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the demonstration applications 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
****************************************************************************/
32
#include <qapplication.h>
33
#include <qclipboard.h>
34
#include <qcolordialog.h>
35
#include <qcombobox.h>
38
#include <qfiledialog.h>
39
#include <qfileinfo.h>
40
#include <qfontdatabase.h>
41
#include <qlineedit.h>
46
#include <qprintdialog.h>
48
#include <qtabwidget.h>
49
#include <qtextcodec.h>
50
#include <qtextedit.h>
51
#include <qtextdocumentfragment.h>
52
#include <qtextformat.h>
54
#include <qtextcursor.h>
55
#include <qtextlist.h>
60
const QString rsrcPath = ":/images/mac";
62
const QString rsrcPath = ":/images/win";
65
TextEdit::TextEdit(QWidget *parent)
72
tabWidget = new QTabWidget(this);
73
connect(tabWidget, SIGNAL(currentChanged(int)),
74
this, SLOT(editorChanged()));
75
setCentralWidget(tabWidget);
77
connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
79
if (qApp->argc() == 1) {
80
if (!load("example.html"))
83
for (int i = 1; i < qApp->argc(); ++i)
84
load(qApp->argv()[i]);
87
setWindowTitle(tr("Rich Text [*]"));
90
void TextEdit::setupFileActions()
92
QToolBar *tb = new QToolBar(this);
93
tb->setWindowTitle(tr("File Actions"));
96
QMenu *menu = new QMenu(tr("&File"), this);
97
menuBar()->addMenu(menu);
101
a = new QAction(QIcon(rsrcPath + "/filenew.png"), tr("&New..."), this);
102
a->setShortcut(Qt::CTRL + Qt::Key_N);
103
connect(a, SIGNAL(triggered()), this, SLOT(fileNew()));
107
a = new QAction(QIcon(rsrcPath + "/fileopen.png"), tr("&Open..."), this);
108
a->setShortcut(Qt::CTRL + Qt::Key_O);
109
connect(a, SIGNAL(triggered()), this, SLOT(fileOpen()));
113
menu->addSeparator();
115
actionSave = a = new QAction(QIcon(rsrcPath + "/filesave.png"), tr("&Save..."), this);
116
a->setShortcut(Qt::CTRL + Qt::Key_S);
117
connect(a, SIGNAL(triggered()), this, SLOT(fileSave()));
118
a->setEnabled(false);
122
a = new QAction(tr("Save &As..."), this);
123
connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
125
menu->addSeparator();
127
a = new QAction(QIcon(rsrcPath + "/fileprint.png"), tr("&Print..."), this);
128
a->setShortcut(Qt::CTRL + Qt::Key_P);
129
connect(a, SIGNAL(triggered()), this, SLOT(filePrint()));
133
a = new QAction(tr("&Close"), this);
134
a->setShortcut(Qt::CTRL + Qt::Key_W);
135
connect(a, SIGNAL(triggered()), this, SLOT(fileClose()));
138
a = new QAction(tr("E&xit"), this);
139
a->setShortcut(Qt::CTRL + Qt::Key_Q);
140
connect(a, SIGNAL(triggered()), this, SLOT(fileExit()));
144
void TextEdit::setupEditActions()
146
QToolBar *tb = new QToolBar(this);
147
tb->setWindowTitle(tr("Edit Actions"));
150
QMenu *menu = new QMenu(tr("&Edit"), this);
151
menuBar()->addMenu(menu);
154
a = actionUndo = new QAction(QIcon(rsrcPath + "/editundo.png"), tr("&Undo"), this);
155
a->setShortcut(Qt::CTRL + Qt::Key_Z);
158
a = actionRedo = new QAction(QIcon(rsrcPath + "/editredo.png"), tr("&Redo"), this);
159
a->setShortcut(Qt::CTRL + Qt::Key_Y);
162
menu->addSeparator();
163
a = actionCut = new QAction(QIcon(rsrcPath + "/editcut.png"), tr("Cu&t"), this);
164
a->setShortcut(Qt::CTRL + Qt::Key_X);
167
a = actionCopy = new QAction(QIcon(rsrcPath + "/editcopy.png"), tr("&Copy"), this);
168
a->setShortcut(Qt::CTRL + Qt::Key_C);
171
a = actionPaste = new QAction(QIcon(rsrcPath + "/editpaste.png"), tr("&Paste"), this);
172
a->setShortcut(Qt::CTRL + Qt::Key_V);
175
actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
178
void TextEdit::setupTextActions()
180
QToolBar *tb = new QToolBar(this);
181
tb->setWindowTitle(tr("Format Actions"));
184
QMenu *menu = new QMenu(tr("F&ormat"), this);
185
menuBar()->addMenu(menu);
187
actionTextBold = new QAction(QIcon(rsrcPath + "/textbold.png"), tr("&Bold"), this);
188
actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
191
actionTextBold->setFont(bold);
192
connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
193
tb->addAction(actionTextBold);
194
menu->addAction(actionTextBold);
195
actionTextBold->setCheckable(true);
197
actionTextItalic = new QAction(QIcon(rsrcPath + "/textitalic.png"), tr("&Italic"), this);
198
actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
200
italic.setItalic(true);
201
actionTextItalic->setFont(italic);
202
connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic()));
203
tb->addAction(actionTextItalic);
204
menu->addAction(actionTextItalic);
205
actionTextItalic->setCheckable(true);
207
actionTextUnderline = new QAction(QIcon(rsrcPath + "/textunder.png"), tr("&Underline"), this);
208
actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
210
underline.setUnderline(true);
211
actionTextUnderline->setFont(underline);
212
connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline()));
213
tb->addAction(actionTextUnderline);
214
menu->addAction(actionTextUnderline);
215
actionTextUnderline->setCheckable(true);
217
menu->addSeparator();
219
QActionGroup *grp = new QActionGroup(this);
220
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(textAlign(QAction *)));
222
actionAlignLeft = new QAction(QIcon(rsrcPath + "/textleft.png"), tr("&Left"), grp);
223
actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L);
224
actionAlignLeft->setCheckable(true);
225
actionAlignCenter = new QAction(QIcon(rsrcPath + "/textcenter.png"), tr("C&enter"), grp);
226
actionAlignCenter->setShortcut(Qt::CTRL + Qt::Key_E);
227
actionAlignCenter->setCheckable(true);
228
actionAlignRight = new QAction(QIcon(rsrcPath + "/textright.png"), tr("&Right"), grp);
229
actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R);
230
actionAlignRight->setCheckable(true);
231
actionAlignJustify = new QAction(QIcon(rsrcPath + "/textjustify.png"), tr("&Justify"), grp);
232
actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J);
233
actionAlignJustify->setCheckable(true);
235
tb->addActions(grp->actions());
236
menu->addActions(grp->actions());
238
menu->addSeparator();
242
actionTextColor = new QAction(pix, tr("&Color..."), this);
243
connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
244
tb->addAction(actionTextColor);
245
menu->addAction(actionTextColor);
248
tb = new QToolBar(this);
249
tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
250
tb->setWindowTitle(tr("Format Actions"));
251
addToolBarBreak(Qt::TopToolBarArea);
254
comboStyle = new QComboBox(tb);
255
tb->addWidget(comboStyle);
256
comboStyle->addItem("Standard");
257
comboStyle->addItem("Bullet List (Disc)");
258
comboStyle->addItem("Bullet List (Circle)");
259
comboStyle->addItem("Bullet List (Square)");
260
comboStyle->addItem("Ordered List (Decimal)");
261
comboStyle->addItem("Ordered List (Alpha lower)");
262
comboStyle->addItem("Ordered List (Alpha upper)");
263
connect(comboStyle, SIGNAL(activated(int)),
264
this, SLOT(textStyle(int)));
266
comboFont = new QComboBox(tb);
267
tb->addWidget(comboFont);
268
comboFont->setEditable(true);
270
comboFont->addItems(db.families());
271
connect(comboFont, SIGNAL(activated(const QString &)),
272
this, SLOT(textFamily(const QString &)));
273
comboFont->setCurrentIndex(comboFont->findText(QApplication::font().family()));
275
comboSize = new QComboBox(tb);
276
comboSize->setObjectName("comboSize");
277
tb->addWidget(comboSize);
278
comboSize->setEditable(true);
280
foreach(int size, db.standardSizes())
281
comboSize->addItem(QString::number(size));
283
connect(comboSize, SIGNAL(activated(const QString &)),
284
this, SLOT(textSize(const QString &)));
285
comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font()
289
bool TextEdit::load(const QString &f)
291
if (!QFile::exists(f))
293
QTextEdit *edit = createNewEditor(QFileInfo(f).fileName());
295
if (!file.open(QFile::ReadOnly))
298
QByteArray data = file.readAll();
299
QTextCodec *codec = Qt::codecForHtml(data);
300
QString str = codec->toUnicode(data);
301
if (Qt::mightBeRichText(str)) {
304
str = QString::fromLocal8Bit(data);
305
edit->setPlainText(str);
308
filenames.insert(edit, f);
312
void TextEdit::fileNew()
317
void TextEdit::fileOpen()
319
QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
320
QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
325
void TextEdit::fileSave()
329
if (filenames.find(currentEditor) == filenames.end()) {
332
QFile file(*filenames.find(currentEditor));
333
if (!file.open(QFile::WriteOnly))
335
QTextStream ts(&file);
336
ts.setCodec(QTextCodec::codecForName("UTF-8"));
337
ts << currentEditor->document()->toHtml("UTF-8");
338
currentEditor->document()->setModified(false);
342
void TextEdit::fileSaveAs()
346
QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
347
QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
349
filenames.insert(currentEditor, fn);
351
tabWidget->setTabText(tabWidget->indexOf(currentEditor), QFileInfo(fn).fileName());
355
void TextEdit::filePrint()
359
#ifndef QT_NO_PRINTER
360
QPrinter printer(QPrinter::HighResolution);
361
printer.setFullPage(true);
363
QPrintDialog *dlg = new QPrintDialog(&printer, this);
364
if (dlg->exec() == QDialog::Accepted) {
365
currentEditor->document()->print(&printer);
371
void TextEdit::fileClose()
373
const bool hadFocus = (currentEditor && currentEditor->hasFocus());
374
delete currentEditor;
375
currentEditor = qobject_cast<QTextEdit *>(tabWidget->currentWidget());
376
if (currentEditor && hadFocus)
377
currentEditor->setFocus();
380
void TextEdit::fileExit()
385
void TextEdit::textBold()
389
currentEditor->setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
392
void TextEdit::textUnderline()
396
currentEditor->setFontUnderline(actionTextUnderline->isChecked());
399
void TextEdit::textItalic()
403
currentEditor->setFontItalic(actionTextItalic->isChecked());
406
void TextEdit::textFamily(const QString &f)
410
currentEditor->setFontFamily(f);
413
void TextEdit::textSize(const QString &p)
417
currentEditor->setFontPointSize(p.toFloat());
420
void TextEdit::textStyle(int styleIndex)
425
QTextCursor cursor = currentEditor->textCursor();
427
if (styleIndex != 0) {
428
QTextListFormat::Style style = QTextListFormat::ListDisc;
430
switch (styleIndex) {
433
style = QTextListFormat::ListDisc;
436
style = QTextListFormat::ListCircle;
439
style = QTextListFormat::ListSquare;
442
style = QTextListFormat::ListDecimal;
445
style = QTextListFormat::ListLowerAlpha;
448
style = QTextListFormat::ListUpperAlpha;
452
cursor.beginEditBlock();
454
QTextBlockFormat blockFmt = cursor.blockFormat();
456
QTextListFormat listFmt;
458
if (cursor.currentList()) {
459
listFmt = cursor.currentList()->format();
461
listFmt.setIndent(blockFmt.indent() + 1);
462
blockFmt.setIndent(0);
463
cursor.setBlockFormat(blockFmt);
466
listFmt.setStyle(style);
468
cursor.createList(listFmt);
470
cursor.endEditBlock();
473
QTextBlockFormat bfmt;
474
bfmt.setObjectIndex(-1);
475
cursor.mergeBlockFormat(bfmt);
479
void TextEdit::textColor()
483
QColor col = QColorDialog::getColor(currentEditor->textColor(), this);
486
currentEditor->setTextColor(col);
490
void TextEdit::textAlign(QAction *a)
494
if (a == actionAlignLeft)
495
currentEditor->setAlignment(Qt::AlignLeft);
496
else if (a == actionAlignCenter)
497
currentEditor->setAlignment(Qt::AlignHCenter);
498
else if (a == actionAlignRight)
499
currentEditor->setAlignment(Qt::AlignRight);
500
else if (a == actionAlignJustify)
501
currentEditor->setAlignment(Qt::AlignJustify);
504
void TextEdit::currentCharFormatChanged(const QTextCharFormat &format)
506
fontChanged(format.font());
507
colorChanged(format.foreground().color());
508
alignmentChanged(currentEditor->alignment());
511
void TextEdit::clipboardDataChanged()
513
actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
516
void TextEdit::fontChanged(const QFont &f)
518
comboFont->setCurrentIndex(comboFont->findText(f.family()));
519
comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
520
actionTextBold->setChecked(f.bold());
521
actionTextItalic->setChecked(f.italic());
522
actionTextUnderline->setChecked(f.underline());
525
void TextEdit::colorChanged(const QColor &c)
529
actionTextColor->setIcon(pix);
532
void TextEdit::alignmentChanged(Qt::Alignment a)
534
if (a & Qt::AlignLeft)
535
actionAlignLeft->setChecked(true);
536
else if (a & Qt::AlignHCenter)
537
actionAlignCenter->setChecked(true);
538
else if (a & Qt::AlignRight)
539
actionAlignRight->setChecked(true);
540
else if (a & Qt::AlignJustify)
541
actionAlignJustify->setChecked(true);
544
void TextEdit::editorChanged()
547
disconnect(currentEditor->document(), SIGNAL(modificationChanged(bool)),
548
actionSave, SLOT(setEnabled(bool)));
549
disconnect(currentEditor->document(), SIGNAL(modificationChanged(bool)),
550
this, SLOT(setWindowModified(bool)));
551
disconnect(currentEditor->document(), SIGNAL(undoAvailable(bool)),
552
actionUndo, SLOT(setEnabled(bool)));
553
disconnect(currentEditor->document(), SIGNAL(redoAvailable(bool)),
554
actionRedo, SLOT(setEnabled(bool)));
556
disconnect(actionUndo, SIGNAL(triggered()), currentEditor->document(), SLOT(undo()));
557
disconnect(actionRedo, SIGNAL(triggered()), currentEditor->document(), SLOT(redo()));
559
disconnect(actionCut, SIGNAL(triggered()), currentEditor, SLOT(cut()));
560
disconnect(actionCopy, SIGNAL(triggered()), currentEditor, SLOT(copy()));
561
disconnect(actionPaste, SIGNAL(triggered()), currentEditor, SLOT(paste()));
563
disconnect(currentEditor, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
564
disconnect(currentEditor, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
568
currentEditor = qobject_cast<QTextEdit *>(tabWidget->currentWidget());
572
fontChanged(currentEditor->font());
573
colorChanged(currentEditor->textColor());
574
alignmentChanged(currentEditor->alignment());
576
connect(currentEditor->document(), SIGNAL(modificationChanged(bool)),
577
actionSave, SLOT(setEnabled(bool)));
578
connect(currentEditor->document(), SIGNAL(modificationChanged(bool)),
579
this, SLOT(setWindowModified(bool)));
580
connect(currentEditor->document(), SIGNAL(undoAvailable(bool)),
581
actionUndo, SLOT(setEnabled(bool)));
582
connect(currentEditor->document(), SIGNAL(redoAvailable(bool)),
583
actionRedo, SLOT(setEnabled(bool)));
585
setWindowModified(currentEditor->document()->isModified());
586
actionSave->setEnabled(currentEditor->document()->isModified());
587
actionUndo->setEnabled(currentEditor->document()->isUndoAvailable());
588
actionRedo->setEnabled(currentEditor->document()->isRedoAvailable());
590
connect(actionUndo, SIGNAL(triggered()), currentEditor->document(), SLOT(undo()));
591
connect(actionRedo, SIGNAL(triggered()), currentEditor->document(), SLOT(redo()));
593
const bool selection = currentEditor->textCursor().hasSelection();
594
actionCut->setEnabled(selection);
595
actionCopy->setEnabled(selection);
597
connect(actionCut, SIGNAL(triggered()), currentEditor, SLOT(cut()));
598
connect(actionCopy, SIGNAL(triggered()), currentEditor, SLOT(copy()));
599
connect(actionPaste, SIGNAL(triggered()), currentEditor, SLOT(paste()));
601
connect(currentEditor, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
602
connect(currentEditor, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
605
QTextEdit *TextEdit::createNewEditor(const QString &title)
607
QTextEdit *edit = new QTextEdit;
608
connect(edit, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
609
this, SLOT(currentCharFormatChanged(const QTextCharFormat &)));
611
int tab = tabWidget->addTab(edit, title.isEmpty() ? tr("noname") : title);
612
tabWidget->setCurrentIndex(tab);