-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainWindow.cpp
More file actions
178 lines (149 loc) · 5.38 KB
/
mainWindow.cpp
File metadata and controls
178 lines (149 loc) · 5.38 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <QDebug>
#include <QFile>
#include <QMenuBar>
#include <QSignalMapper>
#include <QMessageBox>
#include "common.h"
#include "componentGraph.h"
#include "componentGraphReader.h"
#include "componentGraphScene.h"
#include "componentGraphView.h"
#include "componentGraphWriter.h"
#include "mainWindow.h"
#include "inputDialogs.h"
#include "components/testComponent.h"
#include "components/intGeneratorComponent.h"
#include "components/boolGeneratorComponent.h"
#include "components/printIntComponent.h"
#include "components/rippleCarryAdder.h"
#include "components/binaryMultiplier.h"
#include "components/instructionFetcher.h"
#include "components/arithmeticLogicUnit.h"
#include "components/controlUnit.h"
#include "components/registers.h"
#include "components/luaComponent.h"
namespace CutePathSim
{
MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
{
setupUi(this);
// add the graph view
m_componentGraphView = new ComponentGraphView(this);
m_componentGraphScene = new ComponentGraphScene();
m_componentGraphView->setScene(m_componentGraphScene);
setCentralWidget(m_componentGraphView);
// FIXME: remove this test code
IntGeneratorComponent *outputs42 = new IntGeneratorComponent("Outputs_14", 14);
IntGeneratorComponent *outputs5 = new IntGeneratorComponent("Outputs_3", 3);
PrintIntComponent *printInt = new PrintIntComponent("Print_Int");
BinaryMultiplier *multiplier = new BinaryMultiplier("BinaryMultiplier", 8);
multiplier->setLayout(Component::EXPANDED);
m_componentGraphScene->addComponent(outputs42);
m_componentGraphScene->addComponent(outputs5);
m_componentGraphScene->addComponent(printInt);
m_componentGraphScene->addComponent(multiplier);
outputs42->getOutput("output")->connect(multiplier->getInput("a"));
outputs5->getOutput("output")->connect(multiplier->getInput("b"));
multiplier->getOutput("product")->connect(printInt->getInput("input"));
ControlUnit *controlUnit = new ControlUnit("Control Unit");
m_componentGraphScene->addComponent(controlUnit);
InstructionFetcher *instructionFetcher = new InstructionFetcher("Instruction Fetcher");
m_componentGraphScene->addComponent(instructionFetcher);
ArithmeticLogicUnit *alu = new ArithmeticLogicUnit("ALU");
m_componentGraphScene->addComponent(alu);
Registers *registers = new Registers("Registers");
m_componentGraphScene->addComponent(registers);
LuaComponent *lua = new LuaComponent("Lua Component");
m_componentGraphScene->addComponent(lua);
instructionFetcher->getOutput("output")->connect(controlUnit->getInput("instruction"));
m_componentGraphScene->rootGraph()->run();
// populate the menus
m_viewMenu->addAction(m_componentGraphView->zoomInAction());
m_viewMenu->addAction(m_componentGraphView->zoomOutAction());
m_viewMenu->addAction(m_componentGraphView->fitViewAction());
// connect the signals/slots
connect(m_addComponent, SIGNAL(activated()), this, SLOT(addComponent()));
connect(m_runSimulation, SIGNAL(activated()), this, SLOT(runSimulation()));
connect(m_fileSave, SIGNAL(activated()), this, SLOT(saveComponentGraph()));
connect(m_fileOpen, SIGNAL(activated()), this, SLOT(loadComponentGraph()));
}
MainWindow::~MainWindow()
{
delete m_componentGraphView;
delete m_componentGraphScene;
}
void MainWindow::newSimulation()
{
}
void MainWindow::openSimulation()
{
}
void MainWindow::runSimulation()
{
m_componentGraphScene->rootGraph()->run();
}
void MainWindow::addComponent()
{
QString name = promptString(tr("Enter a unique name for the component."));
if(m_componentGraphScene->rootGraph()->getComponent(name) != 0)
{
QMessageBox::warning(this, tr("Component name not unique"), tr("Component with that name already exists. Please enter a unique name."));
return;
}
m_componentGraphScene->rootGraph()->addComponent(new LuaComponent(name));
}
void MainWindow::saveComponentGraph()
{
QFile file("testFile.xml");
if(file.open(QIODevice::WriteOnly))
{
ComponentGraphWriter writer(m_componentGraphScene->rootGraph());
writer.writeFile(&file);
file.close();
}
else
{
QMessageBox::warning(this, tr("Failed to open file for writing"), file.errorString());
}
}
void MainWindow::loadComponentGraph()
{
QFile file("testFile.xml");
if(file.open(QIODevice::ReadOnly))
{
m_componentGraphScene->clearRootGraph();
ComponentGraphReader reader(m_componentGraphScene->rootGraph());
reader.readFile(&file);
file.close();
}
else
{
QMessageBox::warning(this, tr("Failed to open file for reading"), file.errorString());
}
}
void MainWindow::addDock(QWidget *widget)
{
if(m_guiDocks.contains(widget))
return;
QDockWidget *dock = new QDockWidget(widget->windowTitle(), this);
dock->setWidget(widget);
addDockWidget(Qt::LeftDockWidgetArea, dock);
}
void MainWindow::removeDock(QWidget *widget)
{
if(!m_guiDocks.contains(widget))
return;
delete m_guiDocks.take(widget);
return;
}
bool MainWindow::event(QEvent *event)
{
if(event->type() == ComponentDockEvent::EventType)
{
addDock(static_cast<ComponentDockEvent*>(event)->component()->getToolBox());
return true;
}
return QMainWindow::event(event);
}
MainWindow *mainWindow;
}