Messagelib

themecombobox.cpp
1 /* SPDX-FileCopyrightText: 2009 James Bendig <[email protected]>
2 
3  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
4 */
5 #include "themecombobox.h"
6 
7 #include "utils/themecombobox.h"
8 #include "utils/themecombobox_p.h"
9 
10 #include "core/manager.h"
11 #include "core/theme.h"
12 #include "messagelistsettings.h"
13 #include "storagemodel.h"
14 
15 using namespace MessageList::Core;
16 using namespace MessageList::Utils;
17 
18 ThemeComboBox::ThemeComboBox(QWidget *parent)
19  : QComboBox(parent)
20  , d(new ThemeComboBoxPrivate(this))
21 {
22  if (Manager::instance()) {
23  d->slotLoadThemes();
24  } else {
25  setEnabled(false);
26  }
27 }
28 
29 ThemeComboBox::~ThemeComboBox() = default;
30 
31 QString ThemeComboBox::currentTheme() const
32 {
33  return itemData(currentIndex()).toString();
34 }
35 
36 void ThemeComboBox::writeDefaultConfig() const
37 {
38  KConfigGroup group(MessageListSettings::self()->config(), "MessageListView::StorageModelThemes");
39 
40  const QString themeID = currentTheme();
41  group.writeEntry(QStringLiteral("DefaultSet"), themeID);
42  if (Manager::instance()) {
43  Manager::instance()->themesConfigurationCompleted();
44  }
45 }
46 
47 void ThemeComboBox::writeStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool isPrivateSetting) const
48 {
49  writeStorageModelConfig(storageModel->id(), isPrivateSetting);
50 }
51 
52 void ThemeComboBox::writeStorageModelConfig(const QString &id, bool isPrivateSetting) const
53 {
54  if (Manager::instance()) {
55  QString themeID;
56  if (isPrivateSetting) {
57  themeID = currentTheme();
58  } else { // explicitly use default theme id when using default theme.
59  themeID = Manager::instance()->defaultTheme()->id();
60  }
61  Manager::instance()->saveThemeForStorageModel(id, themeID, isPrivateSetting);
62  Manager::instance()->themesConfigurationCompleted();
63  }
64 }
65 
66 void ThemeComboBox::readStorageModelConfig(const Akonadi::Collection &col, bool &isPrivateSetting)
67 {
68  if (Manager::instance()) {
69  const Theme *theme = Manager::instance()->themeForStorageModel(col, &isPrivateSetting);
70  d->setCurrentTheme(theme);
71  }
72 }
73 
74 void ThemeComboBox::readStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool &isPrivateSetting)
75 {
76  if (Manager::instance()) {
77  const Theme *theme = Manager::instance()->themeForStorageModel(storageModel, &isPrivateSetting);
78  d->setCurrentTheme(theme);
79  }
80 }
81 
82 void ThemeComboBox::selectDefault()
83 {
84  if (Manager::instance()) {
85  const Theme *defaultTheme = Manager::instance()->defaultTheme();
86  d->setCurrentTheme(defaultTheme);
87  }
88 }
89 
90 void ThemeComboBox::slotLoadThemes()
91 {
92  d->slotLoadThemes();
93 }
94 
95 void ThemeComboBoxPrivate::slotLoadThemes()
96 {
97  if (!Manager::instance()) {
98  return;
99  }
100  q->clear();
101 
102  // Get all message list themes and sort them into alphabetical order.
103  QList<Theme *> themes = Manager::instance()->themes().values();
104  std::sort(themes.begin(), themes.end(), MessageList::Core::Theme::compareName);
105 
106  for (const Theme *theme : std::as_const(themes)) {
107  q->addItem(theme->name(), QVariant(theme->id()));
108  }
109 }
110 
111 void ThemeComboBoxPrivate::setCurrentTheme(const Theme *theme)
112 {
113  Q_ASSERT(theme != nullptr);
114 
115  const QString themeID = theme->id();
116  const int themeIndex = q->findData(QVariant(themeID));
117  q->setCurrentIndex(themeIndex);
118 }
119 
120 #include "moc_themecombobox.cpp"
const QString & name() const
Returns the name of this OptionSet.
Definition: optionset.h:59
KSharedConfigPtr config()
The Theme class defines the visual appearance of the MessageList.
Definition: theme.h:48
const QString & id() const
Returns the unique id of this OptionSet.
Definition: optionset.h:51
The QAbstractItemModel based interface that you need to provide for your storage to work with Message...
QList::iterator begin()
QList::iterator end()
virtual QString id() const =0
Returns an unique id for this Storage collection.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Mar 22 2023 04:07:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.