1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the documentation of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
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 https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QFile> |
52 | #include <QFileDialog> |
53 | #include <QTextStream> |
54 | #include <QMessageBox> |
55 | #if defined(QT_PRINTSUPPORT_LIB) |
56 | #include <QtPrintSupport/qtprintsupportglobal.h> |
57 | #if QT_CONFIG(printer) |
58 | #if QT_CONFIG(printdialog) |
59 | #include <QPrintDialog> |
60 | #endif // QT_CONFIG(printdialog) |
61 | #include <QPrinter> |
62 | #endif // QT_CONFIG(printer) |
63 | #endif // QT_PRINTSUPPORT_LIB |
64 | #include <QFont> |
65 | #include <QFontDialog> |
66 | |
67 | #include "notepad.h" |
68 | #include "ui_notepad.h" |
69 | |
70 | Notepad::Notepad(QWidget *parent) : |
71 | QMainWindow(parent), |
72 | ui(new Ui::Notepad) |
73 | { |
74 | ui->setupUi(this); |
75 | this->setCentralWidget(ui->textEdit); |
76 | |
77 | connect(sender: ui->actionNew, signal: &QAction::triggered, receiver: this, slot: &Notepad::newDocument); |
78 | connect(sender: ui->actionOpen, signal: &QAction::triggered, receiver: this, slot: &Notepad::open); |
79 | connect(sender: ui->actionSave, signal: &QAction::triggered, receiver: this, slot: &Notepad::save); |
80 | connect(sender: ui->actionSave_as, signal: &QAction::triggered, receiver: this, slot: &Notepad::saveAs); |
81 | connect(sender: ui->actionPrint, signal: &QAction::triggered, receiver: this, slot: &Notepad::print); |
82 | connect(sender: ui->actionExit, signal: &QAction::triggered, receiver: this, slot: &Notepad::exit); |
83 | connect(sender: ui->actionCopy, signal: &QAction::triggered, receiver: this, slot: &Notepad::copy); |
84 | connect(sender: ui->actionCut, signal: &QAction::triggered, receiver: this, slot: &Notepad::cut); |
85 | connect(sender: ui->actionPaste, signal: &QAction::triggered, receiver: this, slot: &Notepad::paste); |
86 | connect(sender: ui->actionUndo, signal: &QAction::triggered, receiver: this, slot: &Notepad::undo); |
87 | connect(sender: ui->actionRedo, signal: &QAction::triggered, receiver: this, slot: &Notepad::redo); |
88 | connect(sender: ui->actionFont, signal: &QAction::triggered, receiver: this, slot: &Notepad::selectFont); |
89 | connect(sender: ui->actionBold, signal: &QAction::triggered, receiver: this, slot: &Notepad::setFontBold); |
90 | connect(sender: ui->actionUnderline, signal: &QAction::triggered, receiver: this, slot: &Notepad::setFontUnderline); |
91 | connect(sender: ui->actionItalic, signal: &QAction::triggered, receiver: this, slot: &Notepad::setFontItalic); |
92 | connect(sender: ui->actionAbout, signal: &QAction::triggered, receiver: this, slot: &Notepad::about); |
93 | |
94 | // Disable menu actions for unavailable features |
95 | #if !defined(QT_PRINTSUPPORT_LIB) || !QT_CONFIG(printer) |
96 | ui->actionPrint->setEnabled(false); |
97 | #endif |
98 | |
99 | #if !QT_CONFIG(clipboard) |
100 | ui->actionCut->setEnabled(false); |
101 | ui->actionCopy->setEnabled(false); |
102 | ui->actionPaste->setEnabled(false); |
103 | #endif |
104 | } |
105 | |
106 | Notepad::~Notepad() |
107 | { |
108 | delete ui; |
109 | } |
110 | |
111 | void Notepad::newDocument() |
112 | { |
113 | currentFile.clear(); |
114 | ui->textEdit->setText(QString()); |
115 | } |
116 | |
117 | void Notepad::open() |
118 | { |
119 | QString fileName = QFileDialog::getOpenFileName(parent: this, caption: "Open the file" ); |
120 | QFile file(fileName); |
121 | currentFile = fileName; |
122 | if (!file.open(flags: QIODevice::ReadOnly | QFile::Text)) { |
123 | QMessageBox::warning(parent: this, title: "Warning" , text: "Cannot open file: " + file.errorString()); |
124 | return; |
125 | } |
126 | setWindowTitle(fileName); |
127 | QTextStream in(&file); |
128 | QString text = in.readAll(); |
129 | ui->textEdit->setText(text); |
130 | file.close(); |
131 | } |
132 | |
133 | void Notepad::save() |
134 | { |
135 | QString fileName; |
136 | // If we don't have a filename from before, get one. |
137 | if (currentFile.isEmpty()) { |
138 | fileName = QFileDialog::getSaveFileName(parent: this, caption: "Save" ); |
139 | currentFile = fileName; |
140 | } else { |
141 | fileName = currentFile; |
142 | } |
143 | QFile file(fileName); |
144 | if (!file.open(flags: QIODevice::WriteOnly | QFile::Text)) { |
145 | QMessageBox::warning(parent: this, title: "Warning" , text: "Cannot save file: " + file.errorString()); |
146 | return; |
147 | } |
148 | setWindowTitle(fileName); |
149 | QTextStream out(&file); |
150 | QString text = ui->textEdit->toPlainText(); |
151 | out << text; |
152 | file.close(); |
153 | } |
154 | |
155 | void Notepad::saveAs() |
156 | { |
157 | QString fileName = QFileDialog::getSaveFileName(parent: this, caption: "Save as" ); |
158 | QFile file(fileName); |
159 | |
160 | if (!file.open(flags: QFile::WriteOnly | QFile::Text)) { |
161 | QMessageBox::warning(parent: this, title: "Warning" , text: "Cannot save file: " + file.errorString()); |
162 | return; |
163 | } |
164 | currentFile = fileName; |
165 | setWindowTitle(fileName); |
166 | QTextStream out(&file); |
167 | QString text = ui->textEdit->toPlainText(); |
168 | out << text; |
169 | file.close(); |
170 | } |
171 | |
172 | void Notepad::print() |
173 | { |
174 | #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer) |
175 | QPrinter printDev; |
176 | #if QT_CONFIG(printdialog) |
177 | QPrintDialog dialog(&printDev, this); |
178 | if (dialog.exec() == QDialog::Rejected) |
179 | return; |
180 | #endif // QT_CONFIG(printdialog) |
181 | ui->textEdit->print(printer: &printDev); |
182 | #endif // QT_CONFIG(printer) |
183 | } |
184 | |
185 | void Notepad::exit() |
186 | { |
187 | QCoreApplication::quit(); |
188 | } |
189 | |
190 | void Notepad::copy() |
191 | { |
192 | #if QT_CONFIG(clipboard) |
193 | ui->textEdit->copy(); |
194 | #endif |
195 | } |
196 | |
197 | void Notepad::cut() |
198 | { |
199 | #if QT_CONFIG(clipboard) |
200 | ui->textEdit->cut(); |
201 | #endif |
202 | } |
203 | |
204 | void Notepad::paste() |
205 | { |
206 | #if QT_CONFIG(clipboard) |
207 | ui->textEdit->paste(); |
208 | #endif |
209 | } |
210 | |
211 | void Notepad::undo() |
212 | { |
213 | ui->textEdit->undo(); |
214 | } |
215 | |
216 | void Notepad::redo() |
217 | { |
218 | ui->textEdit->redo(); |
219 | } |
220 | |
221 | void Notepad::selectFont() |
222 | { |
223 | bool fontSelected; |
224 | QFont font = QFontDialog::getFont(ok: &fontSelected, parent: this); |
225 | if (fontSelected) |
226 | ui->textEdit->setFont(font); |
227 | } |
228 | |
229 | void Notepad::setFontUnderline(bool underline) |
230 | { |
231 | ui->textEdit->setFontUnderline(underline); |
232 | } |
233 | |
234 | void Notepad::setFontItalic(bool italic) |
235 | { |
236 | ui->textEdit->setFontItalic(italic); |
237 | } |
238 | |
239 | void Notepad::setFontBold(bool bold) |
240 | { |
241 | bold ? ui->textEdit->setFontWeight(QFont::Bold) : |
242 | ui->textEdit->setFontWeight(QFont::Normal); |
243 | } |
244 | |
245 | void Notepad::about() |
246 | { |
247 | QMessageBox::about(parent: this, title: tr(s: "About MDI" ), |
248 | text: tr(s: "The <b>Notepad</b> example demonstrates how to code a basic " |
249 | "text editor using QtWidgets" )); |
250 | |
251 | } |
252 | |