API Documentation
Loading...
Searching...
No Matches
QTTools.h
Go to the documentation of this file.
1/**--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Widgets
28File: QTTools
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/PopupInfo.h>
35#include <NDEVR/StringStream.h>
36#include <NDEVR/BaseValues.h>
37#include <NDEVR/Resource.h>
38#include <NDEVR/Translator.h>
39#include <NDEVR/Event.h>
40#if NDEVR_SUPPORTS_THREADING
41#include <QThread>
42#endif
43#include <QEvent>
44#include <QAbstractEventDispatcher>
45#include <QMessageBox>
46#include <QMetaEnum>
47
48class QFontMetrics;
49class QMessageBox;
50class QFont;
51class QStyle;
52class QColor;
53class QFile;
54class QSurfaceFormat;
55class QScreen;
56class QWindow;
57namespace detail {
58 template <typename F>
59 struct FEvent final : public QEvent {
60 using Fun = typename std::decay<F>::type;
62 FEvent(Fun && fun) : QEvent(QEvent::None), fun(std::move(fun)) {}
63 FEvent(const Fun & fun) : QEvent(QEvent::None), fun(fun) {}
64 ~FEvent() { fun(); }
65 };
66}
67namespace NDEVR
68{
69 class RGBColor;
70 class File;
71 class Ribbon;
72 struct Font;
73 class ImageData;
74 class KeyEvent;
75 struct GenericOption;
76 struct Module;
77
78 /**--------------------------------------------------------------------------------------------------
79 class: QTTools
80
81 Manages static logic dealing with Qt and relating to the application
82
83 Author: Tyler Parke
84
85 Date: 2023-01-16
86 *-----------------------------------------------------------------------------------------------**/
88 {
89 public:
90 static void postToThread(std::function<void()>&& fun, QThread * thread = nullptr);
91 static void postToThread(QObject* target, std::function<void()>&& fun);
92 static void requestScreenOrientation(bool horizontal);
93 static void RequestScreenResolution(const Vector<2, uint04>& size);
94 static KeyEvent::KEY ConvertNativeKeyEvent(sint08 key);
95 static QColor convert(const RGBColor& color);
96 static RGBColor convert(const QColor& color);
97 static Font convert(const QFont& q_font);
98 static QFont convert(const Font& font);
99 static void MultiplyFontSize(QFont& font, fltp08 multiplier);
100 static QSize ScaleFontSizeToFit(QFont& font, const QString& text, const QRect& rect, bool shrink = true, bool grow = false);
101 static void FitTextToRect(QString& text, QFont& font, const QRectF& size, bool allow_new_line = true);
102 static QString AutoWrapText(const QString& value, const QFontMetrics& metrics, fltp08 length);
103 static Buffer<QFont> GetFontRecursive(QWidget* widget);
104 static void SetFontRecursive(QWidget* widget, const QFont& font);
105 static void SetFontRecursive(QWidget* widget, const Buffer<QFont>& font);
106 static void SetStyleRecursive(QWidget* widget, QStyle* style);
107 static fltp08 PointSize(const QFont& font);
108 static fltp08 PixelSize(const QFont& font);
109 static fltp08 DPI();
110 static void ForceSizeCalculation(QWidget* widget, bool process_events = false);
111 static bool IsWidgetVisibleOnScreen(QWidget* widget);
112 static QScreen* ScreenForWidget(const QWidget* widget);
113 static QPixmap* DrawAnimationWidget(QWidget* widget, bool draw_background, QPixmap* pixmap = nullptr, QSize size = QSize());
114 static QPixmap* DrawFullWidget(QWidget* widget, bool draw_background, QPixmap* pixmap = nullptr, QSize size = QSize());
115 static qreal DevicePixelRatio(QWindow* window = nullptr);
116 static ProgressInfo* log();
117 static File convert(const QFile& file);
118 static QRect PopupMenuLocation(QWidget* widget, const QSize& size);
119 static QRect PopupMenuLocation(QRect popup_origin, const QSize& size);
120 static void ClearLayout(QLayout* layout, bool delete_objects = false);
121 static bool ShowDialog(QWidget* widget, const PopupInfo& info, const PopupInfo& end_location = PopupInfo());
122 static void HideDialog(QWidget* widget, const PopupInfo& end_location = PopupInfo());
123 static bool IsVisibleToUser(QWidget* widget);
124 static bool ShowMessageBox(const TranslatedString& title, const TranslatedString& text
125 , QMessageBox::StandardButtons buttons = { QMessageBox::StandardButton::Ok }
126 , std::function<void(QMessageBox::StandardButton, PopupInfo info)> on_select = nullptr
127 , const PopupInfo& popup_origin = PopupInfo());
128 static bool ShowMessageBox(const TranslatedString& title, const String& image, const TranslatedString& text
129 , QMessageBox::StandardButtons buttons = { QMessageBox::StandardButton::Ok }
130 , std::function<void(QMessageBox::StandardButton, PopupInfo info)> on_select = nullptr
131 , const PopupInfo& popup_origin = PopupInfo());
132
133 static bool ShowMessageBox(const TranslatedString& title, const TranslatedString& text
134 , const Buffer<TranslatedString, uint04, ObjectAllocator<false>>& button_labels
135 , std::function<void(TranslatedString, PopupInfo info)> on_select = nullptr
136 , const PopupInfo& popup_origin = PopupInfo());
137 static bool ShowMessageBox(const TranslatedString& title, const String& image, const TranslatedString& text
138 , const Buffer<TranslatedString, uint04, ObjectAllocator<false>>& button_labels
139 , std::function<void(TranslatedString, PopupInfo info)> on_select = nullptr
140 , const PopupInfo& popup_origin = PopupInfo());
141
142 static bool ShowMessageBox(const GenericOption& options, std::function<bool(GenericOption, PopupInfo info)> on_select
143 , const PopupInfo& popup_origin = PopupInfo());
144 static bool ShowMessageBox(const GenericOption& options, const String& image, std::function<bool(GenericOption, PopupInfo info)> on_select
145 , const PopupInfo& popup_origin = PopupInfo());
146 static void RequestExit(bool relaunch, const PopupInfo& popup_origin = PopupInfo());
147 static void ShowExperimentalFeatureDialog(const TranslatedString& feature);
148 static fltp08 DefaultAnimationScale();
149 static QSurfaceFormat DefaultSurfaceFormat();
150 static void FormatPallete(bool error, QWidget* widget);
151 static void ClearPallete(QWidget* widget);
152 static void ServiceQT();
153 static KeyEvent convert(QKeyEvent* event);
154 static Module QTModule();
156
157 template<typename EnumType>
158 static String EnumString(const EnumType& enumValue)
159 {
160 const char* enumName = qt_getEnumName(enumValue);
161 const QMetaObject* metaObject = qt_getEnumMetaObject(enumValue);
162 if (metaObject)
163 {
164 const int enumIndex = metaObject->indexOfEnumerator(enumName);
165 return String(metaObject->className()) +" : "+ String(enumName) +" : "+ String(metaObject->enumerator(enumIndex).valueToKey(enumValue));
166 }
167
168 return String(enumName) +" : "+ String(static_cast<int>(enumValue));
169 }
170 };
172 {
173 public:
174 explicit QTAbstractResourceListener(QObject* parent = nullptr);
175
177 virtual void onChanged() = 0;
178 void onValueChanged() override final;
179 bool m_has_been_posted;
180 bool m_safe_to_delete;
181 };
183 {
184 public:
185 explicit QTResourceListener(const std::function<void()>& callback, QObject* parent);
186 virtual ~QTResourceListener();
187 void onChanged() override final;
188 const std::function<void()> m_callback;
189 };
192 #define _tqa(var) (_t(var).translation().getAs<QString>())//returns raw tranlation, should not be used for window titles
193 #define _tqs(var) (String(_t(var)).getAs<QString>())//returns pseudo translation. Should be used for window titles
194 #define _tdq(var, def) (_td(var, def).translation().getAs<QString>())
195}
196
#define NDEVR_WIDGETS_API
Definition DLLInfo.h:81
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
Definition File.h:47
Definition Event.h:42
KEY
Definition Event.h:45
Definition MemoryManager.h:283
Definition ProgressInfo.hpp:43
Definition QTTools.h:172
Definition QTTools.h:183
const std::function< void()> m_callback
Definition QTTools.h:188
Definition QTTools.h:88
static bool ShowMessageBox(const TranslatedString &title, const String &image, const TranslatedString &text, const Buffer< TranslatedString, uint04, ObjectAllocator< false > > &button_labels, std::function< void(TranslatedString, PopupInfo info)> on_select=nullptr, const PopupInfo &popup_origin=PopupInfo())
static bool ShowMessageBox(const TranslatedString &title, const TranslatedString &text, const Buffer< TranslatedString, uint04, ObjectAllocator< false > > &button_labels, std::function< void(TranslatedString, PopupInfo info)> on_select=nullptr, const PopupInfo &popup_origin=PopupInfo())
static bool ShowMessageBox(const TranslatedString &title, const TranslatedString &text, QMessageBox::StandardButtons buttons={ QMessageBox::StandardButton::Ok }, std::function< void(QMessageBox::StandardButton, PopupInfo info)> on_select=nullptr, const PopupInfo &popup_origin=PopupInfo())
static bool ShowMessageBox(const TranslatedString &title, const String &image, const TranslatedString &text, QMessageBox::StandardButtons buttons={ QMessageBox::StandardButton::Ok }, std::function< void(QMessageBox::StandardButton, PopupInfo info)> on_select=nullptr, const PopupInfo &popup_origin=PopupInfo())
static String EnumString(const EnumType &enumValue)
Definition QTTools.h:158
static Resource< bool > is_horizontal
Definition QTTools.h:155
static bool ShowMessageBox(const GenericOption &options, const String &image, std::function< bool(GenericOption, PopupInfo info)> on_select, const PopupInfo &popup_origin=PopupInfo())
static bool ShowMessageBox(const GenericOption &options, std::function< bool(GenericOption, PopupInfo info)> on_select, const PopupInfo &popup_origin=PopupInfo())
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:53
Definition Toggle.h:41
Definition ResourceListener.h:55
Definition String.h:40
Definition StringStream.h:62
Definition TranslatedString.h:9
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
Definition ACIColor.h:37
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Definition BaseValues.hpp:86
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
aiColor4D convert(RGBColor value)
Definition AssimpFactory.cpp:271
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:181
Definition QTTools.h:57
Definition File.h:213
Definition Font.h:41
Definition GenericOptions.h:53
Definition ModuleManager.h:54
Definition PopupInfo.h:10
Definition QTTools.h:59
FEvent(Fun &&fun)
Definition QTTools.h:62
typename std::decay< F >::type Fun
Definition QTTools.h:60
~FEvent()
Definition QTTools.h:64
Fun fun
Definition QTTools.h:61
FEvent(const Fun &fun)
Definition QTTools.h:63