File indexing completed on 2022-11-29 19:13:37

0001 /*
0002     SPDX-FileCopyrightText: 2012 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003     SPDX-FileCopyrightText: 2014 Till Theato <root@ttill.de>
0004 
0005     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 
0007     This file is part of Kdenlive. See www.kdenlive.org.
0008 */
0009 
0010 #include "docktitlebarmanager.h"
0011 #include "core.h"
0012 #include "kdenlivesettings.h"
0013 #include "mainwindow.h"
0014 #include <KLocalizedString>
0015 
0016 DockTitleBarManager::DockTitleBarManager(QObject *parent)
0017     : QObject(parent)
0018 {
0019     m_switchAction = new QAction(i18n("Show Title Bars"), this);
0020     m_switchAction->setCheckable(true);
0021     m_switchAction->setChecked(KdenliveSettings::showtitlebars());
0022     pCore->window()->addAction(QStringLiteral("show_titlebars"), m_switchAction);
0023     connect(m_switchAction, &QAction::triggered, this, &DockTitleBarManager::slotShowTitleBars);
0024     connect(pCore->window(), &MainWindow::GUISetupDone, this, &DockTitleBarManager::slotInstallRightClick);
0025 }
0026 
0027 void DockTitleBarManager::slotInstallRightClick()
0028 {
0029     // install right click
0030     QList<QTabBar *> tabs = pCore->window()->findChildren<QTabBar *>();
0031     for (QTabBar *tab : qAsConst(tabs)) {
0032         tab->setContextMenuPolicy(Qt::CustomContextMenu);
0033         tab->setAcceptDrops(true);
0034         tab->setChangeCurrentOnDrag(true);
0035         connect(tab, &QWidget::customContextMenuRequested, this, &DockTitleBarManager::slotSwitchTitleBars);
0036     }
0037 
0038     connectDocks(true);
0039     slotUpdateTitleBars();
0040 }
0041 
0042 void DockTitleBarManager::connectDocks(bool doConnect)
0043 {
0044     // connect
0045     QList<QDockWidget *> docks = pCore->window()->findChildren<QDockWidget *>();
0046     for (QDockWidget *dock : qAsConst(docks)) {
0047         if (doConnect) {
0048             connect(dock, &QDockWidget::dockLocationChanged, this, &DockTitleBarManager::slotUpdateDockLocation);
0049             connect(dock, &QDockWidget::topLevelChanged, this, &DockTitleBarManager::slotUpdateTitleBars);
0050         } else {
0051             disconnect(dock, &QDockWidget::dockLocationChanged, this, &DockTitleBarManager::slotUpdateDockLocation);
0052             disconnect(dock, &QDockWidget::topLevelChanged, this, &DockTitleBarManager::slotUpdateTitleBars);
0053         }
0054     }
0055 }
0056 
0057 void DockTitleBarManager::slotUpdateDockLocation(Qt::DockWidgetArea dockLocationArea)
0058 {
0059     slotUpdateTitleBars(dockLocationArea != Qt::NoDockWidgetArea);
0060 }
0061 
0062 void DockTitleBarManager::slotShowTitleBars(bool show)
0063 {
0064     KdenliveSettings::setShowtitlebars(show);
0065     slotUpdateTitleBars();
0066 }
0067 
0068 void DockTitleBarManager::slotUpdateTitleBars(bool isTopLevel)
0069 {
0070     QList<QTabBar *> tabbars = pCore->window()->findChildren<QTabBar *>();
0071     for (QTabBar *tab : qAsConst(tabbars)) {
0072         tab->setAcceptDrops(true);
0073         tab->setChangeCurrentOnDrag(true);
0074         // Fix tabbar tooltip containing ampersand
0075         for (int i = 0; i < tab->count(); i++) {
0076             tab->setTabToolTip(i, tab->tabText(i).replace('&', ""));
0077         }
0078     }
0079 
0080     if (!KdenliveSettings::showtitlebars() && !isTopLevel) {
0081         return;
0082     }
0083 
0084     QList<QDockWidget *> docks = pCore->window()->findChildren<QDockWidget *>();
0085     for (QDockWidget *dock : qAsConst(docks)) {
0086         QWidget *bar = dock->titleBarWidget();
0087         auto handleRemoveBar = [&dock, &bar]() -> void {
0088             if (bar) {
0089                 dock->setTitleBarWidget(nullptr);
0090                 delete bar;
0091             }
0092         };
0093 
0094         if (!KdenliveSettings::showtitlebars()) {
0095             if (!dock->isFloating() && bar == nullptr) {
0096                 dock->setTitleBarWidget(new QWidget());
0097             }
0098             continue;
0099         }
0100 
0101         if (dock->isFloating()) {
0102             handleRemoveBar();
0103             continue;
0104         }
0105         // Since Qt 5.6 we only display title bar in non tabbed dockwidgets
0106         QList<QDockWidget *> docked = pCore->window()->tabifiedDockWidgets(dock);
0107         if (docked.isEmpty()) {
0108             handleRemoveBar();
0109             continue;
0110         }
0111 
0112         const bool hasVisibleDockSibling = std::find_if(std::begin(docked), std::end(docked), [](QDockWidget *sub) {
0113                                                return sub->toggleViewAction()->isChecked() && !sub->isTopLevel();
0114                                            }) != std::end(docked);
0115 
0116         if (!hasVisibleDockSibling) {
0117             handleRemoveBar();
0118             continue;
0119         }
0120 
0121         if (!bar) {
0122             dock->setTitleBarWidget(new QWidget());
0123         }
0124     }
0125 }
0126 
0127 void DockTitleBarManager::slotSwitchTitleBars()
0128 {
0129     m_switchAction->trigger();
0130 }