From 0cdc4b138a33e290d2441fe5690f2d99720e417a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Mon, 12 May 2025 17:04:23 +0200 Subject: [PATCH] new feature: changing the name of modes --- .../LibreVNA-GUI/modehandler.cpp | 10 +++++--- .../PC_Application/LibreVNA-GUI/modehandler.h | 2 +- .../LibreVNA-GUI/modewindow.cpp | 24 +++++++++++++++++++ .../PC_Application/LibreVNA-GUI/modewindow.h | 1 + 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Software/PC_Application/LibreVNA-GUI/modehandler.cpp b/Software/PC_Application/LibreVNA-GUI/modehandler.cpp index dc33f2f..cab8f5d 100644 --- a/Software/PC_Application/LibreVNA-GUI/modehandler.cpp +++ b/Software/PC_Application/LibreVNA-GUI/modehandler.cpp @@ -179,10 +179,14 @@ void ModeHandler::setStatusBarMessageChanged(const QString &msg) } } -bool ModeHandler::nameAllowed(const QString &name) +bool ModeHandler::nameAllowed(const QString &name, unsigned int ignoreIndex) { - for(auto m : modes) { - if(m->getName() == name) { + for(unsigned int i=0;igetName() == name) { /* name already taken, no duplicates allowed * when importing, name is used as value */ diff --git a/Software/PC_Application/LibreVNA-GUI/modehandler.h b/Software/PC_Application/LibreVNA-GUI/modehandler.h index b371ad9..a839882 100644 --- a/Software/PC_Application/LibreVNA-GUI/modehandler.h +++ b/Software/PC_Application/LibreVNA-GUI/modehandler.h @@ -29,7 +29,7 @@ public: Mode* getMode(int index); std::vector getModes(); - bool nameAllowed(const QString &name); + bool nameAllowed(const QString &name, unsigned int ignoreIndex=-1); int findIndex(Mode *targetMode); Mode* findFirstOfType(Mode::Type t); diff --git a/Software/PC_Application/LibreVNA-GUI/modewindow.cpp b/Software/PC_Application/LibreVNA-GUI/modewindow.cpp index ec3e3f8..f521971 100644 --- a/Software/PC_Application/LibreVNA-GUI/modewindow.cpp +++ b/Software/PC_Application/LibreVNA-GUI/modewindow.cpp @@ -45,6 +45,9 @@ void ModeWindow::SetupUi() tabBar->setStyleSheet("QTabBar::tab { height: " + QString::number(aw->menuBar()->height()) + "px;}"); tabBar->setTabsClosable(true); cornerWidget->layout()->addWidget(tabBar); + connect(tabBar, &QTabBar::tabBarDoubleClicked, this, [=](int index) { + renameMode(index); + }); auto bAdd = new QPushButton(); QIcon icon; @@ -94,6 +97,11 @@ void ModeWindow::SetupUi() menu->addSeparator(); auto submenuAdd = new QMenu("Create new"); menu->addMenu(submenuAdd); + auto rename = new QAction("Rename active mode"); + connect(rename, &QAction::triggered, this, [=](){ + renameMode(handler->getCurrentIndex()); + }); + menu->addAction(rename); auto mAdd = new QMenu(); for(unsigned int i=0;i<(int) Mode::Type::Last;i++) { @@ -180,3 +188,19 @@ void ModeWindow::CurrentModeChanged(int modeIndex) } menuActions[modeIndex]->setChecked(true); } + +void ModeWindow::renameMode(int modeIndex) +{ + auto mode = handler->getMode(modeIndex); + auto newName = QInputDialog::getText(this, "Rename", "Enter new name for mode \""+mode->getName()+"\":"); + if(newName.isEmpty()) { + return; + } + if(handler->nameAllowed(newName, modeIndex)) { + mode->setName(newName); + tabBar->setTabText(modeIndex, newName); + menu->actions()[modeIndex]->setText(newName); + } else { + InformationBox::ShowError("Error", "Unable to set name. Mode names must be unique."); + } +} diff --git a/Software/PC_Application/LibreVNA-GUI/modewindow.h b/Software/PC_Application/LibreVNA-GUI/modewindow.h index eb468ad..2597cf7 100644 --- a/Software/PC_Application/LibreVNA-GUI/modewindow.h +++ b/Software/PC_Application/LibreVNA-GUI/modewindow.h @@ -29,6 +29,7 @@ private slots: void ModeCreated(int modeIndex); void ModeClosed(int modeIndex); void CurrentModeChanged(int modeIndex); + void renameMode(int modeIndex); }; #endif // MODEWINDOW_H