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

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

SMPlayer: update trunk to latest 0.8.7

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