swift
settingsguicomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "settingsguicomponent.h"
5 
6 #include <QColorDialog>
7 #include <QFont>
8 #include <QFontComboBox>
9 #include <QMessageBox>
10 #include <QStyleFactory>
11 
12 #include "ui_settingsguicomponent.h"
13 
15 #include "gui/guiapplication.h"
16 #include "misc/logmessage.h"
17 
18 using namespace swift::misc;
19 using namespace swift::gui::settings;
20 using namespace swift::core::context;
21 
22 namespace swift::gui::components
23 {
24  CSettingsGuiComponent::CSettingsGuiComponent(QWidget *parent) : QFrame(parent), ui(new Ui::CSettingsGuiComponent)
25  {
26  ui->setupUi(this);
27 
28  ui->cb_SettingsGuiWidgetStyle->clear();
29  ui->cb_SettingsGuiWidgetStyle->insertItems(0, QStyleFactory::keys());
30 
31  // Widget style
32  connect(ui->hs_SettingsGuiOpacity, &QSlider::valueChanged, this, &CSettingsGuiComponent::changedWindowsOpacity);
33  connect(ui->cb_SettingsGuiWidgetStyle, qOverload<int>(&QComboBox::currentIndexChanged), this,
34  &CSettingsGuiComponent::widgetStyleChanged, Qt::QueuedConnection);
35 
36  ui->comp_SettingsFonts->setStyleSheetDefaultColor();
37 
38  // selection
39  connect(ui->rb_PreferExtendedSelection, &QRadioButton::released, this,
40  &CSettingsGuiComponent::selectionChanged);
41  connect(ui->rb_PreferMultiSelection, &QRadioButton::released, this, &CSettingsGuiComponent::selectionChanged);
42 
43  this->guiSettingsChanged();
44  }
45 
47 
49  {
50  ui->hs_SettingsGuiOpacity->setVisible(!hide);
51  ui->lbl_SettingsGuiOpacity->setVisible(!hide);
52  }
53 
55  {
56  ui->hs_SettingsGuiOpacity->setValue(static_cast<int>(value));
57  }
58 
59  void CSettingsGuiComponent::selectionChanged()
60  {
61  QAbstractItemView::SelectionMode sm = QAbstractItemView::NoSelection;
62  if (ui->rb_PreferExtendedSelection->isChecked()) { sm = QAbstractItemView::ExtendedSelection; }
63  else if (ui->rb_PreferMultiSelection->isChecked()) { sm = QAbstractItemView::MultiSelection; }
64  if (sm == m_guiSettings.get().getPreferredSelection()) { return; }
65  const CStatusMessage m =
66  m_guiSettings.setAndSaveProperty(CGeneralGuiSettings::IndexPreferredSelection, CVariant::fromValue(sm));
67  CLogMessage::preformatted(m);
68  }
69 
70  void CSettingsGuiComponent::guiSettingsChanged()
71  {
72  const CGeneralGuiSettings settings(m_guiSettings.getThreadLocal());
73  const int index = ui->cb_SettingsGuiWidgetStyle->findText(settings.getWidgetStyle());
74  if (index != ui->cb_SettingsGuiWidgetStyle->currentIndex())
75  {
76  ui->cb_SettingsGuiWidgetStyle->setCurrentIndex(index);
77  }
78 
79  switch (settings.getPreferredSelection())
80  {
81  case QAbstractItemView::ExtendedSelection: ui->rb_PreferExtendedSelection->setChecked(true); break;
82  case QAbstractItemView::MultiSelection: ui->rb_PreferMultiSelection->setChecked(true); break;
83  default: break;
84  }
85  }
86 
87  void CSettingsGuiComponent::widgetStyleChanged(int index)
88  {
89  const QString widgetStyle = ui->cb_SettingsGuiWidgetStyle->itemText(index);
90  const CGeneralGuiSettings settings = m_guiSettings.getThreadLocal();
91  if (!settings.isDifferentValidWidgetStyle(widgetStyle)) { return; }
92 
100  const int ret = QMessageBox::information(this, tr("Change style?"),
101  tr("Changing style requires a restart.\nChanges will be visible a the "
102  "next start.\nDo you want to save your changes?"),
103  QMessageBox::Ok | QMessageBox::Cancel);
104 
105  if (ret != QMessageBox::Ok)
106  {
107  ui->cb_SettingsGuiWidgetStyle->setCurrentText(settings.getWidgetStyle());
108  return;
109  }
110 
112  {
113  // Style changes freeze the GUI, must not be done in flight mode
114  CLogMessage(this).validationError(u"Cannot change style while connected to network");
115  ui->cb_SettingsGuiWidgetStyle->setCurrentText(settings.getWidgetStyle());
116  return;
117  }
118  const CStatusMessage m = m_guiSettings.setAndSaveProperty(CGeneralGuiSettings::IndexWidgetStyle, widgetStyle);
119  CLogMessage::preformatted(m);
120  }
121 } // namespace swift::gui::components
const context::IContextNetwork * getIContextNetwork() const
Direct access to contexts if a CCoreFacade has been initialized.
virtual bool isConnected() const =0
Network connected?
void changedWindowsOpacity(int opacity)
Change the windows opacity 0..100.
void setGuiOpacity(double value)
GUI Opacity 0-100%.
void hideOpacity(bool hide)
Hide opacity elements.
bool isDifferentValidWidgetStyle(const QString &style) const
Has changed widget style.
Definition: guisettings.cpp:23
const QString & getWidgetStyle() const
Widget style.
Definition: guisettings.h:35
Class for emitting a log message.
Definition: logmessage.h:27
Derived & validationError(const char16_t(&format)[N])
Set the severity to error, providing a format string, and adding the validation category.
Streamable status message, e.g.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.