| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2024 Kacper Donat <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of FreeCAD. * |
| 5 |
* * |
| 6 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 7 |
* under the terms of the GNU Lesser General Public License as * |
| 8 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 9 |
* License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with FreeCAD. If not, see * |
| 18 |
* <https://www.gnu.org/licenses/>. * |
| 19 |
* * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
|
| 23 |
#include <QHBoxLayout> |
| 24 |
#include <QTimer> |
| 25 |
|
| 26 |
|
| 27 |
#include "MainWindow.h" |
| 28 |
#include "ToolBarAreaWidget.h" |
| 29 |
#include "ToolBarManager.h" |
| 30 |
#include <Base/Tools.h> |
| 31 |
|
| 32 |
using namespace Gui; |
| 33 |
|
| 34 |
ToolBarAreaWidget::ToolBarAreaWidget( |
| 35 |
QWidget* parent, |
| 36 |
ToolBarArea area, |
| 37 |
const ParameterGrp::handle& hParam, |
| 38 |
boost::signals2::scoped_connection& conn, |
| 39 |
QTimer* timer |
| 40 |
) |
| 41 |
: QWidget(parent) |
| 42 |
, _sizingTimer(timer) |
| 43 |
, _hParam(hParam) |
| 44 |
, _conn(conn) |
| 45 |
, _area(area) |
| 46 |
{ |
| 47 |
_layout = new QHBoxLayout(this); |
| 48 |
_layout->setContentsMargins(QMargins()); |
| 49 |
} |
| 50 |
|
| 51 |
void ToolBarAreaWidget::addWidget(QWidget* widget) |
| 52 |
{ |
| 53 |
// if widget already exist don't do anything |
| 54 |
if (_layout->indexOf(widget) >= 0) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if (auto toolbar = qobject_cast<ToolBar*>(widget)) { |
| 59 |
toolbar->updateCustomGripVisibility(); |
| 60 |
} |
| 61 |
|
| 62 |
_layout->addWidget(widget); |
| 63 |
adjustParent(); |
| 64 |
|
| 65 |
QString name = widget->objectName(); |
| 66 |
|
| 67 |
if (!name.isEmpty()) { |
| 68 |
Base::ConnectionBlocker block(_conn); |
| 69 |
_hParam->SetInt(widget->objectName().toUtf8().constData(), _layout->count() - 1); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void ToolBarAreaWidget::insertWidget(int index, QWidget* widget) |
| 74 |
{ |
| 75 |
int currentIndex = _layout->indexOf(widget); |
| 76 |
|
| 77 |
// we are inserting widget at the same place, this is no-op |
| 78 |
if (currentIndex == index) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// widget already exists in the area, we need to first remove it and then recreate |
| 83 |
if (currentIndex > 0) { |
| 84 |
_layout->removeWidget(widget); |
| 85 |
} |
| 86 |
|
| 87 |
_layout->insertWidget(index, widget); |
| 88 |
|
| 89 |
if (auto toolbar = qobject_cast<ToolBar*>(widget)) { |
| 90 |
toolbar->updateCustomGripVisibility(); |
| 91 |
} |
| 92 |
|
| 93 |
adjustParent(); |
| 94 |
saveState(); |
| 95 |
} |
| 96 |
|
| 97 |
void ToolBarAreaWidget::removeWidget(QWidget* widget) |
| 98 |
{ |
| 99 |
_layout->removeWidget(widget); |
| 100 |
|
| 101 |
if (auto toolbar = qobject_cast<ToolBar*>(widget)) { |
| 102 |
toolbar->updateCustomGripVisibility(); |
| 103 |
} |
| 104 |
|
| 105 |
QString name = widget->objectName(); |
| 106 |
if (!name.isEmpty()) { |
| 107 |
Base::ConnectionBlocker block(_conn); |
| 108 |
_hParam->RemoveInt(name.toUtf8().constData()); |
| 109 |
} |
| 110 |
|
| 111 |
adjustParent(); |
| 112 |
} |
| 113 |
|
| 114 |
void ToolBarAreaWidget::adjustParent() |
| 115 |
{ |
| 116 |
if (_sizingTimer) { |
| 117 |
_sizingTimer->start(10); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
void ToolBarAreaWidget::saveState() |
| 122 |
{ |
| 123 |
Base::ConnectionBlocker block(_conn); |
| 124 |
|
| 125 |
for (auto& v : _hParam->GetIntMap()) { |
| 126 |
_hParam->RemoveInt(v.first.c_str()); |
| 127 |
} |
| 128 |
|
| 129 |
foreachToolBar([this](QToolBar* toolbar, int idx, ToolBarAreaWidget*) { |
| 130 |
_hParam->SetInt(toolbar->objectName().toUtf8().constData(), idx); |
| 131 |
}); |
| 132 |
} |
| 133 |
|
| 134 |
void ToolBarAreaWidget::restoreState(const std::map<int, QToolBar*>& toolbars) |
| 135 |
{ |
| 136 |
for (const auto& [index, toolbar] : toolbars) { |
| 137 |
bool visible = toolbar->isVisible(); |
| 138 |
getMainWindow()->removeToolBar(toolbar); |
| 139 |
toolbar->setOrientation(Qt::Horizontal); |
| 140 |
insertWidget(index, toolbar); |
| 141 |
toolbar->setVisible(visible); |
| 142 |
} |
| 143 |
|
| 144 |
for (const auto& [name, visible] : _hParam->GetBoolMap()) { |
| 145 |
auto widget = findChild<QWidget*>(QString::fromUtf8(name.c_str())); |
| 146 |
|
| 147 |
if (widget) { |
| 148 |
widget->setVisible(visible); |
| 149 |
} |
| 150 |
} |
| 151 |
} |