class DeviceView(QtWidgets.QTableView): def __init__(self, device, parent=None): super().__init__(parent=parent) self.proxy_model = QSortFilterProxyModel() self.proxy_model.setFilterKeyColumn(-1) self.proxy_model.setDynamicSortFilter(True) self.setModel(self.proxy_model) self.models = {} self._device = None # Set the property last self.device = device def clear(self): for model in self.models.values(): model.stop() self.models.clear() self._device = None @property def device(self): return self._device @device.setter def device(self, device): if device is self._device: return if self._device is not None: self.models[device].stop() self._device = device if device: try: model = self.models[device] except KeyError: model = PolledDeviceModel(device=device) self.models[device] = model model.start() self.proxy_model.setSourceModel(model)
class ToolTable(QTableView): toolSelected = Signal(int) def __init__(self, parent=None): super(ToolTable, self).__init__(parent) self.clicked.connect(self.onClick) self.tool_model = ToolModel(self) self.item_delegate = ItemDelegate(columns=self.tool_model._columns) self.setItemDelegate(self.item_delegate) self.proxy_model = QSortFilterProxyModel() self.proxy_model.setFilterKeyColumn(0) self.proxy_model.setSourceModel(self.tool_model) self.setModel(self.proxy_model) # Properties self._columns = self.tool_model._columns self._confirm_actions = False self._current_tool_color = QColor('sage') self._current_tool_bg = None # Appearance/Behaviour settings self.setSortingEnabled(True) self.verticalHeader().hide() self.setAlternatingRowColors(True) self.setSelectionBehavior(QTableView.SelectRows) self.setSelectionMode(QTableView.SingleSelection) self.horizontalHeader().setStretchLastSection(True) self.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder) @Slot() def saveToolTable(self): if not self.confirmAction("Do you want to save changes and\n" "load tool table into LinuxCNC?"): return self.tool_model.saveToolTable() @Slot() def loadToolTable(self): if not self.confirmAction("Do you want to re-load the tool table?\n" "All unsaved changes will be lost."): return self.tool_model.loadToolTable() @Slot() def deleteSelectedTool(self): """Delete the currently selected item""" current_row = self.selectedRow() if current_row == -1: # no row selected return tdata = self.tool_model.toolDataFromRow(current_row) tnum = tdata['T'] # should not delete tool if currently loaded in spindle. Warn user if tnum == self.tool_model.stat.tool_in_spindle: box = QMessageBox( QMessageBox.Warning, "Can't delete current tool!", "Tool #{} is currently loaded in the spindle.\n" "Please remove tool from spindle and try again.".format(tnum), QMessageBox.Ok, parent=self) box.show() return False if not self.confirmAction( 'Are you sure you want to delete T{tdata[T]}?\n' '"{tdata[R]}"'.format(tdata=tdata)): return self.tool_model.removeTool(current_row) @Slot() def selectPrevious(self): """Select the previous item in the view.""" self.selectRow(self.selectedRow() - 1) return True @Slot() def selectNext(self): """Select the next item in the view.""" self.selectRow(self.selectedRow() + 1) return True @Slot() def clearToolTable(self, confirm=True): """Remove all items from the model""" if confirm: if not self.confirmAction( "Do you want to delete the whole tool table?"): return self.tool_model.clearToolTable() @Slot() def addTool(self): """Appends a new item to the model""" self.tool_model.addTool() self.selectRow(self.tool_model.rowCount() - 1) @Slot() def loadSelectedTool(self): """Loads the currently selected tool""" # see: https://forum.linuxcnc.org/41-guis/36042?start=50#151820 current_row = self.selectedRow() if current_row == -1: # no row selected return tnum = self.tool_model.toolDataFromRow(current_row)['T'] issue_mdi("T%s M6" % tnum) def selectedRow(self): """Returns the row number of the currently selected row, or 0""" tool_no = self.selectionModel().currentIndex().row() return tool_no def onClick(self, index): row = index.row() tnum = self.tool_model.toolDataFromRow(row)['T'] self.toolSelected.emit(tnum) def confirmAction(self, message): if not self._confirm_actions: return True box = QMessageBox.question(self, 'Confirm Action', message, QMessageBox.Yes, QMessageBox.No) if box == QMessageBox.Yes: return True else: return False @Property(bool) def confirmActions(self): return self._confirm_actions @confirmActions.setter def confirmActions(self, confirm): self._confirm_actions = confirm @Property(QColor) def currentToolColor(self): return self.tool_model.current_tool_color @currentToolColor.setter def currentToolColor(self, color): self.tool_model.current_tool_color = color @Property(QColor) def currentToolBackground(self): return self.tool_model.current_tool_bg or QColor() @currentToolBackground.setter def currentToolBackground(self, color): self.tool_model.current_tool_bg = color def insertToolAbove(self): # it does not make sense to insert tools, since the numbering # of all the other tools would have to change. self.addTool() raise DeprecationWarning("insertToolAbove() will be removed in " "the future, use addTool() instead") def insertToolBelow(self): self.addTool() raise DeprecationWarning("insertToolBelow() will be removed in " "the future, use addTool() instead")
class OffsetTable(QTableView): def __init__(self, parent=None): super(OffsetTable, self).__init__(parent) self.setEnabled(False) self.offset_model = OffsetModel(self) # Properties self._columns = self.offset_model._columns self._confirm_actions = False self._current_row_color = QColor('sage') self.proxy_model = QSortFilterProxyModel() self.proxy_model.setFilterKeyColumn(0) self.proxy_model.setSourceModel(self.offset_model) self.item_delegate = ItemDelegate(columns=self._columns) self.setItemDelegate(self.item_delegate) self.setModel(self.proxy_model) # Appearance/Behaviour settings self.setSortingEnabled(False) self.setAlternatingRowColors(True) self.setSelectionBehavior(QTableView.SelectRows) self.setSelectionMode(QTableView.SingleSelection) self.horizontalHeader().setStretchLastSection(False) self.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder) STATUS.all_axes_homed.notify(self.handle_home_signal) def handle_home_signal(self, all_axes): if all_axes: self.setEnabled(True) else: self.setEnabled(False) @Slot() def saveOffsetTable(self): if self.isEnabled(): if not self.confirmAction("Do you want to save changes and\n" "load offset table into LinuxCNC?"): return self.offset_model.saveOffsetTable() @Slot() def loadOffsetTable(self): if not self.confirmAction("Do you want to re-load the offset table?\n" "All unsaved changes will be lost."): return self.offset_model.loadOffsetTable() @Slot() def deleteSelectedOffset(self): """Delete the currently selected item""" current_row = self.selectedRow() if current_row == -1: # no row selected return if not self.confirmAction("Are you sure you want to delete offset {}?".format(current_row)): return self.offset_model.clearRow(current_row) # @Slot() # def selectPrevious(self): # """Select the previous item in the view.""" # self.selectRow(self.selectedRow() - 1) # return True # @Slot() # def selectNext(self): # """Select the next item in the view.""" # self.selectRow(self.selectedRow() + 1) # return True @Slot() def clearOffsetTable(self, confirm=True): """Remove all items from the model""" if confirm: if not self.confirmAction("Do you want to delete the whole offsets table?"): return self.offset_model.clearRows() def selectedRow(self): """Returns the row number of the currently selected row, or 0""" return self.selectionModel().currentIndex().row() def confirmAction(self, message): if not self._confirm_actions: return True box = QMessageBox.question(self, 'Confirm Action', message, QMessageBox.Yes, QMessageBox.No) if box == QMessageBox.Yes: return True else: return False @Property(str) def displayColumns(self): return "".join(self._columns) @displayColumns.setter def displayColumns(self, columns): self._columns = [col for col in columns.upper() if col in 'XYZABCUVWR'] self.offset_model.setColumns(self._columns) self.itemDelegate().setColumns(self._columns) @Property(bool) def confirmActions(self): return self._confirm_actions @confirmActions.setter def confirmActions(self, confirm): self._confirm_actions = confirm @Property(QColor) def currentRowColor(self): return self.offset_model.current_row_color @currentRowColor.setter def currentRowColor(self, color): self.offset_model.current_row_color = color
class ToolTable(QTableView): def __init__(self, parent=None): super(ToolTable, self).__init__(parent) self.tool_model = ToolModel(self) self.item_delegate = ItemDelegate(columns=self.tool_model._columns) self.setItemDelegate(self.item_delegate) self.proxy_model = QSortFilterProxyModel() self.proxy_model.setFilterKeyColumn(0) self.proxy_model.setSourceModel(self.tool_model) self.setModel(self.proxy_model) # Properties self._columns = self.tool_model._columns self._confirm_actions = False self._current_tool_color = QColor('sage') # Appearance/Behaviour settings self.setSortingEnabled(True) self.verticalHeader().hide() self.setAlternatingRowColors(True) self.setSelectionBehavior(QTableView.SelectRows) self.setSelectionMode(QTableView.SingleSelection) self.horizontalHeader().setStretchLastSection(True) self.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder) @Slot() def saveToolTable(self): if not self.confirmAction("Do you want to save changes and\n" "load tool table into LinuxCNC?"): return self.tool_model.saveToolTable() @Slot() def loadToolTable(self): if not self.confirmAction("Do you want to re-load the tool table?\n" "All unsaved changes will be lost."): return self.tool_model.loadToolTable() @Slot() def deleteSelectedTool(self): """Delete the currently selected item""" current_row = self.selectedRow() if current_row == -1: # no row selected return tdata = self.tool_model.toolDataFromRow(current_row) if not self.confirmAction( "Are you sure you want to delete T{tdata[T]}?\n" "{tdata[R]}".format(tdata=tdata)): return self.tool_model.removeTool(current_row) @Slot() def selectPrevious(self): """Select the previous item in the view.""" self.selectRow(self.selectedRow() - 1) return True @Slot() def selectNext(self): """Select the next item in the view.""" self.selectRow(self.selectedRow() + 1) return True @Slot() def clearToolTable(self, confirm=True): """Remove all items from the model""" if confirm: if not self.confirmAction( "Do you want to delete the whole tool table?"): return self.tool_model.clearToolTable() @Slot() def addTool(self): """Appends a new item to the model""" self.tool_model.addTool() self.selectRow(self.tool_model.rowCount() - 1) def selectedRow(self): """Returns the row number of the currently selected row, or 0""" return self.selectionModel().currentIndex().row() def confirmAction(self, message): if not self._confirm_actions: return True box = QMessageBox.question(self, 'Confirm Action', message, QMessageBox.Yes, QMessageBox.No) if box == QMessageBox.Yes: return True else: return False @Property(str) def displayColumns(self): return "".join(self._columns) @displayColumns.setter def displayColumns(self, columns): self._columns = [ col for col in columns.upper() if col in 'TPXYZABCUVWDIJQR' ] self.tool_model.setColumns(self._columns) self.itemDelegate().setColumns(self._columns) @Property(bool) def confirmActions(self): return self._confirm_actions @confirmActions.setter def confirmActions(self, confirm): self._confirm_actions = confirm @Property(QColor) def currentToolColor(self): return self.tool_model.current_tool_color @currentToolColor.setter def currentToolColor(self, color): self.tool_model.current_tool_color = color def insertToolAbove(self): # it does not make sense to insert tools, since the numbering # of all the other tools would have to change. self.addTool() raise DeprecationWarning("insertToolAbove() will be removed in " "the future, use addTool() instead") def insertToolBelow(self): self.addTool() raise DeprecationWarning("insertToolBelow() will be removed in " "the future, use addTool() instead")
class AbstractComboListInputWidget(QComboBox): """ A custom QComboBox with a completer / model. This is designed to be an abstract class that will be inherited by the the GSV and Node ComboBoxes Attributes: exists (bool) flag used to determine whether or not the popup menu for the menu change should register or not (specific to copy/paste of a node. In plain english... this flag is toggled to hide the Warning PopUp Box from displaying to the user in some events. item_list (list): string list of all of the items in the list. Updating this will auto update the default settings for blank setups previous_text (str): the previous items text. This is stored for cancel events and allowing the user to return to the previous item after cancelling. """ TYPE = 'list' def __init__(self, parent=None): super(AbstractComboListInputWidget, self).__init__(parent) self.main_widget = self.parent() self.previous_text = '' self.setExistsFlag(True) # setup line edit #self.line_edit = QLineEdit("Select & Focus", self) self.line_edit = QLineEdit(self) self.line_edit.editingFinished.connect(self.userFinishedEditing) self.setLineEdit(self.line_edit) self.setEditable(True) # setup completer self.completer = QCompleter(self) self.completer.setCompletionMode(QCompleter.PopupCompletion) self.completer.setCaseSensitivity(Qt.CaseInsensitive) self.completer.setPopup(self.view()) self.setCompleter(self.completer) self.pFilterModel = QSortFilterProxyModel(self) # set size policy ( this will ignore the weird resizing effects) size_policy = QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Preferred) self.setSizePolicy(size_policy) # initialize model... model = QStandardItemModel() self.setModel(model) self.setModelColumn(0) def populate(self, item_list): """ Creates all of the items from the item list and appends them to the model. item_list (list): list of strings to be displayed to the user this should be set with the setCleanItemsFunction. """ self.setItemList(item_list) for i, item_name in enumerate(self.getItemList()): item = QStandardItem(item_name) self.model().setItem(i, 0, item) self.setExistsFlag(False) def update(self): """ Updates the model items with all of the graph state variables. This is very similar to the populate call, except that with this, it will remove all of the items except for the current one. Which will ensure that an indexChanged event is not registered thus updating the UI. """ self.setExistsFlag(False) self.model().clear() self.populate(self.getCleanItems()) self.setCurrentIndexToText(self.previous_text) self.setExistsFlag(True) def setModel(self, model): # somehow this super makes the node type not resize... super(AbstractListInputWidget, self).setModel(model) self.pFilterModel.setSourceModel(model) self.completer.setModel(self.pFilterModel) def setModelColumn(self, column): self.completer.setCompletionColumn(column) self.pFilterModel.setFilterKeyColumn(column) super(AbstractListInputWidget, self).setModelColumn(column) def view(self): return self.completer.popup() """ UTILS """ def next_completion(self): row = self.completer.currentRow() # if does not exist reset if not self.completer.setCurrentRow(row + 1): self.completer.setCurrentRow(0) # if initializing if self.completer.popup().currentIndex().row() == -1: self.completer.setCurrentRow(0) index = self.completer.currentIndex() self.completer.popup().setCurrentIndex(index) def previous_completion(self): row = self.completer.currentRow() numRows = self.completer.completionCount() # if wrapping if not self.completer.setCurrentRow(row - 1): self.completer.setCurrentRow(numRows - 1) # if initializing if self.completer.popup().currentIndex().row() == -1: self.completer.setCurrentRow(numRows - 1) index = self.completer.currentIndex() self.completer.popup().setCurrentIndex(index) def setCurrentIndexToText(self, text): """ Sets the current index to the text provided. If no match can be found, this will default back to the first index. If no first index... create '' ? """ self.setExistsFlag(False) # get all matches items = self.model().findItems(text, Qt.MatchExactly) # set to index of match if len(items) > 0: index = self.model().indexFromItem(items[0]).row() self.setCurrentIndex(index) else: self.setCurrentIndex(0) self.previous_text = self.currentText() self.setExistsFlag(True) def isUserInputValid(self): """ Determines if the new user input is currently in the model. Returns True if this is an existing item, Returns false if it is not. """ items = self.model().findItems(self.currentText(), Qt.MatchExactly) if len(items) > 0: return True else: return False def setupStyleSheet(self): width = self.width() dropdown_width = int(width * 0.35) style_sheet_args = iColor.style_sheet_args style_sheet_args['width'] = dropdown_width # QComboBox {{ # border: None; # background-color: rgba{rgba_gray_0} # }} style_sheet = """ QComboBox{{ border: None; background-color: rgba{rgba_gray_0}; color: rgba{rgba_text}; }} QComboBox::drop-down {{ width: {width}px; }} QLineEdit{{ border: None; background-color: rgba{rgba_gray_0}; color: rgba{rgba_text}; }} QListView{{ border: None; background-color: rgba{rgba_gray_0}; color: rgba{rgba_text}; }} QListView::item:hover{{ background-color: rgba(255,0,0,255); }} """.format(**style_sheet_args) self.completer.popup().setStyleSheet(""" QListView{{ border: None; background-color: rgba{rgba_gray_0}; color: rgba{rgba_text}; }} QListView::item:hover{{ background-color: rgba(255,0,0,255); }} """.format(**style_sheet_args)) self.setStyleSheet(style_sheet) """ API """ def __selectionChangedEmit(self): pass def setSelectionChangedEmitEvent(self, method): """ sets the method for the selection changed emit call this will be called everytime the user hits enter/return inside of the line edits as a way of sending an emit signal from the current text changed (finalized) before input changed event... """ self.__selectionChangedEmit = method def __getCleanItems(self): return [] def setCleanItemsFunction(self, function): """ Sets the function to get the list of strings to populate the model function (function): function to return a list of strings to be shown to the user """ self.__getCleanItems = function def getCleanItems(self): """ Returns a list of strings based off of the function set with setCleanItemsFunction """ return self.__getCleanItems() """ EVENTS """ def userFinishedEditing(self): is_input_valid = self.isUserInputValid() if is_input_valid: self.__selectionChangedEmit() self.previous_text = self.currentText() #self.userFinishedEditingEvent(self.currentText()) else: self.setCurrentIndexToText(self.previous_text) def resizeEvent(self, event): self.setupStyleSheet() return QComboBox.resizeEvent(self, event) def event(self, event, *args, **kwargs): """ Registering key presses in here as for some reason they don't work in the keyPressEvent method... """ if event.type() == QEvent.KeyPress: # tab if event.key() == Qt.Key_Tab: self.next_completion() return True # shift tab elif event.key() == Qt.Key_Tab + 1: self.previous_completion() return True # enter pressed elif event.key() in [Qt.Key_Return, Qt.Key_Enter, Qt.Key_Down]: self.__selectionChangedEmit() elif event.type() == QEvent.MouseButtonRelease: self.completer.setPopup(self.view()) return QComboBox.event(self, event, *args, **kwargs) """ PROPERTIES """ def getExistsFlag(self): return self._exists def setExistsFlag(self, exists): self._exists = exists def getItemList(self): return self._item_list def setItemList(self, item_list): if self.previous_text == '': item_list.insert(0, '') self._item_list = item_list @property def previous_text(self): return self._previous_text @previous_text.setter def previous_text(self, previous_text): self._previous_text = previous_text