| 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 |
#include <QAbstractItemView> |
| 24 |
#include <QActionGroup> |
| 25 |
#include <QApplication> |
| 26 |
#include <QMenuBar> |
| 27 |
#include <QScreen> |
| 28 |
#include <QStatusBar> |
| 29 |
#include <QToolBar> |
| 30 |
#include <QLayout> |
| 31 |
#include <QTimer> |
| 32 |
|
| 33 |
|
| 34 |
#include "Base/Tools.h" |
| 35 |
#include "Action.h" |
| 36 |
#include "BitmapFactory.h" |
| 37 |
#include "Command.h" |
| 38 |
#include "Dialogs/DlgPreferencesImp.h" |
| 39 |
#include "MainWindow.h" |
| 40 |
#include "WorkbenchSelector.h" |
| 41 |
#include "ToolBarAreaWidget.h" |
| 42 |
|
| 43 |
|
| 44 |
using namespace Gui; |
| 45 |
|
| 46 |
WorkbenchComboBox::WorkbenchComboBox(WorkbenchGroup* aGroup, QWidget* parent) |
| 47 |
: QComboBox(parent) |
| 48 |
{ |
| 49 |
setIconSize(QSize(16, 16)); |
| 50 |
setToolTip(aGroup->toolTip()); |
| 51 |
setStatusTip(aGroup->action()->statusTip()); |
| 52 |
setWhatsThis(aGroup->action()->whatsThis()); |
| 53 |
refreshList(aGroup->getEnabledWbActions()); |
| 54 |
connect(aGroup, &WorkbenchGroup::workbenchListRefreshed, this, &WorkbenchComboBox::refreshList); |
| 55 |
connect(aGroup->groupAction(), &QActionGroup::triggered, this, [this, aGroup](QAction* action) { |
| 56 |
setCurrentIndex(aGroup->actions().indexOf(action)); |
| 57 |
}); |
| 58 |
connect(this, qOverload<int>(&WorkbenchComboBox::activated), aGroup, [aGroup](int index) { |
| 59 |
aGroup->actions()[index]->trigger(); |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
void WorkbenchComboBox::showPopup() |
| 64 |
{ |
| 65 |
int rows = count(); |
| 66 |
if (rows > 0) { |
| 67 |
int height = view()->sizeHintForRow(0); |
| 68 |
int maxHeight = QApplication::primaryScreen()->size().height(); |
| 69 |
view()->setMinimumHeight(qMin(height * rows, maxHeight / 2)); |
| 70 |
} |
| 71 |
|
| 72 |
QComboBox::showPopup(); |
| 73 |
} |
| 74 |
|
| 75 |
void WorkbenchComboBox::refreshList(QList<QAction*> actionList) |
| 76 |
{ |
| 77 |
clear(); |
| 78 |
|
| 79 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 80 |
"User parameter:BaseApp/Preferences/Workbenches" |
| 81 |
); |
| 82 |
|
| 83 |
auto itemStyle = static_cast<WorkbenchItemStyle>(hGrp->GetInt("WorkbenchSelectorItem", 0)); |
| 84 |
|
| 85 |
for (QAction* action : actionList) { |
| 86 |
QIcon icon = action->icon(); |
| 87 |
|
| 88 |
if (icon.isNull() || itemStyle == WorkbenchItemStyle::TextOnly) { |
| 89 |
addItem(action->text()); |
| 90 |
} |
| 91 |
else if (itemStyle == WorkbenchItemStyle::IconOnly) { |
| 92 |
addItem(icon, {}); // empty string to ensure that only icon is displayed |
| 93 |
} |
| 94 |
else { |
| 95 |
addItem(icon, action->text()); |
| 96 |
} |
| 97 |
|
| 98 |
if (action->isChecked()) { |
| 99 |
this->setCurrentIndex(this->count() - 1); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
WorkbenchTabWidget::WorkbenchTabWidget(WorkbenchGroup* aGroup, QWidget* parent) |
| 105 |
: QWidget(parent) |
| 106 |
, wbActionGroup(aGroup) |
| 107 |
{ |
| 108 |
setToolTip(aGroup->toolTip()); |
| 109 |
setStatusTip(aGroup->action()->statusTip()); |
| 110 |
setWhatsThis(aGroup->action()->whatsThis()); |
| 111 |
setObjectName(QStringLiteral("WbTabBar")); |
| 112 |
|
| 113 |
tabBar = new WbTabBar(this); |
| 114 |
moreButton = new QToolButton(this); |
| 115 |
layout = new QBoxLayout(QBoxLayout::LeftToRight, this); |
| 116 |
|
| 117 |
layout->setContentsMargins(0, 0, 0, 0); |
| 118 |
layout->addWidget(tabBar); |
| 119 |
layout->addWidget(moreButton); |
| 120 |
layout->setAlignment(moreButton, Qt::AlignCenter); |
| 121 |
|
| 122 |
setLayout(layout); |
| 123 |
|
| 124 |
moreButton->setIcon(Gui::BitmapFactory().iconFromTheme("list-add")); |
| 125 |
moreButton->setToolButtonStyle(Qt::ToolButtonIconOnly); |
| 126 |
moreButton->setPopupMode(QToolButton::InstantPopup); |
| 127 |
moreButton->setMenu(new QMenu(moreButton)); |
| 128 |
moreButton->setObjectName(QStringLiteral("WbTabBarMore")); |
| 129 |
|
| 130 |
if (parent->inherits("QToolBar")) { |
| 131 |
// when toolbar is created it is not yet placed in its designated area |
| 132 |
// therefore we need to wait a bit and then update layout when it is ready |
| 133 |
// this is prone to race conditions, but Qt does not supply any event that |
| 134 |
// informs us about toolbar changing its placement. |
| 135 |
// |
| 136 |
// previous implementation saved that information to user settings and |
| 137 |
// restored last layout but this creates issues when default workbench has |
| 138 |
// different layout than last visited one |
| 139 |
QTimer::singleShot(500, [this]() { updateLayout(); }); |
| 140 |
} |
| 141 |
|
| 142 |
tabBar->setDocumentMode(true); |
| 143 |
tabBar->setUsesScrollButtons(true); |
| 144 |
tabBar->setDrawBase(true); |
| 145 |
tabBar->setIconSize(QSize(16, 16)); |
| 146 |
|
| 147 |
updateWorkbenchList(); |
| 148 |
|
| 149 |
connect(aGroup, &WorkbenchGroup::workbenchListRefreshed, this, &WorkbenchTabWidget::updateWorkbenchList); |
| 150 |
connect( |
| 151 |
aGroup->groupAction(), |
| 152 |
&QActionGroup::triggered, |
| 153 |
this, |
| 154 |
&WorkbenchTabWidget::handleWorkbenchSelection |
| 155 |
); |
| 156 |
connect(tabBar, &QTabBar::currentChanged, this, &WorkbenchTabWidget::handleTabChange); |
| 157 |
|
| 158 |
if (auto toolBar = qobject_cast<QToolBar*>(parent)) { |
| 159 |
connect(toolBar, &QToolBar::topLevelChanged, this, &WorkbenchTabWidget::updateLayout); |
| 160 |
connect(toolBar, &QToolBar::orientationChanged, this, &WorkbenchTabWidget::updateLayout); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
inline Qt::LayoutDirection WorkbenchTabWidget::direction() const |
| 165 |
{ |
| 166 |
return _direction; |
| 167 |
} |
| 168 |
|
| 169 |
void WorkbenchTabWidget::setDirection(Qt::LayoutDirection direction) |
| 170 |
{ |
| 171 |
_direction = direction; |
| 172 |
|
| 173 |
Q_EMIT directionChanged(direction); |
| 174 |
} |
| 175 |
|
| 176 |
inline int WorkbenchTabWidget::temporaryWorkbenchTabIndex() const |
| 177 |
{ |
| 178 |
if (direction() == Qt::RightToLeft) { |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
int nextTabIndex = tabBar->count(); |
| 183 |
|
| 184 |
return temporaryWorkbenchAction ? nextTabIndex - 1 : nextTabIndex; |
| 185 |
} |
| 186 |
|
| 187 |
QAction* WorkbenchTabWidget::workbenchActivateActionByTabIndex(int tabIndex) const |
| 188 |
{ |
| 189 |
if (temporaryWorkbenchAction && tabIndex == temporaryWorkbenchTabIndex()) { |
| 190 |
return temporaryWorkbenchAction; |
| 191 |
} |
| 192 |
|
| 193 |
auto it = tabIndexToAction.find(tabIndex); |
| 194 |
|
| 195 |
if (it != tabIndexToAction.end()) { |
| 196 |
return it->second; |
| 197 |
} |
| 198 |
|
| 199 |
return nullptr; |
| 200 |
} |
| 201 |
|
| 202 |
int WorkbenchTabWidget::tabIndexForWorkbenchActivateAction(QAction* workbenchActivateAction) const |
| 203 |
{ |
| 204 |
if (workbenchActivateAction == temporaryWorkbenchAction) { |
| 205 |
return temporaryWorkbenchTabIndex(); |
| 206 |
} |
| 207 |
|
| 208 |
return actionToTabIndex.at(workbenchActivateAction); |
| 209 |
} |
| 210 |
|
| 211 |
WorkbenchItemStyle Gui::WorkbenchTabWidget::itemStyle() const |
| 212 |
{ |
| 213 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 214 |
"User parameter:BaseApp/Preferences/Workbenches" |
| 215 |
); |
| 216 |
return static_cast<WorkbenchItemStyle>(hGrp->GetInt("WorkbenchSelectorItem", 0)); |
| 217 |
} |
| 218 |
|
| 219 |
void WorkbenchTabWidget::updateLayout() |
| 220 |
{ |
| 221 |
if (!parentWidget()) { |
| 222 |
setToolBarArea(Gui::ToolBarArea::TopToolBarArea); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
if (auto toolBar = qobject_cast<QToolBar*>(parentWidget())) { |
| 227 |
setSizePolicy(toolBar->sizePolicy()); |
| 228 |
tabBar->setSizePolicy(toolBar->sizePolicy()); |
| 229 |
|
| 230 |
if (toolBar->isFloating()) { |
| 231 |
setToolBarArea( |
| 232 |
toolBar->orientation() == Qt::Horizontal ? Gui::ToolBarArea::TopToolBarArea |
| 233 |
: Gui::ToolBarArea::LeftToolBarArea |
| 234 |
); |
| 235 |
return; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
auto toolBarArea = Gui::ToolBarManager::getInstance()->toolBarArea(parentWidget()); |
| 240 |
|
| 241 |
setToolBarArea(toolBarArea); |
| 242 |
|
| 243 |
tabBar->setSelectionBehaviorOnRemove( |
| 244 |
direction() == Qt::LeftToRight ? QTabBar::SelectLeftTab : QTabBar::SelectRightTab |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
void WorkbenchTabWidget::handleWorkbenchSelection(QAction* selectedWorkbenchAction) |
| 249 |
{ |
| 250 |
if (wbActionGroup->getDisabledWbActions().contains(selectedWorkbenchAction)) { |
| 251 |
if (temporaryWorkbenchAction == selectedWorkbenchAction) { |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
setTemporaryWorkbenchTab(selectedWorkbenchAction); |
| 256 |
} |
| 257 |
|
| 258 |
updateLayout(); |
| 259 |
|
| 260 |
tabBar->setCurrentIndex(tabIndexForWorkbenchActivateAction(selectedWorkbenchAction)); |
| 261 |
} |
| 262 |
|
| 263 |
void WorkbenchTabWidget::setTemporaryWorkbenchTab(QAction* workbenchActivateAction) |
| 264 |
{ |
| 265 |
auto temporaryTabIndex = temporaryWorkbenchTabIndex(); |
| 266 |
|
| 267 |
if (temporaryWorkbenchAction) { |
| 268 |
temporaryWorkbenchAction = nullptr; |
| 269 |
tabBar->removeTab(temporaryTabIndex); |
| 270 |
} |
| 271 |
|
| 272 |
temporaryWorkbenchAction = workbenchActivateAction; |
| 273 |
|
| 274 |
if (!workbenchActivateAction) { |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
addWorkbenchTab(workbenchActivateAction, temporaryTabIndex); |
| 279 |
|
| 280 |
adjustSize(); |
| 281 |
} |
| 282 |
|
| 283 |
void WorkbenchTabWidget::handleTabChange(int selectedTabIndex) |
| 284 |
{ |
| 285 |
// Prevents from rapid workbench changes on initialization as this can cause |
| 286 |
// some serious race conditions. |
| 287 |
if (isInitializing) { |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
if (auto workbenchActivateAction = workbenchActivateActionByTabIndex(selectedTabIndex)) { |
| 292 |
workbenchActivateAction->trigger(); |
| 293 |
} |
| 294 |
|
| 295 |
if (selectedTabIndex != temporaryWorkbenchTabIndex()) { |
| 296 |
setTemporaryWorkbenchTab(nullptr); |
| 297 |
} |
| 298 |
|
| 299 |
adjustSize(); |
| 300 |
} |
| 301 |
|
| 302 |
void WorkbenchTabWidget::updateWorkbenchList() |
| 303 |
{ |
| 304 |
if (isInitializing) { |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
tabBar->setItemStyle(itemStyle()); |
| 309 |
|
| 310 |
// As clearing and adding tabs can cause changing current tab in QTabBar. |
| 311 |
// This in turn will cause workbench to change, so we need to prevent |
| 312 |
// processing of such events until the QTabBar is fully prepared. |
| 313 |
Base::StateLocker lock(isInitializing); |
| 314 |
|
| 315 |
actionToTabIndex.clear(); |
| 316 |
tabIndexToAction.clear(); |
| 317 |
|
| 318 |
// tabs->clear() (QTabBar has no clear) |
| 319 |
for (int i = tabBar->count() - 1; i >= 0; --i) { |
| 320 |
tabBar->removeTab(i); |
| 321 |
} |
| 322 |
|
| 323 |
for (QAction* action : wbActionGroup->getEnabledWbActions()) { |
| 324 |
addWorkbenchTab(action); |
| 325 |
} |
| 326 |
|
| 327 |
if (temporaryWorkbenchAction != nullptr) { |
| 328 |
setTemporaryWorkbenchTab(temporaryWorkbenchAction); |
| 329 |
} |
| 330 |
|
| 331 |
buildPrefMenu(); |
| 332 |
adjustSize(); |
| 333 |
} |
| 334 |
|
| 335 |
int WorkbenchTabWidget::addWorkbenchTab(QAction* action, int tabIndex) |
| 336 |
{ |
| 337 |
auto itemStyle = this->itemStyle(); |
| 338 |
|
| 339 |
// if tabIndex is negative we assume that tab must be placed at the end of tabBar (default behavior) |
| 340 |
if (tabIndex < 0) { |
| 341 |
tabIndex = tabBar->count(); |
| 342 |
} |
| 343 |
|
| 344 |
// for the maps we consider order in which tabs have been added |
| 345 |
// that's why here we use tabBar->count() instead of tabIndex |
| 346 |
actionToTabIndex[action] = tabBar->count(); |
| 347 |
tabIndexToAction[tabBar->count()] = action; |
| 348 |
|
| 349 |
QIcon icon = action->icon(); |
| 350 |
if (icon.isNull() || itemStyle == WorkbenchItemStyle::TextOnly) { |
| 351 |
tabBar->insertTab(tabIndex, action->text()); |
| 352 |
} |
| 353 |
else if (itemStyle == WorkbenchItemStyle::IconOnly) { |
| 354 |
tabBar->insertTab(tabIndex, icon, {}); // empty string to ensure only icon is displayed |
| 355 |
} |
| 356 |
else { |
| 357 |
tabBar->insertTab(tabIndex, icon, action->text()); |
| 358 |
} |
| 359 |
|
| 360 |
tabBar->setTabToolTip(tabIndex, action->toolTip()); |
| 361 |
|
| 362 |
if (action->isChecked()) { |
| 363 |
tabBar->setCurrentIndex(tabIndex); |
| 364 |
} |
| 365 |
|
| 366 |
return tabIndex; |
| 367 |
} |
| 368 |
|
| 369 |
void WorkbenchTabWidget::setToolBarArea(Gui::ToolBarArea area) |
| 370 |
{ |
| 371 |
switch (area) { |
| 372 |
case Gui::ToolBarArea::LeftToolBarArea: |
| 373 |
case Gui::ToolBarArea::RightToolBarArea: { |
| 374 |
setDirection(Qt::LeftToRight); |
| 375 |
layout->setDirection( |
| 376 |
direction() == Qt::LeftToRight ? QBoxLayout::TopToBottom : QBoxLayout::BottomToTop |
| 377 |
); |
| 378 |
tabBar->setShape( |
| 379 |
area == Gui::ToolBarArea::LeftToolBarArea ? QTabBar::RoundedWest : QTabBar::RoundedEast |
| 380 |
); |
| 381 |
break; |
| 382 |
} |
| 383 |
|
| 384 |
case Gui::ToolBarArea::TopToolBarArea: |
| 385 |
case Gui::ToolBarArea::BottomToolBarArea: |
| 386 |
case Gui::ToolBarArea::LeftMenuToolBarArea: |
| 387 |
case Gui::ToolBarArea::RightMenuToolBarArea: |
| 388 |
case Gui::ToolBarArea::StatusBarToolBarArea: { |
| 389 |
bool isTop = area == Gui::ToolBarArea::TopToolBarArea |
| 390 |
|| area == Gui::ToolBarArea::LeftMenuToolBarArea |
| 391 |
|| area == Gui::ToolBarArea::RightMenuToolBarArea; |
| 392 |
|
| 393 |
bool isRightAligned = area == Gui::ToolBarArea::RightMenuToolBarArea |
| 394 |
|| area == Gui::ToolBarArea::StatusBarToolBarArea; |
| 395 |
|
| 396 |
setDirection(isRightAligned ? Qt::RightToLeft : Qt::LeftToRight); |
| 397 |
layout->setDirection( |
| 398 |
direction() == Qt::LeftToRight ? QBoxLayout::LeftToRight : QBoxLayout::RightToLeft |
| 399 |
); |
| 400 |
tabBar->setShape(isTop ? QTabBar::RoundedNorth : QTabBar::RoundedSouth); |
| 401 |
break; |
| 402 |
} |
| 403 |
default: |
| 404 |
// no-op |
| 405 |
break; |
| 406 |
} |
| 407 |
|
| 408 |
adjustSize(); |
| 409 |
} |
| 410 |
|
| 411 |
void WorkbenchTabWidget::buildPrefMenu() |
| 412 |
{ |
| 413 |
auto menu = moreButton->menu(); |
| 414 |
|
| 415 |
menu->clear(); |
| 416 |
|
| 417 |
// Add disabled workbenches, sorted alphabetically. |
| 418 |
for (auto action : wbActionGroup->getDisabledWbActions()) { |
| 419 |
if (action->text() == QStringLiteral("<none>")) { |
| 420 |
continue; |
| 421 |
} |
| 422 |
|
| 423 |
menu->addAction(action); |
| 424 |
} |
| 425 |
|
| 426 |
menu->addSeparator(); |
| 427 |
|
| 428 |
QAction* preferencesAction = menu->addAction(tr("Preferences")); |
| 429 |
connect(preferencesAction, &QAction::triggered, this, []() { |
| 430 |
Gui::Dialog::DlgPreferencesImp cDlg(getMainWindow()); |
| 431 |
cDlg.activateGroupPage(QStringLiteral("Workbenches"), 0); |
| 432 |
cDlg.exec(); |
| 433 |
}); |
| 434 |
} |
| 435 |
|
| 436 |
void WorkbenchTabWidget::adjustSize() |
| 437 |
{ |
| 438 |
QWidget::adjustSize(); |
| 439 |
|
| 440 |
parentWidget()->adjustSize(); |
| 441 |
} |
| 442 |
|
| 443 |
void WorkbenchComboBox::setVisible(bool visible) |
| 444 |
{ |
| 445 |
if (!visible && parentWidget() && parentWidget()->isVisible()) { |
| 446 |
// ignore attempts to hide while parent is visible |
| 447 |
// this works around a layout bug when toolbar grip is attached |
| 448 |
// in the menu bar area |
| 449 |
QTimer::singleShot(0, this, [this]() { |
| 450 |
if (auto toolbar = parentWidget()) { |
| 451 |
if (auto areaWidget = toolbar->parentWidget()) { |
| 452 |
areaWidget->adjustSize(); |
| 453 |
} |
| 454 |
} |
| 455 |
}); |
| 456 |
return; |
| 457 |
} |
| 458 |
QComboBox::setVisible(visible); |
| 459 |
} |
| 460 |
|
| 461 |
#include "moc_WorkbenchSelector.cpp" |