File indexing completed on 2025-10-12 03:31:34

0001 /*
0002     File                 : ThemesComboBox.cpp
0003     Project              : LabPlot
0004     Description          : Preview of all themes in a QComboBox
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2017 Alexander Semke <alexander.semke@web.de>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "kdefrontend/widgets/ThemesComboBox.h"
0011 #include "kdefrontend/widgets/ThemesWidget.h"
0012 
0013 #include <QEvent>
0014 #include <QGroupBox>
0015 #include <QVBoxLayout>
0016 
0017 #include <KLocalizedString>
0018 
0019 /*!
0020     \class ThemesComboBox
0021     \brief Preview of all themes in a QComboBox.
0022 
0023     \ingroup backend/widgets
0024 */
0025 ThemesComboBox::ThemesComboBox(QWidget* parent)
0026     : QComboBox(parent) {
0027     auto* layout = new QVBoxLayout;
0028     m_view = new ThemesWidget(this);
0029     m_groupBox = new QGroupBox;
0030 
0031     layout->setContentsMargins(0, 0, 0, 0);
0032     layout->setSpacing(0);
0033     layout->addWidget(m_view);
0034 
0035     m_groupBox->setLayout(layout);
0036     m_groupBox->setParent(parent, Qt::Popup);
0037     m_groupBox->hide();
0038     m_groupBox->installEventFilter(this);
0039 
0040     addItem(QString());
0041     setCurrentIndex(0);
0042 
0043     connect(m_view, &ThemesWidget::themeSelected, this, &ThemesComboBox::handleThemeChanged);
0044 }
0045 
0046 void ThemesComboBox::showPopup() {
0047     m_groupBox->show();
0048     m_groupBox->resize(this->width(), 250);
0049     m_groupBox->move(mapToGlobal(this->rect().topLeft()));
0050 }
0051 
0052 void ThemesComboBox::hidePopup() {
0053     m_groupBox->hide();
0054 }
0055 
0056 /*!
0057     catches the MouseButtonPress-event and hides the tree view on mouse clicking.
0058 */
0059 bool ThemesComboBox::eventFilter(QObject* object, QEvent* event) {
0060     if ((object == m_groupBox) && event->type() == QEvent::MouseButtonPress) {
0061         m_groupBox->hide();
0062         this->setFocus();
0063         return true;
0064     }
0065 
0066     return QComboBox::eventFilter(object, event);
0067 }
0068 
0069 void ThemesComboBox::handleThemeChanged(const QString& theme) {
0070     if (theme != currentText()) {
0071         if (theme.isEmpty())
0072             setItemText(0, i18n("Default")); // default theme
0073         else
0074             setItemText(0, theme);
0075         Q_EMIT currentThemeChanged(theme);
0076     }
0077 
0078     m_groupBox->hide();
0079 }