-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.cpp
More file actions
222 lines (206 loc) · 7.28 KB
/
editor.cpp
File metadata and controls
222 lines (206 loc) · 7.28 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "editor.h"
Editor::Editor(QWidget *parent) : QWidget(parent)
{
// Initialize text field
textField = new TextEdit(this);
// Add highlighter
highlighter = new Highlighter(this, textField->document());
// Set up layout
textLayout = new QHBoxLayout();
this->setLayout(textLayout);
textLayout->addStretch(); // Add spacing
textLayout->addWidget(textField); // Add textfield to layout
textLayout->addStretch(); // add spacing
// Apply style and configurations
this->setStyle();
// No file is open
fileIsOpen = false;
openFilename = "";
// Initialize shortcut vector
quickInsert = QVector< QPair<QString,QString> >(8,QPair<QString,QString>("",""));
}
Editor::~Editor(){
delete textField;
delete textLayout;
}
QString Editor::getConfHelper(QString str, QString conf){
int n, m;
m = conf.indexOf(str);
n = conf.indexOf(":",m)+1;
m = conf.indexOf("\n",n);
return conf.mid(n,m-n).trimmed();
}
void Editor::setStyle(){
// Open config and stylesheet files
QString style, conf;
try {
style = this->fileToStr("style.sty");
conf = this->fileToStr("config.cfg");
} catch (QString) {
QString tmp = this->dataToStr();
if (tmp != "") tmp += "\n";
tmp += "Error opening config.cfg or style.sty";
this->strToData(tmp);
style = "";
conf = "";
}
// Get width
int widthPercentage = this->getConfHelper("Width-percentage:",conf).toInt();
// Get tab-width
int tabWidth = this->getConfHelper("Tab-width:",conf).toInt();
// Set configurations
textField->setTabStopWidth(tabWidth); // Tab width
textLayout->setStretch(0,(100-widthPercentage)/2); // Set spacing stretch
textLayout->setStretch(1,widthPercentage); // Set textField stretch
textLayout->setStretch(2,(100-widthPercentage)/2); // Set spacing stretch
// Get and set colors
style.replace("BGCOLOR",this->getConfHelper("Background-color:",conf));
style.replace("SCCOLOR",this->getConfHelper("Scroll-color:",conf));
style.replace("LACOLOR",this->getConfHelper("Light-app-color:",conf));
style.replace("DACOLOR",this->getConfHelper("Dark-app-color:",conf));
style.replace("FCOLOR",this->getConfHelper("Font-color:",conf));
style.replace("SCOLOR",this->getConfHelper("Selected-font-color:",conf));
style.replace("SBCOLOR",this->getConfHelper("Selected-background-color:",conf));
// Get font styles
style.replace("FFAMILY",this->getConfHelper("Font:",conf));
style.replace("FSIZE",this->getConfHelper("Font-size:",conf));
style.replace("FBOLD",( (this->getConfHelper("Font-bold:",conf) == "true") ? "bold" : "normal" ));
style.replace("FITALIC",( (this->getConfHelper("Font-italic:",conf) == "true") ? "italic" : "normal" ));
style.replace("FUNDERLINE",( (this->getConfHelper("Font-underline:",conf) == "true") ? "underline" : "none" ));
// Apply style
qApp->setStyleSheet(style);
}
void Editor::setSyntax(QString filename){
// Find extension
QString filetype;
for (int i=filename.length()-1; i>=0; i--){
if (filename.mid(i,1) == "."){
filetype = " " + filename.mid(i+1,filename.length()-i) + " ";
}
}
// Set highlighting
if (QString(" cpp cc cxx h hpp hxx ").contains(filetype)) highlighter->loadSyntax("syntax/cpp.syn");
else if (QString(" latex tex bib ").contains(filetype)) highlighter->loadSyntax("syntax/latex.syn");
else if (QString(" syn cfg ").contains(filetype)) highlighter->loadSyntax("syntax/coderoom.syn");
else highlighter->loadSyntax("syntax/text.syn");
}
void Editor::chooseSyntax(){
// Load data
QString filename = QFileDialog::getOpenFileName(this,tr("Open File"), "syntax", tr("CodeRoom Syn Files (*.syn)"));
// Check if filename is empty
if (filename == "") return;
// Set syntax
highlighter->loadSyntax(filename);
this->strToData(this->dataToStr()); // Refresh
}
void Editor::openFile(){
// Load data
QString filename = QFileDialog::getOpenFileName(this,tr("Open File"), "", tr("All Files (*)"));
// Open file
this->openFile(filename);
}
void Editor::openFile(QString filename){
// Check if filename is empty
if (filename == "") return;
// Set syntax
this->setSyntax(filename);
// Set data
this->strToData(this->fileToStr(filename));
// File is open
fileIsOpen = true;
openFilename = filename;
}
void Editor::saveFile(){
if (fileIsOpen){
// Save to open file
this->strToFile(this->dataToStr(), openFilename);
} else {
// Save as
this->saveFileAs();
}
}
void Editor::saveFileAs(){
// Save as
QString filename = QFileDialog::getSaveFileName(this,tr("Save As"), "", tr("All Files (*)"));
// Check if filename is empty
if (filename == "") return;
// Save file
this->strToFile(this->dataToStr(), filename);
fileIsOpen = true;
openFilename = filename;
// Set syntax
this->setSyntax(filename);
this->strToData(this->dataToStr()); // Refresh
}
void Editor::newFile(){
// Clear text field
this->strToData("");
// Erase memory
fileIsOpen = false;
openFilename = "";
}
QString Editor::dataToStr(){
// Convert current data to plain text string
return textField->toPlainText();
}
void Editor::strToFile(QString str, QString filename){
// Open file
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) throw QString("Error opening file");
// Create stream and write data
QTextStream out(&file);
out << str;
file.close();
}
void Editor::strToData(QString str){
textField->setText(str);
}
QString Editor::fileToStr(QString filename){
// Open file
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) throw QString("Error opening file");
// Create stream and load data
QTextStream in(&file);
QString str = "";
while (!in.atEnd()) {
QString line = in.readLine();
str.append(line);
str.append("\n");
}
file.close();
// Return string
return str;
}
bool Editor::checkAndSave(){
// Check if file has been removed
if (fileIsOpen){
try {
this->fileToStr(openFilename);
} catch (...) {
fileIsOpen = false;
openFilename = "";
}
}
// Check and save
if ( (fileIsOpen && textField->toPlainText().trimmed() != this->fileToStr(openFilename).trimmed()) || (!fileIsOpen && !textField->toPlainText().isEmpty()) ){
QMessageBox saveBox;
saveBox.setText("The document has been modified.");
saveBox.setInformativeText("Do you want to save your changes?");
saveBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
saveBox.setDefaultButton(QMessageBox::Save);
int save = saveBox.exec();
switch (save) {
case QMessageBox::Save:
this->saveFile();
return true;
case QMessageBox::Discard:
return true;
case QMessageBox::Cancel:
return false;
default:
return false;
}
}
// If file is unchanged, allow action
return true;
}