File indexing completed on 2024-12-08 04:27:17

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()) {
0096                 handleRemoveBar();
0097             } else if (bar == nullptr) {
0098                 dock->setTitleBarWidget(new QWidget());
0099             }
0100             continue;
0101         }
0102 
0103         if (dock->isFloating()) {
0104             handleRemoveBar();
0105             continue;
0106         }
0107         // Since Qt 5.6 we only display title bar in non tabbed dockwidgets
0108         QList<QDockWidget *> docked = pCore->window()->tabifiedDockWidgets(dock);
0109         if (docked.isEmpty()) {
0110             handleRemoveBar();
0111             continue;
0112         }
0113 
0114         const bool hasVisibleDockSibling = std::find_if(std::begin(docked), std::end(docked), [](QDockWidget *sub) {
0115                                                return sub->toggleViewAction()->isChecked() && !sub->isTopLevel();
0116                                            }) != std::end(docked);
0117 
0118         if (!hasVisibleDockSibling) {
0119             handleRemoveBar();
0120             continue;
0121         }
0122 
0123         if (!bar) {
0124             dock->setTitleBarWidget(new QWidget());
0125         }
0126     }
0127 }
0128 
0129 void DockTitleBarManager::slotSwitchTitleBars()
0130 {
0131     m_switchAction->trigger();
0132 }