File indexing completed on 2024-04-28 04:37:14

0001 /*
0002     SPDX-FileCopyrightText: 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "areadisplay.h"
0008 
0009 #include "mainwindow.h"
0010 #include "workingsetcontroller.h"
0011 #include "core.h"
0012 
0013 #include <sublime/area.h>
0014 #include <interfaces/iuicontroller.h>
0015 
0016 #include <KLocalizedString>
0017 
0018 #include <QMenu>
0019 #include <QToolButton>
0020 #include <QHBoxLayout>
0021 #include <QLabel>
0022 #include <QMenuBar>
0023 #include <QEvent>
0024 
0025 using namespace KDevelop;
0026 
0027 AreaDisplay::AreaDisplay(KDevelop::MainWindow* parent)
0028     : QWidget(parent)
0029     , m_mainWindow(parent)
0030 {
0031     setLayout(new QHBoxLayout);
0032     m_separator = new QLabel(QStringLiteral("|"), this);
0033     m_separator->setEnabled(false);
0034     m_separator->setVisible(false);
0035     layout()->addWidget(m_separator);
0036 
0037     layout()->setContentsMargins(0, 0, 0, 0);
0038     auto closedWorkingSets = Core::self()->workingSetControllerInternal()->createSetManagerWidget(m_mainWindow);
0039     closedWorkingSets->setParent(this);
0040     layout()->addWidget(closedWorkingSets);
0041 
0042     m_button = new QToolButton(this);
0043     m_button->setToolTip(i18n(
0044         "Execute actions to change the area.<br />"
0045         "An area is a tool view configuration for a specific use case. "
0046         "From here you can also navigate back to the default code area."));
0047     m_button->setAutoRaise(true);
0048     m_button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0049     m_button->setPopupMode(QToolButton::InstantPopup);
0050     layout()->addWidget(m_button);
0051 
0052     connect(parent, &MainWindow::areaChanged, this, &AreaDisplay::newArea);
0053 }
0054 
0055 void AreaDisplay::newArea(Sublime::Area* area)
0056 {
0057     if(m_button->menu())
0058         m_button->menu()->deleteLater();
0059 
0060     Sublime::Area* currentArea = m_mainWindow->area();
0061 
0062     m_button->setText(currentArea->title());
0063     m_button->setIcon(QIcon::fromTheme(currentArea->iconName()));
0064 
0065     auto* m = new QMenu(m_button);
0066     m->addActions(area->actions());
0067     if (currentArea->objectName() != QLatin1String("code")) {
0068         if(!m->actions().isEmpty())
0069             m->addSeparator();
0070         m->addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18nc("@action:inmenu", "Back to Code"), this, &AreaDisplay::backToCode, QKeySequence(Qt::AltModifier | Qt::Key_Backspace));
0071     }
0072     m_button->setMenu(m);
0073 
0074     //remove the additional widgets we might have added for the last area
0075     auto* l = qobject_cast<QBoxLayout*>(layout());
0076     if(l->count()>=4) {
0077         QLayoutItem* item = l->takeAt(0);
0078         delete item->widget();
0079         delete item;
0080     }
0081     QWidget* w = Core::self()->workingSetControllerInternal()->createSetManagerWidget(m_mainWindow, area);
0082     w->installEventFilter(this);
0083     m_separator->setVisible(w->isVisible());
0084     l->insertWidget(0, w);
0085 }
0086 
0087 bool AreaDisplay::eventFilter(QObject* obj, QEvent* event)
0088 {
0089     if (event->type() == QEvent::Show) {
0090         m_separator->setVisible(true);
0091         // Recalculate menu bar widget sizes after showing the working set button (not done automatically)
0092         QMetaObject::invokeMethod(m_mainWindow->menuBar(), &QMenuBar::adjustSize, Qt::QueuedConnection);
0093     } else if (event->type() == QEvent::Hide) {
0094         m_separator->setVisible(false);
0095         QMetaObject::invokeMethod(m_mainWindow->menuBar(), &QMenuBar::adjustSize, Qt::QueuedConnection);
0096     }
0097 
0098     return QObject::eventFilter(obj, event);
0099 }
0100 
0101 void AreaDisplay::backToCode()
0102 {
0103     auto oldArea = m_mainWindow->area();
0104     QString workingSet = oldArea->workingSet();
0105     ICore::self()->uiController()->switchToArea(QStringLiteral("code"), IUiController::ThisWindow);
0106     m_mainWindow->area()->setWorkingSet(workingSet, oldArea->workingSetPersistent(), oldArea);
0107 }
0108 
0109 QSize AreaDisplay::minimumSizeHint() const
0110 {
0111     QSize hint = QWidget::minimumSizeHint();
0112     hint = hint.boundedTo(QSize(hint.width(), m_mainWindow->menuBar()->height()-1));
0113     return hint;
0114 }
0115 
0116 QSize AreaDisplay::sizeHint() const
0117 {
0118     QSize hint = QWidget::sizeHint();
0119     hint = hint.boundedTo(QSize(hint.width(), m_mainWindow->menuBar()->height()-1));
0120     return hint;
0121 }
0122 
0123 #include "moc_areadisplay.cpp"