| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2025 Benjamin Nauck <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include <QMenu> |
| 25 |
#include <QClipboard> |
| 26 |
#include <QApplication> |
| 27 |
#include <QStatusBar> |
| 28 |
#include <QAction> |
| 29 |
#include <QContextMenuEvent> |
| 30 |
#include <QHideEvent> |
| 31 |
|
| 32 |
#include "StatusBarLabel.h" |
| 33 |
#include <App/Application.h> |
| 34 |
|
| 35 |
namespace Gui |
| 36 |
{ |
| 37 |
|
| 38 |
StatusBarLabel::StatusBarLabel(QWidget* parent, const std::string& parameterName) |
| 39 |
: QLabel(parent) |
| 40 |
{ |
| 41 |
if (!parameterName.empty()) { |
| 42 |
hGrp = App::GetApplication().GetParameterGroupByPath( |
| 43 |
"User parameter:BaseApp/Preferences/MainWindow" |
| 44 |
); |
| 45 |
|
| 46 |
// set visibility before storing parameterName to avoid saving it immediately |
| 47 |
setVisible(hGrp->GetBool(parameterName.c_str(), true)); |
| 48 |
|
| 49 |
// now we can store parameterName |
| 50 |
this->parameterName = parameterName; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
void StatusBarLabel::contextMenuEvent(QContextMenuEvent* event) |
| 55 |
{ |
| 56 |
QMenu menu(this); |
| 57 |
|
| 58 |
// Reproduce standard status bar widget menu |
| 59 |
if (auto* statusBar = qobject_cast<QStatusBar*>(parentWidget())) { |
| 60 |
for (QObject* child : statusBar->children()) { |
| 61 |
QWidget* widget = qobject_cast<QWidget*>(child); |
| 62 |
if (!widget) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
auto title = widget->windowTitle(); |
| 66 |
if (title.isEmpty()) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
QAction* action = menu.addAction(title); |
| 71 |
action->setCheckable(true); |
| 72 |
action->setChecked(widget->isVisible()); |
| 73 |
QObject::connect(action, &QAction::toggled, widget, &QWidget::setVisible); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (textInteractionFlags() & Qt::TextSelectableByMouse) { |
| 78 |
menu.addSeparator(); // ---------- |
| 79 |
|
| 80 |
// Copy + Select All |
| 81 |
menu.addAction(tr("Copy"), [this]() { |
| 82 |
QApplication::clipboard()->setText(this->selectedText()); |
| 83 |
}); |
| 84 |
menu.addAction(tr("Select All"), [this]() { this->setSelection(0, this->text().length()); }); |
| 85 |
} |
| 86 |
|
| 87 |
menu.exec(event->globalPos()); |
| 88 |
} |
| 89 |
|
| 90 |
void StatusBarLabel::setVisible(bool visible) |
| 91 |
{ |
| 92 |
if (!parameterName.empty() && hGrp) { |
| 93 |
hGrp->SetBool(parameterName.c_str(), visible); |
| 94 |
} |
| 95 |
if (!visible) { |
| 96 |
clear(); // Clear text |
| 97 |
} |
| 98 |
QLabel::setVisible(visible); |
| 99 |
} |
| 100 |
|
| 101 |
} // namespace Gui |