File indexing completed on 2024-05-12 15:28:21
0001 /*************************************************************************** 0002 File : ThemesWidget.cpp 0003 Project : LabPlot 0004 Description : widget for selecting themes 0005 -------------------------------------------------------------------- 0006 Copyright : (C) 2016 Prakriti Bhardwaj (p_bhardwaj14@informatik.uni-kl.de) 0007 Copyright : (C) 2016 Alexander Semke (alexander.semke@web.de) 0008 0009 ***************************************************************************/ 0010 0011 /*************************************************************************** 0012 * * 0013 * This program is free software; you can redistribute it and/or modify * 0014 * it under the terms of the GNU General Public License as published by * 0015 * the Free Software Foundation; either version 2 of the License, or * 0016 * (at your option) any later version. * 0017 * * 0018 * This program is distributed in the hope that it will be useful, * 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0021 * GNU General Public License for more details. * 0022 * * 0023 * You should have received a copy of the GNU General Public License * 0024 * along with this program; if not, write to the Free Software * 0025 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0026 * Boston, MA 02110-1301 USA * 0027 * * 0028 ***************************************************************************/ 0029 #include "ThemesWidget.h" 0030 #include "kdefrontend/ThemeHandler.h" 0031 0032 #include <QApplication> 0033 #include <QDesktopWidget> 0034 #include <QFile> 0035 #include <QListWidgetItem> 0036 #include <QPainter> 0037 #include <QStandardItemModel> 0038 #include <QStandardPaths> 0039 0040 #include <KLocalizedString> 0041 #include <KMessageBox> 0042 // #include <knewstuff3/downloaddialog.h> 0043 0044 #include <cmath> 0045 0046 /*! 0047 \class ThemesWidget 0048 \brief Widget for showing theme previews and for selecting a theme. 0049 0050 \ingroup kdefrontend 0051 */ 0052 ThemesWidget::ThemesWidget(QWidget* parent) : QListView(parent) { 0053 setSelectionMode(QAbstractItemView::SingleSelection); 0054 setWordWrap(true); 0055 setViewMode(QListWidget::IconMode); 0056 setResizeMode(QListWidget::Adjust); 0057 setDragDropMode(QListView::NoDragDrop); 0058 0059 //make the icon 3x3cm big and show two of them in the height 0060 static const int themeIconSize = std::ceil(3.0/2.54 * QApplication::desktop()->physicalDpiX()); 0061 setIconSize(QSize(themeIconSize, themeIconSize)); 0062 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0063 0064 //show preview pixmaps 0065 auto* mContentItemModel = new QStandardItemModel(this); 0066 QStringList themeList = ThemeHandler::themes(); 0067 QStringList themeImgPathList = QStandardPaths::locateAll(QStandardPaths::DataLocation, "themes/screenshots/", QStandardPaths::LocateDirectory); 0068 if (themeImgPathList.isEmpty()) { 0069 delete mContentItemModel; 0070 return; 0071 } 0072 0073 const QString& themeImgPath = themeImgPathList.first(); 0074 QString tempPath; 0075 0076 for (int i = 0; i < themeList.size(); ++i) { 0077 auto* listItem = new QStandardItem(); 0078 0079 tempPath = themeImgPath + themeList.at(i) + ".png"; 0080 if (!QFile::exists(tempPath)) 0081 tempPath = themeImgPath + "Unavailable.png"; 0082 0083 listItem->setIcon(QIcon(QPixmap(tempPath))); 0084 if (themeList.at(i) == QLatin1String("Default")) { 0085 listItem->setText(i18n("Default")); 0086 mContentItemModel->insertRow(0, listItem); 0087 } else { 0088 listItem->setText(themeList.at(i)); 0089 mContentItemModel->appendRow(listItem); 0090 } 0091 } 0092 0093 //adding download themes option 0094 //TODO: activate this later 0095 // QStandardItem* listItem = new QStandardItem(); 0096 // listItem->setIcon(QIcon::fromTheme("get-hot-new-stuff")); 0097 // listItem->setText("Download Themes"); 0098 // listItem->setData("file_download_theme", Qt::UserRole); 0099 // mContentItemModel->appendRow(listItem); 0100 0101 QListView::setModel(mContentItemModel); 0102 0103 //SLOTS 0104 connect(this, &ThemesWidget::clicked, this, &ThemesWidget::applyClicked); 0105 } 0106 0107 void ThemesWidget::applyClicked(const QModelIndex& index) { 0108 const QString& themeName = index.data(Qt::DisplayRole).toString(); 0109 0110 //TODO: activate this later 0111 // if (themeName == "file_download_theme") 0112 // this->downloadThemes(); 0113 // else 0114 0115 if (index.row() == 0) 0116 emit themeSelected(QString()); //item with the string "None" was selected -> no theme 0117 else 0118 emit themeSelected(themeName); 0119 } 0120 0121 //TODO: activate this later 0122 // void ThemesWidget::downloadThemes() { 0123 // KNS3::DownloadDialog dialog("labplot2_themes.knsrc", this); 0124 // dialog.exec(); 0125 // foreach (const KNS3::Entry& e, dialog.changedEntries()) { 0126 // kDebug() << "Changed Entry: " << e.name(); 0127 // } 0128 // } 0129 0130 void ThemesWidget::setFixedMode() { 0131 //resize the widget to show three items only 0132 int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); 0133 QFont font; 0134 QFontMetrics fm(font); 0135 static const int themeIconSize = std::ceil(3.0/2.54 * QApplication::desktop()->physicalDpiX()); 0136 QSize widgetSize(themeIconSize + style()->pixelMetric(QStyle::PM_ScrollBarExtent) + frameWidth*2, 0137 3*(themeIconSize + fm.height() + 2* frameWidth) + fm.height() + frameWidth); 0138 setMinimumSize(widgetSize); 0139 setMaximumSize(widgetSize); 0140 }