File indexing completed on 2024-06-02 03:49:42

0001 /*
0002     File                 : ThemesWidget.cpp
0003     Project              : LabPlot
0004     Description          : widget for selecting themes
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2016 Prakriti Bhardwaj <p_bhardwaj14@informatik.uni-kl.de>
0007     SPDX-FileCopyrightText: 2016 Alexander Semke <alexander.semke@web.de>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "ThemesWidget.h"
0012 #include "kdefrontend/ThemeHandler.h"
0013 
0014 #include <KLocalizedString>
0015 #include <KMessageBox>
0016 // #include <knewstuff3/downloaddialog.h>
0017 
0018 #include <QApplication>
0019 #include <QFile>
0020 #include <QListWidgetItem>
0021 #include <QPainter>
0022 #include <QScreen>
0023 #include <QStandardItemModel>
0024 #include <QStandardPaths>
0025 
0026 #include <cmath>
0027 
0028 #include <gsl/gsl_const_cgs.h>
0029 
0030 /*!
0031     \class ThemesWidget
0032     \brief Widget for showing theme previews and for selecting a theme.
0033 
0034     \ingroup kdefrontend
0035  */
0036 ThemesWidget::ThemesWidget(QWidget* parent)
0037     : QListView(parent) {
0038     setSelectionMode(QAbstractItemView::SingleSelection);
0039     setWordWrap(true);
0040     setViewMode(QListWidget::IconMode);
0041     setResizeMode(QListWidget::Adjust);
0042     setDragDropMode(QListView::NoDragDrop);
0043 
0044     // make the icon 3x3cm big and show two of them in the height
0045     static const int themeIconSize = std::ceil(3.0 / GSL_CONST_CGS_INCH * QApplication::primaryScreen()->physicalDotsPerInchX());
0046     setIconSize(QSize(themeIconSize, themeIconSize));
0047     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0048 
0049     // show preview pixmaps
0050     auto* mContentItemModel = new QStandardItemModel(this);
0051     auto themeList = ThemeHandler::themes();
0052     auto themeImgPathList = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, QStringLiteral("themes/screenshots/"), QStandardPaths::LocateDirectory);
0053     if (themeImgPathList.isEmpty()) {
0054         delete mContentItemModel;
0055         return;
0056     }
0057 
0058     const QString& themeImgPath = themeImgPathList.first();
0059 
0060     for (int i = 0; i < themeList.size(); ++i) {
0061         auto* listItem = new QStandardItem();
0062 
0063         QString tempPath = themeImgPath + themeList.at(i) + QStringLiteral(".png");
0064         if (!QFile::exists(tempPath))
0065             tempPath = themeImgPath + QStringLiteral("Unavailable.png");
0066 
0067         listItem->setIcon(QIcon(QPixmap(tempPath)));
0068         if (themeList.at(i) == QLatin1String("Default")) {
0069             listItem->setText(i18n("Default"));
0070             mContentItemModel->insertRow(0, listItem);
0071         } else {
0072             listItem->setText(themeList.at(i));
0073             mContentItemModel->appendRow(listItem);
0074         }
0075     }
0076 
0077     // adding download themes option
0078     // TODO: activate this later
0079     //  auto* listItem = new QStandardItem();
0080     //  listItem->setIcon(QIcon::fromTheme("get-hot-new-stuff"));
0081     //  listItem->setText("Download Themes");
0082     //  listItem->setData("file_download_theme", Qt::UserRole);
0083     //  mContentItemModel->appendRow(listItem);
0084 
0085     QListView::setModel(mContentItemModel);
0086 
0087     // SLOTS
0088     connect(this, &ThemesWidget::clicked, this, &ThemesWidget::applyClicked);
0089 }
0090 
0091 void ThemesWidget::applyClicked(const QModelIndex& index) {
0092     const QString& themeName = index.data(Qt::DisplayRole).toString();
0093 
0094     // TODO: activate this later
0095     //  if (themeName == "file_download_theme")
0096     //      this->downloadThemes();
0097     //  else
0098 
0099     if (index.row() == 0)
0100         Q_EMIT themeSelected(QString()); // item with the string "None" was selected -> no theme
0101     else
0102         Q_EMIT themeSelected(themeName);
0103 }
0104 
0105 // TODO: activate this later
0106 //  void ThemesWidget::downloadThemes() {
0107 //      KNS3::DownloadDialog dialog("labplot2_themes.knsrc", this);
0108 //      dialog.exec();
0109 //      foreach (const KNS3::Entry& e, dialog.changedEntries()) {
0110 //          kDebug() << "Changed Entry: " << e.name();
0111 //      }
0112 //  }
0113 
0114 void ThemesWidget::setFixedMode() {
0115     // resize the widget to show three items only
0116     int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
0117     QFont font;
0118     QFontMetrics fm(font);
0119     static const int themeIconSize = std::ceil(3.0 / GSL_CONST_CGS_INCH * QApplication::primaryScreen()->physicalDotsPerInchX());
0120     QSize widgetSize(themeIconSize + style()->pixelMetric(QStyle::PM_ScrollBarExtent) + frameWidth * 2,
0121                      3 * (themeIconSize + fm.height() + 2 * frameWidth) + fm.height() + frameWidth);
0122     setMinimumSize(widgetSize);
0123     setMaximumSize(widgetSize);
0124 }