Ejemplo n.º 1
0
    def setup_ui(self):
        uic.loadUi('Pretzel/ui/items/removeitems/removeitems.ui', self)

        self.items_model = ItemsModel()
        self.items_list.setModel(self.items_model)

        self.bind_signals()
Ejemplo n.º 2
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.items_model = ItemsModel()

        # Setup the user interface
        self.setup_ui()
Ejemplo n.º 3
0
class ViewItems(QDockWidget):
    def __init__(self, *args, parent=None, **kwargs):
        super().__init__(*args, **kwargs)

        self.items_model = ItemsModel()
        # TODO: Add the ability to (at least) copy the notes
        # TODO: Add the ability to view the pictograms in self.items_view
        self.table_model = ItemsTableModel(headers=[
            "Name", "Chemical Formula", "Warning Label", "Danger Level"
        ])  #, "Notes", "Pictograms"])

        self.setup_ui()

    def setup_ui(self):
        # TODO: Add QStackedLayout for different filter options (not just name)
        uic.loadUi("Pretzel/ui/items/viewitems/viewitems.ui", self)

        # Setup the table
        self.items_view.setAlternatingRowColors(True)
        self.items_view.setSortingEnabled(True)

        # Setup the sorting
        self.proxy_model = QSortFilterProxyModel(self)
        self.proxy_model.setFilterKeyColumn(0)
        self.proxy_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
        self.proxy_model.sort(0, Qt.AscendingOrder)
        self.proxy_model.setSourceModel(self.table_model)

        # Setup the models
        self.items_view.setModel(self.proxy_model)
        self.load_items()

        # Set the delegates
        # Warning label delegate
        warning_label_delegate = WarningLabelDelegate(self)
        self.items_view.setItemDelegateForColumn(2, warning_label_delegate)
        # Danger level delegate
        danger_level_delegate = DangerLevelDelegate(self)
        self.items_view.setItemDelegateForColumn(3, danger_level_delegate)

        self.bind_signals()

    def bind_signals(self):
        self.filter_entry.textEdited.connect(self.filter_items)

    def load_items(self):
        items = []
        for i in load_items(database=utils.get_database_path()):
            items.append(list(i.values())[:-2])

        self.items_model.items = items
        self.items_model.update()
        self.table_model.layoutAboutToBeChanged.emit()
        self.table_model.mirror_model(self.items_model)

    @pyqtSlot(str)
    def filter_items(self, text: str):
        # Filter the packages
        self.proxy_model.setFilterFixedString(text)
Ejemplo n.º 4
0
    def __init__(self, *args, parent=None, **kwargs):
        super().__init__(*args, **kwargs)

        self.items_model = ItemsModel()
        # TODO: Add the ability to (at least) copy the notes
        # TODO: Add the ability to view the pictograms in self.items_view
        self.table_model = ItemsTableModel(headers=[
            "Name", "Chemical Formula", "Warning Label", "Danger Level"
        ])  #, "Notes", "Pictograms"])

        self.setup_ui()
