source: smplayer/trunk/src/skingui/mediapanel.cpp@ 176

Last change on this file since 176 was 176, checked in by Silvan Scherrer, 9 years ago

smplayer: update trunk to version 16.4

File size: 9.1 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2016 Ricardo Villalba <rvm@users.sourceforge.net>
3 umplayer, Copyright (C) 2010 Ori Rejwan
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20#include "mediapanel.h"
21#include <QPainter>
22#include <QFont>
23#include <QFontMetrics>
24#include <QTimerEvent>
25#include <QGridLayout>
26#include <QLabel>
27#include <QHelpEvent>
28#include <QToolTip>
29#include <QDebug>
30#include "qpropertysetter.h"
31#include "widgetactions.h"
32#include "core.h"
33#include "config.h"
34#include "actiontools.h"
35
36
37MediaPanel::MediaPanel(QWidget *parent)
38 : QWidget(parent), duration(0)
39{
40 ui.setupUi(this);
41 setAttribute(Qt::WA_StyledBackground, true);
42 setMinimumWidth(270);
43
44 if (fontInfo().pixelSize() > 12) {
45 QFont f = font();
46 f.setPixelSize(12);
47 setFont(f);
48 }
49
50 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
51 mediaLabel = new ScrollingLabel(this);
52 resolutionLabel = new QLabel(this);
53 resolutionLabel->setObjectName("panel-resolution");
54 resolutionLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
55 repeatButton = new MyButton(this);
56 shuffleButton = new MyButton(this);
57 seeker = new PanelTimeSeeker;
58 seeker->setObjectName("panel-seeker");
59 seeker->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
60#ifdef SEEKBAR_RESOLUTION
61 seeker->setRange(0, SEEKBAR_RESOLUTION);
62#endif
63 seeker->installEventFilter(this);
64 mediaLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
65 mediaLabel->setObjectName("panel-main-label");
66 layout = new QGridLayout;
67 elapsedLabel = new QLabel(this);
68 elapsedLabel->setObjectName("panel-elapsed-label");
69 elapsedLabel->setMargin(0);
70 elapsedLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
71 elapsedLabel->setIndent(3);
72 elapsedLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
73 totalLabel = new QLabel(this);
74 totalLabel->setObjectName("panel-total-label");
75 totalLabel->setMargin(0);
76 totalLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
77 totalLabel->setIndent(3);
78 /*
79 layout->addWidget( mediaLabel, 0, 0, 1, 2 );
80 layout->addWidget( resolutionLabel, 0, 2, 1, 1 );
81 layout->addWidget( repeatButton, 0, 3 );
82 layout->addWidget( shuffleButton, 0, 4 );
83 layout->addWidget(elapsedLabel, 1, 0, 1, 1);
84 layout->addWidget(seeker, 1, 1, 1, 2);
85 layout->addWidget(totalLabel, 1, 3, 1, 2);
86 */
87 rearrangeWidgets(false);
88 layout->setSpacing(0);
89 layout->setContentsMargins(8,3,8, 3);
90 elapsedLabel->setText("00:00:00");
91 totalLabel->setText("00:00:00");
92 //resolutionLabel->setText("1920x1024");
93 //resolutionLabel->hide();
94 setLayout(layout);
95 timer = new QTimer(this);
96 timer->setSingleShot(true);
97 timer->setInterval(2000);
98 connect(timer, SIGNAL(timeout()), this, SLOT(reverseStatus()));
99 connect(seeker, SIGNAL(valueChanged(int)), this, SIGNAL(seekerChanged(int)));
100 connect(seeker, SIGNAL(wheelUp()), this, SIGNAL(seekerWheelUp()));
101 connect(seeker, SIGNAL(wheelDown()), this, SIGNAL(seekerWheelDown()));
102}
103
104MediaPanel::~MediaPanel() {
105}
106
107void MediaPanel::rearrangeWidgets(bool resolution_visible) {
108 if (resolution_visible) {
109 layout->addWidget( mediaLabel, 0, 0, 1, 2 );
110 layout->addWidget( resolutionLabel, 0, 2, 1, 1 );
111 layout->addWidget( repeatButton, 0, 3 );
112 layout->addWidget( shuffleButton, 0, 4 );
113 layout->addWidget(elapsedLabel, 1, 0, 1, 1);
114 layout->addWidget(seeker, 1, 1, 1, 2);
115 layout->addWidget(totalLabel, 1, 3, 1, 2);
116 resolutionLabel->setVisible(true);
117 } else {
118 layout->addWidget( mediaLabel, 0, 0, 1, 2 );
119 layout->addWidget( repeatButton, 0, 2 );
120 layout->addWidget( shuffleButton, 0, 3 );
121 layout->addWidget(elapsedLabel, 1, 0, 1, 1);
122 layout->addWidget(seeker, 1, 1, 1, 1);
123 layout->addWidget(totalLabel, 1, 2, 1, 2);
124 resolutionLabel->setVisible(false);
125 }
126}
127
128void MediaPanel::setResolutionVisible(bool b) {
129 rearrangeWidgets(b);
130}
131
132void MediaPanel::setScrollingEnabled(bool b) {
133 mediaLabel->setScrollingEnabled(b);
134}
135
136void MediaPanel::paintEvent(QPaintEvent * e) {
137 QPainter p(this);
138 p.drawPixmap(0,0,leftBackground.width(), 53, leftBackground);
139 p.drawPixmap(width() - rightBackground.width(), 0, rightBackground.width(), 53, rightBackground );
140 p.drawTiledPixmap(leftBackground.width(), 0, width() - leftBackground.width() - rightBackground.width(), 53, centerBackground );
141}
142
143void MediaPanel::setShuffleIcon( MyIcon icon ) {
144 shuffleButton->setMyIcon(icon);
145 shuffleButton->setFixedSize(icon.size(MyIcon::Normal, MyIcon::Off));
146}
147
148void MediaPanel::setRepeatIcon(MyIcon icon) {
149 repeatButton->setMyIcon(icon);
150 repeatButton->setFixedSize(icon.size(MyIcon::Normal, MyIcon::Off));
151}
152
153void MediaPanel::setActionCollection(QList<QAction *>actions) {
154 //ActionTools::findAction("aaa", actions);
155 SETACTIONTOBUTTON(shuffleButton, "pl_shuffle");
156 SETACTIONTOBUTTON(repeatButton, "pl_repeat");
157
158 retranslateStrings();
159}
160
161void MediaPanel::setMplayerState(int state) {
162 Core::State s = static_cast<Core::State>(state);
163 if (s == Core::Stopped) {
164 seeker->setEnabled(false);
165 }
166 else
167 if (s == Core::Paused || s == Core::Playing) {
168 seeker->setEnabled(true);
169 }
170}
171
172void MediaPanel::setDuration(int duration) {
173 this->duration = duration;
174 if (duration == 0) {
175 seeker->setState(PanelSeeker::Stopped, true);
176 }
177 else {
178 seeker->setState(PanelSeeker::Stopped, false);
179 }
180 setTotalText(Helper::formatTime(duration));
181 setElapsedText(Helper::formatTime(0));
182}
183
184void MediaPanel::setMediaLabelText(QString text) {
185 mediaLabel->setText(text);
186 mediaLabel->update();
187 originalTitle = text;
188}
189
190void MediaPanel::setResolutionLabelText(QString text) {
191 resolutionLabel->setText(text);
192}
193
194void MediaPanel::setStatusText(QString text, int time) {
195 mediaLabel->setText(text);
196 mediaLabel->update();
197 if (time > 0)
198 timer->start(time);
199 else
200 timer->stop();
201}
202
203void MediaPanel::reverseStatus() {
204 setMediaLabelText(originalTitle);
205}
206
207void MediaPanel::setBuffering(bool enable) {
208 if (enable) {
209 seeker->setState(PanelSeeker::Buffering, true);
210 }
211 else
212 {
213 seeker->setState(PanelSeeker::Buffering, false);
214 }
215}
216
217void MediaPanel::setSeeker(int v) {
218 seeker->setSliderValue(v);
219}
220
221bool MediaPanel::eventFilter(QObject *o, QEvent *e) {
222 if (o == seeker && e->type() == QEvent::ToolTip) {
223 QHelpEvent *helpEvent = static_cast<QHelpEvent *>(e);
224 qreal value = seeker->valueForPos(helpEvent->pos().x())* duration/seeker->maximum();
225 if (value >=0 && value <= duration) {
226 QToolTip::showText(helpEvent->globalPos(),Helper::formatTime(value), seeker);
227 } else {
228 QToolTip::hideText();
229 }
230 }
231 return false;
232}
233
234// Language change stuff
235void MediaPanel::changeEvent(QEvent *e) {
236 if (e->type() == QEvent::LanguageChange) {
237 retranslateStrings();
238 } else {
239 QWidget::changeEvent(e);
240 }
241}
242
243void MediaPanel::retranslateStrings() {
244 if (shuffleButton) shuffleButton->setToolTip(tr("Shuffle playlist"));
245 if (repeatButton) repeatButton->setToolTip(tr("Repeat playlist"));
246}
247
248void ScrollingLabel::paintEvent(QPaintEvent * e) {
249 QPainter p(this);
250 p.setFont(font());
251 p.setPen(palette().color(foregroundRole()));
252 p.setRenderHint(QPainter::TextAntialiasing, true);
253 QRect widgetRect = rect();
254 if (textRect.width() <= width()) {
255 p.drawText(widgetRect, Qt::AlignVCenter | Qt::AlignLeading, mText );
256 } else {
257 p.translate(-scrollPos, 0);
258 p.drawText(widgetRect.adjusted(0,0,scrollPos, 0), Qt::AlignVCenter | Qt::AlignLeading, mText);
259 p.translate(textRect.width() + gap, 0);
260 p.drawText(widgetRect.adjusted(0, 0, scrollPos - gap - textRect.width(), 0) , Qt::AlignVCenter | Qt::AlignLeading, mText);
261 }
262 p.end();
263}
264
265void ScrollingLabel::setText(QString text) {
266 mText = text;
267 updateLabel();
268 repaint();
269}
270
271void ScrollingLabel::changeEvent(QEvent * e) {
272 if (e->type() == QEvent::FontChange) {
273 updateLabel();
274 }
275}
276
277void ScrollingLabel::updateLabel() {
278 QFontMetrics fm(font());
279 QRect rect = fm.boundingRect(mText);
280 textRect = rect;
281
282 if (timerId > 0) {
283 killTimer(timerId);
284 timerId = -1;
285 scrollPos = 0;
286 }
287
288 if (scrolling_enabled) {
289 if (rect.width() > width()) {
290 timerId = startTimer(20);
291 }
292 }
293}
294
295void ScrollingLabel::timerEvent(QTimerEvent * t) {
296 scrollPos += 1;
297 scrollPos = scrollPos % (textRect.width() + gap);
298 update();
299}
300
301ScrollingLabel::ScrollingLabel(QWidget* parent ) {
302 scrollPos =0;
303 timerId = -1;
304 scrolling_enabled = false;
305 textRect = QRect();
306 setAttribute(Qt::WA_StyledBackground, true);
307 setText("SMPlayer");
308}
309
310void ScrollingLabel::setScrollingEnabled(bool b) {
311 scrolling_enabled = b;
312 updateLabel();
313 repaint();
314}
315
316void ScrollingLabel::resizeEvent(QResizeEvent *) {
317 updateLabel();
318}
319
320QSize ScrollingLabel::sizeHint() const {
321 QFontMetrics fm(font());
322 return QSize(0, fm.height());
323}
324
325#include "moc_mediapanel.cpp"
Note: See TracBrowser for help on using the repository browser.