-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
70 lines (59 loc) · 1.82 KB
/
mainwindow.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "mainwindow.h"
#include <QDialog>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QtPrintSupport/QPrintDialog>
#include <QtPrintSupport/QPrinter>
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
this->setCentralWidget(ui->textEdit);
}
MainWindow::~MainWindow() { delete ui; }
void MainWindow::on_actionExit_triggered() { this->close(); }
void MainWindow::on_actionNew_triggered() {
currentFile.clear();
ui->textEdit->setText(QString());
}
void MainWindow::on_actionOpen_triggered() {
QString fileName = QFileDialog::getOpenFileName(this, "Open file");
QFile file(fileName);
currentFile = fileName;
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning",
"Failed to open file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream in(&file);
QString text = in.readAll();
ui->textEdit->setText(text);
file.close();
}
void MainWindow::on_actionSaveAs_triggered() {
QString fileName = QFileDialog::getSaveFileName(this, "Save file as");
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning",
"Failed to save file: " + file.errorString());
return;
}
setWindowTitle(fileName);
currentFile = fileName;
QTextStream out(&file);
QString fileText = ui->textEdit->toPlainText();
out << fileText;
file.close();
}
void MainWindow::on_actionPrint_triggered() {
QPrinter printer;
QPrintDialog printerDialog(&printer, this);
if (printerDialog.exec() == QDialog::Rejected) {
QMessageBox::warning(this, "Warning", "Cannot access printer");
return;
}
ui->textEdit->print(&printer);
}