File indexing completed on 2024-05-12 04:38:23

0001 /*
0002     SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "closedworkingsetswidget.h"
0008 
0009 #include <sublime/area.h>
0010 
0011 #include <shell/core.h>
0012 
0013 #include "mainwindow.h"
0014 #include "workingsetcontroller.h"
0015 
0016 #include "workingset.h"
0017 #include "workingsettoolbutton.h"
0018 #include "debug.h"
0019 
0020 #include <QHBoxLayout>
0021 #include <QMenuBar>
0022 
0023 using namespace KDevelop;
0024 
0025 WorkingSet* workingSet(const QString& id)
0026 {
0027     return Core::self()->workingSetControllerInternal()->workingSet(id);
0028 }
0029 
0030 ClosedWorkingSetsWidget::ClosedWorkingSetsWidget( MainWindow* window )
0031     : QWidget(nullptr), m_mainWindow(window)
0032 {
0033     connect(window, &MainWindow::areaChanged,
0034             this, &ClosedWorkingSetsWidget::areaChanged);
0035 
0036     auto l = new QHBoxLayout(this);
0037     setLayout(l);
0038     l->setContentsMargins(0, 0, 0, 0);
0039     l->setDirection(QBoxLayout::RightToLeft);
0040 
0041     if (window->area()) {
0042         areaChanged(window->area());
0043     }
0044 
0045     connect(Core::self()->workingSetControllerInternal(), &WorkingSetController::aboutToRemoveWorkingSet,
0046             this, &ClosedWorkingSetsWidget::removeWorkingSet);
0047 
0048     connect(Core::self()->workingSetControllerInternal(), &WorkingSetController::workingSetAdded,
0049             this, &ClosedWorkingSetsWidget::addWorkingSet);
0050 }
0051 
0052 void ClosedWorkingSetsWidget::areaChanged( Sublime::Area* area )
0053 {
0054     if (m_connectedArea) {
0055         disconnect(m_connectedArea, &Sublime::Area::changedWorkingSet,
0056                    this, &ClosedWorkingSetsWidget::changedWorkingSet);
0057     }
0058 
0059     m_connectedArea = area;
0060     connect(m_connectedArea, &Sublime::Area::changedWorkingSet,
0061             this, &ClosedWorkingSetsWidget::changedWorkingSet);
0062 
0063     // clear layout
0064     qDeleteAll(m_buttons);
0065     m_buttons.clear();
0066 
0067     // add sets from new area
0068     const auto allWorkingSets = Core::self()->workingSetControllerInternal()->allWorkingSets();
0069     for (WorkingSet* set : allWorkingSets) {
0070         if (!set->hasConnectedArea(area) && set->isPersistent()) {
0071             addWorkingSet(set);
0072         }
0073     }
0074 }
0075 
0076 void ClosedWorkingSetsWidget::changedWorkingSet(Sublime::Area *area, Sublime::Area*, const QString &from, const QString &to)
0077 {
0078     Q_ASSERT(area == m_connectedArea);
0079     Q_UNUSED(area);
0080 
0081     if (!from.isEmpty()) {
0082         WorkingSet* oldSet = workingSet(from);
0083         addWorkingSet(oldSet);
0084     }
0085 
0086     if (!to.isEmpty()) {
0087         WorkingSet* newSet = workingSet(to);
0088         removeWorkingSet(newSet);
0089     }
0090 }
0091 
0092 void ClosedWorkingSetsWidget::removeWorkingSet( WorkingSet* set )
0093 {
0094     delete m_buttons.take(set);
0095     Q_ASSERT(m_buttons.size() == layout()->count());
0096     // Recalculate menu bar widget sizes after removing a working set button (not done automatically)
0097     m_mainWindow->menuBar()->adjustSize();
0098 }
0099 
0100 void ClosedWorkingSetsWidget::addWorkingSet( WorkingSet* set )
0101 {
0102     if (m_buttons.contains(set)) {
0103         return;
0104     }
0105 
0106     if (set->isEmpty()) {
0107 //             qCDebug(SHELL) << "skipping" << set->id() << "because empty";
0108         return;
0109     }
0110 
0111 //     qCDebug(SHELL) << "adding button for" << set->id();
0112     auto* button = new WorkingSetToolButton(this, set);
0113     button->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored));
0114 
0115     layout()->addWidget(button);
0116     m_buttons[set] = button;
0117 }
0118 
0119 #include "moc_closedworkingsetswidget.cpp"