| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | #include <QtGui> |
|---|
| 33 | |
|---|
| 34 | #include <Qsci/qsciscintilla.h> |
|---|
| 35 | |
|---|
| 36 | #include "mainwindow.h" |
|---|
| 37 | |
|---|
| 38 | MainWindow::MainWindow() |
|---|
| 39 | { |
|---|
| 40 | textEdit = new QsciScintilla; |
|---|
| 41 | setCentralWidget(textEdit); |
|---|
| 42 | |
|---|
| 43 | createActions(); |
|---|
| 44 | createMenus(); |
|---|
| 45 | createToolBars(); |
|---|
| 46 | createStatusBar(); |
|---|
| 47 | |
|---|
| 48 | readSettings(); |
|---|
| 49 | |
|---|
| 50 | connect(textEdit, SIGNAL(textChanged()), |
|---|
| 51 | this, SLOT(documentWasModified())); |
|---|
| 52 | |
|---|
| 53 | setCurrentFile(""); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void MainWindow::closeEvent(QCloseEvent *event) |
|---|
| 57 | { |
|---|
| 58 | if (maybeSave()) { |
|---|
| 59 | writeSettings(); |
|---|
| 60 | event->accept(); |
|---|
| 61 | } else { |
|---|
| 62 | event->ignore(); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | void MainWindow::newFile() |
|---|
| 67 | { |
|---|
| 68 | if (maybeSave()) { |
|---|
| 69 | textEdit->clear(); |
|---|
| 70 | setCurrentFile(""); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | void MainWindow::open() |
|---|
| 75 | { |
|---|
| 76 | if (maybeSave()) { |
|---|
| 77 | QString fileName = QFileDialog::getOpenFileName(this); |
|---|
| 78 | if (!fileName.isEmpty()) |
|---|
| 79 | loadFile(fileName); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | bool MainWindow::save() |
|---|
| 84 | { |
|---|
| 85 | if (curFile.isEmpty()) { |
|---|
| 86 | return saveAs(); |
|---|
| 87 | } else { |
|---|
| 88 | return saveFile(curFile); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | bool MainWindow::saveAs() |
|---|
| 93 | { |
|---|
| 94 | QString fileName = QFileDialog::getSaveFileName(this); |
|---|
| 95 | if (fileName.isEmpty()) |
|---|
| 96 | return false; |
|---|
| 97 | |
|---|
| 98 | return saveFile(fileName); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void MainWindow::about() |
|---|
| 102 | { |
|---|
| 103 | QMessageBox::about(this, tr("About Application"), |
|---|
| 104 | tr("The <b>Application</b> example demonstrates how to " |
|---|
| 105 | "write modern GUI applications using Qt, with a menu bar, " |
|---|
| 106 | "toolbars, and a status bar.")); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void MainWindow::documentWasModified() |
|---|
| 110 | { |
|---|
| 111 | setWindowModified(textEdit->isModified()); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | void MainWindow::createActions() |
|---|
| 115 | { |
|---|
| 116 | newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); |
|---|
| 117 | newAct->setShortcut(tr("Ctrl+N")); |
|---|
| 118 | newAct->setStatusTip(tr("Create a new file")); |
|---|
| 119 | connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); |
|---|
| 120 | |
|---|
| 121 | openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); |
|---|
| 122 | openAct->setShortcut(tr("Ctrl+O")); |
|---|
| 123 | openAct->setStatusTip(tr("Open an existing file")); |
|---|
| 124 | connect(openAct, SIGNAL(triggered()), this, SLOT(open())); |
|---|
| 125 | |
|---|
| 126 | saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); |
|---|
| 127 | saveAct->setShortcut(tr("Ctrl+S")); |
|---|
| 128 | saveAct->setStatusTip(tr("Save the document to disk")); |
|---|
| 129 | connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); |
|---|
| 130 | |
|---|
| 131 | saveAsAct = new QAction(tr("Save &As..."), this); |
|---|
| 132 | saveAsAct->setStatusTip(tr("Save the document under a new name")); |
|---|
| 133 | connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); |
|---|
| 134 | |
|---|
| 135 | exitAct = new QAction(tr("E&xit"), this); |
|---|
| 136 | exitAct->setShortcut(tr("Ctrl+Q")); |
|---|
| 137 | exitAct->setStatusTip(tr("Exit the application")); |
|---|
| 138 | connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); |
|---|
| 139 | |
|---|
| 140 | cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); |
|---|
| 141 | cutAct->setShortcut(tr("Ctrl+X")); |
|---|
| 142 | cutAct->setStatusTip(tr("Cut the current selection's contents to the " |
|---|
| 143 | "clipboard")); |
|---|
| 144 | connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut())); |
|---|
| 145 | |
|---|
| 146 | copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); |
|---|
| 147 | copyAct->setShortcut(tr("Ctrl+C")); |
|---|
| 148 | copyAct->setStatusTip(tr("Copy the current selection's contents to the " |
|---|
| 149 | "clipboard")); |
|---|
| 150 | connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy())); |
|---|
| 151 | |
|---|
| 152 | pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); |
|---|
| 153 | pasteAct->setShortcut(tr("Ctrl+V")); |
|---|
| 154 | pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " |
|---|
| 155 | "selection")); |
|---|
| 156 | connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste())); |
|---|
| 157 | |
|---|
| 158 | aboutAct = new QAction(tr("&About"), this); |
|---|
| 159 | aboutAct->setStatusTip(tr("Show the application's About box")); |
|---|
| 160 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); |
|---|
| 161 | |
|---|
| 162 | aboutQtAct = new QAction(tr("About &Qt"), this); |
|---|
| 163 | aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); |
|---|
| 164 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
|---|
| 165 | |
|---|
| 166 | cutAct->setEnabled(false); |
|---|
| 167 | copyAct->setEnabled(false); |
|---|
| 168 | connect(textEdit, SIGNAL(copyAvailable(bool)), |
|---|
| 169 | cutAct, SLOT(setEnabled(bool))); |
|---|
| 170 | connect(textEdit, SIGNAL(copyAvailable(bool)), |
|---|
| 171 | copyAct, SLOT(setEnabled(bool))); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void MainWindow::createMenus() |
|---|
| 175 | { |
|---|
| 176 | fileMenu = menuBar()->addMenu(tr("&File")); |
|---|
| 177 | fileMenu->addAction(newAct); |
|---|
| 178 | fileMenu->addAction(openAct); |
|---|
| 179 | fileMenu->addAction(saveAct); |
|---|
| 180 | fileMenu->addAction(saveAsAct); |
|---|
| 181 | fileMenu->addSeparator(); |
|---|
| 182 | fileMenu->addAction(exitAct); |
|---|
| 183 | |
|---|
| 184 | editMenu = menuBar()->addMenu(tr("&Edit")); |
|---|
| 185 | editMenu->addAction(cutAct); |
|---|
| 186 | editMenu->addAction(copyAct); |
|---|
| 187 | editMenu->addAction(pasteAct); |
|---|
| 188 | |
|---|
| 189 | menuBar()->addSeparator(); |
|---|
| 190 | |
|---|
| 191 | helpMenu = menuBar()->addMenu(tr("&Help")); |
|---|
| 192 | helpMenu->addAction(aboutAct); |
|---|
| 193 | helpMenu->addAction(aboutQtAct); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | void MainWindow::createToolBars() |
|---|
| 197 | { |
|---|
| 198 | fileToolBar = addToolBar(tr("File")); |
|---|
| 199 | fileToolBar->addAction(newAct); |
|---|
| 200 | fileToolBar->addAction(openAct); |
|---|
| 201 | fileToolBar->addAction(saveAct); |
|---|
| 202 | |
|---|
| 203 | editToolBar = addToolBar(tr("Edit")); |
|---|
| 204 | editToolBar->addAction(cutAct); |
|---|
| 205 | editToolBar->addAction(copyAct); |
|---|
| 206 | editToolBar->addAction(pasteAct); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | void MainWindow::createStatusBar() |
|---|
| 210 | { |
|---|
| 211 | statusBar()->showMessage(tr("Ready")); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | void MainWindow::readSettings() |
|---|
| 215 | { |
|---|
| 216 | QSettings settings("Trolltech", "Application Example"); |
|---|
| 217 | QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); |
|---|
| 218 | QSize size = settings.value("size", QSize(400, 400)).toSize(); |
|---|
| 219 | resize(size); |
|---|
| 220 | move(pos); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | void MainWindow::writeSettings() |
|---|
| 224 | { |
|---|
| 225 | QSettings settings("Trolltech", "Application Example"); |
|---|
| 226 | settings.setValue("pos", pos()); |
|---|
| 227 | settings.setValue("size", size()); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | bool MainWindow::maybeSave() |
|---|
| 231 | { |
|---|
| 232 | if (textEdit->isModified()) { |
|---|
| 233 | int ret = QMessageBox::warning(this, tr("Application"), |
|---|
| 234 | tr("The document has been modified.\n" |
|---|
| 235 | "Do you want to save your changes?"), |
|---|
| 236 | QMessageBox::Yes | QMessageBox::Default, |
|---|
| 237 | QMessageBox::No, |
|---|
| 238 | QMessageBox::Cancel | QMessageBox::Escape); |
|---|
| 239 | if (ret == QMessageBox::Yes) |
|---|
| 240 | return save(); |
|---|
| 241 | else if (ret == QMessageBox::Cancel) |
|---|
| 242 | return false; |
|---|
| 243 | } |
|---|
| 244 | return true; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | void MainWindow::loadFile(const QString &fileName) |
|---|
| 248 | { |
|---|
| 249 | QFile file(fileName); |
|---|
| 250 | if (!file.open(QFile::ReadOnly)) { |
|---|
| 251 | QMessageBox::warning(this, tr("Application"), |
|---|
| 252 | tr("Cannot read file %1:\n%2.") |
|---|
| 253 | .arg(fileName) |
|---|
| 254 | .arg(file.errorString())); |
|---|
| 255 | return; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | QTextStream in(&file); |
|---|
| 259 | QApplication::setOverrideCursor(Qt::WaitCursor); |
|---|
| 260 | textEdit->setText(in.readAll()); |
|---|
| 261 | QApplication::restoreOverrideCursor(); |
|---|
| 262 | |
|---|
| 263 | setCurrentFile(fileName); |
|---|
| 264 | statusBar()->showMessage(tr("File loaded"), 2000); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | bool MainWindow::saveFile(const QString &fileName) |
|---|
| 268 | { |
|---|
| 269 | QFile file(fileName); |
|---|
| 270 | if (!file.open(QFile::WriteOnly)) { |
|---|
| 271 | QMessageBox::warning(this, tr("Application"), |
|---|
| 272 | tr("Cannot write file %1:\n%2.") |
|---|
| 273 | .arg(fileName) |
|---|
| 274 | .arg(file.errorString())); |
|---|
| 275 | return false; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | QTextStream out(&file); |
|---|
| 279 | QApplication::setOverrideCursor(Qt::WaitCursor); |
|---|
| 280 | out << textEdit->text(); |
|---|
| 281 | QApplication::restoreOverrideCursor(); |
|---|
| 282 | |
|---|
| 283 | setCurrentFile(fileName); |
|---|
| 284 | statusBar()->showMessage(tr("File saved"), 2000); |
|---|
| 285 | return true; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | void MainWindow::setCurrentFile(const QString &fileName) |
|---|
| 289 | { |
|---|
| 290 | curFile = fileName; |
|---|
| 291 | textEdit->setModified(false); |
|---|
| 292 | setWindowModified(false); |
|---|
| 293 | |
|---|
| 294 | QString shownName; |
|---|
| 295 | if (curFile.isEmpty()) |
|---|
| 296 | shownName = "untitled.txt"; |
|---|
| 297 | else |
|---|
| 298 | shownName = strippedName(curFile); |
|---|
| 299 | |
|---|
| 300 | setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application"))); |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | QString MainWindow::strippedName(const QString &fullFileName) |
|---|
| 304 | { |
|---|
| 305 | return QFileInfo(fullFileName).fileName(); |
|---|
| 306 | } |
|---|