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

« back to all changes in this revision

Viewing changes to demos/textedit/textedit.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 demonstration applications 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 "textedit.h"
 
30
 
 
31
#include <qaction.h>
 
32
#include <qapplication.h>
 
33
#include <qclipboard.h>
 
34
#include <qcolordialog.h>
 
35
#include <qcombobox.h>
 
36
#include <qdebug.h>
 
37
#include <qfile.h>
 
38
#include <qfiledialog.h>
 
39
#include <qfileinfo.h>
 
40
#include <qfontdatabase.h>
 
41
#include <qlineedit.h>
 
42
#include <qlist.h>
 
43
#include <qmenu.h>
 
44
#include <qmenubar.h>
 
45
#include <qpainter.h>
 
46
#include <qprintdialog.h>
 
47
#include <qprinter.h>
 
48
#include <qtabwidget.h>
 
49
#include <qtextcodec.h>
 
50
#include <qtextedit.h>
 
51
#include <qtextdocumentfragment.h>
 
52
#include <qtextformat.h>
 
53
#include <qtoolbar.h>
 
54
#include <qtextcursor.h>
 
55
#include <qtextlist.h>
 
56
 
 
57
#include <limits.h>
 
58
 
 
59
#ifdef Q_WS_MAC
 
60
const QString rsrcPath = ":/images/mac";
 
61
#else
 
62
const QString rsrcPath = ":/images/win";
 
63
#endif
 
64
 
 
65
TextEdit::TextEdit(QWidget *parent)
 
66
    : QMainWindow(parent)
 
67
{
 
68
    setupFileActions();
 
69
    setupEditActions();
 
70
    setupTextActions();
 
71
 
 
72
    tabWidget = new QTabWidget(this);
 
73
    connect(tabWidget, SIGNAL(currentChanged(int)),
 
74
            this, SLOT(editorChanged()));
 
75
    setCentralWidget(tabWidget);
 
76
 
 
77
    connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
 
78
 
 
79
    if (qApp->argc() == 1) {
 
80
        if (!load("example.html"))
 
81
            fileNew();
 
82
    } else {
 
83
        for (int i = 1; i < qApp->argc(); ++i)
 
84
            load(qApp->argv()[i]);
 
85
    }
 
86
 
 
87
    setWindowTitle(tr("Rich Text [*]"));
 
88
}
 
89
 
 
90
void TextEdit::setupFileActions()
 
91
{
 
92
    QToolBar *tb = new QToolBar(this);
 
93
    tb->setWindowTitle(tr("File Actions"));
 
94
    addToolBar(tb);
 
95
 
 
96
    QMenu *menu = new QMenu(tr("&File"), this);
 
97
    menuBar()->addMenu(menu);
 
98
 
 
99
    QAction *a;
 
100
 
 
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()));
 
104
    tb->addAction(a);
 
105
    menu->addAction(a);
 
106
 
 
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()));
 
110
    tb->addAction(a);
 
111
    menu->addAction(a);
 
112
 
 
113
    menu->addSeparator();
 
114
 
 
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);
 
119
    tb->addAction(a);
 
120
    menu->addAction(a);
 
121
 
 
122
    a = new QAction(tr("Save &As..."), this);
 
123
    connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
 
124
    menu->addAction(a);
 
125
    menu->addSeparator();
 
126
 
 
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()));
 
130
    tb->addAction(a);
 
131
    menu->addAction(a);
 
132
 
 
133
    a = new QAction(tr("&Close"), this);
 
134
    a->setShortcut(Qt::CTRL + Qt::Key_W);
 
135
    connect(a, SIGNAL(triggered()), this, SLOT(fileClose()));
 
136
    menu->addAction(a);
 
137
 
 
138
    a = new QAction(tr("E&xit"), this);
 
139
    a->setShortcut(Qt::CTRL + Qt::Key_Q);
 
140
    connect(a, SIGNAL(triggered()), this, SLOT(fileExit()));
 
141
    menu->addAction(a);
 
142
}
 
