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 <QSize>
11#include <QString>
12#include <QTreeView>
13#include <misc/global.h>
14
18inline std::ostream& operator<<(std::ostream& os, const QString& qs)
19{
20 return os << qs.toStdString();
21}
22
26inline QPoint operator-(const QPoint& pt, const QSize& sz)
27{
28 return {pt.x() - sz.width(), pt.y() - sz.height()};
29}
30
34inline QPoint operator+(const QPoint& pt, const QSize& sz)
35{
36 return {pt.x() + sz.width(), pt.y() + sz.height()};
37}
38
42inline QRect operator+(const QRect& rc, const QSize& sz)
43{
44 return {rc.topLeft(), rc.size() + sz};
45}
46
50inline QRect operator-(const QRect& rc, const QSize& sz)
51{
52 return {rc.topLeft(), rc.size() - sz};
53}
54
58inline QRect operator+(const QRect& rc, const QPoint& pt)
59{
60 return {rc.topLeft() + pt, rc.size()};
61}
62
66inline QRect operator+=(QRect& rc, const QPoint& pt)
67{
68 rc = {rc.topLeft() + pt, rc.size()};
69 return rc;
70}
71
75inline QRect operator-(const QRect& rc, const QPoint& pt)
76{
77 return {rc.topLeft() - pt, rc.size()};
78}
79
83inline QRect operator-=(QRect& rc, const QPoint& pt)
84{
85 rc = {rc.topLeft() - pt, rc.size()};
86 return rc;
87}
88
92inline QRect& inflate(QRect& r, int sz)
93{
94 r.adjust(-sz, -sz, sz, sz);
95 return r;
96}
97
101constexpr QRect inflated(const QRect& r, int sz)
102{
103 return r.adjusted(-sz, -sz, sz, sz);
104}
105
111inline QSize asQSize(const QPoint& pt)
112{
113 return {pt.x(), pt.y()};
114}
115
121inline QSizeF asQSizeF(const QPointF& pt)
122{
123 return {pt.x(), pt.y()};
124}
125
126namespace sf
127{
128
134_MISC_FUNC std::string toString(const QRect& rect);
135
142_MISC_FUNC QRect moveRectWithinRect(const QRect& outer, const QRect& inner);
143
148{
149 public:
150 explicit PaletteColors() = default;
151
152 explicit PaletteColors(const QPalette& palette);
153
154 [[nodiscard]] QPalette getPalette() const;
155
156 void setColors(const QPalette& palette);
157
158 [[nodiscard]] bool isEmpty() const;
159
160 void styleFileDialog(QFileDialog& fd) const;
161
162 private:
163 typedef QPair<QPalette::ColorRole, QColor> Pair;
164 QList<Pair> _colors;
165};
166
177_MISC_FUNC QMetaObject::Connection connectByName(
178 const QWidget* widget, const QString& sender_name, const char* signal_name, const QObject* receiver, const char* method_name,
179 Qt::ConnectionType ct = Qt::AutoConnection
180);
181
188_MISC_FUNC QStringList getObjectNamePath(const QObject* object, bool use_rtti = false);
189
195_MISC_FUNC QLayout* getWidgetLayout(const QWidget* widget);
196
204template<typename T>
205static const char* enumToKey(const T value)
206{
207 return QMetaEnum::fromType<T>().valueToKey(value);
208}
209
217template<typename T>
218static T keyToEnum(const char* key, bool* ok = nullptr)
219{
220 return static_cast<T>(QMetaEnum::fromType<T>().keyToValue(key, ok));
221}
222
228template<typename T>
229static const char* enumName()
230{
231 return QMetaEnum::fromType<T>().enumName();
232}
233
240template<typename T>
241static T keyToEnum(QString key)
242{
243 return QMetaEnum::fromType<T>().keyToValue(key);
244}
245
253_MISC_FUNC int indexFromComboBox(const QComboBox* comboBox, const QVariant& value, int default_index = -1);
254
261_MISC_FUNC QPair<int, QFormLayout::ItemRole> getLayoutPosition(const QFormLayout* layout, QObject* target);
262
269_MISC_FUNC int getLayoutIndex(const QBoxLayout* layout, QObject* target);
270
275inline void resizeColumnsToContents(QTreeView* treeView)
276{
277 const auto count = treeView->model()->columnCount({}) - 1;
278 for (int i = 0; i < count; i++)
279 {
280 treeView->resizeColumnToContents(i);
281 }
282}
283
287_MISC_FUNC void dumpObjectProperties(const QObject* obj);
288
295template<typename T>
296T* getSourceModel(const QAbstractItemModel* am)
297{
298 // First check if the passed abstract model is the model we look for.
299 if (auto m = dynamic_cast<const T*>(am))
300 {
301 return const_cast<T*>(m);
302 }
303 // Secondly, when a proxy is used.
304 if (const auto apm = dynamic_cast<const QAbstractProxyModel*>(am))
305 {
306 if (auto m = dynamic_cast<T*>(apm->sourceModel()))
307 {
308 return const_cast<T*>(m);
309 }
310 }
311 return nullptr;
312}
313
317_MISC_FUNC QModelIndex getSourceModelIndex(const QModelIndex& index);
318
325_MISC_FUNC void expandTreeView(QTreeView* tv, bool expand = true, const QModelIndex& index = {});
326
332
333}// namespace sf
334
338inline std::ostream& operator<<(std::ostream& os, const QRect& rect)
339{
340 return os << sf::toString(rect);
341}
Type to hold palette colors.
Definition qt_utils.h:148
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.
void resizeColumnsToContents(QTreeView *treeView)
Resizes all columns to content of a tree view except the last column.
Definition qt_utils.h:275
_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 int getLayoutIndex(const QBoxLayout *layout, QObject *target)
Gets the index from the passed target object in te box-layout.
_MISC_FUNC void expandTreeView(QTreeView *tv, bool expand=true, const QModelIndex &index={})
Expands or collapses a tree view's items.
_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().
T * getSourceModel(const QAbstractItemModel *am)
Gets the model type pointer from the passed abstract model pointer.
Definition qt_utils.h:296
_MISC_FUNC QLayout * getWidgetLayout(const QWidget *widget)
Gets the layout containing the passed widget.
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:66
QSize asQSize(const QPoint &pt)
Gets the passed Qt point as a Qt size class.
Definition qt_utils.h:111
QPoint operator+(const QPoint &pt, const QSize &sz)
Allows moving a QPoint using a QSize.
Definition qt_utils.h:34
QRect & inflate(QRect &r, int sz)
Inflates the passed rect on all sides using an integer.
Definition qt_utils.h:92
std::ostream & operator<<(std::ostream &os, const QString &qs)
Operator stream a QString instance to ans std ostream.
Definition qt_utils.h:18
constexpr QRect inflated(const QRect &r, int sz)
Inflates a copy the rectangle an integer and returns it.
Definition qt_utils.h:101
QPoint operator-(const QPoint &pt, const QSize &sz)
Allows moving a QPoint using a QSize.
Definition qt_utils.h:26
QSizeF asQSizeF(const QPointF &pt)
Gets the passed Qt point as a Qt size class.
Definition qt_utils.h:121
QRect operator-=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:83