Skip to content

Commit

Permalink
FIX: Properly initialise style plugins (#143)
Browse files Browse the repository at this point in the history
* FIX: Create application style after QApplication instantiation

- Otherwise the "styles" plugin folder is not evaluated

* Filter the style selection list

- Do not show "Windows Vista" style on Windows 11 and vice versa, not
"Windows 11" style on Windows 10
  • Loading branch information
till213 authored May 9, 2024
1 parent 66d628a commit 345b1d9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Ornithopter
* A320 V2 (inibuilds)
* Note that any *new* aircraft that is recorded will be automatically added to the aircraft type list (existing behaviour)
- Platform styles that are not really selectable - such as "Windows Vista" style on Windows 11 and "Windows 11" style on Windows 10 - are not shown anymore in the style selection dropdown list
* The selection list is now sorted

### Bug Fixes
- The aircraft type is properly set again when importing a flight (regression from v0.17)
Expand All @@ -19,6 +21,7 @@
- Alternating table rows are not visually distinct [[QTBUG-124564](https://bugreports.qt.io/browse/QTBUG-124564)]
* However the "Windows 11" style can still be set, under *File | Settings... | User Interface*
- Properly validate persisted enumeration values when restoring plugin settings
- The "native styles" ("Windows Vista" on Windows 10 respectively "Windows 11" on Windows 11) are now properly loaded when explicitly set in the application settings

## 0.17.0

Expand Down
34 changes: 33 additions & 1 deletion src/Kernel/include/Kernel/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
#define SYSTEM_H

#include <QString>
#include <QSysInfo>

#include "KernelLib.h"

class KERNEL_API System final
{
public:

/*!
* Returns the name of the currently logged in user.
*
Expand All @@ -42,6 +42,38 @@ class KERNEL_API System final
* \return the name of the logged in user; may be an empty string
*/
static QString getUsername() noexcept;

/*!
* Returns whether the application runs on Windows (e.g. 10 or 11).
*
* \return \c true if the operating system is Windows; \c false else
* \sa isWindows10
* \sa isWindows11
*/
static inline bool isWindows()
{
return QSysInfo::productType() == "windows";
}

/*!
* Returns whether the application runs on Windows 10.
*
* \return \c true if the operating system is Windows 10; \c false else
*/
static inline bool isWindows10()
{
return isWindows() && QSysInfo::productVersion() == "10";
}

/*!
* Returns whether the application runs on Windows 11.
*
* \return \c true if the operating system is Windows 11; \c false else
*/
static inline bool isWindows11()
{
return isWindows() && QSysInfo::productVersion() == "11";
}
};

#endif // SYSTEM_H
24 changes: 7 additions & 17 deletions src/SkyDolly/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <QCoreApplication>
#include <QSysInfo>
#include <QApplication>
#include <QStyle>
#include <QStyleFactory>
#include <QStringList>
#include <QString>
#include <QStringLiteral>
Expand All @@ -43,6 +41,7 @@
#include <Kernel/Version.h>
#include <Kernel/StackTrace.h>
#include <Kernel/Settings.h>
#include <Kernel/System.h>
#include <Kernel/RecentFile.h>
#include <Model/Logbook.h>
#include <PluginManager/SkyConnectManager.h>
Expand Down Expand Up @@ -71,15 +70,7 @@ static void destroySingletons() noexcept
[[deprecated("Do not use once the new Windows 11 style is ready for prime time.")]]
static void applyWindows11DefaultStyleWorkaround() noexcept
{
if (QSysInfo::productType() == QStringLiteral("windows") && QSysInfo::productVersion() == QStringLiteral("11")) {
#ifdef DEBUG
qDebug() << "main: Windows 11 detected, applying Fusion as default style (workaround).";
#endif
const auto style = QStyleFactory::create(QStringLiteral("Fusion"));
if (style != nullptr) {
QApplication::setStyle(style);
}
}
QApplication::setStyle(QString::fromLatin1("Fusion"));
}

int main(int argc, char **argv) noexcept
Expand All @@ -90,17 +81,16 @@ int main(int argc, char **argv) noexcept
QCoreApplication::setApplicationName(Version::getApplicationName());
QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus);

QApplication application(argc, argv);

// Set the user interface style (if not default)
// Implementation note: must be set AFTER QApplication instantiation
const QString styleKey = Settings::getInstance().getStyleKey();
if (styleKey != Settings::DefaultStyleKey) {
const auto style = QStyleFactory::create(styleKey);
if (style != nullptr) {
QApplication::setStyle(style);
}
} else {
QApplication::setStyle(styleKey);
} else if (System::isWindows11()) {
applyWindows11DefaultStyleWorkaround();
}
QApplication application(argc, argv);

// Signals must be registered after the QApplication instantiation, due
// to the QSocketNotifier
Expand Down
1 change: 1 addition & 0 deletions src/UserInterface/include/UserInterface/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,6 @@ private slots:

// Settings
void onReplayLoopChanged() noexcept;
void onStyleKeyChanged() noexcept;
};
#endif // MAINWINDOW_H
31 changes: 21 additions & 10 deletions src/UserInterface/src/Dialog/SettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <Kernel/SampleRate.h>
#include <Kernel/Enum.h>
#include <Kernel/Settings.h>
#include <Kernel/System.h>
#include <Model/SimVar.h>
#include <PluginManager/SkyConnectManager.h>
#include "SettingsDialog.h"
Expand All @@ -57,6 +58,12 @@ namespace

constexpr int ReplayTab = 0;
constexpr int FlightSimulatorTab = 2;

constexpr const char *WindowsStyleKey {"windows"};
constexpr const char *FusionStyleKey {"fusion"};
constexpr const char *WindowsVistaStyleKey {"windowsvista"};
constexpr const char *Windows11StyleKey {"windows11"};
constexpr const char *MacOSStyleKey {"macos"};
}

