From 1cf3756c84ed2876ec2c2682b247d910e219c92a Mon Sep 17 00:00:00 2001 From: Dale Glass Date: Sat, 7 Jan 2023 19:35:52 +0100 Subject: [PATCH] Move screen naming to UI library, and improve naming --- .../scripting/RenderScriptingInterface.cpp | 4 +- .../src/scripting/RenderScriptingInterface.h | 3 -- .../Basic2DWindowOpenGLDisplayPlugin.cpp | 8 +-- libraries/ui/src/ScreenName.cpp | 49 +++++++++++++++++++ libraries/ui/src/ScreenName.h | 32 ++++++++++++ 5 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 libraries/ui/src/ScreenName.cpp create mode 100644 libraries/ui/src/ScreenName.h diff --git a/interface/src/scripting/RenderScriptingInterface.cpp b/interface/src/scripting/RenderScriptingInterface.cpp index 76375b0eda..b4b1b9864e 100644 --- a/interface/src/scripting/RenderScriptingInterface.cpp +++ b/interface/src/scripting/RenderScriptingInterface.cpp @@ -9,6 +9,7 @@ #include "LightingModel.h" #include +#include "ScreenName.h" RenderScriptingInterface* RenderScriptingInterface::getInstance() { @@ -24,6 +25,7 @@ RenderScriptingInterface::RenderScriptingInterface() { }); } + void RenderScriptingInterface::loadSettings() { _renderSettingLock.withReadLock([&] { _renderMethod = (_renderMethodSetting.get()); @@ -208,7 +210,7 @@ QStringList RenderScriptingInterface::getScreens() const { QStringList screens; for(QScreen *screen : qApp->screens()) { - screens << getNameForScreen(screen); + screens << ScreenName::getNameForScreen(screen); } return screens; diff --git a/interface/src/scripting/RenderScriptingInterface.h b/interface/src/scripting/RenderScriptingInterface.h index 0596213f8d..27088daf97 100644 --- a/interface/src/scripting/RenderScriptingInterface.h +++ b/interface/src/scripting/RenderScriptingInterface.h @@ -74,9 +74,6 @@ public: // Need to be called on start up to re-initialize the runtime to the saved setting states void loadSettings(); - static QString getNameForScreen(QScreen *screen) { - return screen->model() + " (" + screen->name() + ", " + screen->serialNumber() + ")"; - } public slots: /*@jsdoc diff --git a/libraries/display-plugins/src/display-plugins/Basic2DWindowOpenGLDisplayPlugin.cpp b/libraries/display-plugins/src/display-plugins/Basic2DWindowOpenGLDisplayPlugin.cpp index 49e27966b4..024d9a0ab6 100644 --- a/libraries/display-plugins/src/display-plugins/Basic2DWindowOpenGLDisplayPlugin.cpp +++ b/libraries/display-plugins/src/display-plugins/Basic2DWindowOpenGLDisplayPlugin.cpp @@ -19,6 +19,7 @@ #include #include #include "SettingHandle.h" +#include "ScreenName.h" const QString Basic2DWindowOpenGLDisplayPlugin::NAME("Desktop"); @@ -167,17 +168,12 @@ bool Basic2DWindowOpenGLDisplayPlugin::isThrottled() const { return _isThrottled; } - -static QString getNameForScreen(QScreen *screen) { - return screen->model() + " (" + screen->name() + ", " + screen->serialNumber() + ")"; -} - QScreen* Basic2DWindowOpenGLDisplayPlugin::getFullscreenTarget() { Setting::Handle _fullScreenScreenSetting { "fullScreenScreen", "" }; QString selectedModel = _fullScreenScreenSetting.get(); for(QScreen *screen : qApp->screens()) { - if (getNameForScreen(screen) == selectedModel) { + if (ScreenName::getNameForScreen(screen) == selectedModel) { return screen; } } diff --git a/libraries/ui/src/ScreenName.cpp b/libraries/ui/src/ScreenName.cpp new file mode 100644 index 0000000000..84fea3d2bb --- /dev/null +++ b/libraries/ui/src/ScreenName.cpp @@ -0,0 +1,49 @@ +// +// Created by Dale Glass on 7/01/2023 +// Copyright 2022 Overte e.V. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "ScreenName.h" + +QString ScreenName::getNameForScreen(QScreen *screen) { + // The data provided by QScreen isn't as convenient as it could be. + // So far testing shows: + // + // Windows: + // model() returns an empty string + // name() returns something like \\.\DISPLAY1 + // + // Linux: + // model() returns a name, like "LG Ultra HD/525000" + // name() returns the output's name, like "HDMI1" + + // So we try to assemble something unique and readable from all the possibilities. + + QString ret; + bool addParens = false; + + ret.append(screen->manufacturer()); + + if (!ret.isEmpty()) { + ret.append(" - "); + } + ret.append(screen->model()); + + addParens = !ret.isEmpty(); + + if(addParens) { + ret.append(" ("); + } + + ret.append(screen->name().replace(QString("\\\\.\\"), QString(""))); + + + if(addParens) { + ret.append(")"); + } + + return ret; +} diff --git a/libraries/ui/src/ScreenName.h b/libraries/ui/src/ScreenName.h new file mode 100644 index 0000000000..e8c08712cb --- /dev/null +++ b/libraries/ui/src/ScreenName.h @@ -0,0 +1,32 @@ +// +// Created by Dale Glass on 7/01/2023 +// Copyright 2022 Overte e.V. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include +#pragma once + +/** + * @brief Screen naming + * + * This class exists because display-plugins and interface need to share the same, + * fairly involved rule for converting QScreen data to user-facing text. + */ +class ScreenName { + public: + /** + * @brief Get a descriptive name for a screen + * + * This is used in the graphics settings, to name monitors. This function tries to generate + * human friendly and unique, even if two identical monitors are present. + * + * @param screen Screen to provide a name for + * @return QString Descriptive name for the screen + */ + static QString getNameForScreen(QScreen *screen); + +};