1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the demonstration applications of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia 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 | 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 |
|
---|
144 | void TextEdit::closeEvent(QCloseEvent *e)
|
---|
145 | {
|
---|
146 | if (maybeSave())
|
---|
147 | e->accept();
|
---|
148 | else
|
---|
149 | e->ignore();
|
---|
150 | }
|
---|
151 |
|
---|
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 |
|
---|
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);
|
---|
|
---|