2 # Copyright (C) 2008 jerous <jerous@gmail.com>
3 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
5 # Nephilim 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 3 of the License, or
8 # (at your option) any later version.
10 # Nephilim 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.
15 # You should have received a copy of the GNU General Public License
16 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
19 from PyQt4
import QtGui
, QtCore
20 from PyQt4
.QtCore
import pyqtSlot
as Slot
23 from common
import APPNAME
, sec2min
, appIcon
27 class winMain(QtGui
.QMainWindow
):
28 """The winMain class is mpc's main window, showing the playlists and control-interface"""
43 def __init__(self
, mpclient
):
44 QtGui
.QMainWindow
.__init
__(self
)
45 self
.settings
= QtCore
.QSettings()
46 self
.mpclient
= mpclient
51 self
.__statuslabel
= QtGui
.QLabel()
52 self
.__time
_slider
= QtGui
.QSlider(QtCore
.Qt
.Horizontal
, self
)
53 self
.__time
_slider
.setMaximumWidth(self
.width()/4)
54 self
.__time
_slider
.sliderReleased
.connect( self
.__on
___time
_slider
_change
)
55 self
.__time
_label
= TimeLabel(self
, mpclient
)
56 self
.mpclient
.time_changed
.connect(self
.__on
_time
_change
)
58 self
.statusBar().addWidget(self
.__statuslabel
)
59 self
.statusBar().addPermanentWidget(self
.__time
_label
)
60 self
.statusBar().addPermanentWidget(self
.__time
_slider
)
62 mBar
= QtGui
.QMenuBar() # create a menubar
64 m
= mBar
.addMenu("File")
66 self
.mConnect
=m
.addAction('Connect ...', QtGui
.QApplication
.instance().show_connect_win
)
67 self
.mConnect
.setIcon(QtGui
.QIcon(appIcon
))
69 self
.mDisconnect
=m
.addAction('Disconnect', self
.mpclient
.disconnect_mpd
)
70 self
.mDisconnect
.setIcon(QtGui
.QIcon(':icons/disconnect.png'))
74 m
.addAction("Quit", QtGui
.QApplication
.instance().quit
).setIcon(QtGui
.QIcon(':icons/gtk-quit.svg'))
77 m
=mBar
.addMenu("Options")
79 m
.addAction("Settings", QtGui
.QApplication
.instance().show_settings_win
).setIcon(QtGui
.QIcon(':icons/gtk-preferences.svg'))
82 self
.__layout
_menu
=mBar
.addMenu("Layout")
84 # create a toolbar for the main menu
85 menu_toolbar
= QtGui
.QToolBar('Main menu', self
)
86 menu_toolbar
.addWidget(mBar
)
87 self
.addToolBar(QtCore
.Qt
.TopToolBarArea
, menu_toolbar
)
89 self
.__update
_layout
_menu
()
90 self
.setDockOptions(QtGui
.QMainWindow
.AllowNestedDocks \
91 |QtGui
.QMainWindow
.AllowTabbedDocks \
92 |QtGui
.QMainWindow
.VerticalTabs
)
93 self
.setDockNestingEnabled(True)
94 self
.restoreGeometry(self
.settings
.value('geometry', QtCore
.QByteArray()))
97 self
.mpclient
.connect_changed
.connect(self
.__on
_connect
_changed
)
98 self
.mpclient
.song_changed
.connect(self
.__on
_song
_change
)
99 self
.mpclient
.state_changed
.connect(self
.__update
_state
_messages
)
101 self
.__update
_state
_messages
()
105 self
.settings
.setValue('geometry', self
.saveGeometry())
107 def __update_layout_menu(self
):
108 self
.__layout
_menu
.clear()
109 self
.__layout
_menu
.addAction('Save layout', self
.save_layout
)
110 self
.__layout
_menu
.addAction('Restore layout', self
.restore_layout
)
111 self
.__layout
_menu
.addSeparator()
112 # create checkable menu
113 a
= QtGui
.QAction('Show titlebars', self
)
115 a
.setChecked(int(self
.settings
.value('show_titlebars', 1)))
116 self
.__toggle
_titlebars
(a
.isChecked())
117 a
.toggled
.connect(self
.__toggle
_titlebars
)
118 self
.__layout
_menu
.addAction(a
)
119 self
.__layout
_menu
.addSeparator()
121 m
= self
.createPopupMenu()
123 for action
in m
.actions():
124 self
.__layout
_menu
.addAction(action
)
126 def __toggle_titlebars(self
, val
):
128 self
.settings
.setValue('show_titlebars', 1)
130 self
.settings
.setValue('show_titlebars', 0)
131 for dock
in self
.__docks
:
133 dock
.setTitleBarWidget(None)
135 dock
.setTitleBarWidget(QtGui
.QWidget())
136 def add_dock(self
, dock
):
138 self
.__docks
.append(dock
)
139 self
.addDockWidget(QtCore
.Qt
.TopDockWidgetArea
, dock
)
140 self
.__update
_layout
_menu
()
141 def remove_dock(self
, dock
):
143 self
.removeDockWidget(dock
)
144 if dock
in self
.__docks
:
145 self
.__docks
.remove(dock
)
146 self
.__update
_layout
_menu
()
148 def save_layout(self
):
149 self
.settings
.setValue('layout', self
.saveState())
150 def restore_layout(self
):
151 layout
= self
.settings
.value('layout')
153 l_file
= QtCore
.QFile(':default_layout')
154 l_file
.open(QtCore
.QIODevice
.ReadOnly
)
155 layout
= l_file
.readAll()
156 self
.restoreState(layout
)
159 def __on_connect_changed(self
, val
):
161 self
.mDisconnect
.setEnabled(True)
162 self
.mConnect
.setEnabled(False)
164 self
.mDisconnect
.setEnabled(False)
165 self
.mConnect
.setEnabled(True)
167 def __update_state_messages(self
):
168 """Update window title and statusbar"""
169 song
= self
.mpclient
.cur_song
170 state
= self
.mpclient
.status
['state']
171 state
= 'playing' if state
== 'play' else 'paused' if state
== 'pause' else 'stopped'
173 self
.setWindowTitle('%s by %s - %s [%s]'%(song
['?title'], song
['?artist'], APPNAME
, state
))
174 self
.__statuslabel
.setText('%s by %s on %s [%s]'%(song
['?title'], song
['?artist'],song
['?album'], state
))
176 self
.setWindowTitle(APPNAME
)
177 self
.__statuslabel
.setText('')
179 def __on___time_slider_change(self
):
180 self
.mpclient
.seek(self
.__time
_slider
.value())
182 def __on_song_change(self
):
183 status
= self
.mpclient
.status
184 self
.__time
_slider
.setMaximum(status
['time'][1])
185 self
.__time
_slider
.setEnabled(True)
186 self
.__update
_state
_messages
()
188 def __on_time_change(self
, new_time
):
189 if not self
.__time
_slider
.isSliderDown():
190 self
.__time
_slider
.setValue(new_time
)
192 class TimeLabel(QtGui
.QLabel
):
196 def __init__(self
, parent
, mpclient
):
197 QtGui
.QLabel
.__init
__(self
, parent
)
199 self
._mpclient
= mpclient
201 self
._mpclient
.time_changed
.connect(self
._update
_text
)
202 self
._update
_text
(self
._mpclient
.status
['time'][0])
205 def _update_text(self
, time
):
206 self
.setText('%s/%s'%(sec2min(time
), sec2min(self
._mpclient
.status
['time'][1])))