| 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 |
#ifndef GUI_TOOLBARAREAWIDGET_H |
| 23 |
#define GUI_TOOLBARAREAWIDGET_H |
| 24 |
|
| 25 |
#include <QLayout> |
| 26 |
#include <QToolBar> |
| 27 |
#include <QPointer> |
| 28 |
#include <QWidget> |
| 29 |
#include <boost/signals2.hpp> |
| 30 |
#include <Base/Parameter.h> |
| 31 |
|
| 32 |
namespace Gui |
| 33 |
{ |
| 34 |
|
| 35 |
// Qt treats area as Flag so in theory toolbar could be in multiple areas at once. |
| 36 |
// We don't do that here so simple enum should suffice. |
| 37 |
enum class ToolBarArea |
| 38 |
{ |
| 39 |
NoToolBarArea, |
| 40 |
LeftToolBarArea, |
| 41 |
RightToolBarArea, |
| 42 |
TopToolBarArea, |
| 43 |
BottomToolBarArea, |
| 44 |
LeftMenuToolBarArea, |
| 45 |
RightMenuToolBarArea, |
| 46 |
StatusBarToolBarArea, |
| 47 |
}; |
| 48 |
|
| 49 |
class ToolBarAreaWidget: public QWidget |
| 50 |
{ |
| 51 |
Q_OBJECT |
| 52 |
using inherited = QWidget; |
| 53 |
|
| 54 |
public: |
| 55 |
ToolBarAreaWidget( |
| 56 |
QWidget* parent, |
| 57 |
ToolBarArea area, |
| 58 |
const ParameterGrp::handle& hParam, |
| 59 |
boost::signals2::scoped_connection& conn, |
| 60 |
QTimer* timer = nullptr |
| 61 |
); |
| 62 |
|
| 63 |
void addWidget(QWidget* widget); |
| 64 |
void insertWidget(int index, QWidget* widget); |
| 65 |
void removeWidget(QWidget* widget); |
| 66 |
|
| 67 |
void adjustParent(); |
| 68 |
|
| 69 |
QWidget* widgetAt(int index) const |
| 70 |
{ |
| 71 |
auto item = _layout->itemAt(index); |
| 72 |
|
| 73 |
return item ? item->widget() : nullptr; |
| 74 |
} |
| 75 |
|
| 76 |
int count() const |
| 77 |
{ |
| 78 |
return _layout->count(); |
| 79 |
} |
| 80 |
|
| 81 |
int indexOf(QWidget* widget) const |
| 82 |
{ |
| 83 |
return _layout->indexOf(widget); |
| 84 |
} |
| 85 |
|
| 86 |
ToolBarArea area() const |
| 87 |
{ |
| 88 |
return _area; |
| 89 |
} |
| 90 |
|
| 91 |
template<class FuncT> |
| 92 |
void foreachToolBar(FuncT&& func) |
| 93 |
{ |
| 94 |
for (int i = 0, count = _layout->count(); i < count; ++i) { |
| 95 |
auto toolbar = qobject_cast<QToolBar*>(widgetAt(i)); |
| 96 |
|
| 97 |
if (!toolbar || toolbar->objectName().isEmpty() |
| 98 |
|| toolbar->objectName().startsWith(QStringLiteral("*"))) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
func(toolbar, i, this); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void saveState(); |
| 107 |
void restoreState(const std::map<int, QToolBar*>& toolbars); |
| 108 |
|
| 109 |
private: |
| 110 |
QHBoxLayout* _layout; |
| 111 |
QPointer<QTimer> _sizingTimer; |
| 112 |
ParameterGrp::handle _hParam; |
| 113 |
boost::signals2::scoped_connection& _conn; |
| 114 |
ToolBarArea _area; |
| 115 |
}; |
| 116 |
|
| 117 |
} // namespace Gui |
| 118 |
|
| 119 |
#endif // GUI_TOOLBARAREAWIDGET_H |