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