diff --git a/src/Gui/CMakeLists.txt b/src/Gui/CMakeLists.txt index 6f0bfbbe0c..ac74cbe8cb 100644 --- a/src/Gui/CMakeLists.txt +++ b/src/Gui/CMakeLists.txt @@ -1382,6 +1382,7 @@ SET(FreeCADGui_CPP_SRCS resource.cpp Control.cpp SpaceballEvent.cpp + StatusBarLabel.cpp PreferencePackManager.cpp Thumbnail.cpp Utilities.cpp @@ -1431,6 +1432,7 @@ SET(FreeCADGui_SRCS WaitCursor.h ManualAlignment.h StartupProcess.h + StatusBarLabel.h TransactionObject.h ToolHandler.h StyleParameters/Parser.h diff --git a/src/Gui/InputHintWidget.cpp b/src/Gui/InputHintWidget.cpp index 6fa5b62361..fe82d60a20 100644 --- a/src/Gui/InputHintWidget.cpp +++ b/src/Gui/InputHintWidget.cpp @@ -31,7 +31,7 @@ #include "InputHint.h" #include "InputHintWidget.h" -Gui::InputHintWidget::InputHintWidget(QWidget* parent) : QLabel(parent) +Gui::InputHintWidget::InputHintWidget(QWidget* parent) : StatusBarLabel(parent, "InputHintEnabled") {} void Gui::InputHintWidget::showHints(const std::list& hints) diff --git a/src/Gui/InputHintWidget.h b/src/Gui/InputHintWidget.h index e9a91c5507..356f42924d 100644 --- a/src/Gui/InputHintWidget.h +++ b/src/Gui/InputHintWidget.h @@ -24,16 +24,16 @@ #ifndef INPUTHINTWIDGET_H #define INPUTHINTWIDGET_H -#include #include #include +#include "StatusBarLabel.h" #include "InputHint.h" namespace Gui { -class GuiExport InputHintWidget : public QLabel +class GuiExport InputHintWidget : public StatusBarLabel { Q_OBJECT diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index 04e76a8bfa..19d4546faf 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -103,6 +103,7 @@ #include "ReportView.h" #include "SelectionView.h" #include "SplashScreen.h" +#include "StatusBarLabel.h" #include "ToolBarManager.h" #include "ToolBoxManager.h" #include "Tree.h" @@ -308,62 +309,6 @@ struct MainWindowP void restoreWindowState(const QByteArray &); }; -/** - * @brief Label for displaying information in the status bar - * - * A QLabel subclass that provides a context menu with additional actions - * similar to the standard status bar widgets. - */ -class StatusBarInfoLabel : public QLabel -{ - Q_OBJECT -public: - explicit StatusBarInfoLabel(QWidget *parent = nullptr) - : QLabel(parent) - { - } -protected: - void contextMenuEvent(QContextMenuEvent *event) override - { - QMenu menu(this); - - // Reproduce standard status bar widget menu - if (auto *bar = qobject_cast(parentWidget())) { - for (QObject *child : bar->children()) { - QWidget *w = qobject_cast(child); - if (!w) { - continue; - } - auto title = w->windowTitle(); - if (title.isEmpty()) { - continue; - } - - QAction *action = menu.addAction(title); - action->setCheckable(true); - action->setChecked(w->isVisible()); - QObject::connect(action, &QAction::toggled, w, &QWidget::setVisible); - } - } - - menu.addSeparator(); // ---------- - - // Copy + Select All - menu.addAction(tr("Copy"), [this]() { - QApplication::clipboard()->setText(this->selectedText()); - }); - menu.addAction(tr("Select All"), [this]() { - this->setSelection(0, this->text().length()); - }); - - menu.exec(event->globalPos()); - } - void hideEvent(QHideEvent *event) override { - clear(); // Clear text - QLabel::hideEvent(event); - } -}; - } // namespace Gui /* TRANSLATOR Gui::MainWindow */ @@ -446,7 +391,7 @@ MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags f) // labels and progressbar d->status = new StatusBarObserver(); - d->actionLabel = new StatusBarInfoLabel(statusBar()); + d->actionLabel = new StatusBarLabel(statusBar()); d->actionLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); d->sizeLabel = new DimensionWidget(statusBar()); @@ -464,7 +409,7 @@ MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags f) statusBar()->addWidget(d->hintLabel); // right side label - d->rightSideLabel = new StatusBarInfoLabel(statusBar()); + d->rightSideLabel = new StatusBarLabel(statusBar(), "QuickMeasureEnabled"); d->rightSideLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); statusBar()->addPermanentWidget(d->rightSideLabel); d->rightSideLabel->setObjectName(QStringLiteral("rightSideLabel")); diff --git a/src/Gui/StatusBarLabel.cpp b/src/Gui/StatusBarLabel.cpp new file mode 100644 index 0000000000..c787b27ba6 --- /dev/null +++ b/src/Gui/StatusBarLabel.cpp @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/**************************************************************************** + * * + * Copyright (c) 2025 Benjamin Nauck * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + ***************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +#include "StatusBarLabel.h" +#include + +namespace Gui { + +StatusBarLabel::StatusBarLabel(QWidget *parent, const std::string& parameterName) + : QLabel(parent) +{ + if (!parameterName.empty()) { + hGrp = App::GetApplication().GetParameterGroupByPath( + "User parameter:BaseApp/Preferences/MainWindow"); + + // set visibility before storing parameterName to avoid saving it immediately + setVisible(hGrp->GetBool(parameterName.c_str(), false)); + + // now we can store parameterName + this->parameterName = parameterName; + } +} + +void StatusBarLabel::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu menu(this); + + // Reproduce standard status bar widget menu + if (auto *statusBar = qobject_cast(parentWidget())) { + for (QObject *child : statusBar->children()) { + QWidget *widget = qobject_cast(child); + if (!widget) { + continue; + } + auto title = widget->windowTitle(); + if (title.isEmpty()) { + continue; + } + + QAction *action = menu.addAction(title); + action->setCheckable(true); + action->setChecked(widget->isVisible()); + QObject::connect(action, &QAction::toggled, widget, &QWidget::setVisible); + } + } + + if (textInteractionFlags() & Qt::TextSelectableByMouse) { + menu.addSeparator(); // ---------- + + // Copy + Select All + menu.addAction(tr("Copy"), [this]() { + QApplication::clipboard()->setText(this->selectedText()); + }); + menu.addAction(tr("Select All"), [this]() { + this->setSelection(0, this->text().length()); + }); + } + + menu.exec(event->globalPos()); +} + +void StatusBarLabel::setVisible(bool visible) +{ + if (!parameterName.empty() && hGrp) { + hGrp->SetBool(parameterName.c_str(), visible); + } + if (!visible) { + clear(); // Clear text + } + QLabel::setVisible(visible); +} + +} // namespace Gui diff --git a/src/Gui/StatusBarLabel.h b/src/Gui/StatusBarLabel.h new file mode 100644 index 0000000000..33d0b0e7ca --- /dev/null +++ b/src/Gui/StatusBarLabel.h @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/**************************************************************************** + * * + * Copyright (c) 2025 Benjamin Nauck * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + ***************************************************************************/ + +#ifndef STATUSBARLABEL_H +#define STATUSBARLABEL_H + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace Gui +{ +/** + * @brief Label for displaying information in the status bar + * + * A QLabel subclass that provides a context menu with additional actions + * similar to the standard status bar widgets. + */ +class GuiExport StatusBarLabel : public QLabel +{ + Q_OBJECT +public: + explicit StatusBarLabel(QWidget *parent, const std::string& parameterName = {}); +protected: + void contextMenuEvent(QContextMenuEvent *event) override; + void setVisible(bool visible) override; +private: + ParameterGrp::handle hGrp; + std::string parameterName; +}; + +} // Namespace Gui +#endif //STATUSBARLABEL_H