cfgappearance.cpp
1 //------------------------------------------------------------------------------
2 // cfgappearance.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "cfgappearance.h"
24 #include "ui_cfgappearance.h"
25 
26 #include "gui/helpers/playersdiagram.h"
27 #include "configuration/doomseekerconfig.h"
28 #include <QColorDialog>
29 #include <QSystemTrayIcon>
30 #include "ip2c/ip2c.h"
31 #include "localization.h"
32 #include "log.h"
33 #include "main.h"
34 
35 DClass<CFGAppearance> : public Ui::CFGAppearance
36 {
37 public:
38  struct SavedState
39  {
40  bool bDrawGridInServerTable;
41  bool bMarkServersWithBuddies;
42  QString buddyServersColor;
43  QString customServersColor;
44  QString lanServersColor;
45  QString slotStyle;
46  };
47 
48  bool readingSettings;
49  SavedState savedState;
50 
51  void resetSavedState()
52  {
53  savedState.bDrawGridInServerTable = gConfig.doomseeker.bDrawGridInServerTable;
54  savedState.bMarkServersWithBuddies = gConfig.doomseeker.bMarkServersWithBuddies;
55  savedState.buddyServersColor = gConfig.doomseeker.buddyServersColor;
56  savedState.customServersColor = gConfig.doomseeker.customServersColor;
57  savedState.lanServersColor = gConfig.doomseeker.lanServersColor;
58  savedState.slotStyle = gConfig.doomseeker.slotStyle;
59  }
60 };
61 
62 DPointered(CFGAppearance)
63 
64 CFGAppearance::CFGAppearance(QWidget *parent)
65 : ConfigPage(parent)
66 {
67  set_reject(&CFGAppearance::reject_);
68  d->setupUi(this);
69  d->readingSettings = false;
70  d->translationRestartNotifierWidget->setVisible(false);
71 }
72 
73 CFGAppearance::~CFGAppearance()
74 {
75 }
76 
77 void CFGAppearance::dynamicAppearanceChange()
78 {
79  if (!d->readingSettings)
80  {
81  saveDynamicSettings();
82  emit appearanceChanged();
83  }
84 }
85 
86 void CFGAppearance::initLanguagesList()
87 {
88  foreach (const LocalizationInfo& obj, Localization::get()->localizations)
89  {
90  const QString& flagName = obj.countryCodeName;
91  const QString& translationName = obj.localeName;
92 
93  QString displayName = obj.niceName;
94  if (translationName == LocalizationInfo::SYSTEM_FOLLOW.localeName)
95  displayName = tr("Use system language");
96 
97  QPixmap flag;
98  if (!flagName.isEmpty())
99  flag = IP2C::instance()->flag(flagName);
100 
101  d->cboLanguage->addItem(flag, displayName, translationName);
102  }
103 }
104 
105 void CFGAppearance::initSlotStyles(const QString &selected)
106 {
107  QList<PlayersDiagramStyle> styles = PlayersDiagram::availableSlotStyles();
108  d->slotStyle->clear();
109  foreach (const PlayersDiagramStyle &style, styles)
110  {
111  d->slotStyle->addItem(style.displayName, style.name);
112  if (style.name == selected)
113  {
114  d->slotStyle->setCurrentIndex(d->slotStyle->count() - 1);
115  }
116  }
117 }
118 
119 void CFGAppearance::reject_()
120 {
121  // Restore dynamically changed settings upon config rejection.
122  gConfig.doomseeker.bDrawGridInServerTable = d->savedState.bDrawGridInServerTable;
123  gConfig.doomseeker.bMarkServersWithBuddies = d->savedState.bMarkServersWithBuddies;
124  gConfig.doomseeker.buddyServersColor = d->savedState.buddyServersColor;
125  gConfig.doomseeker.customServersColor = d->savedState.customServersColor;
126  gConfig.doomseeker.lanServersColor = d->savedState.lanServersColor;
127  gConfig.doomseeker.slotStyle = d->savedState.slotStyle;
128 }
129 
131 {
132  d->readingSettings = true;
133 
134  // Memorize settings that can be dynamically applied so that
135  // we can restore them when config is rejected.
136  d->resetSavedState();
137 
138  // Load settings into widgets.
139  if (d->cboLanguage->count() == 0)
140  {
141  initLanguagesList();
142  }
143  initSlotStyles(gConfig.doomseeker.slotStyle);
144 
145  d->btnCustomServersColor->setColorHtml(gConfig.doomseeker.customServersColor);
146  d->btnBuddyServersColor->setColorHtml(gConfig.doomseeker.buddyServersColor);
147  d->btnLanServersColor->setColorHtml(gConfig.doomseeker.lanServersColor);
148  d->cbMarkServersWithBuddies->setChecked(gConfig.doomseeker.bMarkServersWithBuddies);
149 
150  // Make sure that the tray is available. If it's not, disable tray icon
151  // completely and make sure no change can be done to the configuration in
152  // this manner.
153  if (!QSystemTrayIcon::isSystemTrayAvailable())
154  {
155  gConfig.doomseeker.bUseTrayIcon = false;
156  gConfig.doomseeker.bCloseToTrayIcon = false;
157  d->gboUseTrayIcon->setEnabled(false);
158  }
159 
160  d->gboUseTrayIcon->setChecked(gConfig.doomseeker.bUseTrayIcon);
161 
162  d->cbCloseToTrayIcon->setChecked(gConfig.doomseeker.bCloseToTrayIcon);
163 
164  d->cbColorizeConsole->setChecked(gConfig.doomseeker.bColorizeServerConsole);
165  d->cbDrawGridInServerTable->setChecked(gConfig.doomseeker.bDrawGridInServerTable);
166 
167  d->cbHidePasswords->setChecked(gConfig.doomseeker.bHidePasswords);
168 
169  d->cbLookupHosts->setChecked(gConfig.doomseeker.bLookupHosts);
170 
171  // This is not really an appearance option, but it does change how the list
172  // appears and thus utilized the fact that the appearance options cause the
173  // list to refresh. It also doesn't fit into any of the other existing
174  // categories at this time.
175  d->cbBotsNotPlayers->setChecked(gConfig.doomseeker.bBotsAreNotPlayers);
176 
177  // Set language.
178  const QString &localization = gConfig.doomseeker.localization;
179  LocalizationInfo bestMatchedLocalization = LocalizationInfo::findBestMatch(
180  Localization::get()->localizations, localization);
181  int idxLanguage = -1;
182  if (bestMatchedLocalization.isValid())
183  idxLanguage = d->cboLanguage->findData(bestMatchedLocalization.localeName);
184  if (idxLanguage >= 0)
185  {
186  d->cboLanguage->setCurrentIndex(idxLanguage);
187  }
188  else
189  {
190  // Display that there is something wrong.
191  QString name = gConfig.doomseeker.localization;
192  const QPixmap& icon = IP2C::instance()->flagUnknown;
193  QString str = tr("Unknown language definition \"%1\"").arg(name);
194  d->cboLanguage->addItem(icon, str, name);
195  d->cboLanguage->setCurrentIndex(d->cboLanguage->count() - 1);
196  }
197 
198  d->readingSettings = false;
199 }
200 
202 {
203  saveDynamicSettings();
204 
205  gConfig.doomseeker.bUseTrayIcon = d->gboUseTrayIcon->isChecked();
206  gConfig.doomseeker.bCloseToTrayIcon = d->cbCloseToTrayIcon->isChecked();
207  gConfig.doomseeker.bColorizeServerConsole = d->cbColorizeConsole->isChecked();
208  gConfig.doomseeker.bBotsAreNotPlayers = d->cbBotsNotPlayers->isChecked();
209  gConfig.doomseeker.bHidePasswords = d->cbHidePasswords->isChecked();
210  gConfig.doomseeker.bLookupHosts = d->cbLookupHosts->isChecked();
211  QString localization = d->cboLanguage->itemData(d->cboLanguage->currentIndex()).toString();
212  if (localization != gConfig.doomseeker.localization)
213  {
214  // Translation may be strenuous so do it only if the selected
215  // value actually changed.
216  LocalizationInfo previousLocalization = Localization::get()->currentLocalization();
217 
218  gConfig.doomseeker.localization = localization;
219  gLog << tr("Loading translation \"%1\"").arg(localization);
220  Localization::get()->loadTranslation(localization);
221  if (Localization::get()->currentLocalization() != previousLocalization)
222  {
223  gLog << tr("Program needs to be restarted to fully apply the translation");
224  emit restartNeeded();
225  }
226  }
227 
228  d->resetSavedState();
229  emit appearanceChanged();
230 }
231 
232 void CFGAppearance::saveDynamicSettings()
233 {
234  gConfig.doomseeker.bDrawGridInServerTable = d->cbDrawGridInServerTable->isChecked();
235  gConfig.doomseeker.bMarkServersWithBuddies = d->cbMarkServersWithBuddies->isChecked();
236  gConfig.doomseeker.buddyServersColor = d->btnBuddyServersColor->colorHtml();
237  gConfig.doomseeker.customServersColor = d->btnCustomServersColor->colorHtml();
238  gConfig.doomseeker.lanServersColor = d->btnLanServersColor->colorHtml();
239  gConfig.doomseeker.slotStyle = d->slotStyle->itemData(d->slotStyle->currentIndex()).toString();
240 }
241 
242 void CFGAppearance::setVisibilityOfLanguageChangeNotificationIfNeeded()
243 {
244  QString chosenLocalization = d->cboLanguage->itemData(d->cboLanguage->currentIndex()).toString();
245  bool switchingToOsLocaleWhichIsAlsoTheCurrentLocale = false;
246  if (chosenLocalization == LocalizationInfo::SYSTEM_FOLLOW.localeName)
247  {
248  switchingToOsLocaleWhichIsAlsoTheCurrentLocale =
249  Localization::get()->isCurrentlyLoaded(QLocale::system().name());
250  }
251  d->translationRestartNotifierWidget->setVisible(
252  !Localization::get()->isCurrentlyLoaded(chosenLocalization) &&
253  !switchingToOsLocaleWhichIsAlsoTheCurrentLocale);
254 }
void readSettings()
Reimplement this to read settings from config into widgets.
QString localeName
Compliant with language_country standard. See QLocale::name()
QString niceName
Name that will be displayed to user.
void saveSettings()
Reimplement this to write settings to config from widgets.
QString countryCodeName
The same as code used for country flags in IP2C.
static const LocalizationInfo SYSTEM_FOLLOW
Base class for configuration pages.
Definition: configpage.h:44