Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
qt_utils.h
Go to the documentation of this file.
1#pragma once
2#include <QAbstractProxyModel>
3#include <QComboBox>
4#include <QDialogButtonBox>
5#include <QFileDialog>
6#include <QFormLayout>
7#include <QMetaEnum>
8#include <QPalette>
9#include <QPoint>
10#include <QRect>
11#include <QSettings>
12#include <QSize>
13#include <QString>
14#include <QTreeView>
15#include <misc/global.h>
16
20inline std::ostream& operator<<(std::ostream& os, const QString& qs)
21{
22 // return os << qs.toStdString();
23 return os << qs.toUtf8().constData();
24}
25
29inline QPoint operator-(const QPoint& pt, const QSize& sz)
30{
31 return {pt.x() - sz.width(), pt.y() - sz.height()};
32}
33
37inline QPoint operator+(const QPoint& pt, const QSize& sz)
38{
39 return {pt.x() + sz.width(), pt.y() + sz.height()};
40}
41
45inline QRect operator+(const QRect& rc, const QSize& sz)
46{
47 return {rc.topLeft(), rc.size() + sz};
48}
49
53inline QRect operator-(const QRect& rc, const QSize& sz)
54{
55 return {rc.topLeft(), rc.size() - sz};
56}
57
61inline QRect operator+(const QRect& rc, const QPoint& pt)
62{
63 return {rc.topLeft() + pt, rc.size()};
64}
65
69inline QRect operator+=(QRect& rc, const QPoint& pt)
70{
71 rc = {rc.topLeft() + pt, rc.size()};
72 return rc;
73}
74
78inline QRect operator-(const QRect& rc, const QPoint& pt)
79{
80 return {rc.topLeft() - pt, rc.size()};
81}
82
86inline QRect operator-=(QRect& rc, const QPoint& pt)
87{
88 rc = {rc.topLeft() - pt, rc.size()};
89 return rc;
90}
91
95inline QRect& inflate(QRect& r, int sz)
96{
97 r.adjust(-sz, -sz, sz, sz);
98 return r;
99}
100
104constexpr QRect inflated(const QRect& r, int sz)
105{
106 return r.adjusted(-sz, -sz, sz, sz);
107}
108
114inline QSize asQSize(const QPoint& pt)
115{
116 return {pt.x(), pt.y()};
117}
118
124inline QSizeF asQSizeF(const QPointF& pt)
125{
126 return {pt.x(), pt.y()};
127}
128
129namespace sf
130{
131
137_MISC_FUNC std::string toString(const QRect& rect);
138
145_MISC_FUNC QRect moveRectWithinRect(const QRect& outer, const QRect& inner);
146
151{
152 public:
153 explicit PaletteColors() = default;
154
155 explicit PaletteColors(const QPalette& palette);
156
157 QPalette getPalette() const;
158
159 void setColors(const QPalette& palette);
160
161 bool isEmpty() const;
162
163 void styleFileDialog(QFileDialog& fd) const;
164
165 private:
166 typedef QPair<QPalette::ColorRole, QColor> Pair;
167 QList<Pair> _colors;
168};
169
182template<typename T, typename F>
183QMetaEnum flagsMetaEnum()
184{
185 static_assert(std::is_base_of_v<QObject, T>, "Type T must be a QObject-derived class!");
186 return T::staticMetaObject.enumerator(T::staticMetaObject.indexOfEnumerator(QMetaEnum::fromType<F>().name()));
187}
188
205template<typename QObjectType, typename FlagsType>
207{
208 public:
209 using EnumType = typename FlagsType::enum_type;
210
212 {
213 public:
214 Iterator(const QMetaEnum& metaEnum, const FlagsType& flags, int index)
215 : metaEnum(metaEnum)
216 , flags(flags)
217 , index(index)
218 {
219 next();
220 }
221
223 {
224 return static_cast<EnumType>(metaEnum.value(index));
225 }
226
228 {
229 ++index;
230 next();
231 return *this;
232 }
233
234 bool operator!=(const Iterator& other) const
235 {
236 return index != other.index;
237 }
238
239 private:
240 void next()
241 {
242 while (index < metaEnum.keyCount() && !flags.testFlag(static_cast<EnumType>(metaEnum.value(index))))
243 ++index;
244 }
245
246 QMetaEnum metaEnum;
247 FlagsType flags;
248 int index;
249 };
250
251 explicit FlagsIterator(FlagsType flags)
252 : flags(flags)
253 , metaEnum(flagsMetaEnum<QObjectType, FlagsType>())
254 {}
255
257 {
258 return Iterator(metaEnum, flags, 0);
259 }
260
261 Iterator end() const
262 {
263 return Iterator(metaEnum, flags, metaEnum.keyCount());
264 }
265
266 private:
267 FlagsType flags;
268 QMetaEnum metaEnum;
269};
270
281_MISC_FUNC QMetaObject::Connection connectByName(
282 const QWidget* widget, const QString& sender_name, const char* signal_name, const QObject* receiver, const char* method_name,
283 Qt::ConnectionType ct = Qt::AutoConnection
284);
285
292_MISC_FUNC QStringList getObjectNamePath(const QObject* object, bool use_rtti = false);
293
299_MISC_FUNC QLayout* getWidgetLayout(const QWidget* widget);
300
307template<typename T>
308static const char* enumToKey(const T value)
309{
310 return QMetaEnum::fromType<T>().valueToKey(value);
311}
312
320template<typename T>
321static T keyToEnum(const char* key, bool* ok = nullptr)
322{
323 return static_cast<T>(QMetaEnum::fromType<T>().keyToValue(key, ok));
324}
325
331template<typename T>
332static const char* enumName()
333{
334 return QMetaEnum::fromType<T>().enumName();
335}
336
343template<typename T>
344static T keyToEnum(QString key)
345{
346 return QMetaEnum::fromType<T>().keyToValue(key);
347}
348
356_MISC_FUNC int indexFromComboBox(const QComboBox* comboBox, const QVariant& value, int default_index = -1);
357
361_MISC_FUNC QPair<QLayout*, int> getTopLayoutAndIndex(const QObject* object);
362
369_MISC_FUNC QPair<int, QFormLayout::ItemRole> getLayoutPosition(const QFormLayout* layout, QObject* target);
370
377_MISC_FUNC int getLayoutIndex(const QBoxLayout* layout, QObject* target);
378
383inline void resizeColumnsToContents(QTreeView* treeView)
384{
385 const auto count = treeView->model()->columnCount({}) - 1;
386 for (int i = 0; i < count; i++)
387 {
388 treeView->resizeColumnToContents(i);
389 }
390}
391
395_MISC_FUNC void dumpObjectProperties(const QObject* obj);
396
403template<typename T>
404T* getSourceModel(const QAbstractItemModel* am)
405{
406 // First check if the passed abstract model is the model we look for.
407 if (auto m = dynamic_cast<const T*>(am))
408 {
409 return const_cast<T*>(m);
410 }
411 // Secondly, when a proxy is used.
412 if (const auto apm = dynamic_cast<const QAbstractProxyModel*>(am))
413 {
414 if (auto m = dynamic_cast<T*>(apm->sourceModel()))
415 {
416 return const_cast<T*>(m);
417 }
418 }
419 return nullptr;
420}
421
425_MISC_FUNC QModelIndex getSourceModelIndex(const QModelIndex& index);
426
433_MISC_FUNC void expandTreeView(QTreeView* tv, bool expand = true, const QModelIndex& index = {});
434
440
447_MISC_FUNC QString trimRight(const QString& str, const QString& chars = " ");
448
455_MISC_FUNC QString trimLeft(const QString& str, const QString& chars = " ");
456
463_MISC_FUNC QString trim(const QString& str, const QString& chars = " ");
464
468_MISC_FUNC bool isValidFilePath(const QString& path);
469
473_MISC_FUNC bool isValidDirectoryPath(const QString& path);
474
482_MISC_FUNC void storeWindowState(QSettings& settings, QAnyStringView name, QWidget* widget, bool read);
483
491_MISC_FUNC void storeTreeViewState(QSettings& settings, QAnyStringView name, const QTreeView* tv, bool read);
492
493}// namespace sf
494
498inline std::ostream& operator<<(std::ostream& os, const QRect& rect)
499{
500 return os << sf::toString(rect);
501}
Definition qt_utils.h:212
bool operator!=(const Iterator &other) const
Definition qt_utils.h:234
Iterator(const QMetaEnum &metaEnum, const FlagsType &flags, int index)
Definition qt_utils.h:214
EnumType operator*() const
Definition qt_utils.h:222
Iterator & operator++()
Definition qt_utils.h:227
Wrapper class to iterate over the flags set.
Definition qt_utils.h:207
typename FlagsType::enum_type EnumType
Definition qt_utils.h:209
FlagsIterator(FlagsType flags)
Definition qt_utils.h:251
Iterator end() const
Definition qt_utils.h:261
Iterator begin() const
Definition qt_utils.h:256
Type to hold palette colors.
Definition qt_utils.h:151
PaletteColors()=default
bool isEmpty() const
PaletteColors(const QPalette &palette)
void setColors(const QPalette &palette)
QPalette getPalette() const
void styleFileDialog(QFileDialog &fd) const
#define _MISC_FUNC
Definition misc/global.h:39
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
_MISC_FUNC QModelIndex getSourceModelIndex(const QModelIndex &index)
Gets the source index in case a proxy model has been used.
_MISC_FUNC int indexFromComboBox(const QComboBox *comboBox, const QVariant &value, int default_index=-1)
Gets the index from the passed data value of the passed combo box widget.
_MISC_FUNC QPair< QLayout *, int > getTopLayoutAndIndex(const QObject *object)
Gets the top level QLayout and index of the passed object which can be a QLayout out or QWidget deriv...
_MISC_FUNC std::string trimRight(std::string s, const std::string &t=" ")
Trims a passed string right and returns it.
void resizeColumnsToContents(QTreeView *treeView)
Resizes all columns to content of a tree view except the last column.
Definition qt_utils.h:383
_MISC_FUNC QRect moveRectWithinRect(const QRect &outer, const QRect &inner)
Moves the inner in to the outer rectangle.
_MISC_FUNC QStringList getObjectNamePath(const QObject *object, bool use_rtti=false)
Gets all the parent names from the object's parent in a string list.
_MISC_FUNC bool isGuiApplication()
Checks if the running QCoreApplication is a GUI application.
_MISC_FUNC QPair< int, QFormLayout::ItemRole > getLayoutPosition(const QFormLayout *layout, QObject *target)
Gets the position (row, role) from the passed target object in te form-layout.
_MISC_FUNC std::string trim(std::string s, const std::string &t=" ")
Trims a passed string at both sides and returns it.
_MISC_FUNC int getLayoutIndex(const QBoxLayout *layout, QObject *target)
Gets the index from the passed target object in te box-layout.
_MISC_FUNC bool isValidFilePath(const QString &path)
Gets if the passed filepath is a valid name.
_MISC_FUNC void expandTreeView(QTreeView *tv, bool expand=true, const QModelIndex &index={})
Expands or collapses a tree view's items.
_MISC_FUNC void storeTreeViewState(QSettings &settings, QAnyStringView name, const QTreeView *tv, bool read)
Stores or restores column widths of a QTreeView.
_MISC_FUNC bool isValidDirectoryPath(const QString &path)
Gets if the passed directory path is a valid name.
_MISC_FUNC std::string trimLeft(std::string s, const std::string &t=" ")
Trims a passed string left and returns it.
_MISC_FUNC QMetaObject::Connection connectByName(const QWidget *widget, const QString &sender_name, const char *signal_name, const QObject *receiver, const char *method_name, Qt::ConnectionType ct=Qt::AutoConnection)
Connects signals by name of sender and name of signal.
_MISC_FUNC void dumpObjectProperties(const QObject *obj)
Dumps all the object properties in qDebug().
_MISC_FUNC void storeWindowState(QSettings &settings, QAnyStringView name, QWidget *widget, bool read)
Stores or restores the window position and size from the settings file onto the passed widget.
T * getSourceModel(const QAbstractItemModel *am)
Gets the model type pointer from the passed abstract model pointer.
Definition qt_utils.h:404
_MISC_FUNC QLayout * getWidgetLayout(const QWidget *widget)
Gets the layout containing the passed widget.
QMetaEnum flagsMetaEnum()
Gets the QMetaEnum from a given in a QObject embedded Qt type.
Definition qt_utils.h:183
std::string toString(T value, int digits=0)
The function converts number to a minimal length. It produces 'digits' significant digits in either p...
QRect operator+=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:69
QSize asQSize(const QPoint &pt)
Gets the passed Qt point as a Qt size class.
Definition qt_utils.h:114
QPoint operator+(const QPoint &pt, const QSize &sz)
Allows moving a QPoint using a QSize.
Definition qt_utils.h:37
QRect & inflate(QRect &r, int sz)
Inflates the passed rect on all sides using an integer.
Definition qt_utils.h:95
std::ostream & operator<<(std::ostream &os, const QString &qs)
Operator stream a QString instance to ans std ostream.
Definition qt_utils.h:20
constexpr QRect inflated(const QRect &r, int sz)
Inflates a copy the rectangle an integer and returns it.
Definition qt_utils.h:104
QPoint operator-(const QPoint &pt, const QSize &sz)
Allows moving a QPoint using a QSize.
Definition qt_utils.h:29
QSizeF asQSizeF(const QPointF &pt)
Gets the passed Qt point as a Qt size class.
Definition qt_utils.h:124
QRect operator-=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:86