Source code for vdat.gui.queue

# -*- coding: utf-8 -*-

""" Form implementation generated from reading ui file 'listWindow.ui'

 Created: Mon Jun 15 16:25:52 2015
 by: PyQt4 UI code generator 4.10.4
"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

from PyQt4 import QtCore, QtGui

from vdat.gui.relay import get_relay

try:
    from QtCore import QString
except ImportError:  # For Python3
    QString = str


SIGNAL_NAME = "toggleq"
RECEIVER_NAME = "toggleQueueWindowCB"

_queue = []


[docs]class QueuedCommand(QtGui.QListWidgetItem): """A container for commands on the queue. Also saves the current config object. Parameters ---------- command : :class:`~vdat.command_interpreter.core.CommandInterpreter` instance of the command interpreter to add to the queue label : string A label to appear for this command on the queue tool_tip : string, optional tool tip to show parent : :class:`QtWidget` instance The QtWidget that the menu is attached to """ def __init__(self, command, label, tool_tip=None, parent=None): super(QueuedCommand, self).__init__(parent=parent) self.setText(label) if tool_tip: self.setToolTip(tool_tip) self.command = command
[docs]class ModifyableListWidget(QtGui.QListWidget): """Adds the ability to delete and modify items on the list by hand """
[docs] def keyPressEvent(self, event): """Override the default method, removing the selected entry Parameters ---------- event : :class:`QKeyEvent` object containing details of the key the user pressed """ if event.key() == QtCore.Qt.Key_Delete: self.delete_selected() else: super(ModifyableListWidget, self).keyPressEvent(event)
[docs] def delete_selected(self): """Delete all commands selected by the user """ for item in self.selectedItems(): row = self.row(item) self.takeItem(row)
[docs]class Queue(QtGui.QMainWindow): """A queue that stores user commands and displays them in a GUI window. """ closeSignal = QtCore.pyqtSignal() job_added = QtCore.pyqtSignal() """A signal emitted when a job is added to the queue""" def __init__(self, parent=None): super(Queue, self).__init__(parent=parent) self.setupUi() self.itemList = []
[docs] def setupUi(self): """Setup the queue window """ self.setObjectName("vdatqueue") self.resize(300, 380) self.setWindowTitle("VDAT Command Queue") self.centralwidget = QtGui.QWidget(self) self.centralwidget.setObjectName("centralwidget") self.listWidget = ModifyableListWidget(self.centralwidget) self.listWidget.setGeometry(QtCore.QRect(5, 0, 290, 375)) self.listWidget.setObjectName("listwidget") self.setCentralWidget(self.centralwidget) QtCore.QMetaObject.connectSlotsByName(self) # Connect up the toggle signal emitted by the menu bar (in menu.py) signals = get_relay() signals.get_signal(SIGNAL_NAME).connect(self.toggle) # Connect the closeSignal to the check box in the menu bar toggleCheckBox = signals.get_receiver(RECEIVER_NAME) self.closeSignal.connect(toggleCheckBox)
[docs] def closeEvent(self, event): """Override the user closing the window""" event.ignore() # Ignore the request self.closeSignal.emit() self.toggle(False) # Just hide it instead
[docs] def toggle(self, tggl): """Hide or show the panel""" self.setVisible(tggl)
[docs] def add_command(self, command, label, tool_tip=None): """Add a command to the queue. Emit the job_added signal. Parameters ---------- command : :class:`~vdat.command_interpreter.core.CommandInterpreter` instance of the command interpreter to add to the queue label : string A label to appear for this command on the queue tool_tip : string, optional tool tip to show """ item = QueuedCommand(command, label, tool_tip=tool_tip, parent=self.listWidget) self.listWidget.addItem(item) self.job_added.emit()
[docs] def get_command(self): """Get the top item from the queue. Returns ------- cmd : Callable the function on the queue. None is queue empty config : :class:`pyhetdex.tools.configuration.ConfigParser` instance the configuration with which to run the command (includes target directories). None if queue empty .. todo:: Check that not deleting the item doesnt cause a memory leak. """ item = self.listWidget.takeItem(0) if isinstance(item, QueuedCommand): return item.command.run else: return None
[docs]class QueuAction(QtGui.QAction): """Action for the queue window. Create the menu entry and binds signals known by the queue window to show/hide it """ def __init__(self, *args, **kwargs): super(QueuAction, self).__init__(*args, **kwargs) self.setText("Hide queue") self.setCheckable(True) self.toggle() self.toggled.connect(self._update_text) # Save the toggle function in signals, so the queue window can use it # later signals = get_relay() signals.add_receiver(RECEIVER_NAME, self.toggle) # As above, but save the signal so they can communicate both ways signals.add_signal(SIGNAL_NAME, self.toggled)
[docs] def _update_text(self, toggled): if toggled: self.setText("Hide queue") else: self.setText("Show queue")
[docs]def set_queue(parent=None): """Create a :class:`Queue` instance and save it. You can access it with :func:`get_queue` Parameters ---------- parent : :class:`QtWidget` instance The QtWidget that the menu is attached to """ _queue.append(Queue(parent=parent))
[docs]def get_queue(): """Get the locally stored :class:`Queue` instance""" return _queue[0]