1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-19 08:31:11 +00:00
CLK/OSBindings/Qt/mainwindow.cpp

75 lines
2.1 KiB
C++
Raw Normal View History

#include <QtWidgets>
#include <QObject>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "timer.h"
2020-06-02 02:08:21 +00:00
/*
General Qt implementation notes:
* it seems like Qt doesn't offer a way to constrain the aspect ratio of a view by constraining
the size of the window (i.e. you can use a custom layout to constrain a view, but that won't
affect the window, so isn't useful for this project). Therefore the emulation window
*/
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
createActions();
// Set up the emulation timer. Bluffer's guide: the QTimer will post an
// event to an event loop. QThread is a thread with an event loop.
// My class, Timer, will be wired up to receive the QTimer's events.
qTimer = std::make_unique<QTimer>(this);
qTimer->setInterval(1);
timerThread = std::make_unique<QThread>(this);
timer = std::make_unique<Timer>();
timer->moveToThread(timerThread.get());
connect(qTimer.get(), SIGNAL(timeout()), timer.get(), SLOT(tick()));
// Start the thread and timer.
// TODO: not until there's actually something to display.
timerThread->start();
qTimer->start();
}
void MainWindow::createActions() {
// Create a file menu.
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
// QAction *newAct = new QAction(tr("&New"), this);
// newAct->setShortcuts(QKeySequence::New);
// newAct->setStatusTip(tr("Create a new file"));
// connect(newAct, &QAction::triggered, this, &MainWindow::newFile);
// fileMenu->addAction(newAct);
// Add file option: 'Open..."
QAction *openAct = new QAction(tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, &QAction::triggered, this, &MainWindow::open);
fileMenu->addAction(openAct);
}
void MainWindow::open() {
QString fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty()) {
qDebug() << "Should open" << fileName;
}
}
MainWindow::~MainWindow() {
// Stop the timer by asking its QThread to exit and
// waiting for it to do so.
timerThread->exit();
while(timerThread->isRunning());
}