1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: textedit.cpp Example File (demos/textedit/textedit.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">textedit.cpp Example File<br /><small><small>demos/textedit/textedit.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include "textedit.h"
50
#include <qaction.h>
51
#include <qapplication.h>
52
#include <qclipboard.h>
53
#include <qcolordialog.h>
54
#include <qcombobox.h>
55
#include <qdebug.h>
56
#include <qfile.h>
57
#include <qfiledialog.h>
58
#include <qfileinfo.h>
59
#include <qfontdatabase.h>
60
#include <qlineedit.h>
61
#include <qlist.h>
62
#include <qmenu.h>
63
#include <qmenubar.h>
64
#include <qpainter.h>
65
#include <qprintdialog.h>
66
#include <qprinter.h>
67
#include <qtabwidget.h>
68
#include <qtextcodec.h>
69
#include <qtextedit.h>
70
#include <qtextdocumentfragment.h>
71
#include <qtextformat.h>
72
#include <qtoolbar.h>
73
#include <qtextcursor.h>
74
#include <qtextlist.h>
76
#include <limits.h>
79
const QString rsrcPath = ":/images/mac";
81
const QString rsrcPath = ":/images/win";
84
TextEdit::TextEdit(QWidget *parent)
91
tabWidget = new QTabWidget(this);
92
connect(tabWidget, SIGNAL(currentChanged(int)),
93
this, SLOT(editorChanged()));
94
setCentralWidget(tabWidget);
96
connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
98
if (qApp->argc() == 1) {
99
if (!load("example.html"))
102
for (int i = 1; i < qApp->argc(); ++i)
103
load(qApp->argv()[i]);
106
setWindowTitle(tr("Rich Text [*]"));
109
void TextEdit::setupFileActions()
111
QToolBar *tb = new QToolBar(this);
112
tb->setWindowTitle(tr("File Actions"));
115
QMenu *menu = new QMenu(tr("&File"), this);
116
menuBar()->addMenu(menu);
120
a = new QAction(QIcon(rsrcPath + "/filenew.png"), tr("&New..."), this);
121
a->setShortcut(Qt::CTRL + Qt::Key_N);
122
connect(a, SIGNAL(triggered()), this, SLOT(fileNew()));
124
menu->addAction(a);
126
a = new QAction(QIcon(rsrcPath + "/fileopen.png"), tr("&Open..."), this);
127
a->setShortcut(Qt::CTRL + Qt::Key_O);
128
connect(a, SIGNAL(triggered()), this, SLOT(fileOpen()));
130
menu->addAction(a);
132
menu->addSeparator();
134
actionSave = a = new QAction(QIcon(rsrcPath + "/filesave.png"), tr("&Save..."), this);
135
a->setShortcut(Qt::CTRL + Qt::Key_S);
136
connect(a, SIGNAL(triggered()), this, SLOT(fileSave()));
137
a->setEnabled(false);
139
menu->addAction(a);
141
a = new QAction(tr("Save &As..."), this);
142
connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
143
menu->addAction(a);
144
menu->addSeparator();
146
a = new QAction(QIcon(rsrcPath + "/fileprint.png"), tr("&Print..."), this);
147
a->setShortcut(Qt::CTRL + Qt::Key_P);
148
connect(a, SIGNAL(triggered()), this, SLOT(filePrint()));
150
menu->addAction(a);
152
a = new QAction(tr("&Close"), this);
153
a->setShortcut(Qt::CTRL + Qt::Key_W);
154
connect(a, SIGNAL(triggered()), this, SLOT(fileClose()));
155
menu->addAction(a);
157
a = new QAction(tr("E&xit"), this);
158
a->setShortcut(Qt::CTRL + Qt::Key_Q);
159
connect(a, SIGNAL(triggered()), this, SLOT(fileExit()));
160
menu->addAction(a);
163
void TextEdit::setupEditActions()
165
QToolBar *tb = new QToolBar(this);
166
tb->setWindowTitle(tr("Edit Actions"));
169
QMenu *menu = new QMenu(tr("&Edit"), this);
170
menuBar()->addMenu(menu);
173
a = actionUndo = new QAction(QIcon(rsrcPath + "/editundo.png"), tr("&Undo"), this);
174
a->setShortcut(Qt::CTRL + Qt::Key_Z);
176
menu->addAction(a);
177
a = actionRedo = new QAction(QIcon(rsrcPath + "/editredo.png"), tr("&Redo"), this);
178
a->setShortcut(Qt::CTRL + Qt::Key_Y);
180
menu->addAction(a);
181
menu->addSeparator();
182
a = actionCut = new QAction(QIcon(rsrcPath + "/editcut.png"), tr("Cu&t"), this);
183
a->setShortcut(Qt::CTRL + Qt::Key_X);
185
menu->addAction(a);
186
a = actionCopy = new QAction(QIcon(rsrcPath + "/editcopy.png"), tr("&Copy"), this);
187
a->setShortcut(Qt::CTRL + Qt::Key_C);
189
menu->addAction(a);
190
a = actionPaste = new QAction(QIcon(rsrcPath + "/editpaste.png"), tr("&Paste"), this);
191
a->setShortcut(Qt::CTRL + Qt::Key_V);
193
menu->addAction(a);
194
actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
197
void TextEdit::setupTextActions()
199
QToolBar *tb = new QToolBar(this);
200
tb->setWindowTitle(tr("Format Actions"));
203
QMenu *menu = new QMenu(tr("F&ormat"), this);
204
menuBar()->addMenu(menu);
206
actionTextBold = new QAction(QIcon(rsrcPath + "/textbold.png"), tr("&Bold"), this);
207
actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
210
actionTextBold->setFont(bold);
211
connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
212
tb->addAction(actionTextBold);
213
menu->addAction(actionTextBold);
214
actionTextBold->setCheckable(true);
216
actionTextItalic = new QAction(QIcon(rsrcPath + "/textitalic.png"), tr("&Italic"), this);
217
actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
219
italic.setItalic(true);
220
actionTextItalic->setFont(italic);
221
connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic()));
222
tb->addAction(actionTextItalic);
223
menu->addAction(actionTextItalic);
224
actionTextItalic->setCheckable(true);
226
actionTextUnderline = new QAction(QIcon(rsrcPath + "/textunder.png"), tr("&Underline"), this);
227
actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
229
underline.setUnderline(true);
230
actionTextUnderline->setFont(underline);
231
connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline()));
232
tb->addAction(actionTextUnderline);
233
menu->addAction(actionTextUnderline);
234
actionTextUnderline->setCheckable(true);
236
menu->addSeparator();
238
QActionGroup *grp = new QActionGroup(this);
239
connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(textAlign(QAction *)));
241
actionAlignLeft = new QAction(QIcon(rsrcPath + "/textleft.png"), tr("&Left"), grp);
242
actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L);
243
actionAlignLeft->setCheckable(true);
244
actionAlignCenter = new QAction(QIcon(rsrcPath + "/textcenter.png"), tr("C&enter"), grp);
245
actionAlignCenter->setShortcut(Qt::CTRL + Qt::Key_E);
246
actionAlignCenter->setCheckable(true);
247
actionAlignRight = new QAction(QIcon(rsrcPath + "/textright.png"), tr("&Right"), grp);
248
actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R);
249
actionAlignRight->setCheckable(true);
250
actionAlignJustify = new QAction(QIcon(rsrcPath + "/textjustify.png"), tr("&Justify"), grp);
251
actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J);
252
actionAlignJustify->setCheckable(true);
254
tb->addActions(grp->actions());
255
menu->addActions(grp->actions());
257
menu->addSeparator();
261
actionTextColor = new QAction(pix, tr("&Color..."), this);
262
connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
263
tb->addAction(actionTextColor);
264
menu->addAction(actionTextColor);
266
tb = new QToolBar(this);
267
tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
268
tb->setWindowTitle(tr("Format Actions"));
269
addToolBarBreak(Qt::TopToolBarArea);
272
comboStyle = new QComboBox(tb);
273
tb->addWidget(comboStyle);
274
comboStyle->addItem("Standard");
275
comboStyle->addItem("Bullet List (Disc)");
276
comboStyle->addItem("Bullet List (Circle)");
277
comboStyle->addItem("Bullet List (Square)");
278
comboStyle->addItem("Ordered List (Decimal)");
279
comboStyle->addItem("Ordered List (Alpha lower)");
280
comboStyle->addItem("Ordered List (Alpha upper)");
281
connect(comboStyle, SIGNAL(activated(int)),
282
this, SLOT(textStyle(int)));
284
comboFont = new QComboBox(tb);
285
tb->addWidget(comboFont);
286
comboFont->setEditable(true);
288
comboFont->addItems(db.families());
289
connect(comboFont, SIGNAL(activated(const QString &)),
290
this, SLOT(textFamily(const QString &)));
291
comboFont->setCurrentIndex(comboFont->findText(QApplication::font().family()));
293
comboSize = new QComboBox(tb);
294
comboSize->setObjectName("comboSize");
295
tb->addWidget(comboSize);
296
comboSize->setEditable(true);
298
foreach(int size, db.standardSizes())
299
comboSize->addItem(QString::number(size));
301
connect(comboSize, SIGNAL(activated(const QString &)),
302
this, SLOT(textSize(const QString &)));
303
comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font()
307
bool TextEdit::load(const QString &f)
309
if (!QFile::exists(f))
311
QTextEdit *edit = createNewEditor(QFileInfo(f).fileName());
313
if (!file.open(QFile::ReadOnly))
316
QByteArray data = file.readAll();
317
QTextCodec *codec = Qt::codecForHtml(data);
318
QString str = codec->toUnicode(data);
319
if (Qt::mightBeRichText(str)) {
320
edit->setHtml(str);
322
str = QString::fromLocal8Bit(data);
323
edit->setPlainText(str);
326
filenames.insert(edit, f);
330
void TextEdit::fileNew()
335
void TextEdit::fileOpen()
337
QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
338
QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
343
void TextEdit::fileSave()
347
if (filenames.find(currentEditor) == filenames.end()) {
350
QFile file(*filenames.find(currentEditor));
351
if (!file.open(QFile::WriteOnly))
353
QTextStream ts(&file);
354
ts.setCodec(QTextCodec::codecForName("UTF-8"));
355
ts << currentEditor->document()->toHtml("UTF-8");
356
currentEditor->document()->setModified(false);
360
void TextEdit::fileSaveAs()
364
QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
365
QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
367
filenames.insert(currentEditor, fn);
369
tabWidget->setTabText(tabWidget->indexOf(currentEditor), QFileInfo(fn).fileName());
373
void TextEdit::filePrint()
377
#ifndef QT_NO_PRINTER
378
QPrinter printer(QPrinter::HighResolution);
379
printer.setFullPage(true);
381
QPrintDialog *dlg = new QPrintDialog(&printer, this);
382
if (dlg->exec() == QDialog::Accepted) {
383
currentEditor->document()->print(&printer);
389
void TextEdit::fileClose()
391
const bool hadFocus = (currentEditor && currentEditor->hasFocus());
392
delete currentEditor;
393
currentEditor = qobject_cast<QTextEdit *>(tabWidget->currentWidget());
394
if (currentEditor && hadFocus)
395
currentEditor->setFocus();
398
void TextEdit::fileExit()
403
void TextEdit::textBold()
407
currentEditor->setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
410
void TextEdit::textUnderline()
414
currentEditor->setFontUnderline(actionTextUnderline->isChecked());
417
void TextEdit::textItalic()
421
currentEditor->setFontItalic(actionTextItalic->isChecked());
424
void TextEdit::textFamily(const QString &f)
428
currentEditor->setFontFamily(f);
431
void TextEdit::textSize(const QString &p)
435
currentEditor->setFontPointSize(p.toFloat());
438
void TextEdit::textStyle(int styleIndex)
443
QTextCursor cursor = currentEditor->textCursor();
445
if (styleIndex != 0) {
446
QTextListFormat::Style style = QTextListFormat::ListDisc;
448
switch (styleIndex) {
451
style = QTextListFormat::ListDisc;
454
style = QTextListFormat::ListCircle;
457
style = QTextListFormat::ListSquare;
460
style = QTextListFormat::ListDecimal;
463
style = QTextListFormat::ListLowerAlpha;
466
style = QTextListFormat::ListUpperAlpha;
470
cursor.beginEditBlock();
472
QTextBlockFormat blockFmt = cursor.blockFormat();
474
QTextListFormat listFmt;
476
if (cursor.currentList()) {
477
listFmt = cursor.currentList()->format();
479
listFmt.setIndent(blockFmt.indent() + 1);
480
blockFmt.setIndent(0);
481
cursor.setBlockFormat(blockFmt);
484
listFmt.setStyle(style);
486
cursor.createList(listFmt);
488
cursor.endEditBlock();
491
QTextBlockFormat bfmt;
492
bfmt.setObjectIndex(-1);
493
cursor.mergeBlockFormat(bfmt);
497
void TextEdit::textColor()
501
QColor col = QColorDialog::getColor(currentEditor->textColor(), this);
504
currentEditor->setTextColor(col);
508
void TextEdit::textAlign(QAction *a)
512
if (a == actionAlignLeft)
513
currentEditor->setAlignment(Qt::AlignLeft);
514
else if (a == actionAlignCenter)
515
currentEditor->setAlignment(Qt::AlignHCenter);
516
else if (a == actionAlignRight)
517
currentEditor->setAlignment(Qt::AlignRight);
518
else if (a == actionAlignJustify)
519
currentEditor->setAlignment(Qt::AlignJustify);
522
void TextEdit::currentCharFormatChanged(const QTextCharFormat &format)
524
fontChanged(format.font());
525
colorChanged(format.foreground().color());
526
alignmentChanged(currentEditor->alignment());
529
void TextEdit::clipboardDataChanged()
531
actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
534
void TextEdit::fontChanged(const QFont &f)
536
comboFont->setCurrentIndex(comboFont->findText(f.family()));
537
comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
538
actionTextBold->setChecked(f.bold());
539
actionTextItalic->setChecked(f.italic());
540
actionTextUnderline->setChecked(f.underline());
543
void TextEdit::colorChanged(const QColor &c)
547
actionTextColor->setIcon(pix);
550
void TextEdit::alignmentChanged(Qt::Alignment a)
552
if (a & Qt::AlignLeft)
553
actionAlignLeft->setChecked(true);
554
else if (a & Qt::AlignHCenter)
555
actionAlignCenter->setChecked(true);
556
else if (a & Qt::AlignRight)
557
actionAlignRight->setChecked(true);
558
else if (a & Qt::AlignJustify)
559
actionAlignJustify->setChecked(true);
562
void TextEdit::editorChanged()
565
disconnect(currentEditor->document(), SIGNAL(modificationChanged(bool)),
566
actionSave, SLOT(setEnabled(bool)));
567
disconnect(currentEditor->document(), SIGNAL(modificationChanged(bool)),
568
this, SLOT(setWindowModified(bool)));
569
disconnect(currentEditor->document(), SIGNAL(undoAvailable(bool)),
570
actionUndo, SLOT(setEnabled(bool)));
571
disconnect(currentEditor->document(), SIGNAL(redoAvailable(bool)),
572
actionRedo, SLOT(setEnabled(bool)));
574
disconnect(actionUndo, SIGNAL(triggered()), currentEditor->document(), SLOT(undo()));
575
disconnect(actionRedo, SIGNAL(triggered()), currentEditor->document(), SLOT(redo()));
577
disconnect(actionCut, SIGNAL(triggered()), currentEditor, SLOT(cut()));
578
disconnect(actionCopy, SIGNAL(triggered()), currentEditor, SLOT(copy()));
579
disconnect(actionPaste, SIGNAL(triggered()), currentEditor, SLOT(paste()));
581
disconnect(currentEditor, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
582
disconnect(currentEditor, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
586
currentEditor = qobject_cast<QTextEdit *>(tabWidget->currentWidget());
590
fontChanged(currentEditor->font());
591
colorChanged(currentEditor->textColor());
592
alignmentChanged(currentEditor->alignment());
594
connect(currentEditor->document(), SIGNAL(modificationChanged(bool)),
595
actionSave, SLOT(setEnabled(bool)));
596
connect(currentEditor->document(), SIGNAL(modificationChanged(bool)),
597
this, SLOT(setWindowModified(bool)));
598
connect(currentEditor->document(), SIGNAL(undoAvailable(bool)),
599
actionUndo, SLOT(setEnabled(bool)));
600
connect(currentEditor->document(), SIGNAL(redoAvailable(bool)),
601
actionRedo, SLOT(setEnabled(bool)));
603
setWindowModified(currentEditor->document()->isModified());
604
actionSave->setEnabled(currentEditor->document()->isModified());
605
actionUndo->setEnabled(currentEditor->document()->isUndoAvailable());
606
actionRedo->setEnabled(currentEditor->document()->isRedoAvailable());
608
connect(actionUndo, SIGNAL(triggered()), currentEditor->document(), SLOT(undo()));
609
connect(actionRedo, SIGNAL(triggered()), currentEditor->document(), SLOT(redo()));
611
const bool selection = currentEditor->textCursor().hasSelection();
612
actionCut->setEnabled(selection);
613
actionCopy->setEnabled(selection);
615
connect(actionCut, SIGNAL(triggered()), currentEditor, SLOT(cut()));
616
connect(actionCopy, SIGNAL(triggered()), currentEditor, SLOT(copy()));
617
connect(actionPaste, SIGNAL(triggered()), currentEditor, SLOT(paste()));
619
connect(currentEditor, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
620
connect(currentEditor, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
623
QTextEdit *TextEdit::createNewEditor(const QString &title)
625
QTextEdit *edit = new QTextEdit;
626
connect(edit, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
627
this, SLOT(currentCharFormatChanged(const QTextCharFormat &)));
629
int tab = tabWidget->addTab(edit, title.isEmpty() ? tr("noname") : title);
630
tabWidget->setCurrentIndex(tab);
635
<p /><address><hr /><div align="center">
636
<table width="100%" cellspacing="0" border="0"><tr class="address">
637
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
638
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
639
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
640
</tr></table></div></address></body>