qtutils.h (3497B)
1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #pragma once 5 6 #include "util/window_info.h" 7 8 #include "common/types.h" 9 10 #include <QtCore/QByteArray> 11 #include <QtCore/QMetaType> 12 #include <QtCore/QString> 13 #include <QtGui/QIcon> 14 #include <QtWidgets/QWidget> 15 #include <functional> 16 #include <initializer_list> 17 #include <optional> 18 19 class ByteStream; 20 21 class QComboBox; 22 class QFrame; 23 class QKeyEvent; 24 class QLabel; 25 class QSlider; 26 class QTableView; 27 class QTreeView; 28 class QVariant; 29 class QWidget; 30 class QUrl; 31 32 enum class ConsoleRegion; 33 enum class DiscRegion : u8; 34 namespace GameDatabase { 35 enum class CompatibilityRating : u8; 36 } 37 namespace GameList { 38 enum class EntryType; 39 } 40 41 namespace QtUtils { 42 43 /// Wheel delta is 120 as in winapi. 44 static constexpr float MOUSE_WHEEL_DELTA = 120.0f; 45 46 /// Creates a horizontal line widget. 47 QFrame* CreateHorizontalLine(QWidget* parent); 48 49 /// Returns the greatest parent of a widget, i.e. its dialog/window. 50 QWidget* GetRootWidget(QWidget* widget, bool stop_at_window_or_dialog = true); 51 52 /// Shows or raises a window (brings it to the front). 53 void ShowOrRaiseWindow(QWidget* window); 54 55 /// Closes and deletes a window later, outside of this event pump. 56 template<typename T> 57 [[maybe_unused]] static void CloseAndDeleteWindow(T*& window) 58 { 59 if (!window) 60 return; 61 62 window->close(); 63 64 // Some windows delete themselves. 65 if (window) 66 window->deleteLater(); 67 68 window = nullptr; 69 } 70 71 /// Resizes columns of the table view to at the specified widths. A negative width will stretch the column to use the 72 /// remaining space. 73 void ResizeColumnsForTableView(QTableView* view, const std::initializer_list<int>& widths); 74 void ResizeColumnsForTreeView(QTreeView* view, const std::initializer_list<int>& widths); 75 76 /// Returns a key id for a key event, including any modifiers that we need (e.g. Keypad). 77 /// NOTE: Defined in QtKeyCodes.cpp, not QtUtils.cpp. 78 u32 KeyEventToCode(const QKeyEvent* ev); 79 80 /// Opens a URL with the default handler. 81 void OpenURL(QWidget* parent, const QUrl& qurl); 82 83 /// Opens a URL string with the default handler. 84 void OpenURL(QWidget* parent, const char* url); 85 86 /// Prompts for an address in hex. 87 std::optional<unsigned> PromptForAddress(QWidget* parent, const QString& title, const QString& label, bool code); 88 89 /// Converts a std::string_view to a QString safely. 90 QString StringViewToQString(std::string_view str); 91 92 /// Sets a widget to italics if the setting value is inherited. 93 void SetWidgetFontForInheritedSetting(QWidget* widget, bool inherited); 94 95 /// Binds a label to a slider's value. 96 void BindLabelToSlider(QSlider* slider, QLabel* label, float range = 1.0f); 97 98 /// Changes whether a window is resizable. 99 void SetWindowResizeable(QWidget* widget, bool resizeable); 100 101 /// Adjusts the fixed size for a window if it's not resizeable. 102 void ResizePotentiallyFixedSizeWindow(QWidget* widget, int width, int height); 103 104 /// Returns icon for region. 105 QIcon GetIconForRegion(ConsoleRegion region); 106 QIcon GetIconForRegion(DiscRegion region); 107 108 /// Returns icon for entry type. 109 QIcon GetIconForEntryType(GameList::EntryType type); 110 QIcon GetIconForCompatibility(GameDatabase::CompatibilityRating rating); 111 112 /// Returns the pixel ratio/scaling factor for a widget. 113 qreal GetDevicePixelRatioForWidget(const QWidget* widget); 114 115 /// Returns the common window info structure for a Qt widget. 116 std::optional<WindowInfo> GetWindowInfoForWidget(QWidget* widget); 117 118 } // namespace QtUtils