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

« back to all changes in this revision

Viewing changes to doc/html/demos-textedit-textedit-cpp.html

  • 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
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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">
 
5
<head>
 
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>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
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.
 
29
**
 
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.
 
34
**
 
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.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
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.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &quot;textedit.h&quot;
 
49
 
 
50
    #include &lt;qaction.h&gt;
 
51
    #include &lt;qapplication.h&gt;
 
52
    #include &lt;qclipboard.h&gt;
 
53
    #include &lt;qcolordialog.h&gt;
 
54
    #include &lt;qcombobox.h&gt;
 
55
    #include &lt;qdebug.h&gt;
 
56
    #include &lt;qfile.h&gt;
 
57
    #include &lt;qfiledialog.h&gt;
 
58
    #include &lt;qfileinfo.h&gt;
 
59
    #include &lt;qfontdatabase.h&gt;
 
60
    #include &lt;qlineedit.h&gt;
 
61
    #include &lt;qlist.h&gt;
 
62
    #include &lt;qmenu.h&gt;
 
63
    #include &lt;qmenubar.h&gt;
 
64
    #include &lt;qpainter.h&gt;
 
65
    #include &lt;qprintdialog.h&gt;
 
66
    #include &lt;qprinter.h&gt;
 
67
    #include &lt;qtabwidget.h&gt;
 
68
    #include &lt;qtextcodec.h&gt;
 
69
    #include &lt;qtextedit.h&gt;
 
70
    #include &lt;qtextdocumentfragment.h&gt;
 
71
    #include &lt;qtextformat.h&gt;
 
72
    #include &lt;qtoolbar.h&gt;
 
73
    #include &lt;qtextcursor.h&gt;
 
74
    #include &lt;qtextlist.h&gt;
 
75
 
 
76
    #include &lt;limits.h&gt;
 
77
 
 
78
    #ifdef Q_WS_MAC
 
79
    const QString rsrcPath = &quot;:/images/mac&quot;;
 
80
    #else
 
81
    const QString rsrcPath = &quot;:/images/win&quot;;
 
82
    #endif
 
83
 
 
84
    TextEdit::TextEdit(QWidget *parent)
 
85
        : QMainWindow(parent)
 
86
    {
 
87
        setupFileActions();
 
88
        setupEditActions();
 
89
        setupTextActions();
 
90
 
 
91
        tabWidget = new QTabWidget(this);
 
92
        connect(tabWidget, SIGNAL(currentChanged(int)),
 
93
                this, SLOT(editorChanged()));
 
94
        setCentralWidget(tabWidget);
 
95
 
 
96
        connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
 
97
 
 
98
        if (qApp-&gt;argc() == 1) {
 
99
            if (!load(&quot;example.html&quot;))
 
100
                fileNew();
 
101
        } else {
 
102
            for (int i = 1; i &lt; qApp-&gt;argc(); ++i)
 
103
                load(qApp-&gt;argv()[i]);
 
104
        }
 
105
 
 
106
        setWindowTitle(tr(&quot;Rich Text [*]&quot;));
 
107
    }
 
108
 
 
109
    void TextEdit::setupFileActions()
 
110
    {
 
111
        QToolBar *tb = new QToolBar(this);
 
112
        tb-&gt;setWindowTitle(tr(&quot;File Actions&quot;));
 
113
        addToolBar(tb);
 
114
 
 
115
        QMenu *menu = new QMenu(tr(&quot;&amp;File&quot;), this);
 
116
        menuBar()-&gt;addMenu(menu);
 
117
 
 
118
        QAction *a;
 
119
 
 
120
        a = new QAction(QIcon(rsrcPath + &quot;/filenew.png&quot;), tr(&quot;&amp;New...&quot;), this);
 
121
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_N);
 
122
        connect(a, SIGNAL(triggered()), this, SLOT(fileNew()));
 
123
        tb-&gt;addAction(a);
 
124
        menu-&gt;addAction(a);
 
125
 
 
126
        a = new QAction(QIcon(rsrcPath + &quot;/fileopen.png&quot;), tr(&quot;&amp;Open...&quot;), this);
 
127
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_O);
 
128
        connect(a, SIGNAL(triggered()), this, SLOT(fileOpen()));
 
129
        tb-&gt;addAction(a);
 
130
        menu-&gt;addAction(a);
 
131
 
 
132
        menu-&gt;addSeparator();
 
