Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 1 | #include <QtGui> |
| 2 | #include <QMainWindow> |
| 3 | #include "mainwindow.h" |
| 4 | #include "ui_mainwindow.h" |
| 5 | |
| 6 | MainWindow::MainWindow(QWidget *parent) : |
| 7 | QMainWindow(parent), |
| 8 | ui(new Ui::MainWindow) |
| 9 | { |
| 10 | ui->setupUi(this); |
| 11 | } |
| 12 | |
| 13 | MainWindow::~MainWindow() |
| 14 | { |
| 15 | delete ui; |
| 16 | } |
| 17 | |
| 18 | void MainWindow::newfile(){ |
| 19 | |
| 20 | if (maybeSave()) { |
| 21 | ui->textEdit->clear(); |
| 22 | setCurrentFile(""); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | bool MainWindow::maybeSave(){ |
| 27 | |
| 28 | if (ui->textEdit->document()->isModified()) { |
| 29 | QMessageBox::StandardButton ret; |
| 30 | ret = QMessageBox::warning(this, tr("Application"), |
| 31 | tr("The document has been modified.\n" |
| 32 | "Do you want to save your changes?"), |
| 33 | QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); |
| 34 | if (ret == QMessageBox::Save) |
| 35 | return save(); |
| 36 | else if (ret == QMessageBox::Cancel) |
| 37 | return false; |
| 38 | } |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | void MainWindow::setCurrentFile(const QString &fileName){ |
| 43 | |
| 44 | curFile = fileName; |
| 45 | ui->textEdit->document()->setModified(false); |
| 46 | setWindowModified(false); |
| 47 | |
| 48 | QString shownName = curFile; |
| 49 | if (curFile.isEmpty()) |
| 50 | shownName = "untitled.txt"; |
| 51 | setWindowFilePath(shownName); |
| 52 | } |
| 53 | |
| 54 | bool MainWindow::save(){ |
| 55 | |
| 56 | if (curFile.isEmpty()) { |
| 57 | return saveAs(); |
| 58 | } else { |
| 59 | return saveFile(curFile); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | bool MainWindow::saveAs(){ |
| 64 | |
| 65 | QString fileName = QFileDialog::getSaveFileName(this); |
| 66 | if (fileName.isEmpty()) |
| 67 | return false; |
| 68 | |
| 69 | return saveFile(fileName); |
| 70 | } |
| 71 | |
| 72 | bool MainWindow::saveFile(const QString &fileName){ |
| 73 | |
| 74 | QFile file(fileName); |
| 75 | if (!file.open(QFile::WriteOnly | QFile::Text)) { |
| 76 | QMessageBox::warning(this, tr("Application"), |
| 77 | tr("Cannot write file %1:\n%2.") |
| 78 | .arg(fileName) |
| 79 | .arg(file.errorString())); |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | QTextStream out(&file); |
| 84 | #ifndef QT_NO_CURSOR |
| 85 | QApplication::setOverrideCursor(Qt::WaitCursor); |
| 86 | #endif |
| 87 | out << ui->textEdit->toPlainText(); |
| 88 | #ifndef QT_NO_CURSOR |
| 89 | QApplication::restoreOverrideCursor(); |
| 90 | #endif |
| 91 | |
| 92 | setCurrentFile(fileName); |
| 93 | statusBar()->showMessage(tr("File saved"), 2000); |
| 94 | return true; |
| 95 | } |
| 96 |
Branches:
master
