-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmainwindow.cpp
174 lines (170 loc) · 6.5 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QFileDialog>
#include <QFile>
#include <QTextStream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
firstLoad=true;
ui->setupUi(this);
setUpHighlighter();
//init status bar
ui->outputText->parentWindow=this;
ui->statusBar->showMessage(tr("Ready"));
//--------init toolbar------------
//ui->statusBar->setStyleSheet("QStatusBar{background:rgb(50,50,50);}");
ui->mainToolBar->setMovable(false);
ui->mainToolBar->setStyleSheet("QToolButton:hover {background-color:darkgray} QToolBar {background: rgb(82,82,82);border: none;}");
//--------------------------------
runIcon.addPixmap(QPixmap(":/image/Run.png"));
stopIcon.addPixmap(QPixmap(":/image/stop.png"));
//---------窗口背景颜色-------------
QPalette windowPalette=this->palette();
windowPalette.setColor(QPalette::Active,QPalette::Window,QColor(82,82,82));
windowPalette.setColor(QPalette::Inactive,QPalette::Window,QColor(82,82,82));
this->setPalette(windowPalette);
//--------------------------------
initFileData();
connect(ui->actionNewFile,SIGNAL(triggered(bool)),this,SLOT(newFile()));
connect(ui->actionOpen,SIGNAL(triggered(bool)),this,SLOT(openFile()));
connect(ui->actionSave_File,SIGNAL(triggered(bool)),this,SLOT(saveFile()));
connect(ui->actionUndo,SIGNAL(triggered(bool)),this,SLOT(undo()));
connect(ui->actionRedo,SIGNAL(triggered(bool)),this,SLOT(redo()));
connect(ui->editor,SIGNAL(textChanged()),this,SLOT(changeSaveState()));
connect(ui->actionRun,SIGNAL(triggered(bool)),this,SLOT(run()));
connect(&process,SIGNAL(finished(int)),this,SLOT(runFinished(int)));
connect(&process,SIGNAL(readyReadStandardOutput()),this,SLOT(updateOutput()));
connect(&process,SIGNAL(readyReadStandardError()),this,SLOT(updateError()));
connect(ui->actionAbout,SIGNAL(triggered(bool)),this,SLOT(about()));
fileSaved=true;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setUpHighlighter(){
QFont font;
font.setFamily("Courier");
font.setFixedPitch(true);
//font.setPointSize(20);
ui->editor->setFont(font);
ui->editor->setTabStopWidth(fontMetrics().width(QLatin1Char('9'))*4);
highlighter=new Highlighter(ui->editor->document());
}
void MainWindow::resizeEvent(QResizeEvent *event){
QMainWindow::resizeEvent(event);
ui->editor->setGeometry(10,0,width()-20,height()-ui->statusBar->height()-ui->mainToolBar->height()-80-15);
ui->outputText->setGeometry(10,ui->editor->height()+10,this->width()-20,80);
}
void MainWindow::initFileData(){
fileName=tr("Untitled.cpp");
filePath=tr("~/Desktop/Untitled.cpp");
fileSaved=true;
isRunning=false;
}
void MainWindow::undo(){
ui->editor->undo();
}
void MainWindow::redo(){
ui->editor->redo();
}
void MainWindow::saveFile(){
QString savePath=QFileDialog::getSaveFileName(this,tr("选择保存路径与文件名"),fileName,tr("Cpp File(*.cpp *.c *.h)"));
if(!savePath.isEmpty()){
QFile out(savePath);
out.open(QIODevice::WriteOnly|QIODevice::Text);
QTextStream str(&out);
str<<ui->editor->toPlainText();
out.close();
fileSaved=true;
QRegularExpression re(tr("(?<=\\/)\\w+\\.cpp|(?<=\\/)\\w+\\.c|(?<=\\/)\\w+\\.h"));
fileName=re.match(savePath).captured();
filePath=savePath;
this->setWindowTitle(tr("HJ Editor - ")+fileName);
}
}
void MainWindow::newFile(){
MainWindow *newWindow=new MainWindow();
QRect newPos=this->geometry();
newWindow->setGeometry(newPos.x()+10,newPos.y()+10,newPos.width(),newPos.height());
newWindow->show();
}
void MainWindow::openFile(){
if(!fileSaved){
if(QMessageBox::Save==QMessageBox::question(this,tr("文件未保存"),tr("当前文件没有保存,是否保存?"),QMessageBox::Save,QMessageBox::Cancel))
saveFile();
}
QString openPath=QFileDialog::getOpenFileName(this,tr("选择要打开的文件"),filePath,tr("Cpp File(*.cpp *.c *.h)"));
if(!openPath.isEmpty()){
QFile in(openPath);
in.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream str(&in);
ui->editor->setPlainText(str.readAll());
QRegularExpression re(tr("(?<=\\/)\\w+\\.cpp|(?<=\\/)\\w+\\.c|(?<=\\/)\\w+\\.h"));
fileName=re.match(openPath).captured();
this->setWindowTitle(tr("HJ Editor - ")+fileName);
filePath=openPath;
fileSaved=true;
}
}
void MainWindow::run(){
if(isRunning){
process.terminate();
ui->actionRun->setIcon(runIcon);
return;
}
if(!fileSaved){
if(QMessageBox::Save==QMessageBox::question(this,tr("文件未保存"),tr("文件保存后才能运行,是否保存?"),QMessageBox::Save,QMessageBox::Cancel))
saveFile();
}
if(fileSaved){
//if(process!=nullptr)delete process;
isRunning=true;
ui->statusBar->showMessage(tr("程序运行中..."));
ui->outputText->clear();
output.clear();
error.clear();
QString buildPath;
QRegularExpression re(tr(".*(?=\\.cpp)|.*(?=\\.c)|.*(?=\\.h)"));
buildPath=re.match(filePath).captured();
//qDebug()<<buildPath;
process.start("bash", QStringList() << "-c" << QString(tr("g++ ")+filePath+tr(" -o ")+buildPath+tr(";")+buildPath));
process.waitForStarted();
ui->outputText->setFocus();
ui->actionRun->setIcon(stopIcon);
}
}
void MainWindow::runFinished(int code){
ui->actionRun->setIcon(runIcon);
isRunning=false;
qDebug()<<tr("exit code=")<<code;
ui->statusBar->showMessage(tr("Ready"));
}
void MainWindow::updateOutput(){
output=QString::fromLocal8Bit(process.readAllStandardOutput());
//ui->outputText->setPlainText(output+tr("\n")+error);
ui->outputText->setPlainText(ui->outputText->toPlainText()+output);//+tr("\n"));
}
void MainWindow::updateError(){
error=QString::fromLocal8Bit(process.readAllStandardError());
//ui->outputText->setPlainText(output+tr("\n")+error);
ui->outputText->setPlainText(ui->outputText->toPlainText()+error);//+tr("\n"));
process.terminate();
isRunning=false;
}
void MainWindow::inputData(QString data){
if(isRunning)process.write(data.toLocal8Bit());
}
void MainWindow::closeEvent(QCloseEvent *event){
if(!fileSaved){
if(QMessageBox::Save==QMessageBox::question(this,tr("未保存就要退出?"),tr("当前文件没有保存,是否保存?不保存文件改动将会丢失"),QMessageBox::Save,QMessageBox::Cancel))
saveFile();
fileSaved=true;
}
}
void MainWindow::about(){
QMessageBox::information(this,tr("关于"),tr(" HJ-Editor v1.0 \n 何振邦倾情奉献 \n更多信息访问huajihome.cn"),QMessageBox::Ok);
}