133
 
 
134
        actionSave = a = new QAction(QIcon(rsrcPath + &quot;/filesave.png&quot;), tr(&quot;&amp;Save...&quot;), this);
 
135
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_S);
 
136
        connect(a, SIGNAL(triggered()), this, SLOT(fileSave()));
 
137
        a-&gt;setEnabled(false);
 
138
        tb-&gt;addAction(a);
 
139
        menu-&gt;addAction(a);
 
140
 
 
141
        a = new QAction(tr(&quot;Save &amp;As...&quot;), this);
 
142
        connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
 
143
        menu-&gt;addAction(a);
 
144
        menu-&gt;addSeparator();
 
145
 
 
146
        a = new QAction(QIcon(rsrcPath + &quot;/fileprint.png&quot;), tr(&quot;&amp;Print...&quot;), this);
 
147
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_P);
 
148
        connect(a, SIGNAL(triggered()), this, SLOT(filePrint()));
 
149
        tb-&gt;addAction(a);
 
150
        menu-&gt;addAction(a);
 
151
 
 
152
        a = new QAction(tr(&quot;&amp;Close&quot;), this);
 
153
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_W);
 
154
        connect(a, SIGNAL(triggered()), this, SLOT(fileClose()));
 
155
        menu-&gt;addAction(a);
 
156
 
 
157
        a = new QAction(tr(&quot;E&amp;xit&quot;), this);
 
158
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_Q);
 
159
        connect(a, SIGNAL(triggered()), this, SLOT(fileExit()));
 
160
        menu-&gt;addAction(a);
 
161
    }
 
162
 
 
163
    void TextEdit::setupEditActions()
 
164
    {
 
165
        QToolBar *tb = new QToolBar(this);
 
166
        tb-&gt;setWindowTitle(tr(&quot;Edit Actions&quot;));
 
167
        addToolBar(tb);
 
168
 
 
169
        QMenu *menu = new QMenu(tr(&quot;&amp;Edit&quot;), this);
 
170
        menuBar()-&gt;addMenu(menu);
 
171
 
 
172
        QAction *a;
 
173
        a = actionUndo = new QAction(QIcon(rsrcPath + &quot;/editundo.png&quot;), tr(&quot;&amp;Undo&quot;), this);
 
174
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_Z);
 
175
        tb-&gt;addAction(a);
 
176
        menu-&gt;addAction(a);
 
177
        a = actionRedo = new QAction(QIcon(rsrcPath + &quot;/editredo.png&quot;), tr(&quot;&amp;Redo&quot;), this);
 
178
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_Y);
 
179
        tb-&gt;addAction(a);
 
180
        menu-&gt;addAction(a);
 
181
        menu-&gt;addSeparator();
 
182
        a = actionCut = new QAction(QIcon(rsrcPath + &quot;/editcut.png&quot;), tr(&quot;Cu&amp;t&quot;), this);
 
183
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_X);
 
184
        tb-&gt;addAction(a);
 
185
        menu-&gt;addAction(a);
 
186
        a = actionCopy = new QAction(QIcon(rsrcPath + &quot;/editcopy.png&quot;), tr(&quot;&amp;Copy&quot;), this);
 
187
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_C);
 
188
        tb-&gt;addAction(a);
 
189
        menu-&gt;addAction(a);
 
190
        a = actionPaste = new QAction(QIcon(rsrcPath + &quot;/editpaste.png&quot;), tr(&quot;&amp;Paste&quot;), this);
 
191
        a-&gt;setShortcut(Qt::CTRL + Qt::Key_V);
 
192
        tb-&gt;addAction(a);
 
193
        menu-&gt;addAction(a);
 
194
        actionPaste-&gt;setEnabled(!QApplication::clipboard()-&gt;text().isEmpty());
 
195
    }
 
196
 
 
197
    void TextEdit::setupTextActions()
 
