-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQtTools.cpp
67 lines (60 loc) · 1.76 KB
/
QtTools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "QtTools.h"
void QtTools::centerWidgetOnCursor(QWidget* a_widget)
{
QTimer::singleShot(0, [a_widget]() {
QPoint position = QCursor::pos() - a_widget->rect().center();
if (position.x() < 0)
position.setX(0);
if (position.y() < 0)
position.setY(0);
a_widget->move(position);
});
}
void QtTools::setTabSize(QTextEdit* a_edit, int a_size)
{
QFontMetrics metrics(a_edit->font());
a_edit->setTabStopDistance( a_size * metrics.horizontalAdvance(' ') );
}
void QtTools::setLayoutItemsEnabled(QLayout* a_layout, bool a_enabled)
{
for (int i = 0; i < a_layout->count(); i++)
{
QWidget* widget = a_layout->itemAt(i)->widget();
if (widget != nullptr)
widget->setEnabled(a_enabled);
}
}
void QtTools::setLayoutItemsVisible(QLayout* a_layout, bool a_visible)
{
for (int i = 0; i < a_layout->count(); i++)
{
QWidget* widget = a_layout->itemAt(i)->widget();
if (widget != nullptr)
widget->setVisible(a_visible);
}
}
QPoint QtTools::graphicsScenePosition(const QMouseEvent* a_event, const QGraphicsView* a_view)
{
QPoint local_pos = a_event->localPos().toPoint();
QPoint scene_pos = a_view->mapToScene(local_pos).toPoint();
return scene_pos;
}
QPoint QtTools::graphicsScenePosition(const QWheelEvent* a_event, const QGraphicsView* a_view)
{
QPoint local_pos = a_event->position().toPoint();
QPoint scene_pos = a_view->mapToScene(local_pos).toPoint();
return scene_pos;
}
QRectF QtTools::limitBySceneRect(const QGraphicsView* a_view, const QRectF& a_rect)
{
QRectF rect = a_rect;
if (a_view->scene() != nullptr)
{
QPointF scene_end = a_view->scene()->sceneRect().bottomRight();
if (rect.right() > scene_end.x())
rect.setRight ( scene_end.x());
if (rect.bottom() > scene_end.y())
rect.setBottom( scene_end.y());
}
return rect.normalized();
}