-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtformdoc.cpp
217 lines (176 loc) · 5.26 KB
/
tformdoc.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
208
209
210
211
212
213
214
215
216
217
#include "tformdoc.h"
#include "ui_tformdoc.h"
#include "codeeditor.h"
#include <QVBoxLayout>
#include <QFileInfo>
#include <QFontDialog>
#include <QSaveFile>
#include <QException>
#include <mainwindow.h>
TFormDoc::TFormDoc(QWidget *parent)
: QWidget(parent)
, ui(new Ui::TFormDoc)
{
ui->setupUi(this);
addTextArea(this->font());
this->setAttribute(Qt::WA_DeleteOnClose); //关闭时自动删除
connect(codeEditor, &CodeEditor::modificationChanged,
this, &QWidget::setWindowModified);
connect(codeEditor,&CodeEditor::dropFile,(MainWindow*)parent,&MainWindow::on_editorDropFile);
connect(this,&TFormDoc::statusMessageChange, (MainWindow*)parent,&MainWindow::showStatusMsg);
connect(codeEditor,&CodeEditor::statusMessageChange, (MainWindow*)parent,&MainWindow::showStatusMsg);
}
void TFormDoc::addTextArea(const QFont &font)
{
QVBoxLayout *verticalLayout = new QVBoxLayout(this);
verticalLayout->setSpacing(0);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
verticalLayout->setContentsMargins(0, 0, 0, 0);
codeEditor = new CodeEditor(this);
codeEditor->setObjectName(QString::fromUtf8("CodeEditor"));
codeEditor->setFont(font);
highlighter = new Highlighter(codeEditor->document());
verticalLayout->addWidget(codeEditor);
}
TFormDoc::~TFormDoc()
{
delete ui;
}
QString TFormDoc::loadFromFile(const QString &aFileName,bool bNewFile)
{//打开文件
QFile aFile(aFileName); //以文件方式读出
QString ret = "";
if (aFile.open(QIODevice::ReadOnly | QIODevice::Text)) //以只读文本方式打开文件
{
QTextStream aStream(&aFile); //用文本流读取文件
codeEditor->clear();
codeEditor->setPlainText(aStream.readAll()); //读取文本文件
aFile.close();
m_filename=aFileName; //保存当前文件名
QFileInfo fileInfo(aFileName); //文件信息
QString str=fileInfo.fileName(); //去除路径后的文件名
this->setToolTip(fileInfo.absoluteFilePath());
this->setWindowTitle(str+"[*]");
m_fileOpened=true;
ret = m_filename;
}
this->m_isNewFile = bNewFile;
return ret;
}
QString TFormDoc::currentFileName()
{
return m_filename;
}
bool TFormDoc::isFileOpened()
{ //文件是否已打开
return m_fileOpened;
}
void TFormDoc::afterFileSaved(const QString& filePath)
{
QFileInfo fileInfo(filePath);
this->setWindowTitle(fileInfo.fileName());
this->setToolTip(fileInfo.absoluteFilePath());
this->setWindowModified(false);
this->m_isNewFile = false;
emit statusMessageChange(QString("保存成功:%1").arg(fileInfo.absoluteFilePath()));
}
#include <QFileDialog>
QString TFormDoc::saveToFile(bool needOtherName )
{
QString filePath(m_filename);
QString name = QFileInfo(filePath).fileName();
if(this->m_isNewFile || needOtherName){
filePath = QFileDialog::getSaveFileName(this, tr("Save File"),
filePath,
tr("All files(*.*);;Text files (*.txt);;Xml files(*.xml *.xsd);;cpp files(*.c *.cc *.cpp *.h *.hpp)"));
}
if(filePath.isEmpty()) return "";
QSaveFile aFile(filePath);
if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text))
return "";
aFile.setDirectWriteFallback(false); //使用临时文件
try
{
QString str=codeEditor->toPlainText(); //整个内容作为字符串
QByteArray strBytes=str.toUtf8(); //转换为字节数组, UTF-8编码
aFile.write(strBytes,strBytes.length()); //写入文件
aFile.commit();
afterFileSaved(filePath);
return m_filename;
}
catch (QException &e)
{
qDebug("保存文件的过程发生了错误");
aFile.cancelWriting(); //出现异常时取消写入
emit statusMessageChange("保存文件的过程发生了错误");
return "";
}
}
void TFormDoc::setEditFont()
{
QFont font;
bool ok;
font=codeEditor->font();
font=QFontDialog::getFont(&ok,font);
codeEditor->setFont(font);
}
void TFormDoc::textCut()
{
codeEditor->cut();
}
void TFormDoc::textCopy()
{
codeEditor->copy();
}
void TFormDoc::textPaste()
{
codeEditor->paste();
}
#include <QInputDialog>
void TFormDoc::locateLine()
{
codeEditor->showGoto();
}
const int FONT_MIN_POINT = 6;
const int FONT_MAX_PIONT = 100;
const int FONT_STEP = 2;
void TFormDoc::zoomIn()
{
QFont font;
font= codeEditor->font();
int newSize = font.pointSize() + FONT_STEP;
if(newSize < FONT_MAX_PIONT){
font.setPointSize(newSize);
codeEditor->setFont(font);
}
}
void TFormDoc::zoomOut()
{
QFont font;
font= codeEditor->font();
int newSize = font.pointSize() - FONT_STEP;
if(newSize >= FONT_MIN_POINT){
font.setPointSize(newSize);
codeEditor->setFont(font);
}
}
void TFormDoc::findNext()
{
codeEditor->findNext();
}
void TFormDoc::findPrev()
{
codeEditor->findPrev();
}
QString TFormDoc::selectedText()
{
return codeEditor->textCursor().selectedText();
}
void TFormDoc::textFind()
{
codeEditor->showFind();
}
void TFormDoc::showTextReplace()
{
codeEditor->showTextReplace();
}