198
    {
 
199
        QToolBar *tb = new QToolBar(this);
 
200
        tb-&gt;setWindowTitle(tr(&quot;Format Actions&quot;));
 
201
        addToolBar(tb);
 
202
 
 
203
        QMenu *menu = new QMenu(tr(&quot;F&amp;ormat&quot;), this);
 
204
        menuBar()-&gt;addMenu(menu);
 
205
 
 
206
        actionTextBold = new QAction(QIcon(rsrcPath + &quot;/textbold.png&quot;), tr(&quot;&amp;Bold&quot;), this);
 
207
        actionTextBold-&gt;setShortcut(Qt::CTRL + Qt::Key_B);
 
208
        QFont bold;
 
209
        bold.setBold(true);
 
210
        actionTextBold-&gt;setFont(bold);
 
211
        connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
 
212
        tb-&gt;addAction(actionTextBold);
 
213
        menu-&gt;addAction(actionTextBold);
 
214
        actionTextBold-&gt;setCheckable(true);
 
215
 
 
216
        actionTextItalic = new QAction(QIcon(rsrcPath + &quot;/textitalic.png&quot;), tr(&quot;&amp;Italic&quot;), this);
 
217
        actionTextItalic-&gt;setShortcut(Qt::CTRL + Qt::Key_I);
 
218
        QFont italic;
 
219
        italic.setItalic(true);
 
220
        actionTextItalic-&gt;setFont(italic);
 
221
        connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic()));
 
222
        tb-&gt;addAction(actionTextItalic);
 
223
        menu-&gt;addAction(actionTextItalic);
 
224
        actionTextItalic-&gt;setCheckable(true);
 
225
 
 
226
        actionTextUnderline = new QAction(QIcon(rsrcPath + &quot;/textunder.png&quot;), tr(&quot;&amp;Underline&quot;), this);
 
227
        actionTextUnderline-&gt;setShortcut(Qt::CTRL + Qt::Key_U);
 
228
        QFont underline;
 
229
        underline.setUnderline(true);
 
230
        actionTextUnderline-&gt;setFont(underline);
 
231
        connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline()));
 
232
        tb-&gt;addAction(actionTextUnderline);
 
233
        menu-&gt;addAction(actionTextUnderline);
 
234
        actionTextUnderline-&gt;setCheckable(true);
 
235
 
 
236
        menu-&gt;addSeparator();
 
237
 
 
238
        QActionGroup *grp = new QActionGroup(this);
 
239
        connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(textAlign(QAction *)));
 
240
 
 
241
        actionAlignLeft = new QAction(QIcon(rsrcPath + &quot;/textleft.png&quot;), tr(&quot;&amp;Left&quot;), grp);
 
242
        actionAlignLeft-&gt;setShortcut(Qt::CTRL + Qt::Key_L);
 
243
        actionAlignLeft-&gt;setCheckable(true);
 
244
        actionAlignCenter = new QAction(QIcon(rsrcPath + &quot;/textcenter.png&quot;), tr(&quot;C&amp;enter&quot;), grp);
 
245
        actionAlignCenter-&gt;setShortcut(Qt::CTRL + Qt::Key_E);
 
246
        actionAlignCenter-&gt;setCheckable(true);
 
247
        actionAlignRight = new QAction(QIcon(rsrcPath + &quot;/textright.png&quot;), tr(&quot;&amp;Right&quot;), grp);
 
248
        actionAlignRight-&gt;setShortcut(Qt::CTRL + Qt::Key_R);
 
249
        actionAlignRight-&gt;setCheckable(true);
 
250
        actionAlignJustify = new QAction(QIcon(rsrcPath + &quot;/textjustify.png&quot;), tr(&quot;&amp;Justify&quot;), grp);
 
251
        actionAlignJustify-&gt;setShortcut(Qt::CTRL + Qt::Key_J);
 
252
        actionAlignJustify-&gt;setCheckable(true);
 
253
 
 
254
        tb-&gt;addActions(grp-&gt;actions());
 
255
        menu-&gt;addActions(grp-&gt;actions());
 
256
 
 
257
        menu-&gt;addSeparator();
 
258
 
 
259
        QPixmap pix(16, 16);
 
260
        pix.fill(Qt::black);
 
261
        actionTextColor = new QAction(pix, tr(&quot;&amp;Color...&quot;), this);
 
262
        connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
 
263
        tb-&gt;addAction(actionTextColor);
 
264
        menu-&gt;addAction(actionTextColor);
 
265
 
 
266
        tb = new QToolBar(this);
 
267
        tb-&gt;setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
 
268
        tb-&gt;setWindowTitle(tr(&quot;Format Actions&quot;));
 
269
        addToolBarBreak(Qt::TopToolBarArea);
 
270
        addToolBar(tb);
 
271
 
 
272
        comboStyle = new QComboBox(tb);
 
273
        tb-&gt;addWidget(comboStyle);
 
274
        comboStyle-&gt;addItem(&quot;Standard&quot;);
 
