1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/qt/slider.cpp
3 // Author:      Peter Most, Mariano Reingart
4 // Copyright:   (c) 2010 wxWidgets dev team
5 // Licence:     wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
7 
8 // For compilers that support precompilation, includes "wx.h".
9 #include "wx/wxprec.h"
10 
11 #include "wx/slider.h"
12 #include "wx/qt/private/converter.h"
13 #include "wx/qt/private/winevent.h"
14 
15 #include <QtWidgets/QSlider>
16 
17 class wxQtSlider : public wxQtEventSignalHandler< QSlider, wxSlider >
18 {
19 public:
20     wxQtSlider( wxWindow *parent, wxSlider *handler );
21 
22 private:
23     void valueChanged(int position);
24 };
25 
wxQtSlider(wxWindow * parent,wxSlider * handler)26 wxQtSlider::wxQtSlider( wxWindow *parent, wxSlider *handler )
27     : wxQtEventSignalHandler< QSlider, wxSlider >( parent, handler )
28 {
29     connect(this, &QSlider::valueChanged, this, &wxQtSlider::valueChanged);
30 }
31 
valueChanged(int position)32 void wxQtSlider::valueChanged(int position)
33 {
34     wxSlider *handler = GetHandler();
35     if ( handler )
36     {
37         wxScrollEvent e(wxEVT_SCROLL_CHANGED, handler->GetId(), position,
38                         wxQtConvertOrientation( orientation( ) ));
39         EmitEvent( e );
40 
41         // and also generate a command event for compatibility
42         wxCommandEvent event( wxEVT_SLIDER, handler->GetId() );
43         event.SetInt( position );
44         EmitEvent( event );
45     }
46 }
47 
48 
wxSlider()49 wxSlider::wxSlider() :
50     m_qtSlider(NULL)
51 {
52 }
53 
wxSlider(wxWindow * parent,wxWindowID id,int value,int minValue,int maxValue,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)54 wxSlider::wxSlider(wxWindow *parent,
55          wxWindowID id,
56          int value, int minValue, int maxValue,
57          const wxPoint& pos,
58          const wxSize& size,
59          long style,
60          const wxValidator& validator,
61          const wxString& name)
62 {
63     Create( parent, id, value, minValue, maxValue, pos, size, style, validator, name );
64 }
65 
Create(wxWindow * parent,wxWindowID id,int WXUNUSED (value),int minValue,int maxValue,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)66 bool wxSlider::Create(wxWindow *parent,
67             wxWindowID id,
68             int WXUNUSED(value), int minValue, int maxValue,
69             const wxPoint& pos,
70             const wxSize& size,
71             long style,
72             const wxValidator& validator,
73             const wxString& name)
74 {
75     m_qtSlider = new wxQtSlider( parent, this );
76     m_qtSlider->setOrientation( wxQtConvertOrientation( style, wxSL_HORIZONTAL ) );
77 
78     m_qtSlider->setInvertedAppearance( style & wxSL_INVERSE );
79 
80     m_qtSlider->blockSignals(true);
81     SetRange( minValue, maxValue );
82     m_qtSlider->blockSignals(false);
83     SetPageSize(wxMax(1, (maxValue - minValue) / 10));
84 
85 #if 0 // there are not normally ticks for a wxSlider
86     // draw ticks marks (default bellow if horizontal, right if vertical):
87     if ( style & wxSL_VERTICAL )
88     {
89         m_qtSlider->setTickPosition( style & wxSL_LEFT ? QSlider::TicksLeft :
90                                                          QSlider::TicksRight );
91     }
92     else // horizontal slider
93     {
94         m_qtSlider->setTickPosition( style & wxSL_TOP ? QSlider::TicksAbove :
95                                                         QSlider::TicksBelow );
96     }
97 #endif
98     return QtCreateControl( parent, id, pos, size, style, validator, name );
99 }
100 
GetValue() const101 int wxSlider::GetValue() const
102 {
103     return m_qtSlider->value();
104 }
105 
SetValue(int value)106 void wxSlider::SetValue(int value)
107 {
108     m_qtSlider->blockSignals(true);
109     m_qtSlider->setValue( value );
110     m_qtSlider->blockSignals(false);
111 }
112 
SetRange(int minValue,int maxValue)113 void wxSlider::SetRange(int minValue, int maxValue)
114 {
115     m_qtSlider->blockSignals(true);
116     m_qtSlider->setRange( minValue, maxValue );
117     m_qtSlider->blockSignals(false);
118 }
119 
GetMin() const120 int wxSlider::GetMin() const
121 {
122     return m_qtSlider->minimum();
123 }
124 
GetMax() const125 int wxSlider::GetMax() const
126 {
127     return m_qtSlider->maximum();
128 }
129 
DoSetTickFreq(int freq)130 void wxSlider::DoSetTickFreq(int freq)
131 {
132     m_qtSlider->setTickInterval(freq);
133 }
134 
GetTickFreq() const135 int wxSlider::GetTickFreq() const
136 {
137     return m_qtSlider->tickInterval();
138 }
139 
SetLineSize(int WXUNUSED (lineSize))140 void wxSlider::SetLineSize(int WXUNUSED(lineSize))
141 {
142 }
143 
SetPageSize(int pageSize)144 void wxSlider::SetPageSize(int pageSize)
145 {
146     m_qtSlider->setPageStep(pageSize);
147 }
148 
GetLineSize() const149 int wxSlider::GetLineSize() const
150 {
151     return 0;
152 }
153 
GetPageSize() const154 int wxSlider::GetPageSize() const
155 {
156     return m_qtSlider->pageStep();
157 }
158 
SetThumbLength(int WXUNUSED (lenPixels))159 void wxSlider::SetThumbLength(int WXUNUSED(lenPixels))
160 {
161 }
162 
GetThumbLength() const163 int wxSlider::GetThumbLength() const
164 {
165     return 0;
166 }
167 
168 
GetHandle() const169 QWidget *wxSlider::GetHandle() const
170 {
171     return m_qtSlider;
172 }
173 
174