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