-
Notifications
You must be signed in to change notification settings - Fork 2
/
qcocoagradientbutton.mm
88 lines (61 loc) · 1.98 KB
/
qcocoagradientbutton.mm
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
#include "qcocoagradientbutton.h"
#include "qcocoabutton.h"
#include <QEvent>
class QCocoaGradientButtonPrivate : public QObject
{
protected:
QPointer<QCocoaGradientButton> parent;
int m_nWidthWithoutLastSegment;
public:
QCocoaGradientButtonPrivate(QCocoaGradientButton *pParent)
: QObject(pParent), parent(pParent), m_nWidthWithoutLastSegment(0)
{
}
~QCocoaGradientButtonPrivate()
{
}
friend class QCocoaGradientButton;
};
QCocoaGradientButton::QCocoaGradientButton(QWidget *parent)
: QCocoaSegmentedButton(parent), widgetToAttachedTo(0)
{
setSegmentStyle(QCocoaSegmentedButton::SegmentStyleSmallSquare);
setTrackingMode(QCocoaSegmentedButton::NSSegmentSwitchTrackingMomentary);
pimpl = new QCocoaGradientButtonPrivate(this);
}
QCocoaGradientButton::~QCocoaGradientButton()
{
}
bool QCocoaGradientButton::eventFilter(QObject *, QEvent *ev)
{
if (ev->type() == QEvent::Resize)
{
setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); // remove constraints
updateGeometry();
}
return false;
}
QSize QCocoaGradientButton::sizeHint() const
{
QSize sz = QCocoaSegmentedButton::sizeHint();
if (widgetToAttachedTo)
{
QCocoaGradientButton *p = const_cast<QCocoaGradientButton *>(this);
int parentWidth = widgetToAttachedTo->width();
sz.setWidth(parentWidth);
p->setFixedWidth(segmentCount() - 1, parentWidth - pimpl->m_nWidthWithoutLastSegment);
}
return sz;
}
// the idea is taken from
// http://stackoverflow.com/questions/22586313/nstableview-with-buttons-like-in-system-preferences-using-only-interface-bui/22586314#22586314
void QCocoaGradientButton::attachToWidget(QWidget *w)
{
Q_ASSERT(segmentCount() != 0);
pimpl->m_nWidthWithoutLastSegment = sizeHint().width() + 1; // plus border (?)
setSegmentCount(segmentCount() + 1);
setEnabled(segmentCount() - 1, false);
widgetToAttachedTo = w;
w->installEventFilter(this);
updateGeometry();
}