From 38416d7b804c339b0fc40290dcbe65b3454367ae Mon Sep 17 00:00:00 2001 From: 0penBrain <48731257+0penBrain@users.noreply.github.com> Date: Sat, 18 Mar 2023 17:28:38 +0100 Subject: [PATCH] Gui: dimension indicator moved to custom widget to handle unit schema change in other places --- src/Gui/MainWindow.cpp | 107 +++++++++++++++++++++++++++-------------- src/Gui/MainWindow.h | 2 - 2 files changed, 70 insertions(+), 39 deletions(-) diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index aee8b4cbe6..239862255c 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -150,6 +150,74 @@ private: int _timeout; }; +/** + * The DimensionWidget class is aiming at providing a widget used in the status bar that will: + * - Allow application to display dimension information such as the viewportsize + * - Provide a popup menu allowing user to change the used unit schema (and update if changed elsewhere) + */ +class DimensionWidget : public QPushButton, WindowParameter +{ + Q_OBJECT + +public: + explicit DimensionWidget(QWidget* parent): QPushButton(parent), WindowParameter("Units") + { + setFlat(true); + setText(qApp->translate("Gui::MainWindow", "Dimension")); + setMinimumWidth(120); + + //create the action buttons + auto* menu = new QMenu(this); + auto* actionGrp = new QActionGroup(menu); + int num = static_cast(Base::UnitSystem::NumUnitSystemTypes); + for (int i = 0; i < num; i++) { + QAction* action = menu->addAction(qApp->translate("Gui::Dialog::DlgSettingsUnits", + Base::UnitsApi::getDescription(static_cast(i)))); + actionGrp->addAction(action); + action->setCheckable(true); + QObject::connect(action, &QAction::toggled, this, [this,i](bool checked) { + if(checked) { + // Set and save the Unit System + Base::UnitsApi::setSchema(static_cast(i)); + getWindowParameter()->SetInt("UserSchema", i); + // Update the application to show the unit change + Gui::Application::Instance->onUpdate(); + } + } ); + } + setMenu(menu); + unitChanged(); + getWindowParameter()->Attach(this); + } + + ~DimensionWidget() + { + getWindowParameter()->Detach(this); + } + + void OnChange(Base::Subject &rCaller, const char * sReason) override + { + Q_UNUSED(rCaller) + if (strcmp(sReason, "UserSchema") == 0) { + unitChanged(); + } + } + +private: + void unitChanged(void) + { + int userSchema = getWindowParameter()->GetInt("UserSchema", 0); + auto actions = menu()->actions(); + if(Q_UNLIKELY(userSchema < 0 || userSchema >= actions.size())) { + userSchema = 0; + } + auto action = actions[userSchema]; + if(!action->isChecked()) { // Using a QSignalBlocker leads to issue + action->setChecked(true); + } + } +}; + // ------------------------------------- // Pimpl class struct MainWindowP @@ -300,7 +368,7 @@ MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags f) d->actionLabel = new QLabel(statusBar()); // d->actionLabel->setMinimumWidth(120); - initializeSizeLabel(); + d->sizeLabel = new DimensionWidget(statusBar()); statusBar()->addWidget(d->actionLabel, 1); QProgressBar* progressBar = Gui::SequencerBar::instance()->getProgressBar(statusBar()); @@ -2081,42 +2149,6 @@ void MainWindow::setPaneText(int i, QString text) } } -void MainWindow::initializeSizeLabel() -{ - d->sizeLabel = new QPushButton(statusBar()); - d->sizeLabel->setFlat(true); - d->sizeLabel->setText(tr("Dimension")); - d->sizeLabel->setMinimumWidth(120); - - //create the button actions - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Units"); - int userSchema = hGrp->GetInt("UserSchema", 0); - QActionGroup* actionGrp = new QActionGroup(d->sizeLabel); - actionGrp->setExclusive(true); - - int num = static_cast(Base::UnitSystem::NumUnitSystemTypes); - for (int i = 0; i < num; i++) { - QAction* action = new QAction(qApp->translate("Gui::Dialog::DlgSettingsUnits", Base::UnitsApi::getDescription(static_cast(i))), this); - action->setCheckable(true); - action->setChecked(i == userSchema); - actionGrp->addAction(action); - - QObject::connect(action, &QAction::changed, - this, [i, hGrp] { - // Set and save the Unit System - Base::UnitsApi::setSchema(static_cast(i)); - hGrp->SetInt("UserSchema", i); - - // Update the application to show the unit change - Gui::Application::Instance->onUpdate(); - } - ); - } - auto menu = new QMenu(d->sizeLabel); - menu->addActions(actionGrp->actions()); - d->sizeLabel->setMenu(menu); -} - void MainWindow::customEvent(QEvent* e) { if (e->type() == QEvent::User) { @@ -2247,3 +2279,4 @@ ActionStyleEvent::Style ActionStyleEvent::getType() const #include "moc_MainWindow.cpp" +#include "MainWindow.moc" diff --git a/src/Gui/MainWindow.h b/src/Gui/MainWindow.h index 2edbe61330..aea0570cac 100644 --- a/src/Gui/MainWindow.h +++ b/src/Gui/MainWindow.h @@ -204,8 +204,6 @@ public: enum StatusType {None, Err, Wrn, Pane, Msg, Log, Tmp, Critical}; void showStatus(int type, const QString & message); - void initializeSizeLabel(); - public Q_SLOTS: /** * Updates the standard actions of a text editor such as Cut, Copy, Paste, Undo and Redo.