275
        comboStyle-&gt;addItem(&quot;Bullet List (Disc)&quot;);
 
276
        comboStyle-&gt;addItem(&quot;Bullet List (Circle)&quot;);
 
277
        comboStyle-&gt;addItem(&quot;Bullet List (Square)&quot;);
 
278
        comboStyle-&gt;addItem(&quot;Ordered List (Decimal)&quot;);
 
279
        comboStyle-&gt;addItem(&quot;Ordered List (Alpha lower)&quot;);
 
280
        comboStyle-&gt;addItem(&quot;Ordered List (Alpha upper)&quot;);
 
281
        connect(comboStyle, SIGNAL(activated(int)),
 
282
                this, SLOT(textStyle(int)));
 
283
 
 
284
        comboFont = new QComboBox(tb);
 
285
        tb-&gt;addWidget(comboFont);
 
286
        comboFont-&gt;setEditable(true);
 
287
        QFontDatabase db;
 
288
        comboFont-&gt;addItems(db.families());
 
289
        connect(comboFont, SIGNAL(activated(const QString &amp;)),
 
290
                this, SLOT(textFamily(const QString &amp;)));
 
291
        comboFont-&gt;setCurrentIndex(comboFont-&gt;findText(QApplication::font().family()));
 
292
 
 
293
        comboSize = new QComboBox(tb);
 
294
        comboSize-&gt;setObjectName(&quot;comboSize&quot;);
 
295
        tb-&gt;addWidget(comboSize);
 
296
        comboSize-&gt;setEditable(true);
 
297
 
 
298
        foreach(int size, db.standardSizes())
 
299
            comboSize-&gt;addItem(QString::number(size));
 
300
 
 
301
        connect(comboSize, SIGNAL(activated(const QString &amp;)),
 
302
                this, SLOT(textSize(const QString &amp;)));
 
303
        comboSize-&gt;setCurrentIndex(comboSize-&gt;findText(QString::number(QApplication::font()
 
304
                                                                       .pointSize())));
 
305
    }
 
306
 
 
307
    bool TextEdit::load(const QString &amp;f)
 
308
    {
 
309
        if (!QFile::exists(f))
 
310
            return false;
 
311
        QTextEdit *edit = createNewEditor(QFileInfo(f).fileName());
 
312
        QFile file(f);
 
313
        if (!file.open(QFile::ReadOnly))
 
314
            return false;
 
315
 
 
316
        QByteArray data = file.readAll();
 
317
        QTextCodec *codec = Qt::codecForHtml(data);
 
318
        QString str = codec-&gt;toUnicode(data);
 
319
        if (Qt::mightBeRichText(str)) {
 
320
            edit-&gt;setHtml(str);
 
321
        } else {
 
322
            str = QString::fromLocal8Bit(data);
 
323
            edit-&gt;setPlainText(str);
 
324
        }
 
325
 
 
326
        filenames.insert(edit, f);
 
327
        return true;
 
328
    }
 
329
 
 
330
    void TextEdit::fileNew()
 
331
    {
 
332
        createNewEditor();
 
333
    }
 
334
 
 
335
    void TextEdit::fileOpen()
 
336
    {
 
337
        QString fn = QFileDialog::getOpenFileName(this, tr(&quot;Open File...&quot;),
 
338
                                                  QString(), tr(&quot;HTML-Files (*.htm *.html);;All Files (*)&quot;));
 
339
        if (!fn.isEmpty())
 
340
            load(fn);
 
341
    }
 
342
 
 
343
    void TextEdit::fileSave()
 
344
    {
 
345
        if (!currentEditor)
 
346
            return;
 
347
        if (filenames.find(currentEditor) == filenames.end()) {
 
348
            fileSaveAs();
 
349
        } else {
 
350
            QFile file(*filenames.find(currentEditor));
 
351
            if (!file.open(QFile::WriteOnly))
 
352
                return;
 
353
            QTextStream ts(&amp;file);
 
354
            ts.setCodec(QTextCodec::codecForName(&quot;UTF-8&quot;));
 
355
            ts &lt;&lt; currentEditor-&gt;document()-&gt;toHtml(&quot;UTF-8&quot;);
 
356
            currentEditor-&gt;document()-&gt;setModified(false);
 
357
        }
 
358
    }
 
359
 
 
360
    void TextEdit::fileSaveAs()
 
