forked from regarm/u8085
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
286 lines (251 loc) · 11.3 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
currentFileName(tr(""))
{
ui->setupUi(this);
setWindowTitle(QString(tr("%1[*] - %2").arg(tr("untitled")).arg(tr("u8085"))));
documentWidget = new QTextEdit();
setCentralWidget(documentWidget);
connect(documentWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)));
createActions();
createMenus();
createToolbars();
createDocks();
statusBar()->showMessage(tr("done"));
connect(regDock->M, SIGNAL(textEdited(QString)), memDock, SLOT(MCellChanged(QString)));
connect(regDock->M, SIGNAL(textEdited(QString)),&__8085, SLOT(MCellChanged(QString)));
connect(&__8085.H, SIGNAL(regEditedInternally_SIG(QString)), this, SLOT(HLChanged()));
connect(&__8085.L, SIGNAL(regEditedInternally_SIG(QString)), this, SLOT(HLChanged()));
connect(&__8085.H, SIGNAL(regEditedFromRegDock_SIG(QString)), this, SLOT(HLChanged()));
connect(&__8085.L, SIGNAL(regEditedFromRegDock_SIG(QString)), this, SLOT(HLChanged()));
previousHL = (__8085.H.toInt() << 8) + __8085.L.toInt();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createActions(){
newFileAction = new QAction(tr("&New"), this);
newFileAction->setShortcut(QKeySequence::New);
newFileAction->setStatusTip(tr("Create a new file"));
connect(newFileAction, SIGNAL(triggered()), this, SLOT(newFile()));
openFileInNewWindowAction = new QAction(tr("&Open in new window"), this);
openFileInNewWindowAction -> setShortcut(QKeySequence::Open);
openFileInNewWindowAction -> setStatusTip(tr("Open in new window"));
connect(openFileInNewWindowAction, SIGNAL(triggered(bool)), this, SLOT(fileOpenInNewWindow()));
saveAction = new QAction(tr("&Save"), this);
saveAction -> setShortcut(QKeySequence::Save);
saveAction -> setStatusTip(tr("Save current document"));
connect(saveAction, SIGNAL(triggered(bool)), this, SLOT(saveDoc()));
saveAsAction = new QAction(tr("&Save &as"), this);
saveAsAction -> setShortcut(QKeySequence::SaveAs);
saveAsAction -> setStatusTip(tr("Save current document with differnt file name"));
connect(saveAsAction, SIGNAL(triggered(bool)), this, SLOT(saveAsDoc()));
exitAction = new QAction(tr("&Close"), this);
exitAction -> setShortcut(QKeySequence::Close);
exitAction -> setStatusTip(tr("Close"));
connect(exitAction, SIGNAL(triggered(bool)), this, SLOT(exit()));
undoAction = new QAction(tr("&Undo"), this);
undoAction -> setShortcut(QKeySequence::Undo);
undoAction -> setStatusTip(tr("Undo"));
connect(undoAction, SIGNAL(triggered(bool)), documentWidget->document(), SLOT(undo()));
redoAction = new QAction(tr("&Redo"), this);
redoAction -> setShortcut(QKeySequence::Redo);
redoAction -> setStatusTip(tr("Redo"));
connect(redoAction, SIGNAL(triggered(bool)), documentWidget->document(), SLOT(redo()));
cutAction = new QAction(tr("&Cut"), this);
cutAction -> setShortcut(QKeySequence::Cut);
cutAction -> setStatusTip(tr("Cut selected text"));
// connect(cutAction, SIGNAL(triggered(bool)), documentWidget->document(), SLOT());
copyAction = new QAction(tr("&Copy"), this);
copyAction -> setShortcut(QKeySequence::Copy);
copyAction -> setStatusTip(tr("Copy"));
// connect(undoAction, SIGNAL(triggered(bool)), documentWidget->document(), SLOT(undo()));
pasteAction = new QAction(tr("&Paste"), this);
pasteAction -> setShortcut(QKeySequence::Paste);
pasteAction -> setStatusTip(tr("Undo"));
// connect(undoAction, SIGNAL(triggered(bool)), documentWidget->document(), SLOT(undo()));
assembleAction = new QAction(tr("&Assemble"), this);
assembleAction -> setShortcut(tr("Ctrl+B"));
assembleAction -> setStatusTip(tr("Assemble program"));
connect(assembleAction, SIGNAL(triggered(bool)), this, SLOT(assemble()));
assembleAndRunAction = new QAction(tr("&Assemble and run"), this);
assembleAndRunAction -> setShortcut(tr("Ctrl+Shift+B"));
assembleAndRunAction -> setStatusTip(tr("Assemble and run program"));
connect(assembleAndRunAction, SIGNAL(triggered(bool)), this, SLOT(assembleAndRun()));
runAction = new QAction(tr("&Run"), this);
runAction -> setShortcut(tr("Ctrl+R"));
runAction -> setStatusTip(tr("Run"));
connect(runAction, SIGNAL(triggered(bool)), this, SLOT(runAt2000()));
singleStepingAction = new QAction(tr("Single Steping"), this);
singleStepingAction -> setStatusTip(tr("Single Steping"));
connect(singleStepingAction,SIGNAL(triggered(bool)), this, SLOT(singleSteping()));
nextAction = new QAction(tr("Next"), this);
nextAction -> setStatusTip(tr("Ahead to next Instruction"));
connect(nextAction, SIGNAL(triggered(bool)), this, SLOT(next()));
previousAction = new QAction(tr("Previous"), this);
previousAction -> setStatusTip(tr("Go one Instruction back"));
connect(previousAction, SIGNAL(triggered(bool)), this, SLOT(previous()));
// QIcon docNewIcon;
// docNewIcon.addFile(tr(":/icons/document_icon&16.png"),QSize(16,16));
// docNewIcon.addFile(tr(":/icons/document_icon&24.png"),QSize(24,24));
// docNewIcon.addFile(tr(":/icons/document_icon&32.png"),QSize(32,32));
// docNewIcon.addFile(tr(":/icons/document_icon&48.png"),QSize(48,48));
// newAction->setIcon(docNewIcon);
}
void MainWindow::createMenus(){
QMenu *menu;
menu = menuBar()->addMenu(tr("&File"));
menu->addAction(newFileAction);
menu->addAction(openFileInNewWindowAction);
menu->addAction(saveAction);
menu->addAction(saveAsAction);
menu->addAction(exitAction);
menu = menuBar()->addMenu(tr("&Edit"));
menu->addAction(undoAction);
menu->addAction(redoAction);
menu->addAction(cutAction);
menu->addAction(copyAction);
menu->addAction(pasteAction);
menu = menuBar()->addMenu(tr("Build"));
menu->addAction(assembleAction);
menu->addAction(runAction);
menu->addAction(assembleAndRunAction);
menu = menuBar()->addMenu(tr("Debug"));
menu->addAction(singleStepingAction);
menu->addAction(nextAction);
menu->addAction(previousAction);
}
void MainWindow::createToolbars(){
QToolBar *toolBar;
toolBar = addToolBar(tr("File"));
toolBar->addAction(newFileAction);
}
void MainWindow::closeEvent(QCloseEvent *event){
if(isSafeToClose()){
event->accept();
} else{
event->ignore();
}
}
bool MainWindow::isSafeToClose(){
if(isWindowModified()){
switch(QMessageBox::warning(this, tr("u8085"),
tr("This Docment have unsaved changes.\n"
"Do you want to save it before closed?"),
QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel)){
case QMessageBox::Cancel:
return false;
case QMessageBox::Save:
return saveDoc();
default:
return true;
}
}
return true;
}
void MainWindow::createDocks(){
log = new logDock(tr("Logs"), this);
addDockWidget(Qt::BottomDockWidgetArea, log);
memDock = new MemEditorDock(tr("Memory Editor"),&__8085,this);
addDockWidget(Qt::RightDockWidgetArea, memDock);
memDock->setStyleSheet(tr("QDockWidget > QWidget {border : 1px solid black}"));
regDock = new RegEditor(tr("Register Editor"), &__8085, this);
addDockWidget(Qt::LeftDockWidgetArea, regDock);
setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
}
void MainWindow::newFile(){
if(!currentFileName.isEmpty())
(new MainWindow())->show();
}
void MainWindow::fileOpenInNewWindow(){
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Document"),
tr("."),
tr("u8085 files (*.u8085)\n"
"All files (*)")
);
if(fileName.isEmpty()){
return ;
} else if(currentFileName.isEmpty() && !documentWidget->document()->isModified()){
loadFile(fileName);
} else{
MainWindow *nw = new MainWindow();
nw->loadFile(fileName);
nw->show();
}
}
void MainWindow::loadFile(const QString& fileName){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly|QIODevice::Text)){
QMessageBox::warning(this,tr("File opening error"),tr("Failed to open file."));
return ;
}
QTextStream stream(&file);
documentWidget->document()->setPlainText(stream.readAll());
currentFileName = fileName;
documentWidget->document()->setModified(false);
setWindowTitle(tr("%1[*] - %2").arg(fileName).arg(tr("u8085")));
}
bool MainWindow::saveFile(const QString& fileName){
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly|QIODevice::Text)){
QMessageBox::warning(this, tr("File saving error"), tr("Failed to save file."));
return false;
}
QTextStream stream(&file);
stream << documentWidget->document()->toPlainText();
currentFileName = fileName;
documentWidget->document()->setModified(false);
setWindowTitle(tr("%1[*] - %2").arg(fileName).arg(tr("u8085")));
return true;
}
bool MainWindow::saveDoc(){
if(!documentWidget->document()->isModified()){
return true;
}
if(currentFileName.isEmpty()){
return saveAsDoc();
} else{
return saveFile(currentFileName);
}
}
bool MainWindow::saveAsDoc(){
QFileDialog *myFileDialog = new QFileDialog(this);
myFileDialog->setDefaultSuffix(tr(".u8085"));
QString fileName = myFileDialog->getSaveFileName(this,
tr("Save Document"),
QDir::currentPath(),
tr("u8085 files (*.u8085)\n"
"All files (*)")
);
if(fileName.isEmpty()){
return false;
}
return saveFile(fileName);
}
void MainWindow::exit(){
QApplication::exit();
}
void MainWindow::singleSteping(){}
void MainWindow::next(){}
void MainWindow::previous(){}
void MainWindow::HLChanged(){
if(previousHL >= __8085.lowAddressLimit && previousHL < __8085.upAddressLimit){
disconnect(&__8085.cells[previousHL - __8085.lowAddressLimit], SIGNAL(cellChangedInternally_SIG(QString)), regDock->M,SLOT(setText(QString)));
if(previousHL >= memDock->base && previousHL < memDock->base + memDock->gridS*memDock->gridS){
disconnect(memDock->values[previousHL - memDock->base], SIGNAL(textEdited(QString)), regDock->M,SLOT(setText(QString)));
}
}
previousHL = (__8085.H.toInt() << 8) + __8085.L.toInt();
if(previousHL >= __8085.lowAddressLimit && previousHL < __8085.upAddressLimit){
connect(&__8085.cells[previousHL - __8085.lowAddressLimit], SIGNAL(cellChangedInternally_SIG(QString)), regDock->M,SLOT(setText(QString)));
if(previousHL >= memDock->base && previousHL < memDock->base + memDock->gridS*memDock->gridS){
connect(memDock->values[previousHL - memDock->base], SIGNAL(textEdited(QString)), regDock->M,SLOT(setText(QString)));
}
}
}