143
 
 
144
void TextEdit::setupEditActions()
 
145
{
 
146
    QToolBar *tb = new QToolBar(this);
 
147
    tb->setWindowTitle(tr("Edit Actions"));
 
148
    addToolBar(tb);
 
149
 
 
150
    QMenu *menu = new QMenu(tr("&Edit"), this);
 
151
    menuBar()->addMenu(menu);
 
152
 
 
153
    QAction *a;
 
154
    a = actionUndo = new QAction(QIcon(rsrcPath + "/editundo.png"), tr("&Undo"), this);
 
155
    a->setShortcut(Qt::CTRL + Qt::Key_Z);
 
156
    tb->addAction(a);
 
157
    menu->addAction(a);
 
158
    a = actionRedo = new QAction(QIcon(rsrcPath + "/editredo.png"), tr("&Redo"), this);
 
159
    a->setShortcut(Qt::CTRL + Qt::Key_Y);
 
160
    tb->addAction(a);
 
161
    menu->addAction(a);
 
162
    menu->addSeparator();
 
163
    a = actionCut = new QAction(QIcon(rsrcPath + "/editcut.png"), tr("Cu&t"), this);
 
164
    a->setShortcut(Qt::CTRL + Qt::Key_X);
 
165
    tb->addAction(a);
 
166
    menu->addAction(a);
 
167
    a = actionCopy = new QAction(QIcon(rsrcPath + "/editcopy.png"), tr("&Copy"), this);
 
168
    a->setShortcut(Qt::CTRL + Qt::Key_C);
 
169
    tb->addAction(a);
 
170
    menu->addAction(a);
 
171
    a = actionPaste = new QAction(QIcon(rsrcPath + "/editpaste.png"), tr("&Paste"), this);
 
172
    a->setShortcut(Qt::CTRL + Qt::Key_V);
 
173
    tb->addAction(a);
 
174
    menu->addAction(a);
 
175
    actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
 
176
}
 
177
 
 
178
void TextEdit::setupTextActions()
 
179
{
 
180
    QToolBar *tb = new QToolBar(this);
 
181
    tb->setWindowTitle(tr("Format Actions"));
 
182
    addToolBar(tb);
 
183
 
 
184
    QMenu *menu = new QMenu(tr("F&ormat"), this);
 
185
    menuBar()->addMenu(menu);
 
186
 
 
187
    actionTextBold = new QAction(QIcon(rsrcPath + "/textbold.png"), tr("&Bold"), this);
 
188
    actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
 
189
    QFont bold;
 
190
    bold.setBold(true);
 
191
    actionTextBold->setFont(bold);
 
192
    connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
 
193
    tb->addAction(actionTextBold);
 
194
    menu->addAction(actionTextBold);
 
195
    actionTextBold->setCheckable(true);
 
196
 
 
197
    actionTextItalic = new QAction(QIcon(rsrcPath + "/textitalic.png"), tr("&Italic"), this);
 
198
    actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
 
199
    QFont italic;
 
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);
 
206
 
 
207
    actionTextUnderline = new QAction(QIcon(rsrcPath + "/textunder.png"), tr("&Underline"), this);
 
208
    actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
 
209
    QFont underline;
 
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);
 
216
 
 
217
    menu->addSeparator();
 
218
 
 
219
    QActionGroup *grp = new QActionGroup(this);
 
220
    connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(textAlign(QAction *)));
 
221
 
 
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);
 
234
 
 
235
    tb->addActions(grp->actions());
 
236
    menu->addActions(grp->actions());
 
237
 
 
238
    menu->addSeparator();
 
239
 
 
240
    QPixmap pix(16, 16);
 
241
    pix.fill(Qt::black);
 
242
    actionTextColor = new QAction(pix, tr("&Color..."), this);
 
243
    connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
 
244
    tb->addAction(actionTextColor);
 
245
    menu->addAction(actionTextColor);
 
246
 
 
247
 
 
248
    tb = new QToolBar(this);
 
249
    tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
 
250
    tb->setWindowTitle(tr("Format Actions"));
 
