File indexing completed on 2024-05-12 15:28:20

0001 /***************************************************************************
0002     File                 : ThemesComboBox.cpp
0003     Project              : LabPlot
0004     Description          : Preview of all themes in a QComboBox
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2017 by Alexander Semke (alexander.semke@web.de)
0007 
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 
0029 #include "kdefrontend/widgets/ThemesComboBox.h"
0030 #include "kdefrontend/widgets/ThemesWidget.h"
0031 
0032 #include <QEvent>
0033 #include <QGroupBox>
0034 #include <QVBoxLayout>
0035 
0036 #include <KLocalizedString>
0037 
0038 /*!
0039     \class ThemesComboBox
0040     \brief Preview of all themes in a QComboBox.
0041 
0042     \ingroup backend/widgets
0043 */
0044 ThemesComboBox::ThemesComboBox(QWidget* parent) : QComboBox(parent) {
0045     auto* layout = new QVBoxLayout;
0046     m_view = new ThemesWidget(this);
0047     m_groupBox = new QGroupBox;
0048 
0049     layout->setContentsMargins(0, 0, 0, 0);
0050     layout->setSpacing(0);
0051     layout->addWidget(m_view);
0052 
0053     m_groupBox->setLayout(layout);
0054     m_groupBox->setParent(parent, Qt::Popup);
0055     m_groupBox->hide();
0056     m_groupBox->installEventFilter(this);
0057 
0058     addItem(QString());
0059     setCurrentIndex(0);
0060 
0061     connect(m_view, &ThemesWidget::themeSelected, this, &ThemesComboBox::handleThemeChanged);
0062 }
0063 
0064 void ThemesComboBox::showPopup() {
0065     m_groupBox->show();
0066     m_groupBox->resize(this->width(), 250);
0067     m_groupBox->move(mapToGlobal( this->rect().topLeft() ));
0068 }
0069 
0070 void ThemesComboBox::hidePopup() {
0071     m_groupBox->hide();
0072 }
0073 
0074 /*!
0075     catches the MouseButtonPress-event and hides the tree view on mouse clicking.
0076 */
0077 bool ThemesComboBox::eventFilter(QObject* object, QEvent* event) {
0078     if ( (object == m_groupBox) && event->type() == QEvent::MouseButtonPress ) {
0079         m_groupBox->hide();
0080         this->setFocus();
0081         return true;
0082     }
0083 
0084     return QComboBox::eventFilter(object, event);
0085 }
0086 
0087 void ThemesComboBox::handleThemeChanged(const QString& theme) {
0088     if (theme != currentText()) {
0089         if (theme.isEmpty())
0090             setItemText(0, i18n("Default")); //default theme
0091         else
0092             setItemText(0, theme);
0093         emit currentThemeChanged(theme);
0094     }
0095 
0096     m_groupBox->hide();
0097 }