1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "textedit.h"
43
44 #include <QAction>
45 #include <QApplication>
46 #include <QClipboard>
47 #include <QColorDialog>
48 #include <QComboBox>
49 #include <QFontComboBox>
50 #include <QFile>
51 #include <QFileDialog>
52 #include <QFileInfo>
53 #include <QFontDatabase>
54 #include <QMenu>
55 #include <QMenuBar>
56 #include <QPrintDialog>
57 #include <QPrinter>
58 #include <QTextCodec>
59 #include <QTextEdit>
60 #include <QToolBar>
61 #include <QTextCursor>
62 #include <QTextDocumentWriter>
63 #include <QTextList>
64 #include <QtDebug>
65 #include <QCloseEvent>
66 #include <QMessageBox>
67 #include <QPrintPreviewDialog>
68
69 #ifdef Q_WS_MAC
70 const QString rsrcPath = ":/images/mac";
71 #else
72 const QString rsrcPath = ":/images/win";
73 #endif
74
TextEdit(QWidget * parent)75 TextEdit::TextEdit(QWidget *parent)
76 : QMainWindow(parent)
77 {
78 setToolButtonStyle(Qt::ToolButtonFollowStyle);
79 setupFileActions();
80 setupEditActions();
81 setupTextActions();
82
83 {
84 QMenu *helpMenu = new QMenu(tr("Help"), this);
85 menuBar()->addMenu(helpMenu);
86 helpMenu->addAction(tr("About"), this, SLOT(about()));
87 helpMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt()));
88 }
89
90 textEdit = new QTextEdit(this);
91 connect(textEdit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
92 this, SLOT(currentCharFormatChanged(QTextCharFormat)));
93 connect(textEdit, SIGNAL(cursorPositionChanged()),
94 this, SLOT(cursorPositionChanged()));
95
96 setCentralWidget(textEdit);
97 textEdit->setFocus();
98 setCurrentFileName(QString());
99
100 fontChanged(textEdit->font());
101 colorChanged(textEdit->textColor());
102 alignmentChanged(textEdit->alignment());
103
104 connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
105 actionSave, SLOT(setEnabled(bool)));
106 connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
107 this, SLOT(setWindowModified(bool)));
108 connect(textEdit->document(), SIGNAL(undoAvailable(bool)),
109 actionUndo, SLOT(setEnabled(bool)));
110 connect(textEdit->document(), SIGNAL(redoAvailable(bool)),
111 actionRedo, SLOT(setEnabled(bool)));
112
113 setWindowModified(textEdit->document()->isModified());
114 actionSave->setEnabled(textEdit->document()->isModified());
115 actionUndo->setEnabled(textEdit->document()->isUndoAvailable());
116 actionRedo->setEnabled(textEdit->document()->isRedoAvailable());
117
118 connect(actionUndo, SIGNAL(triggered()), textEdit, SLOT(undo()));
119 connect(actionRedo, SIGNAL(triggered()), textEdit, SLOT(redo()));
120
121 actionCut->setEnabled(false);
122 actionCopy->setEnabled(false);
123
124 connect(actionCut, SIGNAL(triggered()), textEdit, SLOT(cut()));
125 connect(actionCopy, SIGNAL(triggered()), textEdit, SLOT(copy()));
126 connect(actionPaste, SIGNAL(triggered()), textEdit, SLOT(paste()));
127
128 connect(textEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
129 connect(textEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
130
131 #ifndef QT_NO_CLIPBOARD
132 connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
133 #endif
134
135 QString initialFile = ":/example.html";
136 const QStringList args = QCoreApplication::arguments();
137 if (args.count() == 2)
138 initialFile = args.at(1);
139
140 if (!load(initialFile))
141 fileNew();
142 }
143
closeEvent(QCloseEvent * e)144 void TextEdit::closeEvent(QCloseEvent *e)
145 {
146 if (maybeSave())
147 e->accept();
148 else
149 e->ignore();
150 }
151
setupFileActions()152 void TextEdit::setupFileActions()
153 {
154 QToolBar *tb = new QToolBar(this);
155 tb->setWindowTitle(tr("File Actions"));
156 addToolBar(tb);
157
158 QMenu *menu = new QMenu(tr("&File"), this);
159 menuBar()->addMenu(menu);
160
161 QAction *a;
162
163 QIcon newIcon = QIcon::fromTheme("document-new", QIcon(rsrcPath + "/filenew.png"));
164 a = new QAction( newIcon, tr("&New"), this);
165 a->setPriority(QAction::LowPriority);
166 a->setShortcut(QKeySequence::New);
167 connect(a, SIGNAL(triggered()), this, SLOT(fileNew()));
168 tb->addAction(a);
169 menu->addAction(a);
170
171 a = new QAction(QIcon::fromTheme("document-open", QIcon(rsrcPath + "/fileopen.png")),
172 tr("&Open..."), this);
173 a->setShortcut(QKeySequence::Open);
174 connect(a, SIGNAL(triggered()), this, SLOT(fileOpen()));
175 tb->addAction(a);
176 menu->addAction(a);
177
178 menu->addSeparator();
179
180 actionSave = a = new QAction(QIcon::fromTheme("document-save", QIcon(rsrcPath + "/filesave.png")),
181 tr("&Save"), this);
182 a->setShortcut(QKeySequence::Save);
183 connect(a, SIGNAL(triggered()), this, SLOT(fileSave()));
184 a->setEnabled(false);
185 tb->addAction(a);
186 menu->addAction(a);
187
188 a = new QAction(tr("Save &As..."), this);
189 a->setPriority(QAction::LowPriority);
190 connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
191 menu->addAction(a);
192 menu->addSeparator();
193
194 #ifndef QT_NO_PRINTER
195 a = new QAction(QIcon::fromTheme("document-print", QIcon(rsrcPath + "/fileprint.png")),
196 tr("&Print..."), this);
197 a->setPriority(QAction::LowPriority);
198 a->setShortcut(QKeySequence::Print);
199 connect(a, SIGNAL(triggered()), this, SLOT(filePrint()));
200 tb->addAction(a);
201 menu->addAction(a);
202
203 a = new QAction(QIcon::fromTheme("fileprint", QIcon(rsrcPath + "/fileprint.png")),
204 tr("Print Preview..."), this);
205 connect(a, SIGNAL(triggered()), this, SLOT(filePrintPreview()));
206 menu->addAction(a);
207
208 a = new QAction(QIcon::fromTheme("exportpdf", QIcon(rsrcPath + "/exportpdf.png")),
209 tr("&Export PDF..."), this);
210 a->setPriority(QAction::LowPriority);
211 a->setShortcut(Qt::CTRL + Qt::Key_D);
212 connect(a, SIGNAL(triggered()), this, SLOT(filePrintPdf()));
213 tb->addAction(a);
214 menu->addAction(a);
215
216 menu->addSeparator();
217 #endif
218
219 a = new QAction(tr("&Quit"), this);
220 a->setShortcut(Qt::CTRL + Qt::Key_Q);
221 connect(a, SIGNAL(triggered()), this, SLOT(close()));
222 menu->addAction(a);
223 }
224
setupEditActions()225 void TextEdit::setupEditActions()
226 {
227 QToolBar *tb = new QToolBar(this);
228 tb->setWindowTitle(tr("Edit Actions"));
229 addToolBar(tb);
230 QMenu *menu = new QMenu(tr("&Edit"), this);
231 menuBar()->addMenu(menu);
232
233 QAction *a;
234 a = actionUndo = new QAction(QIcon::fromTheme("edit-undo", QIcon(rsrcPath + "/editundo.png")),
235 tr("&Undo"), this);
236 a->setShortcut(QKeySequence::Undo);
237 tb->addAction(a);
238 menu->addAction(a);
239 a = actionRedo = new QAction(QIcon::fromTheme("edit-redo", QIcon(rsrcPath + "/editredo.png")),
240 tr("&Redo"), this);
241 a->setPriority(QAction::LowPriority);
242 a->setShortcut(QKeySequence::Redo);
243 tb->addAction(a);
244 menu->addAction(a);
245 menu->addSeparator();
246 a = actionCut = new QAction(QIcon::fromTheme("edit-cut", QIcon(rsrcPath + "/editcut.png")),
247 tr("Cu&t"), this);
248 a->setPriority(QAction::LowPriority);
249 a->setShortcut(QKeySequence::Cut);
250 tb->addAction(a);
251 menu->addAction(a);
252 a = actionCopy = new QAction(QIcon::fromTheme("edit-copy", QIcon(rsrcPath + "/editcopy.png")),
253 tr("&Copy"), this);
254 a->setPriority(QAction::LowPriority);
255 a->setShortcut(QKeySequence::Copy);
256 tb->addAction(a);
257 menu->addAction(a);
258 a = actionPaste = new QAction(QIcon::fromTheme("edit-paste", QIcon(rsrcPath + "/editpaste.png")),
259 tr("&Paste"), this);
260 a->setPriority(QAction::LowPriority);
261 a->setShortcut(QKeySequence::Paste);
262 tb->addAction(a);
263 menu->addAction(a);
264 #ifndef QT_NO_CLIPBOARD
265 if (const QMimeData *md = QApplication::clipboard()->mimeData())
266 actionPaste->setEnabled(md->hasText());
267 #endif
268 }
269
setupTextActions()270 void TextEdit::setupTextActions()
271 {
272 QToolBar *tb = new QToolBar(this);
273 tb->setWindowTitle(tr("Format Actions"));
274 addToolBar(tb);
275
276 QMenu *menu = new QMenu(tr("F&ormat"), this);
277 menuBar()->addMenu(menu);
278
279 actionTextBold = new QAction(QIcon::fromTheme("format-text-bold", QIcon(rsrcPath + "/textbold.png")),
280 tr("&Bold"), this);
281 actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
282 actionTextBold->setPriority(QAction::LowPriority);
283 QFont bold;
284 bold.setBold(true);
285 actionTextBold->setFont(bold);
286 connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
287 tb->addAction(actionTextBold);
288 menu->addAction(actionTextBold);
289 actionTextBold->setCheckable(true);
290
291 actionTextItalic = new QAction(QIcon::fromTheme("format-text-italic", QIcon(rsrcPath + "/textitalic.png")),
292 tr("&Italic"), this);
293 actionTextItalic->setPriority(QAction::LowPriority);
294 actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
295 QFont italic;
296 italic.setItalic(true);
297 actionTextItalic->setFont(italic);
298 connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic()));
299 tb->addAction(actionTextItalic);
300 menu->addAction(actionTextItalic);
301 actionTextItalic->setCheckable(true);
302
303 actionTextUnderline = new QAction(QIcon::fromTheme("format-text-underline", QIcon(rsrcPath + "/textunder.png")),
304 tr("&Underline"), this);
305 actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
306 actionTextUnderline->setPriority(QAction::LowPriority);
307 QFont underline;
308 underline.setUnderline(true);
309 actionTextUnderline->setFont(underline);
310 connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline()));
311 tb->addAction(actionTextUnderline);
312 menu->addAction(actionTextUnderline);
313 actionTextUnderline->setCheckable(true);
314
315 menu->addSeparator();
316
317 QActionGroup *grp = new QActionGroup(this);
318 connect(grp, SIGNAL(triggered(QAction*)), this, SLOT(textAlign(QAction*)));
319
320 // Make sure the alignLeft is always left of the alignRight
321 if (QApplication::isLeftToRight()) {
322 actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")),
323 tr("&Left"), grp);
324 actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp);
325 actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp);
326 } else {
327 actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp);
328 actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp);
329 actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")), tr("&Left"), grp);
330 }
331 actionAlignJustify = new QAction(QIcon::fromTheme("format-justify-fill", QIcon(rsrcPath + "/textjustify.png")), tr("&Justify"), grp);
332
333 actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L);
334 actionAlignLeft->setCheckable(true);
335 actionAlignLeft->setPriority(QAction::LowPriority);
336 actionAlignCenter->setShortcut(Qt::CTRL + Qt::Key_E);
337 actionAlignCenter->setCheckable(true);
338 actionAlignCenter->setPriority(QAction::LowPriority);
339 actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R);
340 actionAlignRight->setCheckable(true);
341 actionAlignRight->setPriority(QAction::LowPriority);
342 actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J);
343 actionAlignJustify->setCheckable(true);
344 actionAlignJustify->setPriority(QAction::LowPriority);
345
346 tb->addActions(grp->actions());
347 menu->addActions(grp->actions());
348
349 menu->addSeparator();
350
351 QPixmap pix(16, 16);
352 pix.fill(Qt::black);
353 actionTextColor = new QAction(pix, tr("&Color..."), this);
354 connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
355 tb->addAction(actionTextColor);
356 menu->addAction(actionTextColor);
357
358
359 tb = new QToolBar(this);
360 tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
361 tb->setWindowTitle(tr("Format Actions"));
362 addToolBarBreak(Qt::TopToolBarArea);
363 addToolBar(tb);
364
365 comboStyle = new QComboBox(tb);
366 tb->addWidget(comboStyle);
367 comboStyle->addItem("Standard");
368 comboStyle->addItem("Bullet List (Disc)");
369 comboStyle->addItem("Bullet List (Circle)");
370 comboStyle->addItem("Bullet List (Square)");
371 comboStyle->addItem("Ordered List (Decimal)");
372 comboStyle->addItem("Ordered List (Alpha lower)");
373 comboStyle->addItem("Ordered List (Alpha upper)");
374 comboStyle->addItem("Ordered List (Roman lower)");
375 comboStyle->addItem("Ordered List (Roman upper)");
376 connect(comboStyle, SIGNAL(activated(int)),
377 this, SLOT(textStyle(int)));
378
379 comboFont = new QFontComboBox(tb);
380 tb->addWidget(comboFont);
381 connect(comboFont, SIGNAL(activated(QString)),
382 this, SLOT(textFamily(QString)));
383
384 comboSize = new QComboBox(tb);
385 comboSize->setObjectName("comboSize");
386 tb->addWidget(comboSize);
387 comboSize->setEditable(true);
388
389 QFontDatabase db;
390 foreach(int size, db.standardSizes())
391 comboSize->addItem(QString::number(size));
392
393 connect(comboSize, SIGNAL(activated(QString)),
394 this, SLOT(textSize(QString)));
395 comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font()
396 .pointSize())));
397 }
398
load(const QString & f)399 bool TextEdit::load(const QString &f)
400 {
401 if (!QFile::exists(f))
402 return false;
403 QFile file(f);
404 if (!file.open(QFile::ReadOnly))
405 return false;
406
407 QByteArray data = file.readAll();
408 QTextCodec *codec = Qt::codecForHtml(data);
409 QString str = codec->toUnicode(data);
410 if (Qt::mightBeRichText(str)) {
411 textEdit->setHtml(str);
412 } else {
413 str = QString::fromLocal8Bit(data);
414 textEdit->setPlainText(str);
415 }
416
417 setCurrentFileName(f);
418 return true;
419 }
420
maybeSave()421 bool TextEdit::maybeSave()
422 {
423 if (!textEdit->document()->isModified())
424 return true;
425 if (fileName.startsWith(QLatin1String(":/")))
426 return true;
427 QMessageBox::StandardButton ret;
428 ret = QMessageBox::warning(this, tr("Application"),
429 tr("The document has been modified.\n"
430 "Do you want to save your changes?"),
431 QMessageBox::Save | QMessageBox::Discard
432 | QMessageBox::Cancel);
433 if (ret == QMessageBox::Save)
434 return fileSave();
435 else if (ret == QMessageBox::Cancel)
436 return false;
437 return true;
438 }
439
setCurrentFileName(const QString & fileName)440 void TextEdit::setCurrentFileName(const QString &fileName)
441 {
442 this->fileName = fileName;
443 textEdit->document()->setModified(false);
444
445 QString shownName;
446 if (fileName.isEmpty())
447 shownName = "untitled.txt";
448 else
449 shownName = QFileInfo(fileName).fileName();
450
451 setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Rich Text")));
452 setWindowModified(false);
453 }
454
fileNew()455 void TextEdit::fileNew()
456 {
457 if (maybeSave()) {
458 textEdit->clear();
459 setCurrentFileName(QString());
460 }
461 }
462
fileOpen()463 void TextEdit::fileOpen()
464 {
465 QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
466 QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
467 if (!fn.isEmpty())
468 load(fn);
469 }
470
fileSave()471 bool TextEdit::fileSave()
472 {
473 if (fileName.isEmpty())
474 return fileSaveAs();
475
476 QTextDocumentWriter writer(fileName);
477 bool success = writer.write(textEdit->document());
478 if (success)
479 textEdit->document()->setModified(false);
480 return success;
481 }
482
fileSaveAs()483 bool TextEdit::fileSaveAs()
484 {
485 QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
486 QString(), tr("ODF files (*.odt);;HTML-Files (*.htm *.html);;All Files (*)"));
487 if (fn.isEmpty())
488 return false;
489 if (! (fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)) )
490 fn += ".odt"; // default
491 setCurrentFileName(fn);
492 return fileSave();
493 }
494
filePrint()495 void TextEdit::filePrint()
496 {
497 #ifndef QT_NO_PRINTER
498 QPrinter printer(QPrinter::HighResolution);
499 QPrintDialog *dlg = new QPrintDialog(&printer, this);
500 if (textEdit->textCursor().hasSelection())
501 dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
502 dlg->setWindowTitle(tr("Print Document"));
503 if (dlg->exec() == QDialog::Accepted) {
504 textEdit->print(&printer);
505 }
506 delete dlg;
507 #endif
508 }
509
filePrintPreview()510 void TextEdit::filePrintPreview()
511 {
512 #ifndef QT_NO_PRINTER
513 QPrinter printer(QPrinter::HighResolution);
514 QPrintPreviewDialog preview(&printer, this);
515 connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(printPreview(QPrinter*)));
516 preview.exec();
517 #endif
518 }
519
printPreview(QPrinter * printer)520 void TextEdit::printPreview(QPrinter *printer)
521 {
522 #ifdef QT_NO_PRINTER
523 Q_UNUSED(printer);
524 #else
525 textEdit->print(printer);
526 #endif
527 }
528
529
filePrintPdf()530 void TextEdit::filePrintPdf()
531 {
532 #ifndef QT_NO_PRINTER
533 //! [0]
534 QString fileName = QFileDialog::getSaveFileName(this, "Export PDF",
535 QString(), "*.pdf");
536 if (!fileName.isEmpty()) {
537 if (QFileInfo(fileName).suffix().isEmpty())
538 fileName.append(".pdf");
539 QPrinter printer(QPrinter::HighResolution);
540 printer.setOutputFormat(QPrinter::PdfFormat);
541 printer.setOutputFileName(fileName);
542 textEdit->document()->print(&printer);
543 }
544 //! [0]
545 #endif
546 }
547
textBold()548 void TextEdit::textBold()
549 {
550 QTextCharFormat fmt;
551 fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
552 mergeFormatOnWordOrSelection(fmt);
553 }
554
textUnderline()555 void TextEdit::textUnderline()
556 {
557 QTextCharFormat fmt;
558 fmt.setFontUnderline(actionTextUnderline->isChecked());
559 mergeFormatOnWordOrSelection(fmt);
560 }
561
textItalic()562 void TextEdit::textItalic()
563 {
564 QTextCharFormat fmt;
565 fmt.setFontItalic(actionTextItalic->isChecked());
566 mergeFormatOnWordOrSelection(fmt);
567 }
568
textFamily(const QString & f)569 void TextEdit::textFamily(const QString &f)
570 {
571 QTextCharFormat fmt;
572 fmt.setFontFamily(f);
573 mergeFormatOnWordOrSelection(fmt);
574 }
575
textSize(const QString & p)576 void TextEdit::textSize(const QString &p)
577 {
578 qreal pointSize = p.toFloat();
579 if (p.toFloat() > 0) {
580 QTextCharFormat fmt;
581 fmt.setFontPointSize(pointSize);
582 mergeFormatOnWordOrSelection(fmt);
583 }
584 }
585
textStyle(int styleIndex)586 void TextEdit::textStyle(int styleIndex)
587 {
588 QTextCursor cursor = textEdit->textCursor();
589
590 if (styleIndex != 0) {
591 QTextListFormat::Style style = QTextListFormat::ListDisc;
592
593 switch (styleIndex) {
594 default:
595 case 1:
596 style = QTextListFormat::ListDisc;
597 break;
598 case 2:
599 style = QTextListFormat::ListCircle;
600 break;
601 case 3:
602 style = QTextListFormat::ListSquare;
603 break;
604 case 4:
605 style = QTextListFormat::ListDecimal;
606 break;
607 case 5:
608 style = QTextListFormat::ListLowerAlpha;
609 break;
610 case 6:
611 style = QTextListFormat::ListUpperAlpha;
612 break;
613 case 7:
614 style = QTextListFormat::ListLowerRoman;
615 break;
616 case 8:
617 style = QTextListFormat::ListUpperRoman;
618 break;
619 }
620
621 cursor.beginEditBlock();
622
623 QTextBlockFormat blockFmt = cursor.blockFormat();
624
625 QTextListFormat listFmt;
626
627 if (cursor.currentList()) {
628 listFmt = cursor.currentList()->format();
629 } else {
630 listFmt.setIndent(blockFmt.indent() + 1);
631 blockFmt.setIndent(0);
632 cursor.setBlockFormat(blockFmt);
633 }
634
635 listFmt.setStyle(style);
636
637 cursor.createList(listFmt);
638
639 cursor.endEditBlock();
640 } else {
641 // ####
642 QTextBlockFormat bfmt;
643 bfmt.setObjectIndex(-1);
644 cursor.mergeBlockFormat(bfmt);
645 }
646 }
647
textColor()648 void TextEdit::textColor()
649 {
650 QColor col = QColorDialog::getColor(textEdit->textColor(), this);
651 if (!col.isValid())
652 return;
653 QTextCharFormat fmt;
654 fmt.setForeground(col);
655 mergeFormatOnWordOrSelection(fmt);
656 colorChanged(col);
657 }
658
textAlign(QAction * a)659 void TextEdit::textAlign(QAction *a)
660 {
661 if (a == actionAlignLeft)
662 textEdit->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
663 else if (a == actionAlignCenter)
664 textEdit->setAlignment(Qt::AlignHCenter);
665 else if (a == actionAlignRight)
666 textEdit->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
667 else if (a == actionAlignJustify)
668 textEdit->setAlignment(Qt::AlignJustify);
669 }
670
currentCharFormatChanged(const QTextCharFormat & format)671 void TextEdit::currentCharFormatChanged(const QTextCharFormat &format)
672 {
673 fontChanged(format.font());
674 colorChanged(format.foreground().color());
675 }
676
cursorPositionChanged()677 void TextEdit::cursorPositionChanged()
678 {
679 alignmentChanged(textEdit->alignment());
680 }
681
clipboardDataChanged()682 void TextEdit::clipboardDataChanged()
683 {
684 #ifndef QT_NO_CLIPBOARD
685 if (const QMimeData *md = QApplication::clipboard()->mimeData())
686 actionPaste->setEnabled(md->hasText());
687 #endif
688 }
689
about()690 void TextEdit::about()
691 {
692 QMessageBox::about(this, tr("About"), tr("This example demonstrates Qt's "
693 "rich text editing facilities in action, providing an example "
694 "document for you to experiment with."));
695 }
696
mergeFormatOnWordOrSelection(const QTextCharFormat & format)697 void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
698 {
699 QTextCursor cursor = textEdit->textCursor();
700 if (!cursor.hasSelection())
701 cursor.select(QTextCursor::WordUnderCursor);
702 cursor.mergeCharFormat(format);
703 textEdit->mergeCurrentCharFormat(format);
704 }
705
fontChanged(const QFont & f)706 void TextEdit::fontChanged(const QFont &f)
707 {
708 comboFont->setCurrentIndex(comboFont->findText(QFontInfo(f).family()));
709 comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
710 actionTextBold->setChecked(f.bold());
711 actionTextItalic->setChecked(f.italic());
712 actionTextUnderline->setChecked(f.underline());
713 }
714
colorChanged(const QColor & c)715 void TextEdit::colorChanged(const QColor &c)
716 {
717 QPixmap pix(16, 16);
718 pix.fill(c);
719 actionTextColor->setIcon(pix);
720 }
721
alignmentChanged(Qt::Alignment a)722 void TextEdit::alignmentChanged(Qt::Alignment a)
723 {
724 if (a & Qt::AlignLeft) {
725 actionAlignLeft->setChecked(true);
726 } else if (a & Qt::AlignHCenter) {
727 actionAlignCenter->setChecked(true);
728 } else if (a & Qt::AlignRight) {
729 actionAlignRight->setChecked(true);
730 } else if (a & Qt::AlignJustify) {
731 actionAlignJustify->setChecked(true);
732 }
733 }
734
735