251
    addToolBarBreak(Qt::TopToolBarArea);
 
252
    addToolBar(tb);
 
253
 
 
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)));
 
265
 
 
266
    comboFont = new QComboBox(tb);
 
267
    tb->addWidget(comboFont);
 
268
    comboFont->setEditable(true);
 
269
    QFontDatabase db;
 
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()));
 
274
 
 
275
    comboSize = new QComboBox(tb);
 
276
    comboSize->setObjectName("comboSize");
 
277
    tb->addWidget(comboSize);
 
278
    comboSize->setEditable(true);
 
279
 
 
280
    foreach(int size, db.standardSizes())
 
281
        comboSize->addItem(QString::number(size));
 
282
 
 
283
    connect(comboSize, SIGNAL(activated(const QString &)),
 
284
            this, SLOT(textSize(const QString &)));
 
285
    comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font()
 
286
                                                                   .pointSize())));
 
287
}
 
288
 
 
289
bool TextEdit::load(const QString &f)
 
290
{
 
291
    if (!QFile::exists(f))
 
292
        return false;
 
293
    QTextEdit *edit = createNewEditor(QFileInfo(f).fileName());
 
294
    QFile file(f);
 
295
    if (!file.open(QFile::ReadOnly))
 
296
        return false;
 
297
 
 
298
    QByteArray data = file.readAll();
 
299
    QTextCodec *codec = Qt::codecForHtml(data);
 
300
    QString str = codec->toUnicode(data);
 
301
    if (Qt::mightBeRichText(str)) {
 
302
        edit->setHtml(str);
 
303
    } else {
 
304
        str = QString::fromLocal8Bit(data);
 
305
        edit->setPlainText(str);
 
306
    }
 
307
 
 
308
    filenames.insert(edit, f);
 
309
    return true;
 
310
}
 
311
 
 
312
void TextEdit::fileNew()
 
313
{
 
314
    createNewEditor();
 
315
}
 
316
 
 
317
void TextEdit::fileOpen()
 
318
{
 
319
    QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
 
320
                                              QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
 
321
    if (!fn.isEmpty())
 
322
        load(fn);
 
323
}
 
324
 
 
325
void TextEdit::fileSave()
 
326
{
 
327
    if (!currentEditor)
 
328
        return;
 
329
    if (filenames.find(currentEditor) == filenames.end()) {
 
330
        fileSaveAs();
 
331
    } else {
 
332
        QFile file(*filenames.find(currentEditor));
 
333
        if (!file.open(QFile::WriteOnly))
 
334
            return;
 
335
        QTextStream ts(&file);
 
336
        ts.setCodec(QTextCodec::codecForName("UTF-8"));
 
337
        ts << currentEditor->document()->toHtml("UTF-8");
 
338
        currentEditor->document()->setModified(false);
 
339
    }
 
340
}
 
341
 
 
342
void TextEdit::fileSaveAs()
 
343
{
 
344
    if (!currentEditor)
 
345
        return;
 
346
    QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
 
347
                                              QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
 
348
    if (!fn.isEmpty()) {
 
349
        filenames.insert(currentEditor, fn);
 
350
        fileSave();
 
351
        tabWidget->setTabText(tabWidget->indexOf(currentEditor), QFileInfo(fn).fileName());
 
352
    }
 
353
}
 
354
 
 
355
void TextEdit::filePrint()
 
356
{
 
357
    if (!currentEditor)
 
358
        return;
 
359
#ifndef QT_NO_PRINTER
 
360
    QPrinter printer(QPrinter::HighResolution);
 
361
    printer.setFullPage(true);
 
362
 
 
363
    QPrintDialog *dlg = new QPrintDialog(&printer, this);
 
364
    if (dlg->exec() == QDialog::Accepted) {
 
365
        currentEditor->document()->print(&printer);
 
366
    }
 
367
    delete dlg;
 
368
#endif
 
369
}
 
370
 
 
371
void TextEdit::fileClose()
 
