Update API of Printer output device handling so we can handle multiple devices

This commit is contained in:
Arjen Hiemstra 2015-04-07 12:22:45 +02:00
parent dab74e524a
commit a4b09d3dac
3 changed files with 130 additions and 32 deletions

View file

@ -45,7 +45,15 @@ class PrinterApplication(QtApplication):
self._physics = None self._physics = None
self._volume = None self._volume = None
self._platform = None self._platform = None
self._output_source = 'local_file' self._output_devices = {
'local_file': {
'id': 'local_file',
'function': self._writeToLocalFile,
'description': 'Save to Disk',
'icon': 'save',
'priority': 0
}
}
self.activeMachineChanged.connect(self._onActiveMachineChanged) self.activeMachineChanged.connect(self._onActiveMachineChanged)
def _loadPlugins(self): def _loadPlugins(self):
@ -106,6 +114,7 @@ class PrinterApplication(QtApplication):
else: else:
self.requestAddPrinter.emit() self.requestAddPrinter.emit()
self._removableDrivesChanged()
if self._engine.rootObjects: if self._engine.rootObjects:
self.closeSplash() self.closeSplash()
@ -256,39 +265,51 @@ class PrinterApplication(QtApplication):
else: else:
self._platform.setPosition(Vector(0.0, 0.0, 0.0)) self._platform.setPosition(Vector(0.0, 0.0, 0.0))
removableDrivesChanged = pyqtSignal() outputDevicesChanged = pyqtSignal()
@pyqtProperty('QVariantMap', notify = outputDevicesChanged)
def outputDevices(self):
return self._output_devices
outputDeviceChanged = pyqtSignal() @pyqtProperty('QStringList', notify = outputDevicesChanged)
@pyqtProperty(str, notify = outputDeviceChanged) def outputDeviceNames(self):
def outputDevice(self): return self._output_devices.keys()
return self._output_source
@pyqtProperty(str, notify = outputDeviceChanged) ## Add an output device that can be written to.
def outputDeviceIcon(self): #
if self._output_source == 'local_file': # \param id The identifier used to identify the device.
return 'save' # \param device A dictionary of device information.
elif self._output_source == 'sdcard': # It should contains the following:
return 'save_sd' # - function: A function to be called when trying to write to the device. Will be passed the device id as first parameter.
elif self._output_source == 'usb': # - description: A translated string containing a description of what happens when writing to the device.
return 'print_usb' # - icon: The icon to use to represent the device.
# - priority: The priority of the device. The device with the highest priority will be used as the default device.
def addOutputDevice(self, id, device):
self._output_devices[id] = device
self.outputDevicesChanged.emit()
def removeOutputDevice(self, id):
del self._output_devices[id]
self.outputDevicesChanged.emit()
@pyqtSlot() @pyqtSlot(str)
def writeToOutputDevice(self): def writeToOutputDevice(self, device):
pass self._output_devices[device]['function'](device)
@pyqtProperty("QStringList", notify = removableDrivesChanged) writeToLocalFileRequested = pyqtSignal()
def removableDrives(self): def _writeToLocalFile(self, device):
return list(self.getStorageDevice('LocalFileStorage').getRemovableDrives().keys()) self.writeToLocalFileRequested.emit()
@pyqtSlot() def _writeToSD(self, device):
def saveToSD(self):
for node in DepthFirstIterator(self.getController().getScene().getRoot()): for node in DepthFirstIterator(self.getController().getScene().getRoot()):
if type(node) is not SceneNode or not node.getMeshData(): if type(node) is not SceneNode or not node.getMeshData():
continue continue
drives = self.getStorageDevice('LocalFileStorage').getRemovableDrives() try:
path = next(iter(drives.values())) path = self.getStorageDevice('LocalFileStorage').getRemovableDrives()[device]
except KeyError:
Logger.log('e', 'Tried to write to unknown SD card %s', device)
return
filename = os.path.join(path, node.getName()[0:node.getName().rfind('.')] + '.gcode') filename = os.path.join(path, node.getName()[0:node.getName().rfind('.')] + '.gcode')
job = WriteMeshJob(filename, node.getMeshData()) job = WriteMeshJob(filename, node.getMeshData())
@ -296,5 +317,18 @@ class PrinterApplication(QtApplication):
return return
def _removableDrivesChanged(self): def _removableDrivesChanged(self):
print(self.getStorageDevice('LocalFileStorage').getRemovableDrives()) drives = self.getStorageDevice('LocalFileStorage').getRemovableDrives()
self.removableDrivesChanged.emit() for drive in drives:
if drive not in self._output_devices:
self.addOutputDevice(drive, {
'id': drive,
'function': self._writeToSD,
'description': 'Save to SD Card {0}'.format(drive),
'icon': 'save_sd',
'priority': 1
})
for device in self._output_devices:
if not device in drives:
if self._output_devices[device]['function'] == self._writeToSD:
self.removeOutputDevice(device)