struct SettingsDialogPrivate
Expand All @@ -66,19 +73,19 @@ struct SettingsDialogPrivate
updateTimer.setTimerType(Qt::TimerType::PreciseTimer);
if (knownStyleNames.empty()) {
knownStyleNames = {
{ Settings::DefaultStyleKey, qApp->translate("SettingsDialogPrivate", "Default") },
{ QStringLiteral("macos"), QStringLiteral("macOS") },
{ QStringLiteral("windows"), QStringLiteral("Windows") },
{ QStringLiteral("windowsvista"), QStringLiteral("Windows Vista") },
{ QStringLiteral("fusion"), QStringLiteral("Fusion") },
{ QStringLiteral("windows11"), QStringLiteral("Windows 11") }
{ Settings::DefaultStyleKey, QApplication::translate("SettingsDialogPrivate", "Default") },
{ ::WindowsStyleKey, QStringLiteral("Windows") },
{ ::FusionStyleKey, QStringLiteral("Fusion") },
{ ::WindowsVistaStyleKey, QStringLiteral("Windows Vista") },
{ ::Windows11StyleKey, QStringLiteral("Windows 11") },
{ ::MacOSStyleKey, QStringLiteral("macOS") }
};
}
}
QTimer updateTimer;
OptionWidgetIntf *skyConnectOptionWidget {nullptr};

// Key: style key, value: style name
// Key: style key (all lower case), value: style name
static inline std::unordered_map<QString, QString> knownStyleNames;
};

Expand Down Expand Up @@ -155,10 +162,14 @@ void SettingsDialog::initUi() noexcept

// User interface
ui->styleComboBox->addItem(Settings::DefaultStyleKey, Settings::DefaultStyleKey);
const auto styleKeys = QStyleFactory::keys();
auto styleKeys = QStyleFactory::keys();
styleKeys.sort();
for (const auto &key : styleKeys) {
const auto &styleName = d->knownStyleNames.contains(key) ? d->knownStyleNames[key] : key;
ui->styleComboBox->addItem(styleName, key);
const auto lowerKey = key.toLower();
const auto &styleName = d->knownStyleNames.contains(lowerKey) ? d->knownStyleNames[lowerKey] : key;
if (!(System::isWindows10() && lowerKey == ::Windows11StyleKey) && !(System::isWindows11() && lowerKey == ::Windows11StyleKey)) {
ui->styleComboBox->addItem(styleName, lowerKey);
}
}

ui->settingsTabWidget->setCurrentIndex(::ReplayTab);
Expand Down
14 changes: 13 additions & 1 deletion src/UserInterface/src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <vector>
#include <cstdint>
#include <cmath>
#include <chrono>

#include <QApplication>
#include <QByteArray>
Expand Down Expand Up @@ -66,6 +67,7 @@
#include <QMimeData>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QProcess>

#include <Kernel/Unit.h>
#include <Kernel/Const.h>
Expand Down Expand Up @@ -106,7 +108,6 @@
#include "Dialog/LogbookBackupDialog.h"
#include "MainWindow.h"
#include "./ui_MainWindow.h"
#include <chrono>

using namespace std::chrono_literals;

Expand Down Expand Up @@ -340,6 +341,8 @@ void MainWindow::frenchConnection() noexcept
this, &MainWindow::onDefaultMinimalUiEssentialButtonVisibilityChanged);
connect(&settings, &Settings::replayLoopChanged,
this, &MainWindow::onReplayLoopChanged);
connect(&settings, &Settings::styleKeyChanged,
this, &MainWindow::onStyleKeyChanged);

// Logbook connection
connect(&PersistenceManager::getInstance(), &PersistenceManager::connectionChanged,
Expand Down Expand Up @@ -1916,3 +1919,12 @@ void MainWindow::onReplayLoopChanged() noexcept
{
updateControlUi();
}

void MainWindow::onStyleKeyChanged() noexcept
{
// TODO IMPLEMENT ME - This does not work reliably just yet (the executable name seems to be passed as 1st argument,
// causing Sky Dolly to make an attempt to open itself - also, the settings are sometimes not properly persisted (?)
// Settings::getInstance().store();
// qApp->quit();
// QProcess::startDetached(QCoreApplication::applicationFilePath(), {});
}

0 comments on commit 345b1d9

Please sign in to comment.