361
    {
 
362
        if (!currentEditor)
 
363
            return;
 
364
        QString fn = QFileDialog::getSaveFileName(this, tr(&quot;Save as...&quot;),
 
365
                                                  QString(), tr(&quot;HTML-Files (*.htm *.html);;All Files (*)&quot;));
 
366
        if (!fn.isEmpty()) {
 
367
            filenames.insert(currentEditor, fn);
 
368
            fileSave();
 
369
            tabWidget-&gt;setTabText(tabWidget-&gt;indexOf(currentEditor), QFileInfo(fn).fileName());
 
370
        }
 
371
    }
 
372
 
 
373
    void TextEdit::filePrint()
 
374
    {
 
375
        if (!currentEditor)
 
376
            return;
 
377
    #ifndef QT_NO_PRINTER
 
378
        QPrinter printer(QPrinter::HighResolution);
 
379
        printer.setFullPage(true);
 
380
 
 
381
        QPrintDialog *dlg = new QPrintDialog(&amp;printer, this);
 
382
        if (dlg-&gt;exec() == QDialog::Accepted) {
 
383
            currentEditor-&gt;document()-&gt;print(&amp;printer);
 
384
        }
 
385
        delete dlg;
 
386
    #endif
 
387
    }
 
388
 
 
389
    void TextEdit::fileClose()
 
390
    {
 
391
        const bool hadFocus = (currentEditor &amp;&amp; currentEditor-&gt;hasFocus());
 
392
        delete currentEditor;
 
393
        currentEditor = qobject_cast&lt;QTextEdit *&gt;(tabWidget-&gt;currentWidget());
 
394
        if (currentEditor &amp;&amp; hadFocus)
 
395
            currentEditor-&gt;setFocus();
 
396
    }
 
397
 
 
398
    void TextEdit::fileExit()
 
399
    {
 
400
        qApp-&gt;quit();
 
401
    }
 
402
 
 
403
    void TextEdit::textBold()
 
404
    {
 
405
        if (!currentEditor)
 
406
            return;
 
407
        currentEditor-&gt;setFontWeight(actionTextBold-&gt;isChecked() ? QFont::Bold : QFont::Normal);
 
408
    }
 
409
 
 
410
    void TextEdit::textUnderline()
 
411
    {
 
412
        if (!currentEditor)
 
413
            return;
 
414
        currentEditor-&gt;setFontUnderline(actionTextUnderline-&gt;isChecked());
 
415
    }
 
416
 
 
417
    void TextEdit::textItalic()
 
418
    {
 
419
        if (!currentEditor)
 
420
            return;
 
421
        currentEditor-&gt;setFontItalic(actionTextItalic-&gt;isChecked());
 
422
    }
 
423
 
 
424
    void TextEdit::textFamily(const QString &amp;f)
 
425
    {
 
426
        if (!currentEditor)
 
427
            return;
 
428
        currentEditor-&gt;setFontFamily(f);
 
429
    }
 
430
 
 
431
    void TextEdit::textSize(const QString &amp;p)
 
432
    {
 
433
        if (!currentEditor)
 
434
            return;
 
435
        currentEditor-&gt;setFontPointSize(p.toFloat());
 
436
    }
 
437
 
 
438
    void TextEdit::textStyle(int styleIndex)
 
439
    {
 
440
        if (!currentEditor)
 
441
            return;
 
442
 
 
443
        QTextCursor cursor = currentEditor-&gt;textCursor();
 
444
 
 
445
        if (styleIndex != 0) {
 
446
            QTextListFormat::Style style = QTextListFormat::ListDisc;
 
447
 
 
448
            switch (styleIndex) {
 
449
                default:
 
450
                case 1:
 
451
                    style = QTextListFormat::ListDisc;
 
452
                    break;
 
453
                case 2:
 
454
                    style = QTextListFormat::ListCircle;
 
455
                    break;
 
456
                case 3:
 
457
                    style = QTextListFormat::ListSquare;
 
458
                    break;
 
459
                case 4:
 
460
                    style = QTextListFormat::ListDecimal;
 
461
                    break;
 
462
                case 5:
 
463
                    style = QTextListFormat::ListLowerAlpha;
 
464
                    break;
 
465
                case 6:
 
466
                    style = QTextListFormat::ListUpperAlpha;
 
467
                    break;
 
468
            }
 
469
 
 
470
            cursor.beginEditBlock();
 
471
 
 
472
            QTextBlockFormat blockFmt = cursor.blockFormat();
 
473
 
 
474
            QTextListFormat listFmt;
 
475
 
 
476
            if (cursor.currentList()) {
 
477
                listFmt = cursor.currentList()-&gt;format();
 
478
            } else {
 
479
                listFmt.setIndent(blockFmt.indent() + 1);
 
480
                blockFmt.setIndent(0);
 
481
                cursor.setBlockFormat(blockFmt);
 
482
            }
 
483
 
 
484
            listFmt.setStyle(style);
 
485
 
 
486
            cursor.createList(listFmt);
 
487
 
 
488
            cursor.endEditBlock();
 
489
        } else {
 
490
            // ####
 
491
            QTextBlockFormat bfmt;
 
492
            bfmt.setObjectIndex(-1);
 
493
            cursor.mergeBlockFormat(bfmt);
 
494
        }
 
495
    }
 