View file

@ -354,6 +354,7 @@ UM.MainWindow {
Connections { Connections {
target: Printer target: Printer
onRequestAddPrinter: addMachine.visible = true; onRequestAddPrinter: addMachine.visible = true;
onWriteToLocalFileRequested: saveDialog.open();
} }
Component.onCompleted: UM.Theme.load(UM.Resources.getPath(UM.Resources.ThemesLocation, "cura")) Component.onCompleted: UM.Theme.load(UM.Resources.getPath(UM.Resources.ThemesLocation, "cura"))

View file

@ -13,9 +13,38 @@ Button {
property real progress: UM.Backend.progress; property real progress: UM.Backend.progress;
Behavior on progress { NumberAnimation { duration: 250; } } Behavior on progress { NumberAnimation { duration: 250; } }
enabled: progress >= 0.95; // enabled: progress >= 0.95;
iconSource: UM.Theme.icons[Printer.outputDeviceIcon]; property string currentDevice: 'local_file'
property bool defaultOverride: false;
property bool defaultAmbiguous: false;
iconSource: UM.Theme.icons[Printer.outputDevices[base.currentDevice].icon];
tooltip: Printer.outputDevices[base.currentDevice].description;
Connections {
target: Printer;
onOutputDevicesChanged: {
if(!base.defaultOverride) {
base.defaultAmbiguous = false;
var device = null;
for(var i in Printer.outputDevices) {
console.log(i);
if(device == null) {
device = i;
} else if(Printer.outputDevices[i].priority > Printer.outputDevices[device].priority) {
device = i;
} else if(Printer.outputDevices[i].priority == Printer.outputDevices[device].priority) {
base.defaultAmbiguous = true;
}
}
if(device != null) {
base.currentDevice = device;
}
}
}
}
style: ButtonStyle { style: ButtonStyle {
background: UM.AngledCornerRectangle { background: UM.AngledCornerRectangle {
@ -101,6 +130,40 @@ Button {
} }
} }
MouseArea {
anchors.fill: parent;
acceptedButtons: Qt.RightButton;
onClicked: devicesMenu.popup();
}
Menu {
id: devicesMenu;
Instantiator {
model: Printer.outputDeviceNames;
MenuItem {
text: Printer.outputDevices[modelData].description;
checkable: true;
checked: base.defaultAmbiguous ? false : modelData == base.currentDevice;
exclusiveGroup: devicesMenuGroup;
onTriggered: {
base.defaultOverride = true;
base.currentDevice = modelData;
if(base.defaultAmbiguous) {
base.defaultAmbiguous = false;
Printer.writeToOutputDevice(modelData);
}
}
}
onObjectAdded: devicesMenu.insertItem(index, object)
onObjectRemoved: devicesMenu.removeItem(object)
}
ExclusiveGroup { id: devicesMenuGroup; }
}
text: { text: {
if(base.progress < 0) { if(base.progress < 0) {
return qsTr("Please load a 3D model"); return qsTr("Please load a 3D model");
@ -112,10 +175,10 @@ Button {
} }
onClicked: { onClicked: {
if(Printer.outputDevice != "local_file") { if(base.defaultAmbiguous) {
Printer.writeToOutputDevice(); devicesMenu.popup();
} else if(base.saveAction) { } else {
base.saveAction.trigger(); Printer.writeToOutputDevice(base.currentDevice);
} }
} }
} }