-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainwindow.cpp
115 lines (103 loc) · 3.69 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
#include "mainwindow.h"
template <>
QMenu *MainWindow::get<QMenu>(std::string name) {
if (Menus.find(name) == Menus.end())
Menus[name] = menuBar()->addMenu(QString::fromStdString(name));
return Menus[name];
}
template <>
QToolBar *MainWindow::get<QToolBar>(std::string name) {
if (ToolBars.find(name) == ToolBars.end())
ToolBars[name] = new QToolBar(QString::fromStdString(name),this);
return ToolBars[name];
}
template <>
QAction *MainWindow::get<QAction>(std::string name) {
if (Actions.find(name) == Actions.end())
Actions[name] = new QAction(QString::fromStdString(name),this);
return Actions[name];
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
InitToolBar();
InitGraph();
}
MainWindow::~MainWindow() {}
void MainWindow::InitToolBar() {
// clang-format off
this->addToolBar(Qt::TopToolBarArea, get<QToolBar>("ToolBar"));
addActionShowToolBar<QToolBar>("ToolBar","Put tool","Put");
addActionWith<QToolBar>("Put","Mode",&MainWindow::on_DragMode_triggered);
addActionWith<QToolBar>("Put","Resistor",&MainWindow::on_Resistor_triggered);
addActionWith<QToolBar>("Put","Wire",&MainWindow::on_Wire_triggered);
addActionShowToolBar<QToolBar>("ToolBar","File tool","File");
addActionWith<QToolBar>("File","Open",&MainWindow::open_file_triggered);
addActionWith<QToolBar>("File","Save",&MainWindow::save_file_triggered);
get<QToolBar>("File")->setVisible(false);
this->addToolBar(Qt::LeftToolBarArea, get<QToolBar>("Put"));
this->addToolBar(Qt::LeftToolBarArea, get<QToolBar>("File"));
// clang-format on
resize(1000, 800);
}
void MainWindow::showToolBar(QToolBar *toolBar) {
foreach (QToolBar *bar, findChildren<QToolBar *>()) {
if(toolBarArea(bar) != Qt::TopToolBarArea)
bar->setVisible(bar == toolBar);
}
}
void MainWindow::InitGraph() {
scene = new QGraphicsScene(this);
scene->setSceneRect(0, 0, 800, 600);
view = new GraphicsView(scene, this);
setCentralWidget(view);
}
void MainWindow::open_file_triggered() {
qDebug() << "Open Test";
auto file_name = QFileDialog::getOpenFileName(
this, tr("Open File"), "",
tr("TIFF Files (*.tif *.tiff);;All Files (*)"));
if (!file_name.isEmpty()) {
QFile file(file_name);
if (file.open(QIODevice::ReadOnly)) {
QDataStream in(&file);
scene->clear(); // 清空现有场景
while (!in.atEnd()) {
componentType ty;
QPointF pos;
// in >> ty;
in >> pos;
if (ty == _Resistor) {
Resistor *resistor = new Resistor();
resistor->setPos(pos);
scene->addItem(resistor);
}
}
file.close();
}
}
}
void MainWindow::save_file_triggered() {
auto file_name = QFileDialog::getSaveFileName(
this, tr("Save File"), "",
tr("TIFF Files (*.tif *.tiff);;All Files (*)"));
qDebug() << "Save Test" << file_name;
if (!file_name.isEmpty()) {
// 保存图形的基本信息
QFile file(file_name);
if (file.open(QIODevice::WriteOnly)) {
QDataStream out(&file);
// 遍历场景中的每个项并保存其信息
for (auto item : scene->items()) {
if (auto resistor = dynamic_cast<Resistor *>(item)) {
out << _Resistor;
out << resistor->pos();
}
}
file.close();
}
}
}
void MainWindow::on_file_triggered() { qDebug() << "Test file"; }
void MainWindow::on_Resistor_triggered() { view->placeResistor(); }
void MainWindow::on_Capacitor_triggered() { view->placeCapacitor(); }
void MainWindow::on_Wire_triggered() { view->placeWire();}
void MainWindow::on_DragMode_triggered() {view->changemode();}