-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.cpp
More file actions
162 lines (118 loc) · 3.53 KB
/
expression.cpp
File metadata and controls
162 lines (118 loc) · 3.53 KB
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
#include "expression.h"
#include "ui_expression.h"
#include "inputwidget.h"
#include "showwidget.h"
#include "calculatewidget.h"
#include "compoundwidget.h"
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
Expression::Expression(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Expression)
{
ui->setupUi(this);
// connect(ui->actionOpen_O, &QAction::triggered, this, &Expression::on_actionOpen_O_triggered);
// connect(ui->actionSave_S, &QAction::triggered, this, &Expression::on_actionSave_S_triggered);
}
void Expression::on_actionOpen_O_triggered()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("打开文件"),tr(""),tr("文本文件 (*.txt);;所有文件 (*)"));
if (filePath.isEmpty()) {
return;
}
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("错误"), tr("无法打开文件:\n%1").arg(file.errorString()));
return;
}
QTextStream in(&file);
QString line;
expressions.clear();
while(!in.atEnd())
{
line = in.readLine();
if (line.trimmed().isEmpty()) continue;
expressions.push_back(createExpr(line));
}
file.close();
}
void Expression::on_actionSave_S_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("保存文件"),
"",
tr("文本文件 (*.txt);;所有文件 (*)"));
if (fileName.isEmpty()) {
return;
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
QMessageBox::warning(this, tr("错误"), tr("无法保存文件:\n%1").arg(file.errorString()));
return;
}
QTextStream out(&file);
for(size_t i = 0; i < expressions.size(); ++i)
{
out << expressions[i].prefix << "\n";
}
file.close();
}
Expression::~Expression()
{
delete ui;
}
void Expression::on_inputButton_clicked()
{
InputWidget *input = new InputWidget;
input->setAttribute(Qt::WA_DeleteOnClose);
this->hide();
input->show();
connect(input,&InputWidget::returnExpr,this,&Expression::getExpressions);
connect(input,&InputWidget::destroyed,[=]{
this->show();
});
}
void Expression::getExpressions(const std::vector<Expr>& vec)
{
for(size_t i = 0; i < vec.size(); ++i)
{
expressions.push_back(vec[i]);
}
}
void Expression::getExpression(const Expr& exp)
{
expressions.push_back(exp);
}
void Expression::on_showButton_clicked()
{
ShowWidget *show = new ShowWidget(expressions);
show->setAttribute(Qt::WA_DeleteOnClose);
this->hide();
show->show();
connect(show,&ShowWidget::destroyed,[=]{
this->show();
});
}
void Expression::on_evaluateButton_clicked()
{
CalculateWidget *evaluate = new CalculateWidget(expressions);
evaluate->setAttribute(Qt::WA_DeleteOnClose);
this->hide();
evaluate->show();
connect(evaluate,&CalculateWidget::destroyed,[=]{
this->show();
});
}
void Expression::on_compoundButton_clicked()
{
CompoundWidget *compound = new CompoundWidget(expressions);
compound->setAttribute(Qt::WA_DeleteOnClose);
this->hide();
compound->show();
connect(compound,&CompoundWidget::returnExpr,this,&Expression::getExpression);
connect(compound,&CompoundWidget::destroyed,[=]{
this->show();
});
}