| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2024 Pierre-Louis Boyer <development[at]Ondsel.com> * |
| 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 |
#ifndef GUI_WORKBENCHSELECTOR_H |
| 24 |
#define GUI_WORKBENCHSELECTOR_H |
| 25 |
|
| 26 |
#include <QComboBox> |
| 27 |
#include <QTabBar> |
| 28 |
#include <QMenu> |
| 29 |
#include <QToolButton> |
| 30 |
#include <QLayout> |
| 31 |
#include <QWheelEvent> |
| 32 |
|
| 33 |
#include <FCGlobal.h> |
| 34 |
#include <Gui/ToolBarManager.h> |
| 35 |
#include <map> |
| 36 |
|
| 37 |
namespace Gui |
| 38 |
{ |
| 39 |
class WorkbenchGroup; |
| 40 |
|
| 41 |
enum class WorkbenchItemStyle |
| 42 |
{ |
| 43 |
IconAndText = 0, |
| 44 |
IconOnly = 1, |
| 45 |
TextOnly = 2 |
| 46 |
}; |
| 47 |
|
| 48 |
class GuiExport WorkbenchComboBox: public QComboBox |
| 49 |
{ |
| 50 |
Q_OBJECT // NOLINT |
| 51 |
|
| 52 |
public: explicit WorkbenchComboBox(WorkbenchGroup* aGroup, QWidget* parent = nullptr); |
| 53 |
~WorkbenchComboBox() override = default; |
| 54 |
WorkbenchComboBox(WorkbenchComboBox&& rhs) = delete; |
| 55 |
void showPopup() override; |
| 56 |
void setVisible(bool visible) override; |
| 57 |
|
| 58 |
WorkbenchComboBox operator=(WorkbenchComboBox&& rhs) = delete; |
| 59 |
|
| 60 |
public Q_SLOTS: |
| 61 |
void refreshList(QList<QAction*>); |
| 62 |
|
| 63 |
private: |
| 64 |
Q_DISABLE_COPY(WorkbenchComboBox) |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
class GuiExport WorkbenchTabWidget: public QWidget |
| 69 |
{ |
| 70 |
Q_OBJECT // NOLINT |
| 71 |
Q_PROPERTY(Qt::LayoutDirection direction READ direction WRITE setDirection NOTIFY directionChanged) |
| 72 |
|
| 73 |
class WbTabBar: public QTabBar |
| 74 |
{ |
| 75 |
public: |
| 76 |
explicit WbTabBar(QWidget* parent) |
| 77 |
: QTabBar(parent) |
| 78 |
{} |
| 79 |
|
| 80 |
QSize tabSizeHint(int index) const override |
| 81 |
{ |
| 82 |
auto sizeFromParent = QTabBar::tabSizeHint(index); |
| 83 |
|
| 84 |
if (itemStyle() != WorkbenchItemStyle::IconOnly) { |
| 85 |
return sizeFromParent; |
| 86 |
} |
| 87 |
|
| 88 |
QStyleOptionTab opt; |
| 89 |
|
| 90 |
initStyleOption(&opt, index); |
| 91 |
|
| 92 |
int padding = style()->pixelMetric(QStyle::PM_TabBarTabHSpace, &opt, this); |
| 93 |
|
| 94 |
auto csz = iconSize(); |
| 95 |
auto isHorizontal = opt.shape == RoundedNorth || opt.shape == RoundedSouth; |
| 96 |
|
| 97 |
if (isHorizontal) { |
| 98 |
csz.setWidth(csz.width() + padding); |
| 99 |
} |
| 100 |
else { |
| 101 |
csz.setHeight(csz.height() + padding); |
| 102 |
} |
| 103 |
|
| 104 |
auto size = style()->sizeFromContents(QStyle::CT_TabBarTab, &opt, csz, this); |
| 105 |
|
| 106 |
if (isHorizontal) { |
| 107 |
size.setHeight(sizeFromParent.height()); |
| 108 |
} |
| 109 |
else { |
| 110 |
size.setWidth(sizeFromParent.width()); |
| 111 |
} |
| 112 |
|
| 113 |
return size; |
| 114 |
} |
| 115 |
|
| 116 |
void wheelEvent(QWheelEvent* wheelEvent) override |
| 117 |
{ |
| 118 |
// Qt does not expose any way to programmatically control scroll of QTabBar hence |
| 119 |
// we need to use a bit hacky solution of simulating clicks on the scroll buttons |
| 120 |
const auto buttonToClickName = wheelEvent->angleDelta().y() < 0 |
| 121 |
? QStringLiteral("ScrollLeftButton") |
| 122 |
: QStringLiteral("ScrollRightButton"); |
| 123 |
|
| 124 |
// Qt introduces named buttons in Qt 6.3 and 5.15.6, before that they are not available |
| 125 |
if (const auto button = findChild<QAbstractButton*>(buttonToClickName)) { |
| 126 |
button->click(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
WorkbenchItemStyle itemStyle() const |
| 131 |
{ |
| 132 |
return _itemStyle; |
| 133 |
} |
| 134 |
|
| 135 |
void setItemStyle(WorkbenchItemStyle itemStyle) |
| 136 |
{ |
| 137 |
_itemStyle = itemStyle; |
| 138 |
setProperty("style", QString::fromUtf8(workbenchItemStyleToString(itemStyle))); |
| 139 |
} |
| 140 |
|
| 141 |
private: |
| 142 |
WorkbenchItemStyle _itemStyle {WorkbenchItemStyle::IconAndText}; |
| 143 |
|
| 144 |
static const char* workbenchItemStyleToString(WorkbenchItemStyle style) |
| 145 |
{ |
| 146 |
switch (style) { |
| 147 |
case WorkbenchItemStyle::IconAndText: |
| 148 |
return "icon-and-text"; |
| 149 |
case WorkbenchItemStyle::IconOnly: |
| 150 |
return "icon-only"; |
| 151 |
case WorkbenchItemStyle::TextOnly: |
| 152 |
return "text-only"; |
| 153 |
default: |
| 154 |
return "WorkbenchItemStyle-internal-error"; |
| 155 |
} |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
public: |
| 160 |
explicit WorkbenchTabWidget(WorkbenchGroup* aGroup, QWidget* parent = nullptr); |
| 161 |
|
| 162 |
void setToolBarArea(Gui::ToolBarArea area); |
| 163 |
void buildPrefMenu(); |
| 164 |
|
| 165 |
Qt::LayoutDirection direction() const; |
| 166 |
void setDirection(Qt::LayoutDirection direction); |
| 167 |
|
| 168 |
void adjustSize(); |
| 169 |
|
| 170 |
public Q_SLOTS: |
| 171 |
void handleWorkbenchSelection(QAction* selectedWorkbenchAction); |
| 172 |
void handleTabChange(int selectedTabIndex); |
| 173 |
|
| 174 |
void updateLayout(); |
| 175 |
void updateWorkbenchList(); |
| 176 |
|
| 177 |
Q_SIGNALS: |
| 178 |
void directionChanged(const Qt::LayoutDirection&); |
| 179 |
|
| 180 |
protected: |
| 181 |
int addWorkbenchTab(QAction* workbenchActivateAction, int index = -1); |
| 182 |
|
| 183 |
void setTemporaryWorkbenchTab(QAction* workbenchActivateAction); |
| 184 |
int temporaryWorkbenchTabIndex() const; |
| 185 |
|
| 186 |
QAction* workbenchActivateActionByTabIndex(int tabIndex) const; |
| 187 |
int tabIndexForWorkbenchActivateAction(QAction* workbenchActivateAction) const; |
| 188 |
|
| 189 |
WorkbenchItemStyle itemStyle() const; |
| 190 |
|
| 191 |
private: |
| 192 |
bool isInitializing = false; |
| 193 |
|
| 194 |
WorkbenchGroup* wbActionGroup; |
| 195 |
QToolButton* moreButton; |
| 196 |
WbTabBar* tabBar; |
| 197 |
QBoxLayout* layout; |
| 198 |
|
| 199 |
Qt::LayoutDirection _direction = Qt::LeftToRight; |
| 200 |
|
| 201 |
// this action is used for workbenches that are typically disabled |
| 202 |
QAction* temporaryWorkbenchAction = nullptr; |
| 203 |
|
| 204 |
std::map<QAction*, int> actionToTabIndex; |
| 205 |
std::map<int, QAction*> tabIndexToAction; |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
} // namespace Gui |
| 210 |
|
| 211 |
#endif // GUI_WORKBENCHSELECTOR_H |