Ejemplo n.º 5
0
class RemoveItems(QDockWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Setup the user interface
        self.setup_ui()

    def setup_ui(self):
        uic.loadUi('Pretzel/ui/items/removeitems/removeitems.ui', self)

        self.items_model = ItemsModel()
        self.items_list.setModel(self.items_model)

        self.bind_signals()

    def bind_signals(self):
        self.button_add_items.clicked.connect(self.add_items)
        self.button_remove_items.clicked.connect(self.remove_items)

        self.remove_items_button.clicked.connect(self.delete_items)

    @pyqtSlot()
    def add_items(self):
        """ Adds items to the items list """
        add_items_dialog = AddItemsDialog(self)
        add_items_dialog.exec()

    @pyqtSlot()
    def remove_items(self):
        """ Removes the selected items from the items list"""
        indexes = self.items_list.selectedIndexes()

        if indexes:
            first_index = True
            for index in indexes[::-1]:
                # TODO: See if the following is a shortcut way of removing items
                # self.items_model.removeRow(index)

                # Remove the item and refresh
                if first_index:
                    del self.items_model.items[index.row()]
                    first_index = False
                else:
                    del self.items_model.items[index.row() - 1]

            self.items_model.update()

            # Clear the selection (as it is no longer valid)
            self.items_list.clearSelection()

    @pyqtSlot()
    def delete_items(self):
        """ Deletes the items from the database"""

        items = []

        for item_index in range(self.items_model.rowCount()):
            name = self.items_model.items[item_index]["Name"]
            items.append((name))

        remove_items(items)

        self.items_model.items.clear()
        self.items_model.update()
Ejemplo n.º 6
0
class AddItems(QDockWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.items_model = ItemsModel()

        # Setup the user interface
        self.setup_ui()

    def setup_ui(self):
        uic.loadUi("Pretzel/ui/items/additems/additems.ui", self)

        self.items_list.setModel(self.items_model)
        self.previous_index = None

        # Setup the warning label combobox
        self.warning_labels = ["None", "Warning", "Danger"]
        self.combo_warning_label.addItems(self.warning_labels)
        self.combo_warning_label.setCurrentIndex(0)

        # Setup the danger level combobox
        self.danger_levels = ["None", "Low", "Moderate", "High", "Extreme"]
        self.combo_danger_level.addItems(self.danger_levels)
        self.combo_danger_level.setCurrentIndex(0)

        self.bind_signals()

    def bind_signals(self):
        self.button_add_items.clicked.connect(self.add_item)
        self.button_remove_items.clicked.connect(self.remove_items)

        self.items_list.clicked.connect(self.update_item_properties_index)

        # Cycle through layouts
        # TODO: Look at using either QDataWidgetMapper or pyqtconfig (https://github.com/learnpyqt/pyqtconfig)
        for widget_num in range(self.general_tab.layout().count()):
            widget = self.general_tab.layout().itemAt(widget_num)
            if (type(widget) == QHBoxLayout) or (type(widget) == QVBoxLayout):
                for w_num in range(widget.count()):
                    w = widget.itemAt(w_num).widget()
                    if type(w) == QLineEdit:
                        # Use textEdited instead of textChanged!
                        w.textEdited.connect(
                            self.update_item_properties_widget)
                    elif type(w) == QTextEdit:
                        w.textChanged.connect(
                            self.update_item_properties_widget)
                    elif type(w) == QComboBox:
                        w.activated.connect(self.update_item_properties_widget)
                    else:
                        pass

        self.button_add_pictograms.clicked.connect(self.add_pictograms)
        self.button_remove_pictograms.clicked.connect(self.remove_pictograms)

        self.button_save_items.clicked.connect(self.save_items)

    def save_item_settings(self, item_index):
        """ Save an item's settings

            :param item_index: The index of the item to save the settings to """

        row = item_index.row()
        try:
            settings = self.items_model.items[row]
            settings["Name"] = self.entry_name.text()
            settings["Chemical Formula"] = self.entry_chem_formula.text()
            settings["Warning Label"] = self.warning_labels[
                self.combo_warning_label.currentIndex()]
            settings["Danger Level"] = self.danger_levels[
                self.combo_danger_level.currentIndex()]
            settings["Notes"] = self.notes_text.toMarkdown()
            self.items_model.dataChanged.emit(item_index, item_index)
        except:
            pass

    def update_item_display(self, item_index):
        """ Update an item's display

            :param item_index: The index of the item to update the display to """
        # TODO: Get live preview of markdown working

        row = item_index.row()
        try:
            settings = self.items_model.items[row]
            self.entry_name.setText(settings["Name"])
            self.entry_chem_formula.setText(settings["Chemical Formula"])
            self.combo_warning_label.setCurrentIndex(
                self.warning_labels.index(settings["Warning Label"]))
            self.combo_danger_level.setCurrentIndex(
                self.danger_levels.index(settings["Danger Level"]))
            self.notes_text.setMarkdown(settings["Notes"])
            self.pictograms_list.setModel(settings["Pictograms"])
        except:
            pass

    def update_item_properties(self, current_index=None, previous_index=None):
        # Save the last item first, then update the display
        if previous_index:
            # Save the item settings
            self.save_item_settings(previous_index)

        if current_index:
            # Update the display
            self.update_item_display(current_index)

    @pyqtSlot()
    def add_item(self):
        self.items_model.items.append({
            "Name": "New Item",
            "Chemical Formula": "",
            "Warning Label": "None",
            "Danger Level": "None",
            "Notes": "",
            "Pictograms": PictogramModel()
        })

        self.items_model.update()
        self.pictograms_list.setModel(self.items_model.items[-1]["Pictograms"])

        self.entry_name.setText(self.items_model.items[-1]["Name"])

        new_item_index = self.items_model.index(-1)
        self.items_list.setCurrentIndex(new_item_index)

        self.update_item_properties_index(new_item_index)

    @pyqtSlot(QModelIndex)
    def update_item_properties_index(self, current_index: QModelIndex):
        """ Upates item properties based on a QModelIndex

            :param current_index: The current index of a QListView's model
            :type current_index: QModelIndex

        """

        # Don't use the previous index (creates a bug)
        self.update_item_properties(current_index=current_index)

        self.previous_index = current_index

    @pyqtSlot()
    def update_item_properties_widget(self):
        current_index = self.items_list.currentIndex()
        # For some reason, self.notes_text does not like the current_index parameter
        self.update_item_properties(previous_index=current_index)

    @pyqtSlot()
    def remove_items(self):
        indexes = self.items_list.selectedIndexes()

        if indexes:
            first_index = True
            for index in indexes[::-1]:
                # Remove the item and refresh
                if first_index:
                    del self.items_model.items[index.row()]
                    first_index = False
                else:
                    del self.items_model.items[index.row() - 1]

            self.items_model.update()

            # Clear the selection (as it is no longer valid)
            self.items_list.clearSelection()

            self.previous_index = None
            current_index = self.items_list.currentIndex()
            self.update_item_properties_index(current_index=current_index)

    @pyqtSlot()
    def add_pictograms(self):
        # shorten add_pictograms_dialog to apd
        apd = AddPictogramsDialog(parent=self)
        apd.exec()

    @pyqtSlot()
    def remove_pictograms(self):
        indexes = self.pictograms_list.selectedIndexes()

        if indexes:
            first_index = True
            for index in indexes[::-1]:
                # Remove the item and refresh
                item = self.items_model.items[
                    self.items_list.currentIndex().row()]
                if first_index:
                    item["Pictograms"].pictograms.pop(index.row())
                    first_index = False
                else:
                    item["Pictograms"].pictograms.pop(index.row() - 1)

            item["Pictograms"].update()

            # Clear the selection (as it is no longer valid)
            self.items_list.clearSelection()

            current_index = self.items_list.currentIndex()
            self.update_item_properties_index(current_index=current_index)

    @pyqtSlot()
    def save_items(self):
        """ Save the items to the item's database """
        items = []
        for item_index in range(self.items_model.rowCount()):
            name = self.items_model.items[item_index]["Name"]
            chem_formula = self.items_model.items[item_index][
                "Chemical Formula"]
            warning_label = self.items_model.items[item_index]["Warning Label"]
            danger_level = self.items_model.items[item_index]["Danger Level"]
            notes = self.items_model.items[item_index]["Notes"]
            pictos = self.items_model.items[item_index][
                "Pictograms"].pictograms
            pictograms = []
            for picto in pictos:
                # Append the pictograms image path
                pictograms.append(picto)

            pictograms = list_to_string(pictograms)

            items.append((name, chem_formula, warning_label, danger_level,
                          notes, pictograms))

        add_items(items)

        # Clear the items list
        self.items_model.items.clear()
        self.items_model.update()

        # Clear the display
        self.entry_name.setText("")
        self.entry_chem_formula.setText("")
        self.combo_warning_label.setCurrentIndex(
            self.warning_labels.index("None"))
        self.combo_danger_level.setCurrentIndex(
            self.danger_levels.index("None"))
        self.notes_text.setMarkdown("")
        self.pictograms_list.setModel(PictogramModel())