AdvancedControllerRemoteCPP/panel/mainwindow.cpp

92 lines
3 KiB
C++

// mainwindow.cpp - tabs CONTROLLER 1..3 and the selected controller (see mainwindow.h)
#include "mainwindow.h"
#include "deviceview.h"
#include "panelwidget.h"
#include "style.h"
#include "tabbar.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QStackedWidget>
#include <QVBoxLayout>
MainWindow::MainWindow(const QStringList &ports, int baudRate, QWidget *parent)
: QWidget(parent)
{
setWindowTitle(QStringLiteral("AdvancedController - virtual front panel"));
resize(980, 730);
setMinimumSize(660, 480);
QPalette windowPalette = palette();
windowPalette.setColor(QPalette::Window, Style::Background);
windowPalette.setColor(QPalette::WindowText, Style::Text);
setPalette(windowPalette);
setAutoFillBackground(true);
// --- tabs CONTROLLER 1..3 ---
m_tabs = new TabBar(Devices, this);
auto *tabsBox = new QHBoxLayout;
tabsBox->setContentsMargins(8, 8, 8, 0);
tabsBox->addWidget(m_tabs);
// --- one device block per tab; only the selected one is shown, all keep working ---
m_stack = new QStackedWidget(this);
for (int i = 0; i < Devices; ++i) {
auto *device = new DeviceView(i, baudRate, i < ports.size() ? ports.at(i) : QString(), m_stack);
connect(device, &DeviceView::stateChanged, m_tabs, &TabBar::setState);
m_stack->addWidget(device);
m_devices.append(device);
}
auto *root = new QVBoxLayout(this);
root->setContentsMargins(0, 0, 0, 0);
root->setSpacing(0);
root->addLayout(tabsBox);
root->addWidget(m_stack, 1);
connect(m_tabs, &TabBar::selected, this, &MainWindow::select);
select(0);
qApp->installEventFilter(this); // PC keyboard = cross pad
}
MainWindow::~MainWindow()
{
qApp->removeEventFilter(this);
}
void MainWindow::select(int index)
{
m_stack->setCurrentIndex(index);
m_tabs->setActive(index);
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
const bool keyEvent = event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease;
if (keyEvent && isActiveWindow() && !QApplication::activePopupWidget()) {
auto *key = static_cast<QKeyEvent *>(event);
QString button;
switch (key->key()) {
case Qt::Key_Up: button = QStringLiteral("UP"); break;
case Qt::Key_Down: button = QStringLiteral("DOWN"); break;
case Qt::Key_Left: button = QStringLiteral("LEFT"); break;
case Qt::Key_Right: button = QStringLiteral("RIGHT"); break;
case Qt::Key_Return: button = QStringLiteral("ENTER"); break;
default: break;
}
if (!button.isEmpty()) {
if (!key->isAutoRepeat()) { // ignore the key auto-repeat
PanelWidget *panel = m_devices.at(m_stack->currentIndex())->panel();
if (event->type() == QEvent::KeyPress)
panel->press(button);
else
panel->release(button);
}
return true;
}
}
return QWidget::eventFilter(watched, event);
}