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 MainWindow(QtGui
.QMainWindow
):
29 Application's main window class.
45 def __init__(self
, mpclient
):
46 QtGui
.QMainWindow
.__init
__(self
)
47 self
.settings
= QtCore
.QSettings()
48 self
.mpclient
= mpclient
53 self
.__statuslabel
= QtGui
.QLabel()
54 self
.__time
_slider
= QtGui
.QSlider(QtCore
.Qt
.Horizontal
, self
)
55 self
.__time
_slider
.setMaximumWidth(self
.width()/4)
56 self
.__time
_slider
.sliderReleased
.connect( self
.__on
___time
_slider
_change
)
57 self
.__time
_label
= TimeLabel(self
, mpclient
)
58 self
.mpclient
.time_changed
.connect(self
.__on
_time
_change
)
60 self
.statusBar().addWidget(self
.__statuslabel
)
61 self
.statusBar().addPermanentWidget(self
.__time
_label
)
62 self
.statusBar().addPermanentWidget(self
.__time
_slider
)
64 mBar
= QtGui
.QMenuBar() # create a menubar
66 m
= mBar
.addMenu('&Server')
68 self
.mConnect
=m
.addAction('&Connect.', QtGui
.QApplication
.instance().show_connect_win
)
69 self
.mConnect
.setIcon(QtGui
.QIcon(appIcon
))
71 self
.mDisconnect
=m
.addAction('&Disconnect', self
.mpclient
.disconnect_mpd
)
72 self
.mDisconnect
.setIcon(QtGui
.QIcon(':icons/disconnect.png'))
76 m
.addAction('&Update', self
.mpclient
.update_database
)
77 m
.addAction('&Rescan', self
.mpclient
.rescan_database
)
80 m
.addAction("&Quit", QtGui
.QApplication
.instance().quit
).setIcon(QtGui
.QIcon(':icons/gtk-quit.svg'))
83 m
=mBar
.addMenu("Options")
85 m
.addAction("Settings", QtGui
.QApplication
.instance().show_settings_win
).setIcon(QtGui
.QIcon(':icons/gtk-preferences.svg'))
88 self
.__layout
_menu
=mBar
.addMenu("Layout")
90 # create a toolbar for the main menu
91 menu_toolbar
= QtGui
.QToolBar('Main menu', self
)
92 menu_toolbar
.addWidget(mBar
)
93 self
.addToolBar(QtCore
.Qt
.TopToolBarArea
, menu_toolbar
)
95 self
.__update
_layout
_menu
()
96 self
.setDockOptions(QtGui
.QMainWindow
.AllowNestedDocks \
97 |QtGui
.QMainWindow
.AllowTabbedDocks \
98 |QtGui
.QMainWindow
.VerticalTabs
)
99 self
.setDockNestingEnabled(True)
100 self
.restoreGeometry(self
.settings
.value('geometry', QtCore
.QByteArray()))
102 " add event handlers"
103 self
.mpclient
.connect_changed
.connect(self
.__on
_connect
_changed
)
104 self
.mpclient
.song_changed
.connect(self
.__on
_song
_change
)
105 self
.mpclient
.state_changed
.connect(self
.__update
_state
_messages
)
107 self
.__update
_state
_messages
()
111 self
.settings
.setValue('geometry', self
.saveGeometry())
113 def __update_layout_menu(self
):
114 self
.__layout
_menu
.clear()
115 self
.__layout
_menu
.addAction('Save layout', self
.save_layout
)
116 self
.__layout
_menu
.addAction('Restore layout', self
.restore_layout
)
117 self
.__layout
_menu
.addSeparator()
118 # create checkable menu
119 a
= QtGui
.QAction('Show titlebars', self
)
121 a
.setChecked(int(self
.settings
.value('show_titlebars', 1)))
122 self
.__toggle
_titlebars
(a
.isChecked())
123 a
.toggled
.connect(self
.__toggle
_titlebars
)
124 self
.__layout
_menu
.addAction(a
)
125 self
.__layout
_menu
.addSeparator()
127 m
= self
.createPopupMenu()
129 for action
in m
.actions():
130 self
.__layout
_menu
.addAction(action
)
132 def __toggle_titlebars(self
, val
):
134 self
.settings
.setValue('show_titlebars', 1)
136 self
.settings
.setValue('show_titlebars', 0)
137 for dock
in self
.__docks
:
139 dock
.setTitleBarWidget(None)
141 dock
.setTitleBarWidget(QtGui
.QWidget())
142 def add_dock(self
, dock
):
144 self
.__docks
.append(dock
)
145 self
.addDockWidget(QtCore
.Qt
.TopDockWidgetArea
, dock
)
146 self
.__update
_layout
_menu
()
147 def remove_dock(self
, dock
):
149 self
.removeDockWidget(dock
)
150 if dock
in self
.__docks
:
151 self
.__docks
.remove(dock
)
152 self
.__update
_layout
_menu
()
154 def save_layout(self
):
155 self
.settings
.setValue('layout', self
.saveState())
156 def restore_layout(self
):
157 layout
= self
.settings
.value('layout')
159 l_file
= QtCore
.QFile(':default_layout')
160 l_file
.open(QtCore
.QIODevice
.ReadOnly
)
161 layout
= l_file
.readAll()
162 self
.restoreState(layout
)
165 def __on_connect_changed(self
, val
):
167 self
.mDisconnect
.setEnabled(True)
168 self
.mConnect
.setEnabled(False)
170 self
.mDisconnect
.setEnabled(False)
171 self
.mConnect
.setEnabled(True)
173 def __update_state_messages(self
):
174 """Update window title and statusbar"""
175 song
= self
.mpclient
.cur_song
176 state
= self
.mpclient
.status
['state']
177 state
= 'playing' if state
== 'play' else 'paused' if state
== 'pause' else 'stopped'
179 self
.setWindowTitle('%s by %s - %s [%s]'%(song
['?title'], song
['?artist'], APPNAME
, state
))
180 self
.__statuslabel
.setText('%s by %s on %s [%s]'%(song
['?title'], song
['?artist'],song
['?album'], state
))
182 self
.setWindowTitle(APPNAME
)
183 self
.__statuslabel
.setText('')
185 def __on___time_slider_change(self
):
186 self
.mpclient
.seek(self
.__time
_slider
.value())
188 def __on_song_change(self
):
189 status
= self
.mpclient
.status
190 self
.__time
_slider
.setMaximum(status
['time'][1])
191 self
.__time
_slider
.setEnabled(True)
192 self
.__update
_state
_messages
()
194 def __on_time_change(self
, new_time
):
195 if not self
.__time
_slider
.isSliderDown():
196 self
.__time
_slider
.setValue(new_time
)
198 class TimeLabel(QtGui
.QLabel
):
202 def __init__(self
, parent
, mpclient
):
203 QtGui
.QLabel
.__init
__(self
, parent
)
205 self
._mpclient
= mpclient
207 self
._mpclient
.time_changed
.connect(self
._update
_text
)
208 self
._update
_text
(self
._mpclient
.status
['time'][0])
211 def _update_text(self
, time
):
212 self
.setText('%s/%s'%(sec2min(time
), sec2min(self
._mpclient
.status
['time'][1])))