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

Last change on this file since 175 was 170, checked in by Silvan Scherrer, 11 years ago

SMPlayer: updated trunk to 14.9.0

File size: 8.9 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2014 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::paintEvent(QPaintEvent * e) {
133 QPainter p(this);
134 p.drawPixmap(0,0,leftBackground.width(), 53, leftBackground);
135 p.drawPixmap(width() - rightBackground.width(), 0, rightBackground.width(), 53, rightBackground );
136 p.drawTiledPixmap(leftBackground.width(), 0, width() - leftBackground.width() - rightBackground.width(), 53, centerBackground );
137}
138
139void MediaPanel::setShuffleIcon( MyIcon icon ) {
140 shuffleButton->setMyIcon(icon);
141 shuffleButton->setFixedSize(icon.size(MyIcon::Normal, MyIcon::Off));
142}
143
144void MediaPanel::setRepeatIcon(MyIcon icon) {
145 repeatButton->setMyIcon(icon);
146 repeatButton->setFixedSize(icon.size(MyIcon::Normal, MyIcon::Off));
147}
148
149void MediaPanel::setActionCollection(QList<QAction *>actions) {
150 //ActionTools::findAction("aaa", actions);
151 SETACTIONTOBUTTON(shuffleButton, "pl_shuffle");
152 SETACTIONTOBUTTON(repeatButton, "pl_repeat");
153
154 retranslateStrings();
155}
156
157void MediaPanel::setMplayerState(int state) {
158 Core::State s = static_cast<Core::State>(state);
159 if (s == Core::Stopped) {
160 seeker->setEnabled(false);
161 }
162 else
163 if (s == Core::Paused || s == Core::Playing) {
164 seeker->setEnabled(true);
165 }
166}
167
168void MediaPanel::setDuration(int duration) {
169 this->duration = duration;
170 if (duration == 0) {
171 seeker->setState(PanelSeeker::Stopped, true);
172 }
173 else {
174 seeker->setState(PanelSeeker::Stopped, false);
175 }
176 setTotalText(Helper::formatTime(duration));
177 setElapsedText(Helper::formatTime(0));
178}
179
180void MediaPanel::setMediaLabelText(QString text) {
181 mediaLabel->setText(text);
182 mediaLabel->update();
183 originalTitle = text;
184}
185
186void MediaPanel::setResolutionLabelText(QString text) {
187 resolutionLabel->setText(text);
188}
189
190void MediaPanel::setStatusText(QString text, int time) {
191 mediaLabel->setText(text);
192 mediaLabel->update();
193 if (time > 0)
194 timer->start(time);
195 else
196 timer->stop();
197}
198
199void MediaPanel::reverseStatus() {
200 setMediaLabelText(originalTitle);
201}
202
203void MediaPanel::setBuffering(bool enable) {
204 if (enable) {
205 seeker->setState(PanelSeeker::Buffering, true);
206 }
207 else
208 {
209 seeker->setState(PanelSeeker::Buffering, false);
210 }
211}
212
213void MediaPanel::setSeeker(int v) {
214 seeker->setSliderValue(v);
215}
216
217bool MediaPanel::eventFilter(QObject *o, QEvent *e) {
218 if (o == seeker && e->type() == QEvent::ToolTip) {
219 QHelpEvent *helpEvent = static_cast<QHelpEvent *>(e);
220 qreal value = seeker->valueForPos(helpEvent->pos().x())* duration/seeker->maximum();
221 if (value >=0 && value <= duration) {
222 QToolTip::showText(helpEvent->globalPos(),Helper::formatTime(value), seeker);
223 } else {
224 QToolTip::hideText();
225 }
226 }
227 return false;
228}
229
230// Language change stuff
231void MediaPanel::changeEvent(QEvent *e) {
232 if (e->type() == QEvent::LanguageChange) {
233 retranslateStrings();
234 } else {
235 QWidget::changeEvent(e);
236 }
237}
238
239void MediaPanel::retranslateStrings() {
240 if (shuffleButton) shuffleButton->setToolTip(tr("Shuffle playlist"));
241 if (repeatButton) repeatButton->setToolTip(tr("Repeat playlist"));
242}
243
244void ScrollingLabel::paintEvent(QPaintEvent * e) {
245 QPainter p(this);
246 p.setFont(font());
247 p.setPen(palette().color(foregroundRole()));
248 p.setRenderHint(QPainter::TextAntialiasing, true);
249 QRect widgetRect = rect();
250 if (textRect.width() <= width()) {
251 p.drawText(widgetRect, Qt::AlignVCenter | Qt::AlignLeading, mText );
252 } else {
253 p.translate(-scrollPos, 0);
254 p.drawText(widgetRect.adjusted(0,0,scrollPos, 0), Qt::AlignVCenter | Qt::AlignLeading, mText);
255 p.translate(textRect.width() + gap, 0);
256 p.drawText(widgetRect.adjusted(0, 0, scrollPos - gap - textRect.width(), 0) , Qt::AlignVCenter | Qt::AlignLeading, mText);
257 }
258 p.end();
259}
260
261void ScrollingLabel::setText(QString text) {
262 mText = text;
263 updateLabel();
264 repaint();
265}
266
267void ScrollingLabel::changeEvent(QEvent * e) {
268 if (e->type() == QEvent::FontChange) {
269 updateLabel();
270 }
271}
272
273void ScrollingLabel::updateLabel() {
274 QFontMetrics fm(font());
275 QRect rect = fm.boundingRect(mText);
276 textRect = rect;
277
278 if (timerId > 0) {
279 killTimer(timerId);
280 timerId = -1;
281 scrollPos = 0;
282 }
283
284 if (rect.width() > width()) {
285 timerId = startTimer(20);
286 }
287}
288
289void ScrollingLabel::timerEvent(QTimerEvent * t) {
290 scrollPos += 1;
291 scrollPos = scrollPos % (textRect.width() + gap);
292 update();
293}
294
295
296ScrollingLabel::ScrollingLabel(QWidget* parent ) {
297 scrollPos =0;
298 timerId = -1;
299 textRect = QRect();
300 setAttribute(Qt::WA_StyledBackground, true);
301 setText("SMPlayer");
302}
303
304void ScrollingLabel::resizeEvent(QResizeEvent *) {
305 updateLabel();
306}
307
308QSize ScrollingLabel::sizeHint() const {
309 QFontMetrics fm(font());
310 return QSize(0, fm.height());
311}
312
313#include "moc_mediapanel.cpp"
Note: See TracBrowser for help on using the repository browser.