forked from fzyz999/Lucky_OIer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeeditor.cpp
136 lines (111 loc) · 3.01 KB
/
codeeditor.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
/*
* This file is part of Lucky OIer
*
* Copyright (C) 2012 by Wang Luming <wlm199558@126.com>
*
* Lucky OIer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Lucky OIer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Lucky OIer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "codeeditor.h"
codeEditor::codeEditor(QWidget *parent):
QsciScintilla(parent)
{
init();
}
codeEditor::codeEditor(const QString &name, QWidget *parent):
QsciScintilla(parent)
{
init();
open(name);
}
void codeEditor::init()
{
setAutoIndent(true);
setAutoCompletionFillupsEnabled(true);
file_path.clear();
}
bool codeEditor::open(QString name)
{
QFile file;
QFileInfo fileInfo;
file.setFileName(name);
fileInfo.setFile(file);
if(file.open(QIODevice::ReadWrite|QIODevice::Text))
{
if(!read(&file))
return false;
file_path=name;
name=fileInfo.suffix();
if(name=="cpp"||name=="c")
setLexer(new QsciLexerCPP);
else if(name=="pas")
setLexer(new QsciLexerPascal);
return true;
}
return false;
}
bool codeEditor::save()
{
QFile file;
if(file_path.isEmpty())
{
QSettings settings;
file.setFileName(QFileDialog::getSaveFileName(
this,tr("Save File"),
settings.value("files/historyDir").toString()));
}
else
{
file.setFileName(file_path);
}
if(file.open(QIODevice::ReadWrite|QIODevice::Text))
{
if(!write(&file))
return false;
}
return true;
}
void codeEditor::close()
{
if(isModified())
{
QMessageBox msgbox(this);
msgbox.setText(tr("The document has been modified."));
msgbox.setInformativeText(tr("Do you want to save your changes?"));
msgbox.setDefaultButton(QMessageBox::Save);
msgbox.setStandardButtons(QMessageBox::Discard|QMessageBox::Cancel);
int ret=msgbox.exec();
switch (ret)
{
case QMessageBox::Save:
// Save was clicked
if(!save())
{
//Save file failed
}
break;
case QMessageBox::Discard:
// Don't Save was clicked
break;
case QMessageBox::Cancel:
// Cancel was clicked
return ;
break;
default:
// should never be reached
qWarning("codeeditor.cpp: switch(ret) reached an unexcepted code!");
break;
}
}
deleteLater();
}