2026-01-12 05:18:12 -05:00
|
|
|
// SPDX-FileCopyrightText: 2002-2026 PCSX2 Dev Team
|
2024-07-30 13:42:36 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0+
|
2021-12-13 22:12:54 +10:00
|
|
|
|
|
|
|
|
#pragma once
|
2022-10-15 19:03:52 +10:00
|
|
|
|
|
|
|
|
#include "common/WindowInfo.h"
|
|
|
|
|
|
2021-12-13 22:12:54 +10:00
|
|
|
#include <QtCore/QByteArray>
|
|
|
|
|
#include <QtCore/QMetaType>
|
|
|
|
|
#include <QtCore/QString>
|
2023-10-09 18:27:24 -04:00
|
|
|
#include <QtCore/QAbstractItemModel>
|
2025-10-30 20:54:07 +00:00
|
|
|
#include <QtGui/QGuiApplication>
|
|
|
|
|
#include <QtGui/QWindow>
|
|
|
|
|
#if !defined(_WIN32) and !defined(__APPLE__)
|
|
|
|
|
#include <qpa/qplatformnativeinterface.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <QtGui/QScreen>
|
2021-12-13 22:12:54 +10:00
|
|
|
#include <functional>
|
|
|
|
|
#include <initializer_list>
|
2026-06-16 23:19:32 +07:00
|
|
|
#include <string>
|
2022-01-31 15:24:19 +10:00
|
|
|
#include <string_view>
|
2025-10-30 20:54:07 +00:00
|
|
|
#include <type_traits>
|
2021-12-13 22:12:54 +10:00
|
|
|
#include <optional>
|
2026-06-16 23:19:32 +07:00
|
|
|
#include <vector>
|
2021-12-13 22:12:54 +10:00
|
|
|
|
2025-10-30 20:54:07 +00:00
|
|
|
#include "common/Console.h"
|
|
|
|
|
|
2021-12-13 22:12:54 +10:00
|
|
|
class ByteStream;
|
|
|
|
|
|
|
|
|
|
class QAction;
|
|
|
|
|
class QComboBox;
|
2023-09-09 02:15:41 -05:00
|
|
|
class QFileInfo;
|
2021-12-13 22:12:54 +10:00
|
|
|
class QFrame;
|
2025-05-31 16:57:42 +01:00
|
|
|
class QIcon;
|
2024-04-24 01:17:52 +10:00
|
|
|
class QLabel;
|
2021-12-13 22:12:54 +10:00
|
|
|
class QKeyEvent;
|
2024-04-24 01:17:52 +10:00
|
|
|
class QSlider;
|
2021-12-13 22:12:54 +10:00
|
|
|
class QTableView;
|
|
|
|
|
class QTreeView;
|
|
|
|
|
class QVariant;
|
|
|
|
|
class QWidget;
|
|
|
|
|
class QUrl;
|
|
|
|
|
|
|
|
|
|
namespace QtUtils
|
|
|
|
|
{
|
2022-07-02 21:51:23 +10:00
|
|
|
/// Wheel delta is 120 as in winapi.
|
|
|
|
|
static constexpr float MOUSE_WHEEL_DELTA = 120.0f;
|
|
|
|
|
|
2021-12-13 22:12:54 +10:00
|
|
|
/// Marks an action as the "default" - i.e. makes the text bold.
|
|
|
|
|
void MarkActionAsDefault(QAction* action);
|
|
|
|
|
|
|
|
|
|
/// Creates a horizontal line widget.
|
|
|
|
|
QFrame* CreateHorizontalLine(QWidget* parent);
|
|
|
|
|
|
|
|
|
|
/// Returns the greatest parent of a widget, i.e. its dialog/window.
|
|
|
|
|
QWidget* GetRootWidget(QWidget* widget, bool stop_at_window_or_dialog = true);
|
|
|
|
|
|
|
|
|
|
/// Resizes columns of the table view to at the specified widths. A negative width will stretch the column to use the
|
|
|
|
|
/// remaining space.
|
|
|
|
|
void ResizeColumnsForTableView(QTableView* view, const std::initializer_list<int>& widths);
|
|
|
|
|
void ResizeColumnsForTreeView(QTreeView* view, const std::initializer_list<int>& widths);
|
|
|
|
|
|
2025-10-29 17:03:08 +07:00
|
|
|
enum struct ScalingMode
|
|
|
|
|
{
|
|
|
|
|
Fit,
|
|
|
|
|
Fill,
|
|
|
|
|
Stretch,
|
|
|
|
|
Center,
|
|
|
|
|
Tile,
|
|
|
|
|
|
|
|
|
|
MaxCount
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Resize and scale a given Pixmap (and optionally adjust opacity)
|
|
|
|
|
void resizeAndScalePixmap(QPixmap* pm, const int expected_width, const int expected_height, const qreal dpr, const ScalingMode scaling_mode, const float opacity);
|
|
|
|
|
|
2022-06-21 17:47:32 +10:00
|
|
|
/// Returns a key id for a key event, including any modifiers that we need (e.g. Keypad).
|
|
|
|
|
/// NOTE: Defined in QtKeyCodes.cpp, not QtUtils.cpp.
|
|
|
|
|
u32 KeyEventToCode(const QKeyEvent* ev);
|
2021-12-13 22:12:54 +10:00
|
|
|
|
2023-09-09 02:15:41 -05:00
|
|
|
/// Shows a file, or the containing folder if unsupported, with the system file explorer
|
|
|
|
|
void ShowInFileExplorer(QWidget* parent, const QFileInfo& file);
|
|
|
|
|
|
|
|
|
|
/// Get the context menu name for the action performed by ShowInFileExplorer
|
|
|
|
|
QString GetShowInFileExplorerMessage();
|
|
|
|
|
|
2021-12-13 22:12:54 +10:00
|
|
|
/// Opens a URL with the default handler.
|
|
|
|
|
void OpenURL(QWidget* parent, const QUrl& qurl);
|
|
|
|
|
|
|
|
|
|
/// Opens a URL string with the default handler.
|
|
|
|
|
void OpenURL(QWidget* parent, const char* url);
|
|
|
|
|
|
|
|
|
|
/// Opens a URL string with the default handler.
|
|
|
|
|
void OpenURL(QWidget* parent, const QString& url);
|
|
|
|
|
|
2022-01-31 15:24:19 +10:00
|
|
|
/// Converts a std::string_view to a QString safely.
|
2024-05-15 09:42:40 +10:00
|
|
|
QString StringViewToQString(const std::string_view str);
|
2022-06-08 22:15:10 +10:00
|
|
|
|
|
|
|
|
/// Sets a widget to italics if the setting value is inherited.
|
|
|
|
|
void SetWidgetFontForInheritedSetting(QWidget* widget, bool inherited);
|
2022-06-28 22:16:45 +10:00
|
|
|
|
2024-04-24 01:17:52 +10:00
|
|
|
/// Binds a label to a slider's value.
|
|
|
|
|
void BindLabelToSlider(QSlider* slider, QLabel* label, float range = 1.0f);
|
|
|
|
|
|
2022-06-28 22:16:45 +10:00
|
|
|
/// Changes whether a window is resizable.
|
|
|
|
|
void SetWindowResizeable(QWidget* widget, bool resizeable);
|
2025-11-21 22:35:19 +00:00
|
|
|
void SetWindowResizeable(QWindow* window, bool resizeable);
|
2022-06-28 22:16:45 +10:00
|
|
|
|
|
|
|
|
/// Adjusts the fixed size for a window if it's not resizeable.
|
|
|
|
|
void ResizePotentiallyFixedSizeWindow(QWidget* widget, int width, int height);
|
2025-11-21 22:35:19 +00:00
|
|
|
void ResizePotentiallyFixedSizeWindow(QWindow* window, int width, int height);
|
2022-10-15 19:03:52 +10:00
|
|
|
|
2025-10-30 20:54:07 +00:00
|
|
|
/// Returns the common window info structure for a Qt Window/Widget.
|
|
|
|
|
template <class T>
|
|
|
|
|
requires std::is_base_of_v<QWidget, T> || std::is_base_of_v<QWindow, T>
|
|
|
|
|
std::optional<WindowInfo> GetWindowInfoForWindow(T* window)
|
|
|
|
|
{
|
|
|
|
|
WindowInfo wi;
|
|
|
|
|
|
|
|
|
|
// Windows and Apple are easy here since there's no display connection.
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
wi.type = WindowInfo::Type::Win32;
|
|
|
|
|
wi.window_handle = reinterpret_cast<void*>(window->winId());
|
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
|
wi.type = WindowInfo::Type::MacOS;
|
|
|
|
|
wi.window_handle = reinterpret_cast<void*>(window->winId());
|
|
|
|
|
#else
|
|
|
|
|
QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface();
|
|
|
|
|
const QString platform_name = QGuiApplication::platformName();
|
|
|
|
|
|
|
|
|
|
QWindow* windowHandle;
|
|
|
|
|
if constexpr (std::is_base_of_v<QWidget, T>)
|
|
|
|
|
windowHandle = window->windowHandle();
|
|
|
|
|
else
|
|
|
|
|
windowHandle = window;
|
|
|
|
|
|
|
|
|
|
if (platform_name == QStringLiteral("xcb"))
|
|
|
|
|
{
|
|
|
|
|
// Can't get a handle for an unmapped window in X, it doesn't like it.
|
|
|
|
|
if (!window->isVisible())
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLn("Returning null window info for widget because it is not visible.");
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wi.type = WindowInfo::Type::X11;
|
|
|
|
|
wi.display_connection = pni->nativeResourceForWindow("display", windowHandle);
|
|
|
|
|
wi.window_handle = reinterpret_cast<void*>(window->winId());
|
|
|
|
|
}
|
|
|
|
|
else if (platform_name == QStringLiteral("wayland"))
|
|
|
|
|
{
|
|
|
|
|
wi.type = WindowInfo::Type::Wayland;
|
|
|
|
|
wi.display_connection = pni->nativeResourceForWindow("display", windowHandle);
|
|
|
|
|
wi.window_handle = pni->nativeResourceForWindow("surface", windowHandle);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLn("Unknown PNI platform '%s'.", platform_name.toUtf8().constData());
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
qreal dpr;
|
|
|
|
|
if constexpr (std::is_base_of_v<QWidget, T>)
|
|
|
|
|
dpr = window->devicePixelRatioF();
|
|
|
|
|
else
|
|
|
|
|
dpr = window->devicePixelRatio();
|
|
|
|
|
|
2025-11-02 21:56:01 +00:00
|
|
|
wi.surface_width = static_cast<u32>(std::max(static_cast<int>(std::round(static_cast<qreal>(window->width()) * dpr)), 1));
|
|
|
|
|
wi.surface_height = static_cast<u32>(std::max(static_cast<int>(std::round(static_cast<qreal>(window->height()) * dpr)), 1));
|
2025-10-30 20:54:07 +00:00
|
|
|
wi.surface_scale = static_cast<float>(dpr);
|
|
|
|
|
|
|
|
|
|
// Query refresh rate, we need it for sync.
|
|
|
|
|
std::optional<float> surface_refresh_rate = WindowInfo::QueryRefreshRateForWindow(wi);
|
|
|
|
|
if (!surface_refresh_rate.has_value())
|
|
|
|
|
{
|
|
|
|
|
// Fallback to using the screen, getting the rate for Wayland is an utter mess otherwise.
|
|
|
|
|
const QScreen* widget_screen = window->screen();
|
|
|
|
|
if (!widget_screen)
|
|
|
|
|
widget_screen = QGuiApplication::primaryScreen();
|
|
|
|
|
surface_refresh_rate = widget_screen ? static_cast<float>(widget_screen->refreshRate()) : 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wi.surface_refresh_rate = surface_refresh_rate.value();
|
2026-02-15 04:26:15 +10:00
|
|
|
INFO_LOG("Surface refresh rate: {} Hz", wi.surface_refresh_rate);
|
2025-10-30 20:54:07 +00:00
|
|
|
|
|
|
|
|
return wi;
|
|
|
|
|
}
|
2022-12-24 01:51:44 -05:00
|
|
|
|
|
|
|
|
/// Converts a value to a QString of said value with a proper fixed width
|
|
|
|
|
template <typename T>
|
|
|
|
|
QString FilledQStringFromValue(T val, u32 base)
|
|
|
|
|
{
|
|
|
|
|
return QString("%1").arg(QString::number(val, base), sizeof(val) * 2, '0').toUpper();
|
|
|
|
|
};
|
2023-10-09 18:27:24 -04:00
|
|
|
|
|
|
|
|
/// Converts an abstract item model to a CSV string.
|
2023-12-29 20:01:30 -08:00
|
|
|
QString AbstractItemModelToCSV(QAbstractItemModel* model, int role = Qt::DisplayRole, bool useQuotes = false);
|
2025-03-03 22:22:01 +00:00
|
|
|
|
2025-10-12 21:23:54 +01:00
|
|
|
/// Checks if we can use transparency effects e.g. for dock drop indicators.
|
2025-03-03 22:22:01 +00:00
|
|
|
bool IsCompositorManagerRunning();
|
2025-05-31 16:57:42 +01:00
|
|
|
|
|
|
|
|
/// Sets the scalable icon to a given label (svg icons, or icons with multiple size pixmaps)
|
|
|
|
|
/// The icon will then be reloaded on DPR changes using an event filter
|
|
|
|
|
void SetScalableIcon(QLabel* lbl, const QIcon& icon, const QSize& size);
|
2025-11-20 17:50:21 -05:00
|
|
|
|
|
|
|
|
/// Gets the system language code, matching it against available languages
|
|
|
|
|
/// Returns the best matching language code, or "en-US" if no match is found
|
|
|
|
|
QString GetSystemLanguageCode();
|
|
|
|
|
|
|
|
|
|
/// Gets a flag icon for a given language code
|
|
|
|
|
/// Returns an empty QIcon if no flag is available for the language
|
|
|
|
|
QIcon GetFlagIconForLanguage(const QString& language_code);
|
2026-06-16 23:19:32 +07:00
|
|
|
|
2026-07-04 23:09:53 +07:00
|
|
|
/// Returns true if PCSX2 is running inside a Flatpak sandbox.
|
|
|
|
|
bool IsRunningInFlatpak();
|
|
|
|
|
|
|
|
|
|
/// Returns true if PCSX2 is running from an AppImage.
|
|
|
|
|
bool IsRunningInAppImage();
|
|
|
|
|
|
|
|
|
|
/// Creates a desktop or launcher (Start Menu / applications) shortcuts
|
2026-06-16 23:19:32 +07:00
|
|
|
void CreateShortcut(QWidget* parent, const std::string& name, const std::string& game_path,
|
|
|
|
|
std::vector<std::string> passed_cli_args, const std::string& custom_args,
|
2026-07-04 23:09:53 +07:00
|
|
|
const std::string& icon_path, bool is_desktop, bool prompt_for_destination = true);
|
2026-06-16 23:19:32 +07:00
|
|
|
|
|
|
|
|
/// Escapes the given string for use as a shortcut command line argument.
|
|
|
|
|
/// Returns whether the escaping operation was lossless.
|
|
|
|
|
bool EscapeShortcutCommandLine(std::string* arg);
|
2022-12-24 01:51:44 -05:00
|
|
|
} // namespace QtUtils
|