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

Last change on this file since 121 was 119, checked in by Silvan Scherrer, 14 years ago

SMPlayer: latest svn update

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