blob: 9177d6cb5c7de43fe480a49ac95fc0dec8b1127b (
plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#include <QtDebug>
#include <QStackedLayout>
#include <QResizeEvent>
#include "widget/wsizeawarestack.h"
class SizeAwareLayout : public QStackedLayout
{
public:
QSize minimumSize() const
{
QSize s(0, 0) ;
QWidget *w = widget(0);
if (w) {
// Minimum Widget is at index 0;
s = w->minimumSize();
}
return s;
}
int setCurrentIndexForSize(const QSize& s) {
int n = count();
if (n <= 0) {
return -1;
}
int i = currentIndex();
QWidget *wc = widget(i);
bool notFit = false;
if (i > 0) {
// Check minimum, but not for the smallest, it is the fallback
notFit = wc->minimumHeight() > s.height() || wc->minimumWidth() > s.width();
}
if (i < n - 1 && !notFit) {
// Check maximum, but not for the biggest, it is the fallback
notFit = wc->maximumHeight() < s.height() || wc->maximumWidth() < s.width();
}
if (notFit) {
QWidget *w;
for (i = 0; i < n; ++i) {
w = widget(i);
if (w) {
if (w->maximumHeight() >= s.height() && w->maximumWidth() >= s.width() &&
w->minimumHeight() <= s.height() && w->minimumWidth() <= s.width()) {
// perfectly fit
setCurrentIndex(i);
return i;
}
}
}
// no perfect fit, check minimum only to avoid chopping
for (i = n-1; i >= 0; --i) {
w = widget(i);
if (w) {
if (w->minimumHeight() <= s.height() && w->minimumWidth() <= s.width()) {
// fit with gap
setCurrentIndex(i);
return i;
}
}
}
// fallback: take smallest
setCurrentIndex(0);
return 0;
}
return i;
}
};
WSizeAwareStack::WSizeAwareStack(QWidget* parent)
: QWidget(parent),
WBaseWidget(this) {
m_layout = new SizeAwareLayout();
setLayout(m_layout);
}
WSizeAwareStack::~WSizeAwareStack() {
}
int WSizeAwareStack::addWidget(QWidget *widget) {
// smallest widgets should be added first
return m_layout->addWidget(widget);
}
bool WSizeAwareStack::event(QEvent* pEvent) {
if (pEvent->type() == QEvent::ToolTip) {
updateTooltip();
}
return QWidget::event(pEvent);
}
void WSizeAwareStack::resizeEvent(QResizeEvent* event) {
m_layout->setCurrentIndexForSize(event->size());
}
|