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 <QFileDialog>
5#include <QFormLayout>
6#include <QMetaEnum>
7#include <QPalette>
8#include <QPoint>
9#include <QRect>
10#include <QSettings>
11#include <QSize>
12#include <QString>
13#include <QTreeView>
14#include <misc/global.h>
15
19inline std::ostream& operator<<(std::ostream& os, const QString& qs)
20{
21 // return os << qs.toStdString();
22 return os << qs.toUtf8().constData();
23}
24
28inline QPoint operator-(const QPoint& pt, const QSize& sz)
29{
30 return {pt.x() - sz.width(), pt.y() - sz.height()};
31}
32
36inline QPoint operator+(const QPoint& pt, const QSize& sz)
37{
38 return {pt.x() + sz.width(), pt.y() + sz.height()};
39}
40
44inline QRect operator+(const QRect& rc, const QSize& sz)
45{
46 return {rc.topLeft(), rc.size() + sz};
47}
48
52inline QRect operator-(const QRect& rc, const QSize& sz)
53{
54 return {rc.topLeft(), rc.size() - sz};
55}
56
60inline QRect operator+(const QRect& rc, const QPoint& pt)
61{
62 return {rc.topLeft() + pt, rc.size()};
63}
64
68inline QRect operator+=(QRect& rc, const QPoint& pt)
69{
70 rc = {rc.topLeft() + pt, rc.size()};
71 return rc;
72}
73
77inline QRect operator-(const QRect& rc, const QPoint& pt)
78{
79 return {rc.topLeft() - pt, rc.size()};
80}
81
85inline QRect operator-=(QRect& rc, const QPoint& pt)
86{
87 rc = {rc.topLeft() - pt, rc.size()};
88 return rc;
89}
90
94inline QRect& inflate(QRect& r, int sz)
95{
96 r.adjust(-sz, -sz, sz, sz);
97 return r;
98}
99
103constexpr QRect inflated(const QRect& r, int sz)
104{
105 return r.adjusted(-sz, -sz, sz, sz);
106}
107
113inline QSize asQSize(const QPoint& pt)
114{
115 return {pt.x(), pt.y()};
116}
117
123inline QSizeF asQSizeF(const QPointF& pt)
124{
125 return {pt.x(), pt.y()};
126}
127
128namespace sf
129{
130
136_MISC_FUNC std::string toString(const QRect& rect);
137
144_MISC_FUNC QRect moveRectWithinRect(const QRect& outer, const QRect& inner);
145
150{
151 public:
152 explicit PaletteColors() = default;
153
154 explicit PaletteColors(const QPalette& palette);
155
156 QPalette getPalette() const;
157
158 void setColors(const QPalette& palette);
159
160 bool isEmpty() const;
161
162 void styleFileDialog(QFileDialog& fd) const;
163
164 private:
165 typedef QPair<QPalette::ColorRole, QColor> Pair;
166 QList<Pair> _colors;
167};
168
181template<typename T, typename F>
182QMetaEnum flagsMetaEnum()
183{
184 static_assert(std::is_base_of_v<QObject, T>, "Type T must be a QObject-derived class!");
185 return T::staticMetaObject.enumerator(T::staticMetaObject.indexOfEnumerator(QMetaEnum::fromType<F>().name()));
186}
187
204template<typename QObjectType, typename FlagsType>
206{
207 public:
208 using EnumType = typename FlagsType::enum_type;
209
211 {
212 public:
213 Iterator(const QMetaEnum& metaEnum, const FlagsType& flags, int index)
214 : metaEnum(metaEnum)
215 , flags(flags)
216 , index(index)
217 {
218 next();
219 }
220
222 {
223 return static_cast<EnumType>(metaEnum.value(index));
224 }
225
227 {
228 ++index;
229 next();
230 return *this;
231 }
232
233 bool operator!=(const Iterator& other) const
234 {
235 return index != other.index;
236 }
237
238 private:
239 void next()
240 {
241 while (index < metaEnum.keyCount() && !flags.testFlag(static_cast<EnumType>(metaEnum.value(index))))
242 ++index;
243 }
244
245 QMetaEnum metaEnum;
246 FlagsType flags;
247 int index;
248 };
249
250 explicit FlagsIterator(FlagsType flags)
251 : flags(flags)
252 , metaEnum(flagsMetaEnum<QObjectType, FlagsType>())
253 {}
254
256 {
257 return Iterator(metaEnum, flags, 0);
258 }
259
260 Iterator end() const
261 {
262 return Iterator(metaEnum, flags, metaEnum.keyCount());
263 }
264
265 private:
266 FlagsType flags;
267 QMetaEnum metaEnum;
268};
269
280_MISC_FUNC QMetaObject::Connection connectByName(
281 const QWidget* widget, const QString& sender_name, const char* signal_name, const QObject* receiver, const char* method_name,
282 Qt::ConnectionType ct = Qt::AutoConnection
283);
284
291_MISC_FUNC QStringList getObjectNamePath(const QObject* object, bool use_rtti = false);
292
298_MISC_FUNC QLayout* getWidgetLayout(const QWidget* widget);
299
306template<typename T>
307static const char* enumToKey(const T value)
308{
309 return QMetaEnum::fromType<T>().valueToKey(value);
310}
311
319template<typename T>
320static T keyToEnum(const char* key, bool* ok = nullptr)
321{
322 return static_cast<T>(QMetaEnum::fromType<T>().keyToValue(key, ok));
323}
324
330template<typename T>
331static const char* enumName()
332{
333 return QMetaEnum::fromType<T>().enumName();
334}
335
342template<typename T>
343static T keyToEnum(const QString& key)
344{
345 return QMetaEnum::fromType<T>().keyToValue(key);
346}
347
355_MISC_FUNC int indexFromComboBoxByData(const QComboBox* comboBox, const QVariant& value, int default_index = -1);
356
364_MISC_FUNC int indexFromComboBoxByText(const QComboBox* comboBox, const QString& value, int default_index = -1);
365
369_MISC_FUNC QPair<QLayout*, int> getTopLayoutAndIndex(const QObject* object);
370
377_MISC_FUNC QPair<int, QFormLayout::ItemRole> getLayoutPosition(const QFormLayout* layout, QObject* target);
378
385_MISC_FUNC int getLayoutIndex(const QBoxLayout* layout, QObject* target);
386
391inline void resizeColumnsToContents(QTreeView* treeView)
392{
393 const auto count = treeView->model()->columnCount({}) - 1;
394 for (int i = 0; i < count; i++)
395 {
396 treeView->resizeColumnToContents(i);
397 }
398}
399
403_MISC_FUNC void dumpObjectProperties(const QObject* obj);
404
411template<typename T>
412T* getSourceModel(const QAbstractItemModel* am)
413{
414 // First, check if the passed abstract model is the model we look for.
415 if (auto m = dynamic_cast<const T*>(am))
416 {
417 return const_cast<T*>(m);
418 }
419 // Secondly, when a proxy is used.
420 if (const auto apm = dynamic_cast<const QAbstractProxyModel*>(am))
421 {
422 if (auto m = dynamic_cast<T*>(apm->sourceModel()))
423 {
424 return const_cast<T*>(m);
425 }
426 }
427 return nullptr;
428}
429
433_MISC_FUNC QModelIndex getSourceModelIndex(const QModelIndex& index);
434
441_MISC_FUNC void expandTreeView(QTreeView* tv, bool expand = true, const QModelIndex& index = {});
442
448
455_MISC_FUNC QString trimRight(const QString& str, const QString& chars = " ");
456
463_MISC_FUNC QString trimLeft(const QString& str, const QString& chars = " ");
464
471_MISC_FUNC QString trim(const QString& str, const QString& chars = " ");
472
476_MISC_FUNC bool isValidFilePath(const QString& path);
477
481_MISC_FUNC bool isValidDirectoryPath(const QString& path);
482
486_MISC_FUNC QString modelIndexToString(const QModelIndex& index);
487
494_MISC_FUNC QModelIndex modelIndexFromString(const QString& str, const QAbstractItemModel* model);
495
503QColor _MISC_FUNC blendToward(const QColor& from, const QColor& to, float factor);
504
510void _MISC_FUNC dimPalette(QPalette& palette, float factor);
511
512}// namespace sf
513
517inline std::ostream& operator<<(std::ostream& os, const QRect& rect)
518{
519 return os << sf::toString(rect);
520}
Definition qt_utils.h:211
bool operator!=(const Iterator &other) const
Definition qt_utils.h:233
Iterator(const QMetaEnum &metaEnum, const FlagsType &flags, int index)
Definition qt_utils.h:213
EnumType operator*() const
Definition qt_utils.h:221
Iterator & operator++()
Definition qt_utils.h:226
typename FlagsType::enum_type EnumType
Definition qt_utils.h:208
FlagsIterator(FlagsType flags)
Definition qt_utils.h:250
Iterator end() const
Definition qt_utils.h:260
Iterator begin() const
Definition qt_utils.h:255
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 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 deri...
_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 the content of a tree view except the last column.
Definition qt_utils.h:391
_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 the 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 the box-layout.
_MISC_FUNC bool isValidFilePath(const QString &path)
Gets if the passed filepath is a valid name.
_MISC_FUNC QModelIndex modelIndexFromString(const QString &str, const QAbstractItemModel *model)
Converts a path back into a QModelIndex instance.
_MISC_FUNC void expandTreeView(QTreeView *tv, bool expand=true, const QModelIndex &index={})
Expands or collapses a tree view's items.
_MISC_FUNC int indexFromComboBoxByText(const QComboBox *comboBox, const QString &value, int default_index=-1)
Gets the index from the passed item text of the passed combo box widget.
_MISC_FUNC QString modelIndexToString(const QModelIndex &index)
Converts a QModelIndex instance to a QString path.
_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 int indexFromComboBoxByData(const QComboBox *comboBox, const QVariant &value, int default_index=-1)
Gets the index from the passed data value of the passed combo box widget.
T * getSourceModel(const QAbstractItemModel *am)
Gets the model type pointer from the passed abstract model pointer.
Definition qt_utils.h:412
_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:182
std::string toString(T value, int digits=0)
The function converts number to a minimal length. It produces 'digits' significant digits in either p...
void _MISC_FUNC dimPalette(QPalette &palette, float factor)
Dims the 'Disabled' color group using the 'Normal' group and blend factor.
QColor _MISC_FUNC blendToward(const QColor &from, const QColor &to, float factor)
Blends a color towards another color using a factor.
QRect operator+=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:68
QSize asQSize(const QPoint &pt)
Gets the passed Qt point as a Qt size class.
Definition qt_utils.h:113
QPoint operator+(const QPoint &pt, const QSize &sz)
Allows moving a QPoint using a QSize.
Definition qt_utils.h:36
QRect & inflate(QRect &r, int sz)
Inflates the passed rect on all sides using an integer.
Definition qt_utils.h:94
std::ostream & operator<<(std::ostream &os, const QString &qs)
Operator stream a QString instance to ans std ostream.
Definition qt_utils.h:19
constexpr QRect inflated(const QRect &r, int sz)
Inflates a copy the rectangle an integer and returns it.
Definition qt_utils.h:103
QPoint operator-(const QPoint &pt, const QSize &sz)
Allows moving a QPoint using a QSize.
Definition qt_utils.h:28
QSizeF asQSizeF(const QPointF &pt)
Gets the passed Qt point as a Qt size class.
Definition qt_utils.h:123
QRect operator-=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:85