1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2  * (c)LGPL2+
3  *
4  * LXQt - a lightweight, Qt based, desktop toolset
5  * https://lxqt.org
6  *
7  * Copyright: 2010-2011 Razor team
8  * Authors:
9  *   Petr Vanek <petr@scribus.info>
10  *
11  * This program or library is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20 
21  * You should have received a copy of the GNU Lesser General
22  * Public License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301 USA
25  *
26  * END_COMMON_COPYRIGHT_HEADER */
27 
28 #include "iconthemeconfig.h"
29 
30 #include <LXQt/Settings>
31 #include <QStringList>
32 #include <QStringBuilder>
33 #include <QIcon>
34 
IconThemeConfig(LXQt::Settings * settings,QWidget * parent)35 IconThemeConfig::IconThemeConfig(LXQt::Settings* settings, QWidget* parent):
36     QWidget(parent),
37     m_settings(settings)
38 {
39     setupUi(this);
40 
41     initIconsThemes();
42     initControls();
43 
44     connect(iconThemeList, &QTreeWidget::currentItemChanged, this, &IconThemeConfig::settingsChanged);
45     connect(iconFollowColorSchemeCB, &QAbstractButton::clicked, this, &IconThemeConfig::settingsChanged);
46 }
47 
48 
initIconsThemes()49 void IconThemeConfig::initIconsThemes()
50 {
51     QStringList processed;
52     const QStringList baseDirs = QIcon::themeSearchPaths();
53     static const QStringList iconNames = QStringList()
54                     << QStringLiteral("document-open")
55                     << QStringLiteral("document-new")
56                     << QStringLiteral("edit-undo")
57                     << QStringLiteral("media-playback-start");
58 
59     const int iconNamesN = iconNames.size();
60     iconThemeList->setColumnCount(iconNamesN + 2);
61 
62     QList<QTreeWidgetItem *> items;
63     for (const QString &baseDirName : baseDirs)
64     {
65         QDir baseDir(baseDirName);
66         if (!baseDir.exists())
67             continue;
68 
69         const QFileInfoList dirs = baseDir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
70         for (const QFileInfo &dir : dirs)
71         {
72             if (!processed.contains(dir.canonicalFilePath()))
73             {
74                 processed << dir.canonicalFilePath();
75 
76                 IconThemeInfo theme(QDir(dir.canonicalFilePath()));
77                 if (theme.isValid() && (!theme.isHidden()))
78                 {
79                     QTreeWidgetItem *item = new QTreeWidgetItem((QTreeWidget*)nullptr);
80                     item->setSizeHint(0, QSize(42,42)); // make icons non-cropped
81                     item->setData(0, Qt::UserRole, theme.name());
82 
83                     const QVector<QIcon> icons = theme.icons(iconNames);
84 
85                     const int K = icons.size();
86                     for (int i = 0; i < K; ++i)
87                     {
88                         item->setIcon(i, icons.at(i));
89                     }
90 
91                     QString themeDescription;
92                     if (theme.comment().isEmpty())
93                     {
94                         themeDescription = theme.text();
95                     }
96                     else
97                     {
98                         themeDescription = theme.text() % QStringLiteral(" (") % theme.comment() % QStringLiteral(")");
99                     }
100 
101                     item->setText(iconNamesN + 1, themeDescription);
102 
103                     items.append(item);
104                 }
105             }
106         }
107     }
108 
109     iconThemeList->insertTopLevelItems(0, items);
110     for (int i=0; i<iconThemeList->header()->count()-1; ++i)
111     {
112         iconThemeList->resizeColumnToContents(i);
113     }
114 }
115 
116 
initControls()117 void IconThemeConfig::initControls()
118 {
119     QString currentTheme = QIcon::themeName();
120     QTreeWidgetItemIterator it(iconThemeList);
121     while (*it) {
122         if ((*it)->data(0, Qt::UserRole).toString() == currentTheme)
123         {
124             iconThemeList->setCurrentItem((*it));
125             break;
126         }
127         ++it;
128     }
129 
130     iconFollowColorSchemeCB->setChecked(m_settings->value(QStringLiteral("icon_follow_color_scheme"), true).toBool());
131 
132     update();
133 }
134 
135 
~IconThemeConfig()136 IconThemeConfig::~IconThemeConfig()
137 {
138 }
139 
140 
applyIconTheme()141 void IconThemeConfig::applyIconTheme()
142 {
143     if(m_settings->value(QStringLiteral("icon_follow_color_scheme")).toBool() != iconFollowColorSchemeCB->isChecked())
144         m_settings->setValue(QStringLiteral("icon_follow_color_scheme"), iconFollowColorSchemeCB->isChecked());
145 
146     if(QTreeWidgetItem *item = iconThemeList->currentItem()) {
147         const QString theme = item->data(0, Qt::UserRole).toString();
148         if (!theme.isEmpty() && m_settings->value(QStringLiteral("icon_theme")).toString() != theme) {
149             // Ensure that this widget also updates it's own icons
150             QIcon::setThemeName(theme);
151 
152             m_settings->setValue(QStringLiteral("icon_theme"),  theme);
153             m_settings->sync();
154 
155             emit updateOtherSettings();
156         }
157     }
158 }
159