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 "playcontrol.h"
21 #include <QResizeEvent>
22 #include "myaction.h"
23 #include "actiontools.h"
24
PlayControl(QWidget * parent)25 PlayControl::PlayControl(QWidget *parent) :
26 QWidget(parent), playOrPause(true)
27 {
28 setAttribute(Qt::WA_StyledBackground, true);
29 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
30 backwardButton = new MyButton(this);
31 previousButton = new MyButton(this);
32 playPauseButton = new MyButton(this);
33 stopButton = new MyButton(this);
34 nextButton = new MyButton(this);
35 forwardButton = new MyButton(this);
36 recordButton = new MyButton(this);
37 layout = new QHBoxLayout;
38 QSpacerItem* spacer1 = new QSpacerItem(9, 10, QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
39 QSpacerItem* spacer2 = new QSpacerItem(9, 10, QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
40 layout->addSpacerItem(spacer1);
41 layout->addWidget(previousButton);
42 layout->addWidget(backwardButton);
43 layout->addWidget(playPauseButton);
44 layout->addWidget(stopButton);
45 layout->addWidget(recordButton);
46 layout->addWidget(forwardButton);
47 layout->addWidget(nextButton);
48 layout->addSpacerItem(spacer2);
49 layout->setSpacing(0);
50 layout->setContentsMargins( 0, 0, 0, 0);
51 setRecordEnabled(false);
52 setLayout(layout);
53 recordButton->installEventFilter(this);
54 nextButton->installEventFilter(this);
55 previousButton->installEventFilter(this);
56 }
57
58
resizeEvent(QResizeEvent * e)59 void PlayControl::resizeEvent(QResizeEvent *e)
60 {
61 Q_UNUSED(e);
62 updateSize();
63 }
64
updateSize()65 void PlayControl::updateSize()
66 {
67 backwardButton->hide();
68 previousButton->hide();
69 playPauseButton->hide();
70 stopButton->hide();
71 recordButton->hide();
72 nextButton->hide();
73 forwardButton->hide();
74 int totalWidth = 18;
75 totalWidth += playPauseButton->minimumWidth();
76 playPauseButton->show();
77 if(recordButton->isEnabled())
78 {
79 totalWidth += recordButton->minimumWidth();
80 recordButton->show();
81 }
82 else
83 {
84 recordButton->hide();
85 }
86 if(nextButton->isEnabled())
87 {
88 totalWidth += nextButton->minimumWidth();
89 if(width() < totalWidth ) { nextButton->hide(); return; }
90 else nextButton->show();
91 }
92 else
93 {
94 nextButton->hide();
95 }
96
97 if(previousButton->isEnabled())
98 {
99 totalWidth += previousButton->minimumWidth();
100 if(width() < totalWidth ) { previousButton->hide(); return; }
101 else previousButton->show();
102 }
103 else
104 {
105 previousButton->hide();
106 }
107 totalWidth += stopButton->minimumWidth();
108 if(width() < totalWidth) { stopButton->hide(); return; }
109 else stopButton->show();
110 totalWidth += forwardButton->minimumWidth();
111 if(width() < totalWidth) { forwardButton->hide(); return; }
112 else forwardButton->show();
113 totalWidth += backwardButton->minimumWidth();
114 if(width() < totalWidth) { backwardButton->hide(); return; }
115 else backwardButton->show();
116 if(!nextButton->isEnabled()) totalWidth += nextButton->minimumWidth();
117 if(width() < totalWidth) { nextButton->hide(); return; }
118 else nextButton->show();
119 if(!previousButton->isEnabled()) totalWidth += previousButton->minimumWidth();
120 if(width() < totalWidth) { previousButton->hide(); return; }
121 else previousButton->show();
122
123 }
124
updateWidths()125 void PlayControl::updateWidths()
126 {
127 int maxWidth = 18;
128 int totalWidth = 18;
129 totalWidth += playPauseButton->minimumWidth();
130 if(recordButton->isEnabled())
131 {
132 totalWidth += recordButton->minimumWidth();
133 }
134 setMinimumWidth(totalWidth);
135 maxWidth += backwardButton->minimumWidth();
136 maxWidth += previousButton->minimumWidth();
137 maxWidth += playPauseButton->minimumWidth();
138 maxWidth += stopButton->minimumWidth();
139 maxWidth += nextButton->minimumWidth();
140 maxWidth += recordButton->minimumWidth();
141 maxWidth += forwardButton->minimumWidth();
142 if(recordButton->isEnabled())
143 setMaximumWidth(maxWidth);
144 else
145 setMaximumWidth(maxWidth - recordButton->minimumWidth());
146 updateSize();
147 }
148
setActionCollection(QList<QAction * > actions)149 void PlayControl::setActionCollection(QList<QAction *> actions)
150 {
151 /*
152 MyAction *a = static_cast<MyAction*>(actions.at(2));
153 qDebug("*** action: %s", a->objectName().toLatin1().constData());
154 */
155
156 SETACTIONTOBUTTON(backwardButton, "rewind1");
157 SETACTIONTOBUTTON(previousButton, "play_prev");
158 SETACTIONTOBUTTON(playPauseButton, "play_or_pause");
159 SETACTIONTOBUTTON(stopButton, "stop");
160 SETACTIONTOBUTTON(recordButton, "record");
161 SETACTIONTOBUTTON(nextButton, "play_next");
162 SETACTIONTOBUTTON(forwardButton, "forward1");
163
164 retranslateStrings();
165 }
166
eventFilter(QObject * watched,QEvent * event)167 bool PlayControl::eventFilter(QObject *watched, QEvent *event)
168 {
169 if((watched == recordButton || watched == previousButton || watched == nextButton ) && event->type() == QEvent::EnabledChange)
170 {
171 updateWidths();
172 }
173 return false;
174 }
175
176 // Language change stuff
changeEvent(QEvent * e)177 void PlayControl::changeEvent(QEvent *e) {
178 if (e->type() == QEvent::LanguageChange) {
179 retranslateStrings();
180 } else {
181 QWidget::changeEvent(e);
182 }
183 }
184
retranslateStrings()185 void PlayControl::retranslateStrings() {
186 if (backwardButton) backwardButton->setToolTip(tr("Rewind"));
187 if (forwardButton) forwardButton->setToolTip(tr("Forward"));
188 if (playPauseButton) playPauseButton->setToolTip(tr("Play / Pause"));
189 if (stopButton) stopButton->setToolTip(tr("Stop"));
190 if (recordButton) recordButton->setToolTip(tr("Record"));
191 if (nextButton) nextButton->setToolTip(tr("Next file in playlist"));
192 if (previousButton) previousButton->setToolTip(tr("Previous file in playlist"));
193 }
194
195 #include "moc_playcontrol.cpp"
196