-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow_triggers.cpp
142 lines (111 loc) · 3.93 KB
/
mainwindow_triggers.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#include "stdio.h"
void MainWindow::on_actionOpen_triggered()
{
QString *file_name = new QString
{ QFileDialog::getOpenFileName(
this, "Open the file" /*next params: default location, filters*/)
};
QFile *file = new QFile { *file_name };
if(!file->open(QFile::ReadOnly | QFile::Text)){
QMessageBox::warning(this, "..", "File failed to open");
return;
}
QTextStream in(file);
QString *text = new QString { in.readAll() };
ui->textEdit->setText(*text);
file->close();
*file_path_ = *file_name;
delete text;
delete file_name;
delete file;
}
void MainWindow::on_actionNew_triggered()
{
*file_path_ = "";
ui->textEdit->setText("");
}
void MainWindow::on_actionSave_triggered()
{
QFile *file = new QFile { *file_path_ };
if(!file->exists()){
on_actionSave_As_triggered();
return;
}
if(!file->open(QFile::WriteOnly | QFile::Text)){
QMessageBox::warning(this, "..", "File didn't save");
return;
}
QTextStream out(file);
QString *text = new QString { ui->textEdit->toHtml() };
out << *text;
file->flush();
file->close();
delete text;
delete file;
}
void MainWindow::on_actionSave_As_triggered()
{
QString *file_name = new QString
{ QFileDialog::getSaveFileName(
this, "Save File As..." /*next params: default location, filters*/)
};
QFile *file = new QFile { *file_name };
if(!file->open(QFile::WriteOnly | QFile::Text)){
QMessageBox::warning(this, "..", "File didn't save");
return;
}
QTextStream out(file);
QString *text = new QString { ui->textEdit->toHtml() };
out << *text;
file->flush();
file->close();
delete text;
delete file_name;
delete file;
}
// Built in methods within Qt
void MainWindow::on_actionCut_triggered() { ui->textEdit->cut(); }
void MainWindow::on_actionCopy_triggered() { ui->textEdit->copy(); }
void MainWindow::on_actionPaste_triggered() { ui->textEdit->paste(); }
void MainWindow::on_actionRedo_triggered() { ui->textEdit->redo(); }
void MainWindow::on_actionUndo_triggered() { ui->textEdit->undo(); }
void MainWindow::on_qFontComboBox_currentFontChanged(const QFont &f) { ui->textEdit->setCurrentFont(f); }
void MainWindow::on_actionAbout_Notepad_triggered() {
QString *about = new QString[2] {
"About PD Notepad",
"A lightweight notepad program to help learn Qt\nPenguinDogg\nCreated under the MIT license"
};
QMessageBox::about(this, about[0], about[1]);
delete[] about;
}
void MainWindow::on_actionBold_triggered()
{
if(!bold) { ui->textEdit->setFontWeight(QFont::Bold); }
else { ui->textEdit->setFontWeight(QFont::Normal); }
if(!bold) { bold = true; }
else { bold = false; }
}
void MainWindow::on_actionItalics_triggered()
{
if(!italics) { ui->textEdit->setFontItalic(true); }
else { ui->textEdit->setFontItalic(false); }
if(!italics) { italics = true; }
else { italics = false; }
}
void MainWindow::on_actionUnderline_triggered()
{
if(!underlined) { ui->textEdit->setFontUnderline(true); }
else { ui->textEdit->setFontUnderline(false); }
if(!underlined) { underlined = true; }
else { underlined = false; }
}
void MainWindow::on_fontSizeWidget_currentIndexChanged(int index){
qreal item = fontSizeWidget->itemText(index).toInt();
ui->textEdit->setFontPointSize(item);
}