NDEVR
API Documentation
QTTools.h
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;
57
58namespace NDEVR
59{
60 class RGBColor;
61 class File;
62 class Ribbon;
63 class Font;
64 class ImageData;
65 class KeyEvent;
66 struct GenericOption;
67 struct Module;
68 typedef AlocatingAlignedBuffer<TranslatedString, 64> TranslatedStringBuffer;
72 class NDEVR_WIDGETS_API QTTools
73 {
74 public:
80 static void PostToThread(std::function<void()>&& fun, QThread * thread = nullptr);
86 static void PostToThread(QObject* target, std::function<void()>&& fun);
91 static void RequestScreenOrientation(bool horizontal);
108 static QColor convert(const RGBColor& color);
114 static RGBColor convert(const QColor& color);
120 static Font convert(const QFont& q_font);
126 static QFont convert(const Font& font);
132 static void MultiplyFontSize(QFont& font, fltp08 multiplier);
142 static QSize ScaleFontSizeToFit(QFont& font, const QString& text, const QRect& rect, bool shrink = true, bool grow = false);
150 static void FitTextToRect(QString& text, QFont& font, const QRectF& size, bool allow_new_line = true);
159 static void FitTextToCircle(QString& text, QFont& font, const fltp08 radius, fltp08 split_y_fraction, bool allow_new_line = true);
167 static QString AutoWrapText(const QString& value, const QFontMetrics& metrics, fltp08 length);
173 static Buffer<QFont> GetFontRecursive(QWidget* widget);
179 static void SetFontRecursive(QWidget* widget, const QFont& font);
185 static void SetFontRecursive(QWidget* widget, const Buffer<QFont>& font);
191 static void SetStyleRecursive(QWidget* widget, QStyle* style);
197 static fltp08 PointSize(const QFont& font);
203 static fltp08 PixelSize(const QFont& font);
208 static fltp08 DPI();
214 static void ForceSizeCalculation(QWidget* widget, bool process_events = false);
220 static bool IsWidgetVisibleOnScreen(QWidget* widget);
226 static QScreen* ScreenForWidget(const QWidget* widget);
235 static QPixmap* DrawAnimationWidget(QWidget* widget, bool draw_background, QPixmap* pixmap = nullptr, QSize size = QSize());
244 static QPixmap* DrawFullWidget(QWidget* widget, bool draw_background, QPixmap* pixmap = nullptr, QSize size = QSize());
250 static qreal DevicePixelRatio(QWindow* window = nullptr);
255 static LogPtr log();
261 static File convert(const QFile& file);
268 static QRect PopupMenuLocation(QWidget* widget, const QSize& size);
276 static QRect PopupMenuLocation(QWidget* widget, QRect popup_origin, const QSize& size);
282 static void ClearLayout(QLayout* layout, bool delete_objects = false);
290 static bool ShowDialog(QWidget* widget, PopupInfo info, PopupInfo end_location = PopupInfo());
296 static void HideDialog(QWidget* widget, PopupInfo end_location = PopupInfo());
303 static void AutosetName(QWidget* widget, const TranslatedString& title, const StringView& object_class);
309 static bool IsVisibleToUser(QWidget* widget);
319 static bool ShowMessageBox(const TranslatedString& title, const TranslatedString& text
320 , QMessageBox::StandardButtons buttons = { QMessageBox::StandardButton::Ok }
321 , std::function<void(QMessageBox::StandardButton, PopupInfo info)> on_select = nullptr
322 , const PopupInfo& popup_origin = PopupInfo());
333 static bool ShowMessageBox(const TranslatedString& title, const StringView& image, const TranslatedString& text
334 , QMessageBox::StandardButtons buttons = { QMessageBox::StandardButton::Ok }
335 , std::function<void(QMessageBox::StandardButton, PopupInfo info)> on_select = nullptr
336 , const PopupInfo& popup_origin = PopupInfo());
346 static bool ShowMessageBox(const TranslatedString& title, const TranslatedString& text
347 , const TranslatedStringBuffer& button_labels
348 , std::function<void(TranslatedString, PopupInfo info)> on_select = nullptr
349 , const PopupInfo& popup_origin = PopupInfo());
360 static bool ShowMessageBox(const TranslatedString& title, const StringView& image, const TranslatedString& text
361 , const TranslatedStringBuffer& button_labels
362 , std::function<void(TranslatedString, PopupInfo info)> on_select = nullptr
363 , const PopupInfo& popup_origin = PopupInfo());
371 static bool ShowMessageBox(const GenericOption& options, std::function<bool(GenericOption, PopupInfo info)> on_select
372 , const PopupInfo& popup_origin = PopupInfo());
381 static bool ShowMessageBox(const GenericOption& options, const StringView& image, std::function<bool(GenericOption, PopupInfo info)> on_select
382 , const PopupInfo& popup_origin = PopupInfo());
388 static void RequestExit(bool relaunch, const PopupInfo& popup_origin = PopupInfo());
404 static QSurfaceFormat DefaultSurfaceFormat(bool debug);
410 static void FormatPallete(bool error, QWidget* widget);
415 static void ClearPallete(QWidget* widget);
419 static void ServiceQT();
425 static KeyEvent convert(QKeyEvent* event);
430 static Module QTModule();
432
438 template<typename EnumType>
439 static String EnumString(const EnumType& enumValue)
440 {
441 const char* enumName = qt_getEnumName(enumValue);
442 const QMetaObject* metaObject = qt_getEnumMetaObject(enumValue);
443 if (metaObject)
444 {
445 const int enumIndex = metaObject->indexOfEnumerator(enumName);
446 return String(metaObject->className()) +" : "+ String(enumName) +" : "+ String(metaObject->enumerator(enumIndex).valueToKey(enumValue));
447 }
448
449 return String(enumName) +" : "+ String(static_cast<int>(enumValue));
450 }
451 };
452
457 class NDEVR_WIDGETS_API QTAbstractResourceListener : public ResourceListener, public QObject
458 {
459 public:
464 explicit QTAbstractResourceListener(QObject* parent = nullptr);
465
470 virtual void onChanged() = 0;
471
472 protected:
477 bool isSafeToDelete() { return m_safe_to_delete; }
482 bool hasBeenPosted() { return m_has_been_posted; }
483 private:
484 void onValueChanged() override final;
485 bool m_has_been_posted;
486 bool m_safe_to_delete;
487 };
488
492 class NDEVR_WIDGETS_API QTResourceListener : public QTAbstractResourceListener
493 {
494 public:
500 explicit QTResourceListener(const std::function<void()>& callback, QObject* parent);
501 virtual ~QTResourceListener();
505 void onChanged() override final;
506 private:
507 const std::function<void()> m_callback;
508 };
509 template class NDEVR_WIDGETS_API StringStream<QString>;
510 template class NDEVR_WIDGETS_API StringStream<QByteArray>;
511 #define _tqa(var) (_t(var).translation().getAs<QString>())//returns raw tranlation, should not be used for window titles
512 #define _tqs(var) (String(_t(var)).getAs<QString>())//returns pseudo translation. Should be used for window titles
513 #define _tdq(var, def) (_td(var, def).translation().getAs<QString>())
514}
515
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
Information for how to display text data.
Definition Font.h:46
A container for Images managed by the ImageFactory.
Describes a user key press event used to trigger behavior in the NDEVR API.
Definition Event.h:49
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
QTAbstractResourceListener(QObject *parent=nullptr)
Constructs a resource listener tied to a Qt parent object for automatic lifetime management.
void onValueChanged() override final
Called when a subscribed resource value has changed.
virtual void onChanged()=0
Called on the main UI thread when the listened resource changes.
bool isSafeToDelete()
Returns whether it is safe to delete this listener (i.e., not in the middle of a callback).
Definition QTTools.h:477
bool hasBeenPosted()
Returns whether a change notification has been posted but not yet executed.
Definition QTTools.h:482
QTResourceListener(const std::function< void()> &callback, QObject *parent)
Constructs a resource listener that invokes the given callback on the UI thread.
void onChanged() override final
Invokes the stored callback on the main UI thread.
Manages static logic dealing with Qt and relating to the application.
Definition QTTools.h:73
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())
Shows a message box with standard buttons.
static bool ShowMessageBox(const GenericOption &options, const StringView &image, std::function< bool(GenericOption, PopupInfo info)> on_select, const PopupInfo &popup_origin=PopupInfo())
Shows a message box configured by a GenericOption with an image.
static fltp08 PointSize(const QFont &font)
Returns the point size of a font, converting from pixel size if necessary.
static void SetFontRecursive(QWidget *widget, const QFont &font)
Recursively sets the font on a widget and all its children.
static fltp08 DefaultAnimationScale()
Returns the default animation time scale factor.
static bool ShowMessageBox(const TranslatedString &title, const StringView &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())
Shows a message box with an image and standard buttons.
static bool ShowMessageBox(const TranslatedString &title, const StringView &image, const TranslatedString &text, const TranslatedStringBuffer &button_labels, std::function< void(TranslatedString, PopupInfo info)> on_select=nullptr, const PopupInfo &popup_origin=PopupInfo())
Shows a message box with an image and custom-labeled buttons.
static bool IsWidgetVisibleOnScreen(QWidget *widget)
Checks whether a widget is currently visible on any screen.
static QSize ScaleFontSizeToFit(QFont &font, const QString &text, const QRect &rect, bool shrink=true, bool grow=false)
Scales a font size so that the given text fits within the specified rectangle.
static void PostToThread(std::function< void()> &&fun, QThread *thread=nullptr)
Posts a function to be executed on the specified thread, or the main thread if none is given.
static void FitTextToCircle(QString &text, QFont &font, const fltp08 radius, fltp08 split_y_fraction, bool allow_new_line=true)
Adjusts text and font so the text fits within a circle.
static void RequestExit(bool relaunch, const PopupInfo &popup_origin=PopupInfo())
Requests the application to exit, optionally relaunching afterward.
static QRect PopupMenuLocation(QWidget *widget, const QSize &size)
Calculates a suitable popup menu location relative to a widget.
static void FormatPallete(bool error, QWidget *widget)
Applies an error or normal formatting to a widget's palette.
static fltp08 DPI()
Returns the current screen DPI.
static void RequestScreenOrientation(bool horizontal)
Requests the screen orientation to be horizontal or vertical.
static QColor convert(const RGBColor &color)
Converts an NDEVR RGBColor to a QColor.
static bool ShowDialog(QWidget *widget, PopupInfo info, PopupInfo end_location=PopupInfo())
Shows a dialog widget with animation and positioning specified by PopupInfo.
static Font convert(const QFont &q_font)
Converts a QFont to an NDEVR Font.
static KeyEvent convert(QKeyEvent *event)
Converts a Qt QKeyEvent to an NDEVR KeyEvent.
static bool IsVisibleToUser(QWidget *widget)
Checks whether a widget is visible to the user, accounting for popups and tabs.
static QPixmap * DrawFullWidget(QWidget *widget, bool draw_background, QPixmap *pixmap=nullptr, QSize size=QSize())
Renders a complete widget and its children into a pixmap.
static QPixmap * DrawAnimationWidget(QWidget *widget, bool draw_background, QPixmap *pixmap=nullptr, QSize size=QSize())
Renders a widget into a pixmap suitable for animation snapshots.
static void RequestScreenResolution(const Vector< 2, uint04 > &size)
Requests a specific screen resolution.
static void HideDialog(QWidget *widget, PopupInfo end_location=PopupInfo())
Hides a dialog widget with an optional animation to an end location.
static LogPtr log()
Returns the global log for the Widgets module.
static void SetFontRecursive(QWidget *widget, const Buffer< QFont > &font)
Recursively restores fonts on a widget and all its children from a buffer.
static void SetStyleRecursive(QWidget *widget, QStyle *style)
Recursively sets the QStyle on a widget and all its children.
static QString AutoWrapText(const QString &value, const QFontMetrics &metrics, fltp08 length)
Wraps text automatically at word boundaries to fit within a given pixel length.
static QRect PopupMenuLocation(QWidget *widget, QRect popup_origin, const QSize &size)
Calculates a suitable popup menu location relative to a widget with a given origin.
static void ClearPallete(QWidget *widget)
Resets a widget's palette to the default application palette.
static void MultiplyFontSize(QFont &font, fltp08 multiplier)
Multiplies the font size by a given factor.
static void ClearLayout(QLayout *layout, bool delete_objects=false)
Removes all items from a layout, optionally deleting the child widgets.
static Resource< bool > is_horizontal
Resource tracking whether the screen is in horizontal (landscape) orientation.
Definition QTTools.h:431
static KeyEvent::KEY ConvertNativeKeyEvent(sint08 key)
Converts a native platform key code to an NDEVR KeyEvent::KEY value.
static QFont convert(const Font &font)
Converts an NDEVR Font to a QFont.
static void AutosetName(QWidget *widget, const TranslatedString &title, const StringView &object_class)
Automatically sets the object name on a widget based on the title and class.
static QScreen * ScreenForWidget(const QWidget *widget)
Returns the QScreen that contains the given widget.
static void ForceSizeCalculation(QWidget *widget, bool process_events=false)
Forces a widget to recalculate its size, optionally processing pending events.
static Buffer< QFont > GetFontRecursive(QWidget *widget)
Recursively collects the fonts used by a widget and all its children.
static File convert(const QFile &file)
Converts a QFile to an NDEVR File.
static void PostToThread(QObject *target, std::function< void()> &&fun)
Posts a function to be executed on the thread that owns the target QObject.
static fltp08 PixelSize(const QFont &font)
Returns the pixel size of a font, converting from point size if necessary.
static void FitTextToRect(QString &text, QFont &font, const QRectF &size, bool allow_new_line=true)
Adjusts text and font so the text fits within a rectangle, potentially adding line breaks.
static String EnumString(const EnumType &enumValue)
Converts a Qt enum value to a human-readable String including class and enum name.
Definition QTTools.h:439
static RGBColor convert(const QColor &color)
Converts a QColor to an NDEVR RGBColor.
static QSurfaceFormat DefaultSurfaceFormat(bool debug)
Returns the default QSurfaceFormat for OpenGL surfaces.
static qreal DevicePixelRatio(QWindow *window=nullptr)
Returns the device pixel ratio for the given window, or the primary screen if null.
static void ShowExperimentalFeatureDialog(const TranslatedString &feature)
Shows a dialog informing the user that a feature is experimental.
static void ServiceQT()
Processes pending Qt events in the current event loop.
static bool ShowMessageBox(const TranslatedString &title, const TranslatedString &text, const TranslatedStringBuffer &button_labels, std::function< void(TranslatedString, PopupInfo info)> on_select=nullptr, const PopupInfo &popup_origin=PopupInfo())
Shows a message box with custom-labeled buttons.
static bool ShowMessageBox(const GenericOption &options, std::function< bool(GenericOption, PopupInfo info)> on_select, const PopupInfo &popup_origin=PopupInfo())
Shows a message box configured by a GenericOption.
static Module QTModule()
Returns the Module descriptor for the Qt integration module.
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:57
ResourceListener()
Constructs a default ResourceListener.
A core part of the engine, stores variables that can be listened to with ResourceListener which will ...
Definition Resource.h:42
The UI ribbon is a horizontal menu bar typically located at the top of the application interface or a...
Definition Ribbon.h:56
Logic for reading or writing to a string or a user friendly, TranslatedString.
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
The primary namespace for the NDEVR SDK.
double fltp08
Defines an alias representing an 8 byte floating-point number.
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
STL namespace.
Stores a generic option of any type as well as some information about how the user might interact wit...
Base class for extensions, typically added as external DLL's that can modify or enhance the behavior ...
Class which is used to pass arguments and requests for creating a popup dialog or widget.
Definition PopupInfo.h:16