source: smplayer/trunk/src/baseguiplus.cpp@ 167

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

SMPlayer: update trunk to latest 0.8.7

  • Property svn:eol-style set to LF
File size: 17.9 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "baseguiplus.h"
20#include "config.h"
21#include "myaction.h"
22#include "global.h"
23#include "images.h"
24#include "playlist.h"
25
26#ifdef Q_OS_WIN
27#include "favorites.h"
28#else
29#include "tvlist.h"
30#endif
31
32#include "widgetactions.h"
33
34#include <QMenu>
35#include <QCloseEvent>
36#include <QApplication>
37#include <QDesktopWidget>
38
39#if DOCK_PLAYLIST
40#include <QDockWidget>
41#include "playlistdock.h"
42#include "desktopinfo.h"
43
44#define PLAYLIST_ON_SIDES 1
45#endif
46
47using namespace Global;
48
49BaseGuiPlus::BaseGuiPlus( QWidget * parent, Qt::WindowFlags flags)
50 : BaseGui( parent, flags )
51{
52 // Initialize variables
53 mainwindow_visible = true;
54 //infowindow_visible = false;
55 trayicon_playlist_was_visible = false;
56 widgets_size = 0;
57#if DOCK_PLAYLIST
58 fullscreen_playlist_was_visible = false;
59 fullscreen_playlist_was_floating = false;
60 compact_playlist_was_visible = false;
61 ignore_playlist_events = false;
62#endif
63
64 mainwindow_pos = pos();
65
66 tray = new QSystemTrayIcon( Images::icon("logo", 22), this );
67
68 tray->setToolTip( "SMPlayer" );
69 connect( tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
70 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
71
72 quitAct = new MyAction(QKeySequence("Ctrl+Q"), this, "quit");
73 connect( quitAct, SIGNAL(triggered()), this, SLOT(quit()) );
74 openMenu->addAction(quitAct);
75
76 showTrayAct = new MyAction(this, "show_tray_icon" );
77 showTrayAct->setCheckable(true);
78 connect( showTrayAct, SIGNAL(toggled(bool)),
79 tray, SLOT(setVisible(bool)) );
80
81#ifndef Q_OS_OS2
82 optionsMenu->addAction(showTrayAct);
83#else
84 trayAvailable();
85 connect( optionsMenu, SIGNAL(aboutToShow()),
86 this, SLOT(trayAvailable()) );
87#endif
88
89 showAllAct = new MyAction(this, "restore/hide");
90 connect( showAllAct, SIGNAL(triggered()),
91 this, SLOT(toggleShowAll()) );
92
93
94 context_menu = new QMenu(this);
95 context_menu->addAction(showAllAct);
96 context_menu->addSeparator();
97 context_menu->addAction(openFileAct);
98 context_menu->addMenu(recentfiles_menu);
99 context_menu->addAction(openDirectoryAct);
100 context_menu->addAction(openDVDAct);
101 context_menu->addAction(openURLAct);
102 context_menu->addMenu(favorites);
103#ifndef Q_OS_WIN
104 context_menu->addMenu(tvlist);
105 context_menu->addMenu(radiolist);
106#endif
107 context_menu->addSeparator();
108 context_menu->addAction(playOrPauseAct);
109 context_menu->addAction(stopAct);
110 context_menu->addSeparator();
111 context_menu->addAction(playPrevAct);
112 context_menu->addAction(playNextAct);
113 context_menu->addSeparator();
114 context_menu->addAction(showPlaylistAct);
115 context_menu->addAction(showPreferencesAct);
116 context_menu->addSeparator();
117 context_menu->addAction(quitAct);
118
119 tray->setContextMenu( context_menu );
120
121#if DOCK_PLAYLIST
122 // Playlistdock
123 playlistdock = new PlaylistDock(this);
124 playlistdock->setObjectName("playlistdock");
125 playlistdock->setFloating(false); // To avoid that the playlist is visible for a moment
126 playlistdock->setWidget(playlist);
127 playlistdock->setAllowedAreas(Qt::TopDockWidgetArea |
128 Qt::BottomDockWidgetArea
129#if PLAYLIST_ON_SIDES
130 | Qt::LeftDockWidgetArea |
131 Qt::RightDockWidgetArea
132#endif
133 );
134 addDockWidget(Qt::BottomDockWidgetArea, playlistdock);
135 playlistdock->hide();
136 playlistdock->setFloating(true); // Floating by default
137
138 connect( playlistdock, SIGNAL(closed()), this, SLOT(playlistClosed()) );
139#if USE_DOCK_TOPLEVEL_EVENT
140 connect( playlistdock, SIGNAL(topLevelChanged(bool)),
141 this, SLOT(dockTopLevelChanged(bool)) );
142#else
143 connect( playlistdock, SIGNAL(visibilityChanged(bool)),
144 this, SLOT(dockVisibilityChanged(bool)) );
145#endif // USE_DOCK_TOPLEVEL_EVENT
146
147 connect(this, SIGNAL(openFileRequested()), this, SLOT(showAll()));
148
149 ignore_playlist_events = false;
150#endif // DOCK_PLAYLIST
151
152 retranslateStrings();
153
154 loadConfig();
155}
156
157BaseGuiPlus::~BaseGuiPlus() {
158 saveConfig();
159}
160
161bool BaseGuiPlus::startHidden() {
162#if defined(Q_OS_WIN) || defined(Q_OS_OS2)
163 return false;
164#else
165 if ( (!showTrayAct->isChecked()) || (mainwindow_visible) )
166 return false;
167 else
168 return true;
169#endif
170}
171
172void BaseGuiPlus::closeEvent( QCloseEvent * e ) {
173 qDebug("BaseGuiPlus::closeEvent");
174 e->ignore();
175 closeWindow();
176}
177
178void BaseGuiPlus::closeWindow() {
179 qDebug("BaseGuiPlus::closeWindow");
180
181 if (tray->isVisible()) {
182 //e->ignore();
183 exitFullscreen();
184 showAll(false); // Hide windows
185 if (core->state() == Core::Playing) core->stop();
186
187 if (pref->balloon_count > 0) {
188 tray->showMessage( "SMPlayer",
189 tr("SMPlayer is still running here"),
190 QSystemTrayIcon::Information, 3000 );
191 pref->balloon_count--;
192 }
193
194 } else {
195 BaseGui::closeWindow();
196 }
197 //tray->hide();
198
199}
200
201void BaseGuiPlus::quit() {
202 qDebug("BaseGuiPlus::quit");
203 BaseGui::closeWindow();
204}
205
206void BaseGuiPlus::retranslateStrings() {
207 BaseGui::retranslateStrings();
208
209 quitAct->change( Images::icon("exit"), tr("&Quit") );
210 showTrayAct->change( Images::icon("systray"), tr("S&how icon in system tray") );
211
212 updateShowAllAct();
213
214#if DOCK_PLAYLIST
215 playlistdock->setWindowTitle( tr("Playlist") );
216#endif
217}
218
219void BaseGuiPlus::updateShowAllAct() {
220 if (isVisible())
221 showAllAct->change( tr("&Hide") );
222 else
223 showAllAct->change( tr("&Restore") );
224}
225
226void BaseGuiPlus::saveConfig() {
227 qDebug("BaseGuiPlus::saveConfig");
228
229 QSettings * set = settings;
230
231 set->beginGroup( "base_gui_plus");
232
233 set->setValue( "show_tray_icon", showTrayAct->isChecked() );
234 set->setValue( "mainwindow_visible", isVisible() );
235
236 set->setValue( "trayicon_playlist_was_visible", trayicon_playlist_was_visible );
237 set->setValue( "widgets_size", widgets_size );
238#if DOCK_PLAYLIST
239 set->setValue( "fullscreen_playlist_was_visible", fullscreen_playlist_was_visible );
240 set->setValue( "fullscreen_playlist_was_floating", fullscreen_playlist_was_floating );
241 set->setValue( "compact_playlist_was_visible", compact_playlist_was_visible );
242 set->setValue( "ignore_playlist_events", ignore_playlist_events );
243#endif
244
245/*
246#if DOCK_PLAYLIST
247 set->setValue( "playlist_and_toolbars_state", saveState() );
248#endif
249*/
250
251 set->endGroup();
252}
253
254void BaseGuiPlus::loadConfig() {
255 qDebug("BaseGuiPlus::loadConfig");
256
257 QSettings * set = settings;
258
259 set->beginGroup( "base_gui_plus");
260
261 bool show_tray_icon = set->value( "show_tray_icon", false).toBool();
262 showTrayAct->setChecked( show_tray_icon );
263 //tray->setVisible( show_tray_icon );
264
265 mainwindow_visible = set->value("mainwindow_visible", true).toBool();
266
267 trayicon_playlist_was_visible = set->value( "trayicon_playlist_was_visible", trayicon_playlist_was_visible ).toBool();
268 widgets_size = set->value( "widgets_size", widgets_size ).toInt();
269#if DOCK_PLAYLIST
270 fullscreen_playlist_was_visible = set->value( "fullscreen_playlist_was_visible", fullscreen_playlist_was_visible ).toBool();
271 fullscreen_playlist_was_floating = set->value( "fullscreen_playlist_was_floating", fullscreen_playlist_was_floating ).toBool();
272 compact_playlist_was_visible = set->value( "compact_playlist_was_visible", compact_playlist_was_visible ).toBool();
273 ignore_playlist_events = set->value( "ignore_playlist_events", ignore_playlist_events ).toBool();
274#endif
275
276/*
277#if DOCK_PLAYLIST
278 restoreState( set->value( "playlist_and_toolbars_state" ).toByteArray() );
279#endif
280*/
281
282 set->endGroup();
283
284 updateShowAllAct();
285}
286
287
288void BaseGuiPlus::trayIconActivated(QSystemTrayIcon::ActivationReason reason) {
289 qDebug("DefaultGui::trayIconActivated: %d", reason);
290
291 updateShowAllAct();
292
293 if (reason == QSystemTrayIcon::Trigger) {
294 toggleShowAll();
295 }
296 else
297 if (reason == QSystemTrayIcon::MiddleClick) {
298 core->pause();
299 }
300}
301
302void BaseGuiPlus::toggleShowAll() {
303 // Ignore if tray is not visible
304 if (tray->isVisible()) {
305 showAll( !isVisible() );
306 }
307}
308
309void BaseGuiPlus::showAll() {
310 if (!isVisible()) showAll(true);
311}
312
313void BaseGuiPlus::showAll(bool b) {
314 if (!b) {
315 // Hide all
316#if DOCK_PLAYLIST
317 trayicon_playlist_was_visible = (playlistdock->isVisible() &&
318 playlistdock->isFloating() );
319 if (trayicon_playlist_was_visible)
320 playlistdock->hide();
321
322 /*
323 trayicon_playlist_was_visible = playlistdock->isVisible();
324 playlistdock->hide();
325 */
326#else
327 trayicon_playlist_was_visible = playlist->isVisible();
328 playlist_pos = playlist->pos();
329 playlist->hide();
330#endif
331
332 mainwindow_pos = pos();
333 hide();
334
335 /*
336 infowindow_visible = info_window->isVisible();
337 infowindow_pos = info_window->pos();
338 info_window->hide();
339 */
340 } else {
341 // Show all
342 move(mainwindow_pos);
343 show();
344
345#if DOCK_PLAYLIST
346 if (trayicon_playlist_was_visible) {
347 playlistdock->show();
348 }
349#else
350 if (trayicon_playlist_was_visible) {
351 playlist->move(playlist_pos);
352 playlist->show();
353 }
354#endif
355
356 /*
357 if (infowindow_visible) {
358 info_window->show();
359 info_window->move(infowindow_pos);
360 }
361 */
362 }
363 updateShowAllAct();
364}
365
366void BaseGuiPlus::resizeWindow(int w, int h) {
367 qDebug("BaseGuiPlus::resizeWindow: %d, %d", w, h);
368
369 if ( (tray->isVisible()) && (!isVisible()) ) showAll(true);
370
371 BaseGui::resizeWindow(w, h );
372}
373
374void BaseGuiPlus::updateMediaInfo() {
375 qDebug("BaseGuiPlus::updateMediaInfo");
376 BaseGui::updateMediaInfo();
377
378 tray->setToolTip( windowTitle() );
379}
380
381void BaseGuiPlus::setWindowCaption(const QString & title) {
382 tray->setToolTip( title );
383
384 BaseGui::setWindowCaption( title );
385}
386
387
388// Playlist stuff
389void BaseGuiPlus::aboutToEnterFullscreen() {
390 qDebug("BaseGuiPlus::aboutToEnterFullscreen");
391
392 BaseGui::aboutToEnterFullscreen();
393
394#if DOCK_PLAYLIST
395 playlistdock->setAllowedAreas(Qt::NoDockWidgetArea);
396
397 int playlist_screen = QApplication::desktop()->screenNumber(playlistdock);
398 int mainwindow_screen = QApplication::desktop()->screenNumber(this);
399 qDebug("BaseGuiPlus::aboutToEnterFullscreen: mainwindow screen: %d, playlist screen: %d", mainwindow_screen, playlist_screen);
400
401 fullscreen_playlist_was_visible = playlistdock->isVisible();
402 fullscreen_playlist_was_floating = playlistdock->isFloating();
403
404 ignore_playlist_events = true;
405
406 // Hide the playlist if it's in the same screen as the main window
407 if ((playlist_screen == mainwindow_screen) /* ||
408 (!fullscreen_playlist_was_floating) */ )
409 {
410 playlistdock->setFloating(true);
411 playlistdock->hide();
412 }
413#endif
414}
415
416void BaseGuiPlus::aboutToExitFullscreen() {
417 qDebug("BaseGuiPlus::aboutToExitFullscreen");
418
419 BaseGui::aboutToExitFullscreen();
420
421#if DOCK_PLAYLIST
422 playlistdock->setAllowedAreas(Qt::TopDockWidgetArea |
423 Qt::BottomDockWidgetArea
424 #if PLAYLIST_ON_SIDES
425 | Qt::LeftDockWidgetArea |
426 Qt::RightDockWidgetArea
427 #endif
428 );
429
430 if (fullscreen_playlist_was_visible) {
431 playlistdock->show();
432 }
433 playlistdock->setFloating( fullscreen_playlist_was_floating );
434 ignore_playlist_events = false;
435#endif
436}
437
438void BaseGuiPlus::aboutToEnterCompactMode() {
439#if DOCK_PLAYLIST
440 compact_playlist_was_visible = (playlistdock->isVisible() &&
441 !playlistdock->isFloating());
442 if (compact_playlist_was_visible)
443 playlistdock->hide();
444#endif
445
446 widgets_size = height() - panel->height();
447 qDebug("BaseGuiPlus::aboutToEnterCompactMode: widgets_size: %d", widgets_size);
448
449 BaseGui::aboutToEnterCompactMode();
450
451 if (pref->resize_method == Preferences::Always) {
452 resize( width(), height() - widgets_size );
453 }
454}
455
456void BaseGuiPlus::aboutToExitCompactMode() {
457 BaseGui::aboutToExitCompactMode();
458
459 if (pref->resize_method == Preferences::Always) {
460 resize( width(), height() + widgets_size );
461 }
462
463#if DOCK_PLAYLIST
464 if (compact_playlist_was_visible)
465 playlistdock->show();
466#endif
467}
468
469#if DOCK_PLAYLIST
470void BaseGuiPlus::showPlaylist(bool b) {
471 qDebug("BaseGuiPlus::showPlaylist: %d", b);
472 qDebug("BaseGuiPlus::showPlaylist (before): playlist visible: %d", playlistdock->isVisible());
473 qDebug("BaseGuiPlus::showPlaylist (before): playlist position: %d, %d", playlistdock->pos().x(), playlistdock->pos().y());
474 qDebug("BaseGuiPlus::showPlaylist (before): playlist size: %d x %d", playlistdock->size().width(), playlistdock->size().height());
475
476 if ( !b ) {
477 playlistdock->hide();
478 } else {
479 exitFullscreenIfNeeded();
480 playlistdock->show();
481
482 // Check if playlist is outside of the screen
483 if (playlistdock->isFloating()) {
484 if (!DesktopInfo::isInsideScreen(playlistdock)) {
485 qWarning("BaseGuiPlus::showPlaylist: playlist is outside of the screen");
486 playlistdock->move(0,0);
487 }
488 }
489 }
490 //updateWidgets();
491
492 qDebug("BaseGuiPlus::showPlaylist (after): playlist visible: %d", playlistdock->isVisible());
493 qDebug("BaseGuiPlus::showPlaylist (after): playlist position: %d, %d", playlistdock->pos().x(), playlistdock->pos().y());
494 qDebug("BaseGuiPlus::showPlaylist (after): playlist size: %d x %d", playlistdock->size().width(), playlistdock->size().height());
495
496}
497
498void BaseGuiPlus::playlistClosed() {
499 showPlaylistAct->setChecked(false);
500}
501
502#if !USE_DOCK_TOPLEVEL_EVENT
503void BaseGuiPlus::dockVisibilityChanged(bool visible) {
504 qDebug("BaseGuiPlus::dockVisibilityChanged: %d", visible);
505
506 if (!playlistdock->isFloating()) {
507 if (!visible) shrinkWindow(); else stretchWindow();
508 }
509}
510
511#else
512
513void BaseGuiPlus::dockTopLevelChanged(bool floating) {
514 qDebug("BaseGuiPlus::dockTopLevelChanged: %d", floating);
515
516 if (floating) shrinkWindow(); else stretchWindow();
517}
518#endif
519
520void BaseGuiPlus::stretchWindow() {
521 qDebug("BaseGuiPlus::stretchWindow");
522 if ((ignore_playlist_events) || (pref->resize_method!=Preferences::Always)) return;
523
524 qDebug("BaseGuiPlus::stretchWindow: dockWidgetArea: %d", (int) dockWidgetArea(playlistdock) );
525
526 if ( (dockWidgetArea(playlistdock) == Qt::TopDockWidgetArea) ||
527 (dockWidgetArea(playlistdock) == Qt::BottomDockWidgetArea) )
528 {
529 int new_height = height() + playlistdock->height();
530
531 //if (new_height > DesktopInfo::desktop_size(this).height())
532 // new_height = DesktopInfo::desktop_size(this).height() - 20;
533
534 qDebug("BaseGuiPlus::stretchWindow: stretching: new height: %d", new_height);
535 resize( width(), new_height );
536
537 //resizeWindow(core->mset.win_width, core->mset.win_height);
538 }
539
540 else
541
542 if ( (dockWidgetArea(playlistdock) == Qt::LeftDockWidgetArea) ||
543 (dockWidgetArea(playlistdock) == Qt::RightDockWidgetArea) )
544 {
545 int new_width = width() + playlistdock->width();
546
547 qDebug("BaseGuiPlus::stretchWindow: stretching: new width: %d", new_width);
548 resize( new_width, height() );
549 }
550}
551
552void BaseGuiPlus::shrinkWindow() {
553 qDebug("BaseGuiPlus::shrinkWindow");
554 if ((ignore_playlist_events) || (pref->resize_method!=Preferences::Always)) return;
555
556 qDebug("BaseGuiPlus::shrinkWindow: dockWidgetArea: %d", (int) dockWidgetArea(playlistdock) );
557
558 if ( (dockWidgetArea(playlistdock) == Qt::TopDockWidgetArea) ||
559 (dockWidgetArea(playlistdock) == Qt::BottomDockWidgetArea) )
560 {
561 int new_height = height() - playlistdock->height();
562 qDebug("DefaultGui::shrinkWindow: shrinking: new height: %d", new_height);
563 resize( width(), new_height );
564
565 //resizeWindow(core->mset.win_width, core->mset.win_height);
566 }
567
568 else
569
570 if ( (dockWidgetArea(playlistdock) == Qt::LeftDockWidgetArea) ||
571 (dockWidgetArea(playlistdock) == Qt::RightDockWidgetArea) )
572 {
573 int new_width = width() - playlistdock->width();
574
575 qDebug("BaseGuiPlus::shrinkWindow: shrinking: new width: %d", new_width);
576 resize( new_width, height() );
577 }
578}
579
580#endif
581
582// Convenience functions intended for other GUI's
583TimeSliderAction * BaseGuiPlus::createTimeSliderAction(QWidget * parent) {
584 TimeSliderAction * timeslider_action = new TimeSliderAction( parent );
585 timeslider_action->setObjectName("timeslider_action");
586
587#ifdef SEEKBAR_RESOLUTION
588 connect( timeslider_action, SIGNAL( posChanged(int) ),
589 core, SLOT(goToPosition(int)) );
590 connect( core, SIGNAL(positionChanged(int)),
591 timeslider_action, SLOT(setPos(int)) );
592#else
593 connect( timeslider_action, SIGNAL( posChanged(int) ),
594 core, SLOT(goToPos(int)) );
595 connect( core, SIGNAL(posChanged(int)),
596 timeslider_action, SLOT(setPos(int)) );
597#endif
598 connect( timeslider_action, SIGNAL( draggingPos(int) ),
599 this, SLOT(displayGotoTime(int)) );
600#if ENABLE_DELAYED_DRAGGING
601 timeslider_action->setDragDelay( pref->time_slider_drag_delay );
602
603 connect( timeslider_action, SIGNAL( delayedDraggingPos(int) ),
604 this, SLOT(goToPosOnDragging(int)) );
605#else
606 connect( timeslider_action, SIGNAL( draggingPos(int) ),
607 this, SLOT(goToPosOnDragging(int)) );
608#endif
609 return timeslider_action;
610}
611
612VolumeSliderAction * BaseGuiPlus::createVolumeSliderAction(QWidget * parent) {
613 VolumeSliderAction * volumeslider_action = new VolumeSliderAction(parent);
614 volumeslider_action->setObjectName("volumeslider_action");
615
616 connect( volumeslider_action, SIGNAL( valueChanged(int) ),
617 core, SLOT( setVolume(int) ) );
618 connect( core, SIGNAL(volumeChanged(int)),
619 volumeslider_action, SLOT(setValue(int)) );
620
621 return volumeslider_action;
622}
623
624#ifdef Q_OS_OS2
625// we test if xcenter is available at all. if not disable the tray action. this is possible when xcenter is not opened or crashed
626void BaseGuiPlus::trayAvailable() {
627 if (!tray->isSystemTrayAvailable()) {
628 optionsMenu->removeAction(showTrayAct);
629 }
630 else {
631 optionsMenu->addAction(showTrayAct);
632 }
633}
634#endif
635
636#include "moc_baseguiplus.cpp"
Note: See TracBrowser for help on using the repository browser.