496
 
 
497
    void TextEdit::textColor()
 
498
    {
 
499
        if (!currentEditor)
 
500
            return;
 
501
        QColor col = QColorDialog::getColor(currentEditor-&gt;textColor(), this);
 
502
        if (!col.isValid())
 
503
            return;
 
504
        currentEditor-&gt;setTextColor(col);
 
505
        colorChanged(col);
 
506
    }
 
507
 
 
508
    void TextEdit::textAlign(QAction *a)
 
509
    {
 
510
        if (!currentEditor)
 
511
            return;
 
512
        if (a == actionAlignLeft)
 
513
            currentEditor-&gt;setAlignment(Qt::AlignLeft);
 
514
        else if (a == actionAlignCenter)
 
515
            currentEditor-&gt;setAlignment(Qt::AlignHCenter);
 
516
        else if (a == actionAlignRight)
 
517
            currentEditor-&gt;setAlignment(Qt::AlignRight);
 
518
        else if (a == actionAlignJustify)
 
519
            currentEditor-&gt;setAlignment(Qt::AlignJustify);
 
520
    }
 
521
 
 
522
    void TextEdit::currentCharFormatChanged(const QTextCharFormat &amp;format)
 
523
    {
 
524
        fontChanged(format.font());
 
525
        colorChanged(format.foreground().color());
 
526
        alignmentChanged(currentEditor-&gt;alignment());
 
527
    }
 
528
 
 
529
    void TextEdit::clipboardDataChanged()
 
530
    {
 
531
        actionPaste-&gt;setEnabled(!QApplication::clipboard()-&gt;text().isEmpty());
 
532
    }
 
533
 
 
534
    void TextEdit::fontChanged(const QFont &amp;f)
 
535
    {
 
536
        comboFont-&gt;setCurrentIndex(comboFont-&gt;findText(f.family()));
 
537
        comboSize-&gt;setCurrentIndex(comboSize-&gt;findText(QString::number(f.pointSize())));
 
538
        actionTextBold-&gt;setChecked(f.bold());
 
539
        actionTextItalic-&gt;setChecked(f.italic());
 
540
        actionTextUnderline-&gt;setChecked(f.underline());
 
541
    }
 
542
 
 
543
    void TextEdit::colorChanged(const QColor &amp;c)
 
544
    {
 
545
        QPixmap pix(16, 16);
 
546
        pix.fill(c);
 
547
        actionTextColor-&gt;setIcon(pix);
 
548
    }
 
549
 
 
550
    void TextEdit::alignmentChanged(Qt::Alignment a)
 
551
    {
 
552
        if (a &amp; Qt::AlignLeft)
 
553
            actionAlignLeft-&gt;setChecked(true);
 
554
        else if (a &amp; Qt::AlignHCenter)
 
555
            actionAlignCenter-&gt;setChecked(true);
 
556
        else if (a &amp; Qt::AlignRight)
 
557
            actionAlignRight-&gt;setChecked(true);
 
558
        else if (a &amp; Qt::AlignJustify)
 
559
            actionAlignJustify-&gt;setChecked(true);
 
560
    }
 
561
 
 
562
    void TextEdit::editorChanged()
 
