qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
userinterfaceform.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "userinterfaceform.h"
21 #include "ui_userinterfacesettings.h"
22 
23 #include <QDebug>
24 #include <QDesktopWidget>
25 #include <QFileDialog>
26 #include <QFont>
27 #include <QMessageBox>
28 #include <QRegularExpressionValidator>
29 #include <QStyleFactory>
30 #include <QTime>
31 #include <QVector>
32 
33 #include "src/core/core.h"
34 #include "src/core/coreav.h"
39 #include "src/widget/style.h"
41 #include "src/widget/translator.h"
42 #include "src/widget/widget.h"
43 
58  : GenericForm(QPixmap(":/img/settings/general.png"))
59 {
60  parent = myParent;
61 
62  bodyUI = new Ui::UserInterfaceSettings;
63  bodyUI->setupUi(this);
64 
65  // block all child signals during initialization
66  const RecursiveSignalBlocker signalBlocker(this);
67 
69  const QFont chatBaseFont = s.getChatMessageFont();
70  bodyUI->txtChatFontSize->setValue(QFontInfo(chatBaseFont).pixelSize());
71  bodyUI->txtChatFont->setCurrentFont(chatBaseFont);
72  int index = static_cast<int>(s.getStylePreference());
73  bodyUI->textStyleComboBox->setCurrentIndex(index);
74  bodyUI->useNameColors->setChecked(s.getEnableGroupChatsColor());
75 
76  bodyUI->notify->setChecked(s.getNotify());
77  // Note: UI is boolean inversed from settings to maintain setting file backwards compatibility
78  bodyUI->groupOnlyNotfiyWhenMentioned->setChecked(!s.getGroupAlwaysNotify());
79  bodyUI->groupOnlyNotfiyWhenMentioned->setEnabled(s.getNotify());
80  bodyUI->notifySound->setChecked(s.getNotifySound());
81  bodyUI->notifyHide->setChecked(s.getNotifyHide());
82  bodyUI->notifySound->setEnabled(s.getNotify());
83  bodyUI->busySound->setChecked(s.getBusySound());
84  bodyUI->busySound->setEnabled(s.getNotifySound() && s.getNotify());
85 #if DESKTOP_NOTIFICATIONS
86  bodyUI->desktopNotify->setChecked(s.getDesktopNotify());
87  bodyUI->desktopNotify->setEnabled(s.getNotify());
88 #else
89  bodyUI->desktopNotify->hide();
90 #endif
91 
92  bodyUI->showWindow->setChecked(s.getShowWindow());
93 
94  bodyUI->cbGroupchatPosition->setChecked(s.getGroupchatPosition());
95  bodyUI->cbCompactLayout->setChecked(s.getCompactLayout());
96  bodyUI->cbSeparateWindow->setChecked(s.getSeparateWindow());
97  bodyUI->cbDontGroupWindows->setChecked(s.getDontGroupWindows());
98  bodyUI->cbDontGroupWindows->setEnabled(s.getSeparateWindow());
99  bodyUI->cbShowIdenticons->setChecked(s.getShowIdenticons());
100 
101  bodyUI->useEmoticons->setChecked(s.getUseEmoticons());
102  for (auto entry : SmileyPack::listSmileyPacks())
103  bodyUI->smileyPackBrowser->addItem(entry.first, entry.second);
104 
105  smileLabels = {bodyUI->smile1, bodyUI->smile2, bodyUI->smile3, bodyUI->smile4, bodyUI->smile5};
106 
107  int currentPack = bodyUI->smileyPackBrowser->findData(s.getSmileyPack());
108  bodyUI->smileyPackBrowser->setCurrentIndex(currentPack);
109  reloadSmileys();
110  bodyUI->smileyPackBrowser->setEnabled(bodyUI->useEmoticons->isChecked());
111 
112  bodyUI->styleBrowser->addItem(tr("None"));
113  bodyUI->styleBrowser->addItems(QStyleFactory::keys());
114 
115  QString style;
116  if (QStyleFactory::keys().contains(s.getStyle()))
117  style = s.getStyle();
118  else
119  style = tr("None");
120 
121  bodyUI->styleBrowser->setCurrentText(style);
122 
123  for (QString color : Style::getThemeColorNames())
124  bodyUI->themeColorCBox->addItem(color);
125 
126  bodyUI->themeColorCBox->setCurrentIndex(s.getThemeColor());
127  bodyUI->emoticonSize->setValue(s.getEmojiFontPointSize());
128 
129  QLocale ql;
130  QStringList timeFormats;
131  timeFormats << ql.timeFormat(QLocale::ShortFormat) << ql.timeFormat(QLocale::LongFormat)
132  << "hh:mm AP"
133  << "hh:mm:ss AP"
134  << "hh:mm:ss";
135  timeFormats.removeDuplicates();
136  bodyUI->timestamp->addItems(timeFormats);
137 
138  QRegularExpression re(QString("^[^\\n]{0,%0}$").arg(MAX_FORMAT_LENGTH));
139  QRegularExpressionValidator* validator = new QRegularExpressionValidator(re, this);
140  QString timeFormat = s.getTimestampFormat();
141 
142  if (!re.match(timeFormat).hasMatch())
143  timeFormat = timeFormats[0];
144 
145  bodyUI->timestamp->setCurrentText(timeFormat);
146  bodyUI->timestamp->setValidator(validator);
147  on_timestamp_editTextChanged(timeFormat);
148 
149  QStringList dateFormats;
150  dateFormats << QStringLiteral("yyyy-MM-dd") // ISO 8601
151  // format strings from system locale
152  << ql.dateFormat(QLocale::LongFormat) << ql.dateFormat(QLocale::ShortFormat)
153  << ql.dateFormat(QLocale::NarrowFormat) << "dd-MM-yyyy"
154  << "d-MM-yyyy"
155  << "dddd dd-MM-yyyy"
156  << "dddd d-MM";
157 
158  dateFormats.removeDuplicates();
159  bodyUI->dateFormats->addItems(dateFormats);
160 
161  QString dateFormat = s.getDateFormat();
162  if (!re.match(dateFormat).hasMatch())
163  dateFormat = dateFormats[0];
164 
165  bodyUI->dateFormats->setCurrentText(dateFormat);
166  bodyUI->dateFormats->setValidator(validator);
167  on_dateFormats_editTextChanged(dateFormat);
168 
169  eventsInit();
171 }
172 
174 {
176  delete bodyUI;
177 }
178 
180 {
181  if (bodyUI->styleBrowser->currentIndex() == 0)
183  else
185 
186  this->setStyle(QStyleFactory::create(style));
187  parent->setBodyHeadStyle(style);
188 }
189 
191 {
192  Settings::getInstance().setEmojiFontPointSize(bodyUI->emoticonSize->value());
193 }
194 
196 {
197  QString timeExample = QTime::currentTime().toString(format);
198  bodyUI->timeExample->setText(timeExample);
199 
201  QString locale = Settings::getInstance().getTranslation();
202  Translator::translate(locale);
203 }
204 
206 {
207  QString dateExample = QDate::currentDate().toString(format);
208  bodyUI->dateExample->setText(dateExample);
209 
211  QString locale = Settings::getInstance().getTranslation();
212  Translator::translate(locale);
213 }
214 
216 {
217  Settings::getInstance().setUseEmoticons(bodyUI->useEmoticons->isChecked());
218  bodyUI->smileyPackBrowser->setEnabled(bodyUI->useEmoticons->isChecked());
219 }
220 
222 {
223  Settings::StyleType styleType =
224  static_cast<Settings::StyleType>(bodyUI->textStyleComboBox->currentIndex());
226 }
227 
229 {
230  QString filename = bodyUI->smileyPackBrowser->itemData(index).toString();
232  reloadSmileys();
233 }
234 
239 {
241 
242  // sometimes there are no emoticons available, don't crash in this case
243  if (emoticons.isEmpty()) {
244  qDebug() << "reloadSmilies: No emoticons found";
245  return;
246  }
247 
248  QStringList smileys;
249  for (int i = 0; i < emoticons.size(); ++i)
250  smileys.push_front(emoticons.at(i).first());
251 
252  emoticonsIcons.clear();
253  const QSize size(18, 18);
254  for (int i = 0; i < smileLabels.size(); ++i) {
255  std::shared_ptr<QIcon> icon = SmileyPack::getInstance().getAsIcon(smileys[i]);
256  emoticonsIcons.append(icon);
257  smileLabels[i]->setPixmap(icon->pixmap(size));
258  smileLabels[i]->setToolTip(smileys[i]);
259  }
260 
261  // set maximum size of emoji
262  QDesktopWidget desktop;
263  // 8 is the count of row and column in emoji's in widget
264  const int sideSize = 8;
265  int maxSide = qMin(desktop.geometry().height() / sideSize, desktop.geometry().width() / sideSize);
266  QSize maxSize(maxSide, maxSide);
267 
268  QSize actualSize = emoticonsIcons.first()->actualSize(maxSize);
269  bodyUI->emoticonSize->setMaximum(actualSize.width());
270 }
271 
273 {
274  const bool notify = bodyUI->notify->isChecked();
276  bodyUI->groupOnlyNotfiyWhenMentioned->setEnabled(notify);
277  bodyUI->notifySound->setEnabled(notify);
278  bodyUI->busySound->setEnabled(notify && bodyUI->notifySound->isChecked());
279  bodyUI->desktopNotify->setEnabled(notify);
280 }
281 
283 {
284  const bool notify = bodyUI->notifySound->isChecked();
286  bodyUI->busySound->setEnabled(notify);
287 }
288 
290 {
291  const bool notify = bodyUI->desktopNotify->isChecked();
293 }
294 
296 {
297  Settings::getInstance().setBusySound(bodyUI->busySound->isChecked());
298 }
299 
301 {
302  Settings::getInstance().setShowWindow(bodyUI->showWindow->isChecked());
303 }
304 
306 {
307  // Note: UI is boolean inversed from settings to maintain setting file backwards compatibility
308  Settings::getInstance().setGroupAlwaysNotify(!bodyUI->groupOnlyNotfiyWhenMentioned->isChecked());
309 }
310 
312 {
313  Settings::getInstance().setCompactLayout(bodyUI->cbCompactLayout->isChecked());
314 }
315 
317 {
318  bool checked = bodyUI->cbSeparateWindow->isChecked();
319  bodyUI->cbDontGroupWindows->setEnabled(checked);
321 }
322 
324 {
325  Settings::getInstance().setDontGroupWindows(bodyUI->cbDontGroupWindows->isChecked());
326 }
327 
329 {
330  Settings::getInstance().setGroupchatPosition(bodyUI->cbGroupchatPosition->isChecked());
331 }
332 
334 {
335  Settings::getInstance().setShowIdenticons(bodyUI->cbShowIdenticons->isChecked());
336 }
337 
339 {
340  int index = bodyUI->themeColorCBox->currentIndex();
342  Style::setThemeColor(index);
344 }
345 
350 {
351  // Block signals during translation to prevent settings change
352  RecursiveSignalBlocker signalBlocker{this};
353 
354  bodyUI->retranslateUi(this);
355 
356  // Restore text style index once translation is complete
357  bodyUI->textStyleComboBox->setCurrentIndex(
358  static_cast<int>(Settings::getInstance().getStylePreference()));
359 
360  QStringList colorThemes(Style::getThemeColorNames());
361  for (int i = 0; i < colorThemes.size(); ++i) {
362  bodyUI->themeColorCBox->setItemText(i, colorThemes[i]);
363  }
364 
365  bodyUI->styleBrowser->setItemText(0, tr("None"));
366 }
367 
369 {
370  QFont tmpFont = f;
371  const int px = bodyUI->txtChatFontSize->value();
372 
373  if (QFontInfo(tmpFont).pixelSize() != px)
374  tmpFont.setPixelSize(px);
375 
377 }
378 
380 {
382  QFont tmpFont = s.getChatMessageFont();
383  const int fontSize = QFontInfo(tmpFont).pixelSize();
384 
385  if (px != fontSize) {
386  tmpFont.setPixelSize(px);
387  s.setChatMessageFont(tmpFont);
388  }
389 }
390 
392 {
394 }
395 
397 {
399 }
400 
Settings::setTimestampFormat
void setTimestampFormat(const QString &format)
Definition: settings.cpp:1487
UserInterfaceForm::on_busySound_stateChanged
void on_busySound_stateChanged()
Definition: userinterfaceform.cpp:295
SmileyPack::getAsIcon
std::shared_ptr< QIcon > getAsIcon(const QString &key) const
Gets icon accoring to passed emoticon.
Definition: smileypack.cpp:336
profile.h
Settings
Definition: settings.h:51
UserInterfaceForm::on_cbGroupchatPosition_stateChanged
void on_cbGroupchatPosition_stateChanged()
Definition: userinterfaceform.cpp:328
style.h
UserInterfaceForm::on_themeColorCBox_currentIndexChanged
void on_themeColorCBox_currentIndexChanged(int)
Definition: userinterfaceform.cpp:338
Settings::setBusySound
void setBusySound(bool newValue) override
Definition: settings.cpp:1086
Settings::setSeparateWindow
void setSeparateWindow(bool value)
Definition: settings.cpp:1972
Settings::setStylePreference
void setStylePreference(StyleType newValue)
Definition: settings.cpp:1513
recursivesignalblocker.h
SettingsWidget::setBodyHeadStyle
void setBodyHeadStyle(QString style)
Definition: settingswidget.cpp:95
UserInterfaceForm::on_cbCompactLayout_stateChanged
void on_cbCompactLayout_stateChanged()
Definition: userinterfaceform.cpp:311
Settings::getStyle
QString getStyle() const
Definition: settings.cpp:917
settings.h
UserInterfaceForm::on_smileyPackBrowser_currentIndexChanged
void on_smileyPackBrowser_currentIndexChanged(int index)
Definition: userinterfaceform.cpp:228
UserInterfaceForm::on_notifyHide_stateChanged
void on_notifyHide_stateChanged(int)
Definition: userinterfaceform.cpp:396
Settings::setShowIdenticons
void setShowIdenticons(bool value)
Definition: settings.cpp:2011
UserInterfaceForm::smileLabels
QList< QLabel * > smileLabels
Definition: userinterfaceform.h:72
Settings::getChatMessageFont
const QFont & getChatMessageFont() const
Definition: settings.cpp:1420
UserInterfaceForm::on_notifySound_stateChanged
void on_notifySound_stateChanged()
Definition: userinterfaceform.cpp:282
UserInterfaceForm::on_cbDontGroupWindows_stateChanged
void on_cbDontGroupWindows_stateChanged()
Definition: userinterfaceform.cpp:323
Settings::getShowWindow
bool getShowWindow() const override
Definition: settings.cpp:1572
UserInterfaceForm::on_txtChatFont_currentFontChanged
void on_txtChatFont_currentFontChanged(const QFont &f)
Definition: userinterfaceform.cpp:368
Settings::setChatMessageFont
void setChatMessageFont(const QFont &font)
Definition: settings.cpp:1426
Settings::getSmileyPack
QString getSmileyPack() const
Definition: settings.cpp:1455
Settings::getEmojiFontPointSize
int getEmojiFontPointSize() const
Definition: settings.cpp:1468
Translator::translate
static void translate(const QString &localeName)
Loads the translations according to the settings or locale.
Definition: translator.cpp:39
Translator::unregister
static void unregister(void *owner)
Unregisters all handlers of an owner.
Definition: translator.cpp:103
UserInterfaceForm::UserInterfaceForm
UserInterfaceForm(SettingsWidget *myParent)
Constructor of UserInterfaceForm.
Definition: userinterfaceform.cpp:57
Settings::getDontGroupWindows
bool getDontGroupWindows() const
Definition: settings.cpp:1979
Settings::getNotify
bool getNotify() const override
Definition: settings.cpp:1559
QList< QStringList >
Settings::getUseEmoticons
bool getUseEmoticons() const
Definition: settings.cpp:950
Style::setThemeColor
static void setThemeColor(int color)
Definition: style.cpp:333
SmileyPack::getEmoticons
QList< QStringList > getEmoticons() const
Returns all emoticons that was extracted from files, grouped by according icon file.
Definition: smileypack.cpp:325
Settings::getDateFormat
const QString & getDateFormat() const
Definition: settings.cpp:1494
UserInterfaceForm::emoticonsIcons
QList< std::shared_ptr< QIcon > > emoticonsIcons
Definition: userinterfaceform.h:73
Settings::setDontGroupWindows
void setDontGroupWindows(bool value)
Definition: settings.cpp:1985
Style::applyTheme
static void applyTheme()
Reloads some CCS.
Definition: style.cpp:376
UserInterfaceForm::bodyUI
Ui::UserInterfaceSettings * bodyUI
Definition: userinterfaceform.h:75
Style::getThemeColorNames
static QStringList getThemeColorNames()
Definition: style.cpp:105
Settings::setNotifyHide
void setNotifyHide(bool newValue) override
Definition: settings.cpp:1073
Settings::StyleType
StyleType
Definition: settings.h:134
SettingsWidget
Definition: settingswidget.h:42
coreav.h
UserInterfaceForm::on_cbShowIdenticons_stateChanged
void on_cbShowIdenticons_stateChanged()
Definition: userinterfaceform.cpp:333
Settings::getEnableGroupChatsColor
bool getEnableGroupChatsColor() const
Definition: settings.cpp:2173
UserInterfaceForm::on_styleBrowser_currentIndexChanged
void on_styleBrowser_currentIndexChanged(QString style)
Definition: userinterfaceform.cpp:179
Settings::setGroupchatPosition
void setGroupchatPosition(bool value)
Definition: settings.cpp:1998
userinterfaceform.h
smileypack.h
UserInterfaceForm::on_emoticonSize_editingFinished
void on_emoticonSize_editingFinished()
Definition: userinterfaceform.cpp:190
Settings::setEnableGroupChatsColor
void setEnableGroupChatsColor(bool state)
Definition: settings.cpp:2166
SmileyPack::getInstance
static SmileyPack & getInstance()
Returns the singleton instance.
Definition: smileypack.cpp:147
Settings::getGroupchatPosition
bool getGroupchatPosition() const
Definition: settings.cpp:1992
Settings::getBusySound
bool getBusySound() const override
Definition: settings.cpp:1080
Settings::getSeparateWindow
bool getSeparateWindow() const
Definition: settings.cpp:1966
UserInterfaceForm::on_textStyleComboBox_currentTextChanged
void on_textStyleComboBox_currentTextChanged()
Definition: userinterfaceform.cpp:221
widget.h
Settings::getStylePreference
StyleType getStylePreference() const
Definition: settings.cpp:1507
UserInterfaceForm::on_cbSeparateWindow_stateChanged
void on_cbSeparateWindow_stateChanged()
Definition: userinterfaceform.cpp:316
UserInterfaceForm::retranslateUi
void retranslateUi()
Retranslate all elements in the form.
Definition: userinterfaceform.cpp:349
UserInterfaceForm::reloadSmileys
void reloadSmileys()
Reload smileys and size information.
Definition: userinterfaceform.cpp:238
UserInterfaceForm::on_txtChatFontSize_valueChanged
void on_txtChatFontSize_valueChanged(int arg1)
Definition: userinterfaceform.cpp:379
Settings::setEmojiFontPointSize
void setEmojiFontPointSize(int value)
Definition: settings.cpp:1474
UserInterfaceForm::on_desktopNotify_stateChanged
void on_desktopNotify_stateChanged()
Definition: userinterfaceform.cpp:289
Translator::registerHandler
static void registerHandler(const std::function< void()> &, void *owner)
Register a function to be called when the UI needs to be retranslated.
Definition: translator.cpp:93
Settings::setCompactLayout
void setCompactLayout(bool compact)
Definition: settings.cpp:1946
Settings::getInstance
static Settings & getInstance()
Returns the singleton instance.
Definition: settings.cpp:88
SmileyPack::listSmileyPacks
static QList< QPair< QString, QString > > listSmileyPacks()
Does the same as listSmileyPaths, but with default paths.
Definition: smileypack.cpp:156
Settings::setThemeColor
void setThemeColor(int value)
Definition: settings.cpp:2146
Settings::setStyle
void setStyle(const QString &newValue)
Definition: settings.cpp:923
UserInterfaceForm::on_dateFormats_editTextChanged
void on_dateFormats_editTextChanged(const QString &format)
Definition: userinterfaceform.cpp:205
Settings::getNotifySound
bool getNotifySound() const override
Definition: settings.cpp:1054
Settings::getNotifyHide
bool getNotifyHide() const override
Definition: settings.cpp:1067
Settings::getTimestampFormat
const QString & getTimestampFormat() const
Definition: settings.cpp:1481
UserInterfaceForm::on_groupOnlyNotfiyWhenMentioned_stateChanged
void on_groupOnlyNotfiyWhenMentioned_stateChanged()
Definition: userinterfaceform.cpp:305
Settings::getThemeColor
int getThemeColor() const
Definition: settings.cpp:2140
UserInterfaceForm::on_timestamp_editTextChanged
void on_timestamp_editTextChanged(const QString &format)
Definition: userinterfaceform.cpp:195
Settings::getGroupAlwaysNotify
bool getGroupAlwaysNotify() const override
Definition: settings.cpp:1093
settingswidget.h
Settings::getDesktopNotify
bool getDesktopNotify() const override
Definition: settings.cpp:1585
UserInterfaceForm::on_useEmoticons_stateChanged
void on_useEmoticons_stateChanged()
Definition: userinterfaceform.cpp:215
core.h
Settings::getTranslation
QString getTranslation() const
Definition: settings.cpp:1106
Settings::setDateFormat
void setDateFormat(const QString &format)
Definition: settings.cpp:1500
Settings::setDesktopNotify
void setDesktopNotify(bool enabled) override
Definition: settings.cpp:1591
Settings::setNotifySound
void setNotifySound(bool newValue) override
Definition: settings.cpp:1060
Settings::getShowIdenticons
bool getShowIdenticons() const
Definition: settings.cpp:2005
UserInterfaceForm::MAX_FORMAT_LENGTH
const int MAX_FORMAT_LENGTH
Definition: userinterfaceform.h:76
Settings::setShowWindow
void setShowWindow(bool newValue) override
Definition: settings.cpp:1578
Settings::setUseEmoticons
void setUseEmoticons(bool newValue)
Definition: settings.cpp:943
Settings::setSmileyPack
void setSmileyPack(const QString &value)
Definition: settings.cpp:1461
GenericForm::eventsInit
void eventsInit()
Prevent stealing mouse wheel scroll.
Definition: genericsettings.cpp:54
UserInterfaceForm::on_showWindow_stateChanged
void on_showWindow_stateChanged()
Definition: userinterfaceform.cpp:300
translator.h
UserInterfaceForm::on_notify_stateChanged
void on_notify_stateChanged()
Definition: userinterfaceform.cpp:272
Settings::setNotify
void setNotify(bool newValue) override
Definition: settings.cpp:1565
RecursiveSignalBlocker
Recursively blocks all signals from an object and its children.
Definition: recursivesignalblocker.h:27
GenericForm
Definition: genericsettings.h:24
UserInterfaceForm::~UserInterfaceForm
~UserInterfaceForm()
Definition: userinterfaceform.cpp:173
UserInterfaceForm::parent
SettingsWidget * parent
Definition: userinterfaceform.h:74
UserInterfaceForm::on_useNameColors_stateChanged
void on_useNameColors_stateChanged(int value)
Definition: userinterfaceform.cpp:391
Settings::setGroupAlwaysNotify
void setGroupAlwaysNotify(bool newValue) override
Definition: settings.cpp:1099