-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditortab.cpp
executable file
·147 lines (126 loc) · 3.88 KB
/
editortab.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
#include "editortab.h"
#include <QGridLayout>
#include <Qsci/qscilexerpython.h>
#include <Qsci/qscilexerhtml.h>
#include <Qsci/qscilexerjavascript.h>
#include <QFontDatabase>
#include <Qsci/qsciapis.h>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QTabWidget>
EditorTab::EditorTab(bool hostfile, QWidget *parent) : QWidget(parent),
hostfile(hostfile)
{
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->setSpacing(6);
gridLayout->setContentsMargins(0,0,0,0);
gridLayout->setObjectName(QStringLiteral("gridLayout"));
ed = new QsciScintilla();
QsciLexer * lexer = new QsciLexerPython();
ed->setAutoIndent(true);
ed->setIndentationsUseTabs(false);
ed->setIndentationWidth(4);
ed->setMarginLineNumbers(1,true);
ed->setMarginWidth(1,30);
ed->setAutoCompletionCaseSensitivity(false);
ed->setAutoCompletionThreshold(2);
ed->setAutoCompletionFillupsEnabled(true);
ed->setAutoCompletionSource(QsciScintilla::AcsAll);
ed->setEdgeMode(QsciScintilla::EdgeLine);
ed->setEdgeColumn(80);
ed->setEdgeColor(QColor(Qt::lightGray));
// QsciAPIs * api = new QsciAPIs(lexer);
// if (!api->load("microPython.api"))
// qDebug() << "false";
// api->add("asdf");
// api->prepare();
QFont font = QFont("Monospace",12);
font.setStyleHint(QFont::Monospace);
lexer->setFont(font);
ed->setLexer(lexer);
gridLayout->addWidget(ed);
mFile = new QFile();
mFileContent = "";
}
void EditorTab::set_current_content(QString content){
QByteArray aux;
aux.clear();
mFileContent = aux.append(content);
}
void EditorTab::setLexer(QString filePath){
if(filePath.endsWith(".py")){
ed->setLexer(new QsciLexerPython());
}
else if(filePath.endsWith(".html"))
ed->setLexer(new QsciLexerHTML());
else if(filePath.endsWith(".js"))
ed->setLexer(new QsciLexerJavaScript());
QFont font = QFont("Monospace",12);
font.setStyleHint(QFont::Monospace);
if(ed->lexer())
ed->lexer()->setFont(font);
}
EditorTab::EditorTab(QString filePath, bool hostfile, QWidget *parent) : QWidget(parent),
mFilePath(filePath),
hostfile(hostfile)
{
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->setSpacing(6);
gridLayout->setContentsMargins(0,0,0,0);
gridLayout->setObjectName(QStringLiteral("gridLayout"));
ed = new QsciScintilla(this);
ed->setMarginLineNumbers(1,true);
setLexer(filePath);
ed->setAutoIndent(true);
gridLayout->addWidget(ed);
mFile = new QFile();
openFile(filePath);
connect(ed,SIGNAL(textChanged()),this,SLOT(on_content_changed()));
}
void EditorTab::on_content_changed(){
QString edtxt = ed->text();
content_changed(ed->text()!=QString(mFileContent));
}
void EditorTab::set_host_file(bool host){
hostfile = host;
}
void EditorTab::openFile(QString path){
delete mFile;
mFile = new QFile(path);
mFile->open(QIODevice::ReadOnly);
mFileContent = mFile->readAll();
ed->setText(mFileContent);
mFile->close();
}
void EditorTab::saveFile(QString path){
mFileContent.clear();
mFileContent.append(ed->text());
if(hostfile){
delete mFile;
mFile = new QFile(path);
mFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
mFile->write(ed->text().toUtf8());
mFile->close();
mFilePath = QDir().absoluteFilePath(path);
}else{
// QByteArray lines;
// for (int i=0; i<ed->lines();i++)
// lines.append(ed->text());
QString a = QString(ed->text());
saveTargetFile(path,ed->text().toUtf8());
mFilePath = path;
}
on_content_changed();
}
EditorTab::~EditorTab(){
}
void EditorTab::setFilePath(QString filePath){
mFilePath = filePath;
}
QString EditorTab::getFilePath(){
return mFilePath;
}
bool EditorTab::is_host_file(){
return hostfile;
}