563
    {
 
564
        if (currentEditor) {
 
565
            disconnect(currentEditor-&gt;document(), SIGNAL(modificationChanged(bool)),
 
566
                       actionSave, SLOT(setEnabled(bool)));
 
567
            disconnect(currentEditor-&gt;document(), SIGNAL(modificationChanged(bool)),
 
568
                       this, SLOT(setWindowModified(bool)));
 
569
            disconnect(currentEditor-&gt;document(), SIGNAL(undoAvailable(bool)),
 
570
                       actionUndo, SLOT(setEnabled(bool)));
 
571
            disconnect(currentEditor-&gt;document(), SIGNAL(redoAvailable(bool)),
 
572
                       actionRedo, SLOT(setEnabled(bool)));
 
573
 
 
574
            disconnect(actionUndo, SIGNAL(triggered()), currentEditor-&gt;document(), SLOT(undo()));
 
575
            disconnect(actionRedo, SIGNAL(triggered()), currentEditor-&gt;document(), SLOT(redo()));
 
576
 
 
577
            disconnect(actionCut, SIGNAL(triggered()), currentEditor, SLOT(cut()));
 
578
            disconnect(actionCopy, SIGNAL(triggered()), currentEditor, SLOT(copy()));
 
579
            disconnect(actionPaste, SIGNAL(triggered()), currentEditor, SLOT(paste()));
 
580
 
 
581
            disconnect(currentEditor, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
 
582
            disconnect(currentEditor, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
 
583
 
 
584
        }
 
585
 
 
586
        currentEditor = qobject_cast&lt;QTextEdit *&gt;(tabWidget-&gt;currentWidget());
 
587
        if (!currentEditor)
 
588
            return;
 
589
 
 
590
        fontChanged(currentEditor-&gt;font());
 
591
        colorChanged(currentEditor-&gt;textColor());
 
592
        alignmentChanged(currentEditor-&gt;alignment());
 
593
 
 
594
        connect(currentEditor-&gt;document(), SIGNAL(modificationChanged(bool)),
 
595
                actionSave, SLOT(setEnabled(bool)));
 
596
        connect(currentEditor-&gt;document(), SIGNAL(modificationChanged(bool)),
 
597
                this, SLOT(setWindowModified(bool)));
 
598
        connect(currentEditor-&gt;document(), SIGNAL(undoAvailable(bool)),
 
599
                actionUndo, SLOT(setEnabled(bool)));
 
600
        connect(currentEditor-&gt;document(), SIGNAL(redoAvailable(bool)),
 
601
                actionRedo, SLOT(setEnabled(bool)));
 
602
 
 
603
        setWindowModified(currentEditor-&gt;document()-&gt;isModified());
 
604
        actionSave-&gt;setEnabled(currentEditor-&gt;document()-&gt;isModified());
 
605
        actionUndo-&gt;setEnabled(currentEditor-&gt;document()-&gt;isUndoAvailable());
 
606
        actionRedo-&gt;setEnabled(currentEditor-&gt;document()-&gt;isRedoAvailable());
 
607
 
 
608
        connect(actionUndo, SIGNAL(triggered()), currentEditor-&gt;document(), SLOT(undo()));
 
609
        connect(actionRedo, SIGNAL(triggered()), currentEditor-&gt;document(), SLOT(redo()));
 
610
 
 
611
        const bool selection = currentEditor-&gt;textCursor().hasSelection();
 
612
        actionCut-&gt;setEnabled(selection);
 
613
        actionCopy-&gt;setEnabled(selection);
 
614
 
 
615
        connect(actionCut, SIGNAL(triggered()), currentEditor, SLOT(cut()));
 
616
        connect(actionCopy, SIGNAL(triggered()), currentEditor, SLOT(copy()));
 
617
        connect(actionPaste, SIGNAL(triggered()), currentEditor, SLOT(paste()));
 
618
 
 
619
        connect(currentEditor, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
 
620
        connect(currentEditor, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
 
621
    }
 
622
 
 
623
    QTextEdit *TextEdit::createNewEditor(const QString &amp;title)
 
624
    {
 
625
        QTextEdit *edit = new QTextEdit;
 
626
        connect(edit, SIGNAL(currentCharFormatChanged(const QTextCharFormat &amp;)),
 
627
                this, SLOT(currentCharFormatChanged(const QTextCharFormat &amp;)));
 
628
 
 
629
        int tab = tabWidget-&gt;addTab(edit, title.isEmpty() ? tr(&quot;noname&quot;) : title);
 
630
        tabWidget-&gt;setCurrentIndex(tab);
 
631
        edit-&gt;setFocus();
 
632
 
 
633
        return edit;
 
634
    }</pre>
 
635
<p /><address><hr /><div align="center">
 
636
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
637
<td width="30%">Copyright &copy; 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>
 
641
</html>