372
{
 
373
    const bool hadFocus = (currentEditor && currentEditor->hasFocus());
 
374
    delete currentEditor;
 
375
    currentEditor = qobject_cast<QTextEdit *>(tabWidget->currentWidget());
 
376
    if (currentEditor && hadFocus)
 
377
        currentEditor->setFocus();
 
378
}
 
379
 
 
380
void TextEdit::fileExit()
 
381
{
 
382
    qApp->quit();
 
383
}
 
384
 
 
385
void TextEdit::textBold()
 
386
{
 
387
    if (!currentEditor)
 
388
        return;
 
389
    currentEditor->setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
 
390
}
 
391
 
 
392
void TextEdit::textUnderline()
 
393
{
 
394
    if (!currentEditor)
 
395
        return;
 
396
    currentEditor->setFontUnderline(actionTextUnderline->isChecked());
 
397
}
 
398
 
 
399
void TextEdit::textItalic()
 
400
{
 
401
    if (!currentEditor)
 
402
        return;
 
403
    currentEditor->setFontItalic(actionTextItalic->isChecked());
 
404
}
 
405
 
 
406
void TextEdit::textFamily(const QString &f)
 
407
{
 
408
    if (!currentEditor)
 
409
        return;
 
410
    currentEditor->setFontFamily(f);
 
411
}
 
412
 
 
413
void TextEdit::textSize(const QString &p)
 
414
{
 
415
    if (!currentEditor)
 
416
        return;
 
417
    currentEditor->setFontPointSize(p.toFloat());
 
418
}
 
419
 
 
420
void TextEdit::textStyle(int styleIndex)
 
421
{
 
422
    if (!currentEditor)
 
423
        return;
 
424
 
 
425
    QTextCursor cursor = currentEditor->textCursor();
 
426
 
 
427
    if (styleIndex != 0) {
 
428
        QTextListFormat::Style style = QTextListFormat::ListDisc;
 
429
 
 
430
        switch (styleIndex) {
 
431
            default:
 
432
            case 1:
 
433
                style = QTextListFormat::ListDisc;
 
434
                break;
 
435
            case 2:
 
436
                style = QTextListFormat::ListCircle;
 
437
                break;
 
438
            case 3:
 
439
                style = QTextListFormat::ListSquare;
 
440
                break;
 
441
            case 4:
 
442
                style = QTextListFormat::ListDecimal;
 
443
                break;
 
444
            case 5:
 
445
                style = QTextListFormat::ListLowerAlpha;
 
446
                break;
 
447
            case 6:
 
448
                style = QTextListFormat::ListUpperAlpha;
 
449
                break;
 
450
        }
 
451
 
 
452
        cursor.beginEditBlock();
 
453
 
 
454
        QTextBlockFormat blockFmt = cursor.blockFormat();
 
455
 
 
456
        QTextListFormat listFmt;
 
457
 
 
458
        if (cursor.currentList()) {
 
459
            listFmt = cursor.currentList()->format();
 
460
        } else {
 
461
            listFmt.setIndent(blockFmt.indent() + 1);
 
462
            blockFmt.setIndent(0);
 
463
            cursor.setBlockFormat(blockFmt);
 
464
        }
 
465
 
 
466
        listFmt.setStyle(style);
 
467
 
 
468
        cursor.createList(listFmt);
 
469
 
 
470
        cursor.endEditBlock();
 
471
    } else {
 
472
        // ####
 
473
        QTextBlockFormat bfmt;
 
474
        bfmt.setObjectIndex(-1);
 
475
        cursor.mergeBlockFormat(bfmt);
 
476
    }
 
477
}
 
478
 
 
479
void TextEdit::textColor()
 
480
{
 
481
    if (!currentEditor)
 
482
        return;
 
483
    QColor col = QColorDialog::getColor(currentEditor->textColor(), this);
 
484
    if (!col.isValid())
 
485
        return;
 
486
    currentEditor->setTextColor(col);
 
487
    colorChanged(col);
 
488
}
 
489
 
 
490
void TextEdit::textAlign(QAction *a)
 
