summaryrefslogtreecommitdiff
path: root/nephilim/winMain.py
blob: 7a59dd581c9c113b4a99101ce522f0b8c7781f8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#
#    Copyright (C) 2008 jerous <jerous@gmail.com>
#    Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
#
#    Nephilim is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Nephilim is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Nephilim.  If not, see <http://www.gnu.org/licenses/>.
#

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QVariant
import logging

from misc import APPNAME, sec2min, appIcon
from connect_wg import ConnectWidget

DEFAULT_LAYOUT_FILE = 'default_layout'

class winMain(QtGui.QMainWindow):
    """The winMain class is mpc's main window, showing the playlists and control-interface"""
    docks = []

    " menus"
    mConnect    = None
    mDisconnect = None
    mLayout     = None

    wConnect  = None

    mpclient = None
    settings = None

    # Statusbar objects
    statuslabel = None
    time_slider = None
    time_label  = None

    def __init__(self, mpclient):
        QtGui.QWidget.__init__(self)
        self.settings = QtCore.QSettings()
        self.mpclient = mpclient

        self.wConnect = ConnectWidget(self)

        # statusbar
        self.statusBar()
        self.statuslabel = QtGui.QLabel()
        self.time_slider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        self.time_slider.setMaximumWidth(self.width()/4)
        self.connect(self.time_slider, QtCore.SIGNAL('sliderReleased()'), self.__on_time_slider_change)
        self.time_label = QtGui.QLabel()
        self.time_label.duration = '0:00'

        self.statusBar().addWidget(self.statuslabel)
        self.statusBar().addPermanentWidget(self.time_label)
        self.statusBar().addPermanentWidget(self.time_slider)

        mBar = QtGui.QMenuBar() # create a menubar
        # File menu
        m = mBar.addMenu("File")
        m.setTearOffEnabled(True)
        # connect
        self.mConnect=m.addAction('Connect ...', self.wConnect.monitor)
        self.mConnect.setIcon(QtGui.QIcon(appIcon))
        # disconnect
        self.mDisconnect=m.addAction('Disconnect', self.mpclient.disconnect_mpd)
        self.mDisconnect.setIcon(QtGui.QIcon('gfx/disconnect.png'))
        # separator
        m.addSeparator()
        # quit
        m.addAction("Quit", QtGui.QApplication.instance().quit).setIcon(QtGui.QIcon('gfx/gtk-quit.svg'))

        # menu options
        m=mBar.addMenu("Options")
        m.setTearOffEnabled(True)
        # settings
        m.addAction("Settings", QtGui.QApplication.instance().show_settings_win).setIcon(QtGui.QIcon('gfx/gtk-preferences.svg'))

        # menu layout
        self.mLayout=mBar.addMenu("Layout")
        self.mLayout.setTearOffEnabled(True)

        # create a toolbar for the main menu
        menu_toolbar = QtGui.QToolBar('Main menu', self)
        menu_toolbar.addWidget(mBar)
        self.addToolBar(QtCore.Qt.TopToolBarArea, menu_toolbar)

        self.updateLayoutMenu()
        self.setDockOptions(QtGui.QMainWindow.AllowNestedDocks \
                |QtGui.QMainWindow.AllowTabbedDocks \
                |QtGui.QMainWindow.VerticalTabs)
        self.setDockNestingEnabled(True)
        self.restoreGeometry(self.settings.value('geometry').toByteArray())

        " add event handlers"
        self.connect(self.mpclient, QtCore.SIGNAL('connect_changed'), self.__on_connect_changed)
        self.connect(self.mpclient, QtCore.SIGNAL('song_changed'),    self.__on_song_change)
        self.connect(self.mpclient, QtCore.SIGNAL('state_changed'),   self.__update_state_messages)
        self.connect(self.mpclient, QtCore.SIGNAL('time_changed'),    self.__on_time_change)

        self.wConnect.monitor()

        self.__update_state_messages()
        self.show()

    def on_quit(self):
        self.settings.setValue('geometry', QVariant(self.saveGeometry()))

    def updateLayoutMenu(self):
        self.mLayout.clear()
        self.mLayout.addAction('Save layout', self.saveLayout)
        self.mLayout.addAction('Restore layout', self.restore_layout)
        self.mLayout.addSeparator()
        # create checkable menu
        a=QtGui.QAction('Show titlebars', self)
        a.setCheckable(True)
        a.setChecked(self.settings.value('show_titlebars', QVariant(True)).toBool())
        self.toggleTitleBars(a.isChecked())
        self.connect(a, QtCore.SIGNAL('toggled(bool)'), self.toggleTitleBars)
        self.mLayout.addAction(a)
        self.mLayout.addSeparator()
        # can not use iterators, as that gives some creepy error 'bout c++
        actions=self.createPopupMenu().actions()
        for i in xrange(len(actions)):
            self.mLayout.addAction(actions[i])

    def toggleTitleBars(self, val):
        if val:
            self.settings.setValue('show_titlebars', QVariant(True))
        else:
            self.settings.setValue('show_titlebars', QVariant(False))
        for dock in self.docks:
            if val:
                dock.setTitleBarWidget(None)
            else:
                dock.setTitleBarWidget(QtGui.QWidget())
    def addDock(self, dock):
        if dock:
            self.docks.append(dock)
            self.addDockWidget(QtCore.Qt.TopDockWidgetArea, dock)
            self.updateLayoutMenu()
    def removeDock(self, dock):
        if dock:
            if dock in self.docks:
                self.docks.remove(dock)
            self.removeDockWidget(dock)
            self.updateLayoutMenu()

    mMenuVisible=None
    def createPopupMenu(self):
        ret=QtGui.QMenu('Test', self)
        if self.mMenuVisible==None:
            # create checkable menu
            a=QtGui.QAction('Menubar', self)
            a.setCheckable(True)
            a.setChecked(True)
            self.connect(a, QtCore.SIGNAL('toggled(bool)'), self.switchMenubar)

            self.mMenuVisible=a
        ret.addAction(self.mMenuVisible)
        ret.addSeparator()
        menu = QtGui.QMainWindow.createPopupMenu(self)
        if menu:
            actions = menu.actions()
            for i in xrange(len(actions)-1):
                ret.addAction(actions[i])
        return ret
    def switchMenubar(self, val):
        self.menuBar().setVisible(val)
    def setStatus(self, status):
        """Set the text of the statusbar."""
        self.statusBar().showMessage(status, 5000)
        logging.info(status)

    def saveLayout(self):
        self.settings.setValue('layout', QVariant(self.saveState()))
    def restore_layout(self):
        layout = self.settings.value('layout').toByteArray()
        if not layout:
            try:
                layout = open(DEFAULT_LAYOUT_FILE, 'rb').read()
            except IOError:
                logging.error("Error reading default layout.")
                return
        self.restoreState(layout)


    def __on_connect_changed(self, val):
        if val:
            self.mDisconnect.setEnabled(True)
            self.mConnect.setEnabled(False)
        else:
            self.mDisconnect.setEnabled(False)
            self.mConnect.setEnabled(True)

    def __update_state_messages(self):
        """Update window title and statusbar"""
        song = self.mpclient.current_song()
        state = self.mpclient.status()['state']
        state = 'playing' if state == 'play' else 'paused' if state == 'pause' else 'stopped'
        if song:
            self.setWindowTitle('%s by %s - %s [%s]'%(song.title(), song.artist(), APPNAME, state))
            self.statuslabel.setText('%s by %s on %s [%s]'%(song.title(), song.artist(),song.album(), state))
        else:
            self.setWindowTitle(APPNAME)
            self.statuslabel.setText('')

    def __on_time_slider_change(self):
        self.mpclient.seek(self.time_slider.value())

    def __on_song_change(self):
        status = self.mpclient.status()
        self.time_slider.setMaximum(status['length'])
        self.time_slider.setEnabled(True)
        self.time_label.duration = sec2min(status['length'])
        self.__update_state_messages()

    def __on_time_change(self, new_time):
        if not self.time_slider.isSliderDown():
            self.time_slider.setValue(new_time)
        self.time_label.setText(sec2min(new_time) + '/' + self.time_label.duration)