1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2013 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 |
|
---|
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 | repeatButton = new MyButton(this);
|
---|
53 | shuffleButton = new MyButton(this);
|
---|
54 | seeker = new PanelSeeker;
|
---|
55 | seeker->setObjectName("panel-seeker");
|
---|
56 | seeker->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
|
---|
57 | #ifdef SEEKBAR_RESOLUTION
|
---|
58 | seeker->setRange(0, SEEKBAR_RESOLUTION);
|
---|
59 | #endif
|
---|
60 | seeker->installEventFilter(this);
|
---|
61 | mediaLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
---|
62 | mediaLabel->setObjectName("panel-main-label");
|
---|
63 | QGridLayout* layout = new QGridLayout;
|
---|
64 | elapsedLabel = new QLabel(this);
|
---|
65 | elapsedLabel->setObjectName("panel-elapsed-label");
|
---|
66 | elapsedLabel->setMargin(0);
|
---|
67 | elapsedLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
|
---|
68 | elapsedLabel->setIndent(3);
|
---|
69 | totalLabel = new QLabel(this);
|
---|
70 | totalLabel->setObjectName("panel-total-label");
|
---|
71 | totalLabel->setMargin(0);
|
---|
72 | totalLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
|
---|
73 | totalLabel->setIndent(3);
|
---|
74 | layout->addWidget( mediaLabel, 0, 0, 1, 2 );
|
---|
75 | layout->addWidget( repeatButton, 0, 2 );
|
---|
76 | layout->addWidget( shuffleButton, 0, 3 );
|
---|
77 | layout->addWidget(elapsedLabel, 1, 0, 1, 1);
|
---|
78 | layout->addWidget(seeker, 1, 1, 1, 1);
|
---|
79 | layout->addWidget(totalLabel, 1, 2, 1, 2);
|
---|
80 | layout->setSpacing(0);
|
---|
81 | layout->setContentsMargins(8,3,8, 3);
|
---|
82 | elapsedLabel->setText("00:00:00");
|
---|
83 | totalLabel->setText("00:00:00");
|
---|
84 | setLayout(layout);
|
---|
85 | timer = new QTimer(this);
|
---|
86 | timer->setSingleShot(true);
|
---|
87 | timer->setInterval(2000);
|
---|
88 | connect(timer, SIGNAL(timeout()), this, SLOT(reverseStatus()));
|
---|
89 | connect(seeker, SIGNAL(valueChanged(int)), this, SIGNAL(seekerChanged(int)));
|
---|
90 | }
|
---|
91 |
|
---|
92 | MediaPanel::~MediaPanel() {
|
---|
93 | }
|
---|
94 |
|
---|
95 | void MediaPanel::paintEvent(QPaintEvent * e) {
|
---|
96 | QPainter p(this);
|
---|
97 | p.drawPixmap(0,0,leftBackground.width(), 53, leftBackground);
|
---|
98 | p.drawPixmap(width() - rightBackground.width(), 0, rightBackground.width(), 53, rightBackground );
|
---|
99 | p.drawTiledPixmap(leftBackground.width(), 0, width() - leftBackground.width() - rightBackground.width(), 53, centerBackground );
|
---|
100 | }
|
---|
101 |
|
---|
102 | void MediaPanel::setShuffleIcon( MyIcon icon ) {
|
---|
103 | shuffleButton->setMyIcon(icon);
|
---|
104 | shuffleButton->setFixedSize(icon.size(MyIcon::Normal, MyIcon::Off));
|
---|
105 | }
|
---|
106 |
|
---|
107 | void MediaPanel::setRepeatIcon(MyIcon icon) {
|
---|
108 | repeatButton->setMyIcon(icon);
|
---|
109 | repeatButton->setFixedSize(icon.size(MyIcon::Normal, MyIcon::Off));
|
---|
110 | }
|
---|
111 |
|
---|
112 | void MediaPanel::setActionCollection(QList<QAction *>actions) {
|
---|
113 | //ActionTools::findAction("aaa", actions);
|
---|
114 | SETACTIONTOBUTTON(shuffleButton, "pl_shuffle");
|
---|
115 | SETACTIONTOBUTTON(repeatButton, "pl_repeat");
|
---|
116 |
|
---|
117 | retranslateStrings();
|
---|
118 | }
|
---|
119 |
|
---|
120 | void MediaPanel::setMplayerState(int state) {
|
---|
121 | Core::State s = static_cast<Core::State>(state);
|
---|
122 | if (s == Core::Stopped) {
|
---|
123 | seeker->setEnabled(false);
|
---|
124 | }
|
---|
125 | else
|
---|
126 | if (s == Core::Paused || s == Core::Playing) {
|
---|
127 | seeker->setEnabled(true);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | void MediaPanel::setDuration(int duration) {
|
---|
132 | this->duration = duration;
|
---|
133 | if (duration == 0) {
|
---|
134 | seeker->setState(PanelSeeker::Stopped, true);
|
---|
135 | }
|
---|
136 | else {
|
---|
137 | seeker->setState(PanelSeeker::Stopped, false);
|
---|
138 | }
|
---|
139 | setTotalText(Helper::formatTime(duration));
|
---|
140 | setElapsedText(Helper::formatTime(0));
|
---|
141 | }
|
---|
142 |
|
---|
143 | void MediaPanel::setMediaLabelText(QString text) {
|
---|
144 | mediaLabel->setText(text);
|
---|
145 | mediaLabel->update();
|
---|
146 | originalTitle = text;
|
---|
147 | }
|
---|
148 |
|
---|
149 | void MediaPanel::setStatusText(QString text, int time) {
|
---|
150 | mediaLabel->setText(text);
|
---|
151 | mediaLabel->update();
|
---|
152 | if (time > 0)
|
---|
153 | timer->start(time);
|
---|
154 | else
|
---|
155 | timer->stop();
|
---|
156 | }
|
---|
157 |
|
---|
158 | void MediaPanel::reverseStatus() {
|
---|
159 | setMediaLabelText(originalTitle);
|
---|
160 | }
|
---|
161 |
|
---|
162 | void MediaPanel::setBuffering(bool enable) {
|
---|
163 | if (enable) {
|
---|
164 | seeker->setState(PanelSeeker::Buffering, true);
|
---|
165 | }
|
---|
166 | else
|
---|
167 | {
|
---|
168 | seeker->setState(PanelSeeker::Buffering, false);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | void MediaPanel::setSeeker(int v) {
|
---|
173 | seeker->setSliderValue(v);
|
---|
174 | }
|
---|
175 |
|
---|
176 | bool MediaPanel::eventFilter(QObject *o, QEvent *e) {
|
---|
177 | if (o == seeker && e->type() == QEvent::ToolTip) {
|
---|
178 | QHelpEvent *helpEvent = static_cast<QHelpEvent *>(e);
|
---|
179 | qreal value = seeker->valueForPos(helpEvent->pos().x())* duration/seeker->maximum();
|
---|
180 | if (value >=0 && value <= duration) {
|
---|
181 | QToolTip::showText(helpEvent->globalPos(),Helper::formatTime(value), seeker);
|
---|
182 | } else {
|
---|
183 | QToolTip::hideText();
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return false;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // Language change stuff
|
---|
190 | void MediaPanel::changeEvent(QEvent *e) {
|
---|
191 | if (e->type() == QEvent::LanguageChange) {
|
---|
192 | retranslateStrings();
|
---|
193 | } else {
|
---|
194 | QWidget::changeEvent(e);
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | void MediaPanel::retranslateStrings() {
|
---|
199 | if (shuffleButton) shuffleButton->setToolTip(tr("Shuffle playlist"));
|
---|
200 | if (repeatButton) repeatButton->setToolTip(tr("Repeat playlist"));
|
---|
201 | }
|
---|
202 |
|
---|
203 | void ScrollingLabel::paintEvent(QPaintEvent * e) {
|
---|
204 | QPainter p(this);
|
---|
205 | p.setFont(font());
|
---|
206 | p.setPen(palette().color(foregroundRole()));
|
---|
207 | p.setRenderHint(QPainter::TextAntialiasing, true);
|
---|
208 | QRect widgetRect = rect();
|
---|
209 | if (textRect.width() <= width()) {
|
---|
210 | p.drawText(widgetRect, Qt::AlignVCenter | Qt::AlignLeading, mText );
|
---|
211 | } else {
|
---|
212 | p.translate(-scrollPos, 0);
|
---|
213 | p.drawText(widgetRect.adjusted(0,0,scrollPos, 0), Qt::AlignVCenter | Qt::AlignLeading, mText);
|
---|
214 | p.translate(textRect.width() + gap, 0);
|
---|
215 | p.drawText(widgetRect.adjusted(0, 0, scrollPos - gap - textRect.width(), 0) , Qt::AlignVCenter | Qt::AlignLeading, mText);
|
---|
216 | }
|
---|
217 | p.end();
|
---|
218 | }
|
---|
219 |
|
---|
220 | void ScrollingLabel::setText(QString text) {
|
---|
221 | mText = text;
|
---|
222 | updateLabel();
|
---|
223 | repaint();
|
---|
224 | }
|
---|
225 |
|
---|
226 | void ScrollingLabel::changeEvent(QEvent * e) {
|
---|
227 | if (e->type() == QEvent::FontChange) {
|
---|
228 | updateLabel();
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | void ScrollingLabel::updateLabel() {
|
---|
233 | QFontMetrics fm(font());
|
---|
234 | QRect rect = fm.boundingRect(mText);
|
---|
235 | textRect = rect;
|
---|
236 |
|
---|
237 | if (timerId > 0) {
|
---|
238 | killTimer(timerId);
|
---|
239 | timerId = -1;
|
---|
240 | scrollPos = 0;
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (rect.width() > width()) {
|
---|
244 | timerId = startTimer(20);
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | void ScrollingLabel::timerEvent(QTimerEvent * t) {
|
---|
249 | scrollPos += 1;
|
---|
250 | scrollPos = scrollPos % (textRect.width() + gap);
|
---|
251 | update();
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | ScrollingLabel::ScrollingLabel(QWidget* parent ) {
|
---|
256 | scrollPos =0;
|
---|
257 | timerId = -1;
|
---|
258 | textRect = QRect();
|
---|
259 | setAttribute(Qt::WA_StyledBackground, true);
|
---|
260 | setText("SMPlayer");
|
---|
261 | }
|
---|
262 |
|
---|
263 | void ScrollingLabel::resizeEvent(QResizeEvent *) {
|
---|
264 | updateLabel();
|
---|
265 | }
|
---|
266 |
|
---|
267 | QSize ScrollingLabel::sizeHint() const {
|
---|
268 | QFontMetrics fm(font());
|
---|
269 | return QSize(0, fm.height());
|
---|
270 | }
|
---|
271 |
|
---|
272 | #include "moc_mediapanel.cpp"
|
---|