-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
150 lines (121 loc) · 3.64 KB
/
Copy pathmainwindow.cpp
File metadata and controls
150 lines (121 loc) · 3.64 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
#include <QDebug>
#include <QFileDialog>
#include <QFile>
#include <QSettings>
#include <QCloseEvent>
#include "bitslinger.h"
#include "listenerdialog.h"
#include "settingsdialog.h"
#include "abstracttool.h"
#include "utils/recentfilesmenu.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#define QLL QLatin1Literal
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_ui(new Ui::MainWindow),
m_slinger(0)
{
m_ui->setupUi(this);
m_ui->statusBar->setVisible(false);
m_ui->toolsTab->setTabBarAutoHide(true);
setWindowIcon(QIcon(":icons/soundwave.svg"));
m_recent = new RecentFilesMenu(this);
connect(this, SIGNAL(fileOpened(QString)), m_recent, SLOT(addRecentFile(QString)));
connect(this, SIGNAL(fileSaved(QString)), m_recent, SLOT(addRecentFile(QString)));
connect(m_recent, SIGNAL(recentFileTriggered(QString)), this, SLOT(openState(QString)));
m_recent->setTitle(tr("Open Recent"));
m_ui->menuBitslinger->insertMenu(m_ui->action_Save_State, m_recent);
connect(m_ui->action_Listeners, SIGNAL(triggered()), this, SLOT(showListenerDialog()));
connect(m_ui->action_Options, SIGNAL(triggered()), this, SLOT(showSettings()));
connect(m_ui->action_Load_State, SIGNAL(triggered()), this, SLOT(openState()));
connect(m_ui->action_Save_State, SIGNAL(triggered()), this, SLOT(saveState()));
}
MainWindow::~MainWindow()
{
delete m_ui;
}
void MainWindow::addTool(AbstractTool *tool)
{
m_ui->toolsTab->addTab(tool->widget(), tool->icon(), tool->name());
}
void MainWindow::setBitSlinger(BitSlinger *slinger)
{
m_slinger = slinger;
foreach(AbstractTool *tool, slinger->tools()) {
addTool(tool);
}
}
void MainWindow::loadGuiSettings()
{
QSettings settings;
settings.beginGroup(QLL("GUI"));
m_recent->restoreState(settings.value(QLL("RecentFiles")).toByteArray());
foreach(AbstractTool *tool, m_slinger->tools()) {
tool->restoreState(settings.value(tool->name()).toByteArray());
}
}
void MainWindow::saveGuiSettings()
{
QSettings settings;
settings.beginGroup(QLL("GUI"));
settings.setValue(QLL("RecentFiles"), m_recent->saveState());
foreach(AbstractTool *tool, m_slinger->tools()) {
settings.setValue(tool->name(), tool->saveState());
}
}
void MainWindow::showListenerDialog()
{
qDebug() << "showListenerDialog" << m_slinger;
ListenerDialog dlg(this);
dlg.setWindowIcon(QIcon(":icons/stethoscope.svg"));
dlg.setBitSlinger(m_slinger);
dlg.exec();
}
void MainWindow::showSettings()
{
SettingsDialog dlg(m_slinger ,this);
dlg.exec();
}
void MainWindow::openState()
{
qDebug() << "openState" << m_slinger;
QString filename = QFileDialog::getOpenFileName(this);
if (filename.isEmpty())
return;;
openState(filename);
}
bool MainWindow::openState(const QString &filename)
{
QFile f(filename);
if (!f.open(QIODevice::ReadOnly)) {
qDebug() << "Could not open file" << filename;
return false;
}
emit fileOpened(filename);
return m_slinger->readState(&f);
}
void MainWindow::saveState()
{
qDebug() << "saveState" << m_slinger;
QString filename = QFileDialog::getSaveFileName(this);
if (filename.isEmpty())
return;;
saveState(filename);
}
bool MainWindow::saveState(const QString &filename)
{
QFile f(filename);
if (!f.open(QIODevice::WriteOnly)) {
qDebug() << "Could not open file" << filename;
return false;
}
emit fileSaved(filename);
return m_slinger->writeState(&f);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
//### Check if we should really exit
event->accept();
saveGuiSettings();
}