pcsx2/pcsx2-qt/QtUtils.h

101 lines
3.3 KiB
C
Raw Normal View History

2024-04-23 15:17:52 +00:00
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
// SPDX-License-Identifier: GPL-3.0+
2021-12-13 12:12:54 +00:00
#pragma once
2022-10-15 09:03:52 +00:00
#include "common/WindowInfo.h"
2021-12-13 12:12:54 +00:00
#include <QtCore/QByteArray>
#include <QtCore/QMetaType>
#include <QtCore/QString>
#include <QtCore/QAbstractItemModel>
2021-12-13 12:12:54 +00:00
#include <functional>
#include <initializer_list>
2022-01-31 05:24:19 +00:00
#include <string_view>
2021-12-13 12:12:54 +00:00
#include <optional>
class ByteStream;
class QAction;
class QComboBox;
2023-09-09 07:15:41 +00:00
class QFileInfo;
2021-12-13 12:12:54 +00:00
class QFrame;
2024-04-23 15:17:52 +00:00
class QLabel;
2021-12-13 12:12:54 +00:00
class QKeyEvent;
2024-04-23 15:17:52 +00:00
class QSlider;
2021-12-13 12:12:54 +00:00
class QTableView;
class QTreeView;
class QVariant;
class QWidget;
class QUrl;
namespace QtUtils
{
2022-07-02 11:51:23 +00:00
/// Wheel delta is 120 as in winapi.
static constexpr float MOUSE_WHEEL_DELTA = 120.0f;
2021-12-13 12:12:54 +00: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);
/// 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 12:12:54 +00:00
2023-09-09 07:15:41 +00: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 12:12:54 +00: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 05:24:19 +00:00
/// Converts a std::string_view to a QString safely.
QString StringViewToQString(const std::string_view str);
2022-06-08 12:15:10 +00:00
/// Sets a widget to italics if the setting value is inherited.
void SetWidgetFontForInheritedSetting(QWidget* widget, bool inherited);
2022-06-28 12:16:45 +00:00
2024-04-23 15:17:52 +00:00
/// Binds a label to a slider's value.
void BindLabelToSlider(QSlider* slider, QLabel* label, float range = 1.0f);
2022-06-28 12:16:45 +00:00
/// Changes whether a window is resizable.
void SetWindowResizeable(QWidget* widget, bool resizeable);
/// Adjusts the fixed size for a window if it's not resizeable.
void ResizePotentiallyFixedSizeWindow(QWidget* widget, int width, int height);
2022-10-15 09:03:52 +00:00
/// Returns the pixel ratio/scaling factor for a widget.
qreal GetDevicePixelRatioForWidget(const QWidget* widget);
/// Returns the common window info structure for a Qt widget.
std::optional<WindowInfo> GetWindowInfoForWidget(QWidget* widget);
/// 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();
};
/// Converts an abstract item model to a CSV string.
QString AbstractItemModelToCSV(QAbstractItemModel* model, int role = Qt::DisplayRole, bool useQuotes = false);
} // namespace QtUtils