491
{
 
492
    if (!currentEditor)
 
493
        return;
 
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);
 
502
}
 
503
 
 
504
void TextEdit::currentCharFormatChanged(const QTextCharFormat &format)
 
505
{
 
506
    fontChanged(format.font());
 
507
    colorChanged(format.foreground().color());
 
508
    alignmentChanged(currentEditor->alignment());
 
509
}
 
510
 
 
511
void TextEdit::clipboardDataChanged()
 
512
{
 
513
    actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
 
514
}
 
515
 
 
516
void TextEdit::fontChanged(const QFont &f)
 
517
{
 
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());
 
523
}
 
524
 
 
525
void TextEdit::colorChanged(const QColor &c)
 
526
{
 
527
    QPixmap pix(16, 16);
 
528
    pix.fill(c);
 
529
    actionTextColor->setIcon(pix);
 
530
}
 
531
 
 
532
void TextEdit::alignmentChanged(Qt::Alignment a)
 
533
{
 
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);
 
542
}
 
543
 
 
544
void TextEdit::editorChanged()
 
545
{
 
546
    if (currentEditor) {
 
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)));
 
555
 
 
556
        disconnect(actionUndo, SIGNAL(triggered()), currentEditor->document(), SLOT(undo()));
 
557
        disconnect(actionRedo, SIGNAL(triggered()), currentEditor->document(), SLOT(redo()));
 
558
 
 
559
        disconnect(actionCut, SIGNAL(triggered()), currentEditor, SLOT(cut()));
 
560
        disconnect(actionCopy, SIGNAL(triggered()), currentEditor, SLOT(copy()));
 
561
        disconnect(actionPaste, SIGNAL(triggered()), currentEditor, SLOT(paste()));
 
562
 
 
563
        disconnect(currentEditor, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
 
564
        disconnect(currentEditor, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
 
565
 
 
566
    }
 
567
 
 
568
    currentEditor = qobject_cast<QTextEdit *>(tabWidget->currentWidget());
 
569
    if (!currentEditor)
 
570
        return;
 
571
 
 
572
    fontChanged(currentEditor->font());
 
573
    colorChanged(currentEditor->textColor());
 
574
    alignmentChanged(currentEditor->alignment());
 
575
 
 
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)));
 
584
 
 
585
    setWindowModified(currentEditor->document()->isModified());
 
586
    actionSave->setEnabled(currentEditor->document()->isModified());
 
587
    actionUndo->setEnabled(currentEditor->document()->isUndoAvailable());
 
588
    actionRedo->setEnabled(currentEditor->document()->isRedoAvailable());
 
589
 
 
590
    connect(actionUndo, SIGNAL(triggered()), currentEditor->document(), SLOT(undo()));
 
591
    connect(actionRedo, SIGNAL(triggered()), currentEditor->document(), SLOT(redo()));
 
592
 
 
593
    const bool selection = currentEditor->textCursor().hasSelection();
 
594
    actionCut->setEnabled(selection);
 
595
    actionCopy->setEnabled(selection);
 
596
 
 
597
    connect(actionCut, SIGNAL(triggered()), currentEditor, SLOT(cut()));
 
598
    connect(actionCopy, SIGNAL(triggered()), currentEditor, SLOT(copy()));
 
599
    connect(actionPaste, SIGNAL(triggered()), currentEditor, SLOT(paste()));
 
600
 
 
601
    connect(currentEditor, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
 
602
    connect(currentEditor, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
 
603
}
 
604
 
 
605
QTextEdit *TextEdit::createNewEditor(const QString &title)
 
606
{
 
607
    QTextEdit *edit = new QTextEdit;
 
608
    connect(edit, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
 
609
            this, SLOT(currentCharFormatChanged(const QTextCharFormat &)));
 
610
 
 
611
    int tab = tabWidget->addTab(edit, title.isEmpty() ? tr("noname") : title);
 
612
    tabWidget->setCurrentIndex(tab);
 
613
    edit->setFocus();
 
614
 
 
615
    return edit;
 
616
}