1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2016 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "timeslider.h"
|
---|
20 | #include "helper.h"
|
---|
21 |
|
---|
22 | #include <QWheelEvent>
|
---|
23 | #include <QTimer>
|
---|
24 | #include <QToolTip>
|
---|
25 | #include <QDebug>
|
---|
26 |
|
---|
27 | #define DEBUG 0
|
---|
28 |
|
---|
29 | TimeSlider::TimeSlider( QWidget * parent ) : MySlider(parent)
|
---|
30 | , dont_update(false)
|
---|
31 | , position(0)
|
---|
32 | , total_time(0)
|
---|
33 | {
|
---|
34 | setMinimum(0);
|
---|
35 | #ifdef SEEKBAR_RESOLUTION
|
---|
36 | setMaximum(SEEKBAR_RESOLUTION);
|
---|
37 | #else
|
---|
38 | setMaximum(100);
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | setFocusPolicy( Qt::NoFocus );
|
---|
42 | setSizePolicy( QSizePolicy::Expanding , QSizePolicy::Fixed );
|
---|
43 |
|
---|
44 | connect( this, SIGNAL( sliderPressed() ), this, SLOT( stopUpdate() ) );
|
---|
45 | connect( this, SIGNAL( sliderReleased() ), this, SLOT( resumeUpdate() ) );
|
---|
46 | connect( this, SIGNAL( sliderReleased() ), this, SLOT( mouseReleased() ) );
|
---|
47 | connect( this, SIGNAL( valueChanged(int) ), this, SLOT( valueChanged_slot(int) ) );
|
---|
48 | #if ENABLE_DELAYED_DRAGGING
|
---|
49 | connect( this, SIGNAL(draggingPos(int) ), this, SLOT(checkDragging(int)) );
|
---|
50 |
|
---|
51 | last_pos_to_send = -1;
|
---|
52 | timer = new QTimer(this);
|
---|
53 | connect( timer, SIGNAL(timeout()), this, SLOT(sendDelayedPos()) );
|
---|
54 | timer->start(200);
|
---|
55 | #endif
|
---|
56 | }
|
---|
57 |
|
---|
58 | TimeSlider::~TimeSlider() {
|
---|
59 | }
|
---|
60 |
|
---|
61 | void TimeSlider::stopUpdate() {
|
---|
62 | #if DEBUG
|
---|
63 | qDebug("TimeSlider::stopUpdate");
|
---|
64 | #endif
|
---|
65 | dont_update = true;
|
---|
66 | }
|
---|
67 |
|
---|
68 | void TimeSlider::resumeUpdate() {
|
---|
69 | #if DEBUG
|
---|
70 | qDebug("TimeSlider::resumeUpdate");
|
---|
71 | #endif
|
---|
72 | dont_update = false;
|
---|
73 | }
|
---|
74 |
|
---|
75 | void TimeSlider::mouseReleased() {
|
---|
76 | #if DEBUG
|
---|
77 | qDebug("TimeSlider::mouseReleased");
|
---|
78 | #endif
|
---|
79 | emit posChanged( value() );
|
---|
80 | }
|
---|
81 |
|
---|
82 | void TimeSlider::valueChanged_slot(int v) {
|
---|
83 | #if DEBUG
|
---|
84 | qDebug("TimeSlider::changedValue_slot: %d", v);
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | // Only to make things clear:
|
---|
88 | bool dragging = dont_update;
|
---|
89 | if (!dragging) {
|
---|
90 | if (v!=position) {
|
---|
91 | #if DEBUG
|
---|
92 | qDebug(" emitting posChanged");
|
---|
93 | #endif
|
---|
94 | emit posChanged(v);
|
---|
95 | }
|
---|
96 | } else {
|
---|
97 | #if DEBUG
|
---|
98 | qDebug(" emitting draggingPos");
|
---|
99 | #endif
|
---|
100 | emit draggingPos(v);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | #if ENABLE_DELAYED_DRAGGING
|
---|
105 | void TimeSlider::setDragDelay(int d) {
|
---|
106 | qDebug("TimeSlider::setDragDelay: %d", d);
|
---|
107 | timer->setInterval(d);
|
---|
108 | }
|
---|
109 |
|
---|
110 | int TimeSlider::dragDelay() {
|
---|
111 | return timer->interval();
|
---|
112 | }
|
---|
113 |
|
---|
114 | void TimeSlider::checkDragging(int v) {
|
---|
115 | qDebug("TimeSlider::checkDragging: %d", v);
|
---|
116 | last_pos_to_send = v;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void TimeSlider::sendDelayedPos() {
|
---|
120 | if (last_pos_to_send != -1) {
|
---|
121 | qDebug("TimeSlider::sendDelayedPos: %d", last_pos_to_send);
|
---|
122 | emit delayedDraggingPos(last_pos_to_send);
|
---|
123 | last_pos_to_send = -1;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | void TimeSlider::setPos(int v) {
|
---|
129 | #if DEBUG
|
---|
130 | qDebug("TimeSlider::setPos: %d", v);
|
---|
131 | qDebug(" dont_update: %d", dont_update);
|
---|
132 | #endif
|
---|
133 |
|
---|
134 | if (v!=pos()) {
|
---|
135 | if (!dont_update) {
|
---|
136 | position = v;
|
---|
137 | setValue(v);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | int TimeSlider::pos() {
|
---|
143 | return position;
|
---|
144 | }
|
---|
145 |
|
---|
146 | void TimeSlider::wheelEvent(QWheelEvent * e) {
|
---|
147 | //e->ignore();
|
---|
148 |
|
---|
149 | qDebug("TimeSlider::wheelEvent: delta: %d", e->delta());
|
---|
150 | e->accept();
|
---|
151 |
|
---|
152 | if (e->orientation() == Qt::Vertical) {
|
---|
153 | if (e->delta() >= 0)
|
---|
154 | emit wheelUp();
|
---|
155 | else
|
---|
156 | emit wheelDown();
|
---|
157 | } else {
|
---|
158 | qDebug("Timeslider::wheelEvent: horizontal event received, doing nothing");
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | bool TimeSlider::event(QEvent *event) {
|
---|
163 | if (event->type() == QEvent::ToolTip) {
|
---|
164 | QHelpEvent * help_event = static_cast<QHelpEvent *>(event);
|
---|
165 | //qDebug() << "TimeSlider::event: total_time:" << total_time << "x:" << help_event->x();
|
---|
166 | int pos_in_slider = help_event->x() * maximum() / width();
|
---|
167 | int time = pos_in_slider * total_time / maximum();
|
---|
168 | //qDebug() << "TimeSlider::event: time:" << time;
|
---|
169 | if (time >= 0 && time <= total_time) {
|
---|
170 | QToolTip::showText(help_event->globalPos(), Helper::formatTime(time), this);
|
---|
171 | } else {
|
---|
172 | QToolTip::hideText();
|
---|
173 | event->ignore();
|
---|
174 | }
|
---|
175 | return true;
|
---|
176 | }
|
---|
177 | return QWidget::event(event);
|
---|
178 | }
|
---|
179 |
|
---|
180 | #include "moc_timeslider.cpp"
|
---|