-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
207 lines (186 loc) · 4.86 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QTextStream>
#include <aboutdialog.h>
#include <QFontDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setCentralWidget(ui->plainTextEdit);
setCurrentFile("");
connect(ui->plainTextEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionZ_awijanie_wierszy_toggled(bool arg1)
{
if(arg1==true)
{
ui->plainTextEdit->setLineWrapMode(QPlainTextEdit::WidgetWidth);
}
else
{
ui->plainTextEdit->setLineWrapMode(QPlainTextEdit::NoWrap);
}
}
void MainWindow::on_actionOtw_rz_triggered()
{
if(maybeSave())
{
disconnect(ui->plainTextEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
QString filter = "Wszystkie pliki (*.*) ;; Dokument tekstowy (*.txt)";
QString file_path = QFileDialog::getOpenFileName(this, "Otwórz plik", QDir::homePath(),filter);
QFile file(file_path);
filename=file_path;
if(file.open(QFile::ReadWrite | QFile::Text))
{
setCurrentFile(filename);
QTextStream in(&file);
ui->plainTextEdit->setPlainText(in.readAll());
}
file.close();
connect(ui->plainTextEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
}
}
void MainWindow::on_actionZapisz_jako_triggered()
{
QString filter = "Wszystkie pliki (*.*) ;; Dokument tekstowy (*.txt)";
QString file_path = QFileDialog::getSaveFileName(this, "Zapisz jako...", QDir::homePath(),filter);
QFile file(file_path);
if(file_path!="") filename=file_path;
if(file.open(QFile::WriteOnly | QFile::Text))
{
setCurrentFile(filename);
QTextStream out(&file);
QString text = ui->plainTextEdit->toPlainText();
out << text;
}
file.flush();
file.close();
}
void MainWindow::on_actionZapisz_triggered()
{
if(filename=="")
{
on_actionZapisz_jako_triggered();
}
else
{
QFile file(filename);
if(file.open(QFile::WriteOnly | QFile::Text))
{
setCurrentFile(filename);
QTextStream out(&file);
QString text = ui->plainTextEdit->toPlainText();
out << text;
}
file.flush();
file.close();
}
}
void MainWindow::on_actionNowy_triggered()
{
if(maybeSave())
{
filename="";
setCurrentFile("");
ui->plainTextEdit->setPlainText("");
}
}
void MainWindow::on_actionZako_cz_triggered()
{
if(maybeSave())
exit(EXIT_SUCCESS);
}
void MainWindow::on_actionWytnij_triggered()
{
ui->plainTextEdit->cut();
}
void MainWindow::on_actionKopiuj_triggered()
{
ui->plainTextEdit->copy();
}
void MainWindow::on_actionWklej_triggered()
{
ui->plainTextEdit->paste();
}
void MainWindow::on_action_Czcionka_triggered()
{
bool ok;
QFont font = QFontDialog::getFont(&ok,this);
if(ok)
{
QTextCursor cursor = ui->plainTextEdit->textCursor();
QTextCharFormat format = cursor.charFormat();
if(cursor.selectedText()=="")
{
ui->plainTextEdit->setFont(font);
}
else
{
format.setFont(font);
cursor.setCharFormat(format);
}
}else return;
}
void MainWindow::on_actionCofnij_triggered()
{
ui->plainTextEdit->undo();
}
void MainWindow::on_actionPrzywr_triggered()
{
ui->plainTextEdit->redo();
}
void MainWindow::on_actionO_programie_triggered()
{
AboutDialog *about = new AboutDialog(this);
about->show();
}
void MainWindow::setCurrentFile(const QString &fileName)
{
QString curFile = fileName.section("/",-1,-1);
ui->plainTextEdit->textChanged();
setWindowModified(false);
if (curFile.isEmpty())
curFile = "Nowy dokument tekstowy.txt";
setWindowTitle("[*]" + curFile);
}
void MainWindow::documentWasModified()
{
setWindowModified(ui->plainTextEdit->document()->isModified());
}
bool MainWindow::maybeSave()
{
if (!ui->plainTextEdit->document()->isModified())
return true;
const QMessageBox::StandardButton ret
= QMessageBox::warning(this, tr("Ostrzeżenie"),
tr("Dokument został zmodyfikowany.\n"
"Czy chcesz zapisać zmiany?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
switch (ret)
{
case QMessageBox::Save:
on_actionZapisz_triggered();
return true;
case QMessageBox::Cancel:
return false;
default:
break;
}
return true;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (maybeSave()) {
event->accept();
} else {
event->ignore();
}
}