-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqcocoaslider.mm
185 lines (146 loc) · 4.09 KB
/
qcocoaslider.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "qcocoaslider.h"
#import <Foundation/NSAutoreleasePool.h>
#import <AppKit/NSSlider.h>
#import <AppKit/NSSliderCell.h>
class QCocoaSliderPrivate : public QObject
{
protected:
QPointer<QCocoaSlider> parent;
NSSlider *nsSlider;
public:
QCocoaSliderPrivate(NSSlider *pControl, QCocoaSlider *pParent)
: QObject(pParent), parent(pParent), nsSlider(pControl)
{
}
~QCocoaSliderPrivate()
{
}
void valueChanged()
{
emit parent->valueChanged([nsSlider intValue]);
}
friend class QCocoaSlider;
};
@interface QCocoaButtonSliderTarget : NSObject
{
@public
QPointer<QCocoaSliderPrivate> pimpl;
}
-(IBAction)sliderDidMove:(id)sender;
@end
@implementation QCocoaButtonSliderTarget
-(IBAction)sliderDidMove:(id)sender
{
Q_UNUSED(sender);
Q_ASSERT(pimpl);
if (pimpl)
pimpl->valueChanged();
}
@end
QCocoaSlider::QCocoaSlider(QWidget * parent /*= 0*/) : QCocoaWidget(parent)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSSlider *slider = [[NSSlider alloc] init];
pimpl = new QCocoaSliderPrivate(slider, this);
QCocoaButtonSliderTarget *target = [[QCocoaButtonSliderTarget alloc] init];
target->pimpl = pimpl;
[slider setTarget:target];
[slider setAction:@selector(sliderDidMove:)];
setupLayout(slider);
[slider release];
[pool drain];
}
void QCocoaSlider::setSliderType(SliderType type /*= LinearHorizontal*/)
{
switch (type)
{
case QCocoaSlider::LinearHorizontal:
[ [pimpl->nsSlider cell] setSliderType:NSSliderTypeLinear];
setOrientation(Qt::Horizontal);
break;
case QCocoaSlider::LinearVertical:
[ [pimpl->nsSlider cell] setSliderType:NSSliderTypeLinear];
pimpl->nsSlider.vertical = YES;
setOrientation(Qt::Vertical);
break;
case QCocoaSlider::CircularSlider:
[ [pimpl->nsSlider cell] setSliderType:NSSliderTypeCircular];
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
break;
}
}
QSize QCocoaSlider::sizeHint() const
{
NSRect frame = [pimpl->nsSlider frame];
return QSize(frame.size.width, frame.size.height);
}
void QCocoaSlider::setOrientation(Qt::Orientation orientation)
{
// A slider is defined as vertical if its height is greater than its width.
if (orientation == Qt::Vertical)
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
setMaximumWidth(100);
}
else
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
}
void QCocoaSlider::setRange(int min, int max)
{
setMinimum(min);
setMaximum(max);
}
int QCocoaSlider::maximum() const
{
return [pimpl->nsSlider maxValue];
}
int QCocoaSlider::minimum() const
{
return [pimpl->nsSlider minValue];
}
void QCocoaSlider::setMaximum(int maxValue)
{
[pimpl->nsSlider setMaxValue:maxValue];
}
void QCocoaSlider::setMinimum(int minValue)
{
[pimpl->nsSlider setMinValue:minValue];
}
int QCocoaSlider::value() const
{
return [pimpl->nsSlider intValue];
}
void QCocoaSlider::setValue(int value)
{
[pimpl->nsSlider setIntValue:value];
pimpl->valueChanged();
}
void QCocoaSlider::setAltIncrementValue(int increment)
{
[pimpl->nsSlider setAltIncrementValue:increment];
}
void QCocoaSlider::setTickInterval(int ti)
{
int ticks = ceil((maximum() - minimum()) / ti) + 1;
[pimpl->nsSlider setNumberOfTickMarks: ticks];
}
void QCocoaSlider::setTickPosition(QSlider::TickPosition position)
{
switch (position)
{
case QSlider::NoTicks:
[pimpl->nsSlider setNumberOfTickMarks:0]; break;
case QSlider::TicksAbove:
[pimpl->nsSlider setTickMarkPosition:NSTickMarkPositionAbove]; break;
case QSlider::TicksBelow:
[pimpl->nsSlider setTickMarkPosition:NSTickMarkPositionBelow]; break;
case QSlider::TicksBothSides: // not supported in NSSlider
break;
}
}
void QCocoaSlider::setAllowsTickMarkValuesOnly(bool bValuesTicks)
{
[pimpl->nsSlider setAllowsTickMarkValuesOnly: (bValuesTicks ? YES : NO) ];
}