Commits

Mathias Panzenböck committed d9ca8ff

added command based player controller

Comments (0)

Files changed (6)

 UI_PY=contents/code/commands_ui.py contents/code/edit_ui.py
 PY=$(UI_PY) contents/code/main.py contents/code/mediaplayer.py \
 	contents/code/playericon.py contents/code/scrollwidget.py \
+	contents/code/commandsform.py contents/code/editform.py \
 	contents/code/lbls.py
 
 .PHONY: all clean install uninstall pkg ui

contents/code/commandsform.py

+# -*- coding: utf-8 -*-
+##################################################################################
+# Play Control
+# Copyright (C) 2011-2013 Mathias Panzenböck <grosser.meister.morti@gmx.net>
+#
+# This program 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 2
+# of the License, or (at your option) any later version.
+#
+# This program 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 this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+#
+##################################################################################
+
+from PyQt4.QtCore import Qt, pyqtSlot, pyqtSignal
+from PyQt4 import QtGui
+from PyQt4.QtGui import QWidget, QListWidgetItem, QMessageBox
+from commands_ui import Ui_Commands
+from editform import EditForm
+
+def get_data(item):
+	return dict((unicode(key),unicode(val)) for key, val in item.data(Qt.UserRole).toPyObject().iteritems())
+
+class CommandsForm(Ui_Commands,QWidget):
+	__slots__ = 'editForm',
+
+	settingsChanged = pyqtSignal()
+
+	def __init__(self,parent,commands=None):
+		QWidget.__init__(self)
+		self.setParent(parent)
+		self.setupUi(self)
+		self.editForm = EditForm(parent.parent())
+		self.editForm.accepted.connect(self.on_editForm_accepted)
+		if commands:
+			for cmd in commands:
+				item = QListWidgetItem(cmd["identity"])
+				item.setData(Qt.UserRole,cmd)
+				self.listPlayers.addItem(item)
+			
+			self.btnRemove.setEnabled(True)
+			self.btnEdit.setEnabled(True)
+			self.listPlayers.sortItems()
+
+	@property
+	def commands(self):
+		cmds = []
+		for i in range(self.listPlayers.count()):
+			item = self.listPlayers.item(i)
+			cmds.append(get_data(item))
+		return cmds
+
+	@pyqtSlot()
+	def on_btnAdd_clicked(self):
+		name = unicode(self.txtPlayerName.text())
+		data = {"identity": name}
+		item = QListWidgetItem(name)
+		item.setData(Qt.UserRole,data)
+		self.listPlayers.addItem(item)
+		self.listPlayers.sortItems()
+		self.settingsChanged.emit()
+
+	@pyqtSlot()
+	def on_btnEdit_clicked(self):
+		item = self.listPlayers.currentItem()
+		if item:
+			data = get_data(item)
+			self.editForm.setData(data)
+			self.editForm.show()
+
+	@pyqtSlot()
+	def on_editForm_accepted(self):
+		item = self.listPlayers.currentItem()
+		if item:
+			data = self.editForm.data()
+			item.setData(Qt.UserRole,data)
+			item.setText(data["identity"])
+			self.listPlayers.sortItems()
+			self.settingsChanged.emit()
+
+	@pyqtSlot()
+	def on_btnRemove_clicked(self):
+		item = self.listPlayers.currentItem()
+		if item:
+			data = get_data(item)
+			name = data["identity"]
+			msg = QtGui.qApp.tr('Do you really want to delete "%1"?').arg(name)
+			btn = QMessageBox.question(self, msg, msg, QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
+			if btn == QMessageBox.Yes:
+				self.listPlayers.takeItem(self.listPlayers.row(item))
+			self.settingsChanged.emit()
+
+	@pyqtSlot()
+	def on_listPlayers_itemSelectionChanged(self):
+		enabled = bool(self.listPlayers.currentItem())
+		self.btnRemove.setEnabled(enabled)
+		self.btnEdit.setEnabled(enabled)

contents/code/editform.py

+# -*- coding: utf-8 -*-
+##################################################################################
+# Play Control
+# Copyright (C) 2011-2013 Mathias Panzenböck <grosser.meister.morti@gmx.net>
+#
+# This program 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 2
+# of the License, or (at your option) any later version.
+#
+# This program 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 this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+#
+##################################################################################
+
+from PyQt4.QtCore import Qt, pyqtSlot
+from PyQt4.QtGui import QDialog
+from edit_ui import Ui_Edit
+
+class EditForm(QDialog,Ui_Edit):
+	__slots__ = ()
+	def __init__(self,parent,data=None):
+		QDialog.__init__(self)
+		self.setWindowModality(Qt.WindowModal)
+		self.setParent(parent)
+		self.setupUi(self)
+		self.buttonBox.accepted.connect(self.accept)
+		self.buttonBox.rejected.connect(self.reject)
+		if data:
+			self.setData(data)
+
+	def setData(self,data):
+		self.txtIdentity.setText(data.get('identity',''))
+		self.txtIcon.setText(data.get('icon',''))
+		self.txtPlay.setText(data.get('play',''))
+		self.txtPause.setText(data.get('pause',''))
+		self.txtPlayPause.setText(data.get('playPause',''))
+		self.txtStop.setText(data.get('stop',''))
+		self.txtNext.setText(data.get('next',''))
+		self.txtPrev.setText(data.get('prev',''))
+		self.txtForward.setText(data.get('forward',''))
+		self.txtBackward.setText(data.get('backward',''))
+		self.txtStatus.setText(data.get('status',''))
+		self.txtQuit.setText(data.get('quit',''))
+
+	def data(self):
+		return {
+			'identity':  unicode(self.txtIdentity.text()),
+			'icon':      unicode(self.txtIcon.text()),
+			'play':      unicode(self.txtPlay.text()),
+			'pause':     unicode(self.txtPause.text()),
+			'playPause': unicode(self.txtPlayPause.text()),
+			'stop':      unicode(self.txtStop.text()),
+			'next':      unicode(self.txtNext.text()),
+			'prev':      unicode(self.txtPrev.text()),
+			'forward':   unicode(self.txtForward.text()),
+			'backward':  unicode(self.txtBackward.text()),
+			'status':    unicode(self.txtStatus.text()),
+			'quit':      unicode(self.txtQuit.text())
+		}

contents/code/main.py

 #
 ##################################################################################
 
-import sys
+import sys, re
 from PyQt4.QtCore import Qt, QString, QSizeF
 from PyQt4.QtGui import QGraphicsGridLayout, QSizePolicy, \
 	QGraphicsLinearLayout, QGraphicsWidget
-from PyKDE4.plasma import Plasma
 from PyKDE4.kdeui import KIcon, KShortcutsEditor, KActionCollection, \
 	KAction, KShortcut
 from PyKDE4 import plasmascript
 from playericon import PlayerIcon
 from lbls import *
 
+from commandsform import CommandsForm
+
+try:
+	import json
+except ImportError:
+	import simplejson as json
+
+SPACE = re.compile("\\s")
+
 class PlayControl(plasmascript.Applet):
 	def init(self):
 		self.setAspectRatioMode(plasmascript.Plasma.IgnoreAspectRatio)
 		for action in self._actions.actions():
 			action.setShortcutContext(Qt.WindowShortcut)
 
+		cfg = self.config()
+		self._commands = json.loads(unicode(cfg.readEntry("commands","[]").toString()))
+
 		self._dbus_mainloop = dbus.mainloop.qt.DBusQtMainLoop()
 		self._bus = dbus.SessionBus(mainloop=self._dbus_mainloop)
 		self._dbus = self._bus.get_object(dbus.BUS_DAEMON_NAME,dbus.BUS_DAEMON_PATH)
 		shortcuts = KShortcutsEditor(parent)
 		shortcuts.addCollection(self._actions)
 		parent.addPage(shortcuts, "Global Media Player Shortcuts", "preferences-desktop-keyboard")
+		self.commandsConfig = CommandsForm(parent,self._commands)
+		self.commandsConfig.settingsChanged.connect(parent.settingsChangedSlot)
+		parent.addPage(self.commandsConfig, "Command Based Player Control")
+		parent.okClicked.connect(self.configAccepted)
+		parent.applyClicked.connect(self.configAccepted)
 
+	def configAccepted(self):
+		self._commands = self.commandsConfig.commands
+		cfg = self.config()
+		cfg.writeEntry("commands", json.dumps(self._commands))
+		self._refresh()
+	
 	def _track_change(self,metadata,sender=None):
 		self._select_by_owner(sender,True)
 	
 							players[owner] = (name, player)
 		players = players.values()
 
-		# TODO: GUI
-		cmdplayer = CommandMediaPlayer('cmd:greattune-player',dict(
-			identity = 'Greattune Player',
-			icon = '/home/panzi/src/python/plasma/playctrl/magnatune.png',
-			playPause = '/home/panzi/src/shell/gtp-remote.sh playpause',
-			play = '/home/panzi/src/shell/gtp-remote.sh play',
-			pause = '/home/panzi/src/shell/gtp-remote.sh pause',
-			stop = '/home/panzi/src/shell/gtp-remote.sh stop',
-			next = '/home/panzi/src/shell/gtp-remote.sh next',
-			prev = '/home/panzi/src/shell/gtp-remote.sh prev',
-			status = '/home/panzi/src/shell/gtp-remote.sh status',
-			quit = '/home/panzi/src/shell/gtp-remote.sh quit'
-		))
-		players.append((cmdplayer.identity(), cmdplayer))
+		for cmd in self._commands:
+			name = 'cmd:'+SPACE.sub("-",cmd['identity'].lower())
+			player = CommandMediaPlayer(name,cmd)
+			players.append((player.identity(), player))
 
 		players.sort()
 		current = self._player()

contents/ui/commands.ui

      </item>
      <item>
       <widget class="QPushButton" name="btnRemove">
+       <property name="enabled">
+        <bool>false</bool>
+       </property>
        <property name="text">
         <string>&amp;Remove</string>
        </property>
      </item>
      <item>
       <widget class="QPushButton" name="btnEdit">
+       <property name="enabled">
+        <bool>false</bool>
+       </property>
        <property name="text">
         <string>&amp;Edit...</string>
        </property>
     </layout>
    </item>
    <item row="1" column="0">
-    <widget class="QListView" name="listPlayers"/>
+    <widget class="QListWidget" name="listPlayers"/>
    </item>
   </layout>
  </widget>
    <header>klineedit.h</header>
   </customwidget>
  </customwidgets>
+ <tabstops>
+  <tabstop>txtPlayerName</tabstop>
+  <tabstop>btnAdd</tabstop>
+  <tabstop>btnRemove</tabstop>
+  <tabstop>btnEdit</tabstop>
+  <tabstop>listPlayers</tabstop>
+ </tabstops>
  <resources/>
  <connections/>
 </ui>
       <widget class="KLineEdit" name="txtPlay"/>
      </item>
      <item row="2" column="1">
-      <widget class="KLineEdit" name="txtName"/>
+      <widget class="KLineEdit" name="txtIdentity"/>
      </item>
      <item row="2" column="0">
-      <widget class="QLabel" name="lblName">
+      <widget class="QLabel" name="lblIdentity">
        <property name="text">
         <string>&amp;Name</string>
        </property>
        <property name="buddy">
-        <cstring>txtName</cstring>
+        <cstring>txtIdentity</cstring>
        </property>
       </widget>
      </item>
    <header>klineedit.h</header>
   </customwidget>
  </customwidgets>
+ <tabstops>
+  <tabstop>txtIdentity</tabstop>
+  <tabstop>txtIcon</tabstop>
+  <tabstop>txtPlay</tabstop>
+  <tabstop>txtPause</tabstop>
+  <tabstop>txtPlayPause</tabstop>
+  <tabstop>txtStop</tabstop>
+  <tabstop>txtNext</tabstop>
+  <tabstop>txtPrev</tabstop>
+  <tabstop>txtForward</tabstop>
+  <tabstop>txtBackward</tabstop>
+  <tabstop>txtStatus</tabstop>
+  <tabstop>txtQuit</tabstop>
+  <tabstop>buttonBox</tabstop>
+ </tabstops>
  <resources/>
  <connections/>
 </ui>