forked from alesapin/LSPL-IDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
centralwidget.cpp
210 lines (185 loc) · 7.17 KB
/
centralwidget.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
#include "centralwidget.h"
#include <QDebug>
const int CentralWidget::DEFAULT_TIMEOUT = 30000;
const QString CentralWidget::CRASH_PATTERNS = "crash.pat";
const QString CentralWidget::CRASH_TEXT = "crash.txt";
CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), timeOut(DEFAULT_TIMEOUT)
{
watcher = new QFutureWatcher<QSharedPointer<PatternViewMap>>();
compiler = new PatternCompiler();
txt = new TextBasicWidget(this);
pattern = new PatternsBasicWidget(compiler,this);
matches = new MatchesBasicWidget(this);
timeoutTimer = new QTimer(this);
timeoutTimer->setSingleShot(true);
QSplitter* vert = new QSplitter();
QSplitter* horiz = new QSplitter();
QWidget* containter = new QWidget();
QHBoxLayout* containterLay = new QHBoxLayout();
QVBoxLayout* mainLay = new QVBoxLayout();
containterLay->setContentsMargins(0,0,0,0);
horiz->addWidget(txt);
horiz->addWidget(pattern);
containterLay->addWidget(horiz);
containter->setLayout(containterLay);
vert->setOrientation(Qt::Vertical);
vert->addWidget(containter);
vert->addWidget(matches);
// vert->addWidget(progress);
mainLay->addWidget(vert);
setLayout(mainLay);
connect(pattern,SIGNAL(matchClicked()), this,SLOT(slotAnalyze()));
connect(matches,SIGNAL(patternWasUnchecked(QString)),txt,SLOT(slotPatternUncheked(QString)));
connect(matches,SIGNAL(patternWasChecked(QString)),txt,SLOT(slotPatternChecked(QString)));
connect(matches,SIGNAL(showAll()),txt,SLOT(slotHighlightAll()));
connect(matches,SIGNAL(showAll()),pattern,SLOT(slotDisableMatch()));
connect(matches,SIGNAL(hideAll()),txt,SLOT(slotDehighlightAll()));
connect(matches,SIGNAL(rowClicked(int,int)),txt,SLOT(slotSelectFragment(int,int)));
connect(matches,SIGNAL(patternWasUnchecked(QString)),this,SIGNAL(statusHighlighting()));
connect(matches,SIGNAL(patternWasChecked(QString)),this,SIGNAL(statusHighlighting()));
connect(txt,SIGNAL(tabChanged(int)),matches,SLOT(slotChangeTab(int)));
connect(txt,SIGNAL(tabClosed(int)),matches,SLOT(slotCloseTab(int)));
connect(txt,SIGNAL(editEnabled()),matches,SLOT(slotClear()));
connect(txt,SIGNAL(editEnabled()),this,SLOT(slotEdit()));
connect(txt,SIGNAL(checkingEnabled()),matches,SLOT(slotEnableChecking()));
connect(txt,SIGNAL(checkingEnabled()),this,SIGNAL(statusReady()));
connect(txt, SIGNAL(checkingEnabled()),pattern, SLOT(slotEnableMatch()));
connect(watcher,SIGNAL(finished()),this,SLOT(slotDisplay()));
connect(timeoutTimer,SIGNAL(timeout()),this,SLOT(slotTimeout()));
//connect(watcher,SIGNAL(progressValueChanged(int)),this,SLOT(slotProgress(int)));
if (QFile::exists(CRASH_TEXT) && QFile::exists(CRASH_PATTERNS)){
loadAfterCrash(CRASH_TEXT, CRASH_PATTERNS);
}
}
CentralWidget::~CentralWidget()
{
delete compiler;
}
TextBasicWidget *CentralWidget::getTextWidget()
{
return txt;
}
PatternsBasicWidget *CentralWidget::getPatternWidget()
{
return pattern;
}
MatchesBasicWidget *CentralWidget::getMatchesWidget()
{
return matches;
}
PatternCompiler *CentralWidget::getPatternCompiler()
{
return compiler;
}
QStringList CentralWidget::getChoosenPatterns()
{
return pattern->getChoosenPatternsNames();
}
void CentralWidget::loadAfterCrash(const QString &textFileName, const QString &patternFileName)
{
txt->openTextFile(textFileName);
pattern->importPatterns(patternFileName);
QFile::remove(textFileName);
QFile::remove(patternFileName);
}
void CentralWidget::createBackup() const
{
QFile text(CRASH_TEXT);
if(text.open(QFile::WriteOnly | QFile::Text)){
QTextStream ts(&text);
ts << txt->getText();
}
text.close();
QFile pats(CRASH_PATTERNS);
if(pats.open(QFile::WriteOnly | QFile::Text)){
QTextStream ps(&pats);
ps << pattern->getPatternsAsText();
}
pats.close();
}
void CentralWidget::slotAnalyze()
{
QStringList patternNames = pattern->getChoosenPatternsNames();
if(!patternNames.isEmpty() && !txt->getText().isEmpty()){
emit statusEngine();
txt->setReadOnly(true);
QFuture<QSharedPointer<PatternViewMap>> future = QtConcurrent::run(compiler,&PatternCompiler::analyzeText,patternNames,txt->getText());
watcher->setFuture(future);
timeoutTimer->start(timeOut);
} else {
pattern->slotEnableMatch();
}
}
void CentralWidget::slotDisplay()
{
if(!watcher->future().isCanceled()){
timeoutTimer->stop();
QStringList patternNames = pattern->getChoosenPatternsNames();
QSharedPointer<PatternViewMap> m = watcher->future().result();
QSharedPointer<utility::IntervalViewMap> convertedResult = utility::convertMatchesToIntervals(*m);
if(!convertedResult->empty() && !txt->getText().isEmpty()){
emit statusHighlighting();
txt->setMatches(convertedResult);
matches->setMatches(convertedResult,patternNames);
}else if(convertedResult->empty()){
matches->setMatches(convertedResult,patternNames);
}
}
}
void CentralWidget::slotEdit()
{
watcher->cancel();
emit statusCanceled();
}
/**
* TODO
* @brief CentralWidget::slotTimeout
* Ужасный метод, вызываемый из-за невозможности убить
* поток в Qt. Переделать!!!
*/
void CentralWidget::slotTimeout()
{
QMessageBox msgBox(
QMessageBox::Information,
tr("Нет ответа"),
tr("<p align='center'>Наложение шаблонов задумалось... \
Возможно Вы написали неправильные шаблоны, или \
предложенный Вами текст слишком большой. Проверьте \
код более тщательно. Рекомендуется перечитать документацию \
шаблонов и перезагрузить приложение."),
QMessageBox::Yes | QMessageBox::No);
msgBox.setButtonText(QMessageBox::Yes, tr("Reboot app"));
msgBox.setButtonText(QMessageBox::No, tr("Wait"));
if (msgBox.exec() == QMessageBox::Yes) {
createBackup();
watcher->thread()->sleep(1);
QProcess::startDetached(QApplication::applicationFilePath());
int pid = QCoreApplication::applicationPid();
#ifndef Q_OS_WIN
system(QString("kill " + QString::number(pid)).toStdString().c_str());
#endif
QCoreApplication::quit();
}
}
void CentralWidget::slotProgress(int val)
{
//qDebug() << "Progress Value:" << val;
}
void CentralWidget::slotSaveLspl(const QString &filename)
{
xml.setMatches(matches->getSelectedMatches());
xml.setPatterns(pattern->getPatterns());
xml.setText(txt->getText());
xml.writeFile(filename);
}
void CentralWidget::slotLoadLspl(const QString &filename)
{
if(xml.readFile(filename)){
pattern->loadPatterns(xml.getPatterns());
txt->addText(xml.getText());
QSharedPointer<utility::IntervalViewMap> mths = xml.getMatches();
emit statusHighlighting();
txt->setMatches(mths);
matches->setMatches(mths);
}
}