diff --git a/Software/PC_Application/LibreVNA-GUI.pro b/Software/PC_Application/LibreVNA-GUI.pro index 5665ce2..b12ef96 100644 --- a/Software/PC_Application/LibreVNA-GUI.pro +++ b/Software/PC_Application/LibreVNA-GUI.pro @@ -125,7 +125,6 @@ HEADERS += \ json.hpp \ modehandler.h \ mode.h \ - modetabwidget.h \ modewindow.h \ preferences.h \ savable.h \ @@ -247,7 +246,6 @@ SOURCES += \ main.cpp \ modehandler.cpp \ mode.cpp \ - modetabwidget.cpp \ modewindow.cpp \ preferences.cpp \ scpi.cpp \ @@ -312,7 +310,6 @@ FORMS += \ VNA/s2pImportOptions.ui \ aboutdialog.ui \ main.ui \ - modewindow.ui \ preferencesdialog.ui DISTFILES += diff --git a/Software/PC_Application/appwindow.cpp b/Software/PC_Application/appwindow.cpp index 7cb3f28..9e157fa 100644 --- a/Software/PC_Application/appwindow.cpp +++ b/Software/PC_Application/appwindow.cpp @@ -227,12 +227,15 @@ AppWindow::AppWindow(QWidget *parent) } modeHandler = new ModeHandler(this); - auto modeWindow = new ModeWindow(modeHandler, this); + new ModeWindow(modeHandler, this); - setCentralWidget(modeWindow); - modeHandler->createMode("Vector Network Analyzer", Mode::Type::VNA); + central = new QStackedWidget; + setCentralWidget(central); + + auto vnaIndex = modeHandler->createMode("Vector Network Analyzer", Mode::Type::VNA); modeHandler->createMode("Spectrum Analyzer", Mode::Type::SA); modeHandler->createMode("Signal Generator", Mode::Type::SG); + modeHandler->setCurrentIndex(vnaIndex); auto setModeStatusbar = [=](const QString &msg) { lModeInfo.setText(msg); @@ -1128,28 +1131,29 @@ void AppWindow::LoadSetup(nlohmann::json j) modeHandler->closeModes(); - // old style VNA/Generator/Spectrum Analyzer settings + /* old style VNA/Generator/Spectrum Analyzer settings, + * no more than one instance in each mode running */ if(j.contains("VNA")) { - modeHandler->createMode("Vector Network Analyzer", Mode::Type::VNA); - auto * vna = static_cast(modeHandler->findFirstOfType(Mode::Type::VNA)); + auto vnaIndex = modeHandler->createMode("Vector Network Analyzer", Mode::Type::VNA); + auto *vna = static_cast(modeHandler->getMode(vnaIndex)); vna->fromJSON(j["VNA"]); } if(j.contains("Generator")) { - modeHandler->createMode("Generator", Mode::Type::SG); - auto * generator = static_cast(modeHandler->findFirstOfType(Mode::Type::SG)); + auto sgIndex = modeHandler->createMode("Generator", Mode::Type::SG); + auto *generator = static_cast(modeHandler->getMode(sgIndex)); generator->fromJSON(j["Generator"]); } if(j.contains("SpectrumAnalyzer")) { - modeHandler->createMode("Spectrum Analyzer", Mode::Type::SA); - auto * spectrumAnalyzer = static_cast(modeHandler->findFirstOfType(Mode::Type::SA)); + auto saIndex = modeHandler->createMode("Spectrum Analyzer", Mode::Type::SA); + auto *spectrumAnalyzer = static_cast(modeHandler->getMode(saIndex)); spectrumAnalyzer->fromJSON(j["SpectrumAnalyzer"]); } if(j.contains("Modes")) { for(auto jm : j["Modes"]) { auto type = Mode::TypeFromName(QString::fromStdString(jm.value("type", "Invalid"))); if(type != Mode::Type::Last && jm.contains("settings")) { - modeHandler->createMode(QString::fromStdString(jm.value("name", "")), type); - auto m = modeHandler->getMode(modeHandler->getCurrentIndex()); + auto index = modeHandler->createMode(QString::fromStdString(jm.value("name", "")), type); + auto m = modeHandler->getMode(index); m->fromJSON(jm["settings"]); } } @@ -1159,7 +1163,8 @@ void AppWindow::LoadSetup(nlohmann::json j) QString modeName = QString::fromStdString(j.value("activeMode", "")); for(auto m : modeHandler->getModes()) { if(m->getName() == modeName) { - m->activate(); + auto index = modeHandler->findIndex(m); + modeHandler->setCurrentIndex(index); break; } } @@ -1174,6 +1179,11 @@ Device *&AppWindow::getDevice() return device; } +QStackedWidget *AppWindow::getCentral() const +{ + return central; +} + Ui::MainWindow *AppWindow::getUi() const { return ui; diff --git a/Software/PC_Application/appwindow.h b/Software/PC_Application/appwindow.h index d3ec2b1..8b1269e 100644 --- a/Software/PC_Application/appwindow.h +++ b/Software/PC_Application/appwindow.h @@ -41,6 +41,7 @@ public: ~AppWindow(); Ui::MainWindow *getUi() const; + QStackedWidget *getCentral() const; Device*&getDevice(); const QString& getAppVersion() const; diff --git a/Software/PC_Application/mode.cpp b/Software/PC_Application/mode.cpp index 787163f..998b0db 100644 --- a/Software/PC_Application/mode.cpp +++ b/Software/PC_Application/mode.cpp @@ -32,6 +32,8 @@ Mode::~Mode() if(activeMode == this) { deactivate(); } + window->getCentral()->removeWidget(central); + delete central; for(auto d : docks) { delete d; } @@ -64,7 +66,7 @@ void Mode::activate() } QSettings settings; - + window->getCentral()->setCurrentWidget(central); // restore dock and toolbar positions window->restoreState(settings.value("windowState_"+name).toByteArray()); @@ -179,6 +181,7 @@ Mode *Mode::createNew(AppWindow *window, QString name, Mode::Type t) void Mode::finalize(QWidget *centralWidget) { central = centralWidget; + window->getCentral()->addWidget(central); // Set ObjectName for toolbars and docks for(auto d : docks) { d->setObjectName(d->windowTitle()+name); @@ -224,8 +227,3 @@ void Mode::updateGraphColors() } } } - -QWidget *Mode::getCentral() const -{ - return central; -} diff --git a/Software/PC_Application/mode.h b/Software/PC_Application/mode.h index a031d75..ce1627c 100644 --- a/Software/PC_Application/mode.h +++ b/Software/PC_Application/mode.h @@ -45,8 +45,6 @@ public: static Mode *createNew(AppWindow *window, QString name, Type t); - virtual QWidget *getCentral() const; - signals: void statusbarMessage(QString msg); protected: diff --git a/Software/PC_Application/modehandler.cpp b/Software/PC_Application/modehandler.cpp index 61357c7..b565e2b 100644 --- a/Software/PC_Application/modehandler.cpp +++ b/Software/PC_Application/modehandler.cpp @@ -18,19 +18,23 @@ void ModeHandler::shutdown() } } -void ModeHandler::createMode(QString name, Mode::Type t) +int ModeHandler::createMode(QString name, Mode::Type t) { auto mode = Mode::createNew(aw, name, t); - createMode(mode); + return createMode(mode); } -void ModeHandler::createMode(Mode *mode) +int ModeHandler::createMode(Mode *mode) { modes.push_back(mode); - currentModeIndex = int(modes.size()); + currentModeIndex = int(modes.size()) - 1; connect(mode, &Mode::statusbarMessage, this, &ModeHandler::setStatusBarMessageChanged); - emit ModeCreated(currentModeIndex - 1); + auto * m = getMode(currentModeIndex); + m->activate(); + + emit ModeCreated(currentModeIndex); + return (currentModeIndex); } Mode* ModeHandler::getMode(int index) @@ -45,10 +49,12 @@ std::vector ModeHandler::getModes() void ModeHandler::setCurrentIndex(int index) { - if ( (currentModeIndex != index) && (index >= 0)) { +// if ( (getCurrentIndex() != index) && (index >= 0)) { + if ( (getCurrentIndex() != index) && (index >= 0)) { currentModeIndex = index; - auto * mode = modes.at(currentModeIndex); - mode->activate(); + auto * m = getMode(getCurrentIndex()); + m->activate(); + emit CurrentModeChanged(getCurrentIndex()); } } @@ -62,10 +68,10 @@ void ModeHandler::closeMode(int index) disconnect(modes.at(index), &Mode::statusbarMessage, this, &ModeHandler::setStatusBarMessageChanged); delete modes.at(index); modes.erase(modes.begin() + index); - if (currentModeIndex > int(modes.size()) ) { - setCurrentIndex(currentModeIndex - 1); // Select bar before one deleted - auto vna = modes.at(currentModeIndex); - vna->activate(); + if (int(modes.size()) > 0) { + if (getCurrentIndex() == index) { + setCurrentIndex(getCurrentIndex()-1); // Select bar before one deleted + } } emit ModeClosed(index); } @@ -95,6 +101,12 @@ bool ModeHandler::nameAllowed(const QString &name) return true; } +int ModeHandler::findIndex(Mode *targetMode) +{ + auto it = std::find(modes.begin(), modes.end(), targetMode); + return it - modes.begin(); +} + Mode* ModeHandler::findFirstOfType(Mode::Type t) { for(auto m : modes) { diff --git a/Software/PC_Application/modehandler.h b/Software/PC_Application/modehandler.h index b41c758..683701b 100644 --- a/Software/PC_Application/modehandler.h +++ b/Software/PC_Application/modehandler.h @@ -15,7 +15,7 @@ public: ~ModeHandler() = default; void shutdown(); - void createMode(QString name, Mode::Type t); + int createMode(QString name, Mode::Type t); void closeMode(int index); void closeModes(); int getCurrentIndex(); @@ -24,6 +24,7 @@ public: std::vector getModes(); bool nameAllowed(const QString &name); + int findIndex(Mode *targetMode); Mode* findFirstOfType(Mode::Type t); void setAveragingMode(Averaging::Mode m); @@ -33,6 +34,7 @@ signals: void ModeCreated(int modeIndex); void ModeClosed(int modeIndex); + void CurrentModeChanged(int modeIndex); public slots: void setCurrentIndex(int modeIndex); @@ -40,7 +42,7 @@ public slots: private: std::vector modes; int currentModeIndex; - void createMode(Mode *mode); + int createMode(Mode *mode); AppWindow *aw; private slots: diff --git a/Software/PC_Application/modetabwidget.cpp b/Software/PC_Application/modetabwidget.cpp deleted file mode 100644 index e2170b9..0000000 --- a/Software/PC_Application/modetabwidget.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "modetabwidget.h" - -#include -#include - -ModeTabWidget::ModeTabWidget(QWidget* parent): - QTabWidget(parent) -{ - tabBar = new QTabBar; - tabBar->setStyleSheet("QTabBar::tab { height: " + QString::number(parent->height()) + "px;}"); - this->setTabBar(tabBar); - this->setTabsClosable(true); - this->setMovable(true); -} diff --git a/Software/PC_Application/modetabwidget.h b/Software/PC_Application/modetabwidget.h deleted file mode 100644 index 0e1e786..0000000 --- a/Software/PC_Application/modetabwidget.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef MODETABWIDGET_H -#define MODETABWIDGET_H - -#include -#include - -class ModeTabWidget: public QTabWidget -{ - Q_OBJECT -public: - explicit ModeTabWidget(QWidget* parent = nullptr); - ~ModeTabWidget() = default; - -private: - QTabBar * tabBar = nullptr; -}; - -#endif // MODETABWIDGET_H diff --git a/Software/PC_Application/modewindow.cpp b/Software/PC_Application/modewindow.cpp index cacbb38..0e4c94a 100644 --- a/Software/PC_Application/modewindow.cpp +++ b/Software/PC_Application/modewindow.cpp @@ -1,7 +1,6 @@ #include "modewindow.h" #include "mode.h" -#include "ui_modewindow.h" #include "appwindow.h" #include "CustomWidgets/informationbox.h" @@ -14,31 +13,34 @@ ModeWindow::ModeWindow(ModeHandler* handler, AppWindow* aw, QWidget* parent): handler(handler), aw(aw) { - ui = new Ui::ModeWindow; - ui->setupUi(this); SetupUi(); connect(handler, &ModeHandler::ModeCreated, this, &ModeWindow::ModeCreated); connect(handler, &ModeHandler::ModeClosed, this, &ModeWindow::ModeClosed); + connect(handler, &ModeHandler::CurrentModeChanged, this, &ModeWindow::CurrentModeChanged); - connect(ui->tabwidgetModes, &ModeTabWidget::currentChanged, handler, &ModeHandler::setCurrentIndex); - connect(ui->tabwidgetModes, &ModeTabWidget::tabCloseRequested, handler, &ModeHandler::closeMode); - + connect(tabBar, &QTabBar::currentChanged, handler, &ModeHandler::setCurrentIndex); + connect(tabBar, &QTabBar::tabCloseRequested, handler, &ModeHandler::closeMode); } ModeWindow::~ModeWindow() { - delete ui; - ui = nullptr; } void ModeWindow::SetupUi() { - ui->horizontalLayout->setSpacing(0); - ui->horizontalLayout->setMargin(0); - ui->horizontalLayout->setContentsMargins(0,0,0,0); - ui->tabwidgetModes->setUsesScrollButtons(true); + auto cornerWidget = new QWidget(); + cornerWidget->setLayout(new QHBoxLayout); + cornerWidget->layout()->setSpacing(0); + cornerWidget->layout()->setMargin(0); + cornerWidget->layout()->setContentsMargins(0,0,0,0); + cornerWidget->setMaximumHeight(aw->menuBar()->height()); + + tabBar = new QTabBar; + tabBar->setStyleSheet("QTabBar::tab { height: " + QString::number(aw->menuBar()->height()) + "px;}"); + tabBar->setTabsClosable(true); + cornerWidget->layout()->addWidget(tabBar); auto bAdd = new QPushButton(); QIcon icon; @@ -50,7 +52,7 @@ void ModeWindow::SetupUi() icon.addFile(QString::fromUtf8(":/icons/add.png"), QSize(), QIcon::Normal, QIcon::Off); bAdd->setIcon(icon); - bAdd->setMaximumHeight(450); + bAdd->setMaximumHeight(aw->menuBar()->height()); bAdd->setMaximumWidth(40); auto mAdd = new QMenu(); @@ -75,7 +77,10 @@ void ModeWindow::SetupUi() }); } bAdd->setMenu(mAdd); - aw->menuBar()->setCornerWidget(bAdd); + + cornerWidget->layout()->addWidget(bAdd); + + aw->menuBar()->setCornerWidget(cornerWidget); } void ModeWindow::ModeCreated(int modeIndex) @@ -85,15 +90,27 @@ void ModeWindow::ModeCreated(int modeIndex) if (mode) { const auto name = mode->getName(); - auto central = mode->getCentral(); - const auto tabIndex = ui->tabwidgetModes->insertTab(modeIndex, central, name); - ui->tabwidgetModes->setCurrentIndex(tabIndex); + + tabBar->blockSignals(true); + tabBar->insertTab(modeIndex, name); + tabBar->blockSignals(false); + + tabBar->setCurrentIndex(modeIndex); } } void ModeWindow::ModeClosed(int modeIndex) { - auto modeWidget = ui->tabwidgetModes->widget(modeIndex); - ui->tabwidgetModes->removeTab(modeIndex); - delete modeWidget; + tabBar->blockSignals(true); + tabBar->removeTab(modeIndex); + tabBar->blockSignals(false); +} + + +void ModeWindow::CurrentModeChanged(int modeIndex) +{ + if (modeIndex != tabBar->currentIndex()) + { + tabBar->setCurrentIndex(modeIndex); + } } diff --git a/Software/PC_Application/modewindow.h b/Software/PC_Application/modewindow.h index 4a31286..c4a8139 100644 --- a/Software/PC_Application/modewindow.h +++ b/Software/PC_Application/modewindow.h @@ -3,10 +3,6 @@ #include "modehandler.h" -namespace Ui { - class ModeWindow; -} - class ModeWindow: public QWidget { Q_OBJECT @@ -16,13 +12,14 @@ public: private: ModeHandler* handler; - Ui::ModeWindow *ui; void SetupUi(); AppWindow* aw; + QTabBar* tabBar; private slots: void ModeCreated(int modeIndex); void ModeClosed(int modeIndex); + void CurrentModeChanged(int modeIndex); }; #endif // MODEWINDOW_H diff --git a/Software/PC_Application/modewindow.ui b/Software/PC_Application/modewindow.ui deleted file mode 100644 index 4f9e2d3..0000000 --- a/Software/PC_Application/modewindow.ui +++ /dev/null @@ -1,45 +0,0 @@ - - - ModeWindow - - - - 0 - 0 - 22 - 29 - - - - Form - - - - - - Qt::LeftToRight - - - -1 - - - false - - - true - - - - - - - - ModeTabWidget - QTabWidget -
modetabwidget.h
- 1 -
-
- - -