diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index a8a9cce3..9623520e 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -1072,46 +1072,14 @@ class NWProject(): return True def setStatusColours(self, newCols, delCols): - """Update the list of novel file status flags. Also iterate - through the project and replace keys that have been renamed. + """Update the list of novel file status flags. """ - if not (newCols or delCols): - return False - - for entry in newCols: - key = entry.get("key", None) - name = entry.get("name", "") - cols = entry.get("cols", (100, 100, 100)) - if name: - self.statusItems.write(key, name, cols) - - for key in delCols: - self.statusItems.remove(key) - - self.setProjectChanged(True) - - return True + return self._setStatusImport(newCols, delCols, self.statusItems) def setImportColours(self, newCols, delCols): - """Update the list of note file importance flags. Also iterate - through the project and replace keys that have been renamed. + """Update the list of note file importance flags. """ - if not (newCols or delCols): - return False - - for entry in newCols: - key = entry.get("key", None) - name = entry.get("name", "") - cols = entry.get("cols", (100, 100, 100)) - if name: - self.importItems.write(key, name, cols) - - for key in delCols: - self.importItems.remove(key) - - self.setProjectChanged(True) - - return True + return self._setStatusImport(newCols, delCols, self.importItems) def setAutoReplace(self, autoReplace): """Update the auto-replace dictionary. @@ -1251,6 +1219,28 @@ class NWProject(): # Internal Functions ## + def _setStatusImport(self, new, delete, target): + """Update the list of novel file status or importance flags, and + delete those that have been requested deleted. + """ + if not (new or delete): + return False + + order = [] + for entry in new: + key = entry.get("key", None) + name = entry.get("name", "") + cols = entry.get("cols", (100, 100, 100)) + if name: + order.append(target.write(key, name, cols)) + + for key in delete: + target.remove(key) + + target.reorder(order) + + return True + def _loadProjectLocalisation(self): """Load the language data for the current project language. """ diff --git a/novelwriter/core/status.py b/novelwriter/core/status.py index 4e9317f2..9fcfbb88 100644 --- a/novelwriter/core/status.py +++ b/novelwriter/core/status.py @@ -167,6 +167,28 @@ class NWStatus(): else: return self._defaultIcon + def reorder(self, order): + """Reorder the items according to list. + """ + if len(order) != len(self._store): + logger.error("Length mismatch between new and old order") + return False + + if order == list(self._store.keys()): + return True + + store = {} + for key in order: + if key in self._store: + store[key] = self._store[key] + else: + logger.error("Unknown key '%s' in order", key) + return False + + self._store = store + + return True + def resetCounts(self): """Clear the counts of references to the status entries. """ diff --git a/novelwriter/dialogs/projsettings.py b/novelwriter/dialogs/projsettings.py index 9599cc92..a0987418 100644 --- a/novelwriter/dialogs/projsettings.py +++ b/novelwriter/dialogs/projsettings.py @@ -306,6 +306,12 @@ class GuiProjectEditStatus(QWidget): self.delButton = QPushButton(self.theTheme.getIcon("remove"), "") self.delButton.clicked.connect(self._delItem) + self.upButton = QPushButton(self.theTheme.getIcon("up"), "") + self.upButton.clicked.connect(lambda: self._moveItem(-1)) + + self.dnButton = QPushButton(self.theTheme.getIcon("down"), "") + self.dnButton.clicked.connect(lambda: self._moveItem(1)) + # Edit Form # ========= @@ -315,7 +321,7 @@ class GuiProjectEditStatus(QWidget): self.editName.setPlaceholderText(self.tr("Select item to edit")) self.colPixmap = QPixmap(self.iPx, self.iPx) - self.colPixmap.fill(QColor(120, 120, 120)) + self.colPixmap.fill(QColor(100, 100, 100)) self.colButton = QPushButton(QIcon(self.colPixmap), self.tr("Colour")) self.colButton.setIconSize(self.colPixmap.rect().size()) self.colButton.clicked.connect(self._selectColour) @@ -329,6 +335,8 @@ class GuiProjectEditStatus(QWidget): self.listControls = QVBoxLayout() self.listControls.addWidget(self.addButton) self.listControls.addWidget(self.delButton) + self.listControls.addWidget(self.upButton) + self.listControls.addWidget(self.dnButton) self.listControls.addStretch(1) self.editBox = QHBoxLayout() @@ -390,7 +398,7 @@ class GuiProjectEditStatus(QWidget): def _newItem(self): """Create a new status item. """ - newItem = self._addItem(None, self.tr("New Item"), (0, 0, 0), 0) + newItem = self._addItem(None, self.tr("New Item"), (100, 100, 100), 0) newItem.setBackground(self.COL_LABEL, QBrush(QColor(0, 255, 0, 70))) newItem.setBackground(self.COL_USAGE, QBrush(QColor(0, 255, 0, 70))) self.colChanged = True @@ -445,23 +453,47 @@ class GuiProjectEditStatus(QWidget): return item + def _moveItem(self, step): + """Move and item up or down step. + """ + selItem = self._getSelectedItem() + if selItem is None: + return + + tIndex = self.listBox.indexOfTopLevelItem(selItem) + nChild = self.listBox.topLevelItemCount() + nIndex = tIndex + step + if nIndex < 0 or nIndex >= nChild: + return False + + cItem = self.listBox.takeTopLevelItem(tIndex) + self.listBox.insertTopLevelItem(nIndex, cItem) + self.listBox.clearSelection() + + cItem.setSelected(True) + self.colChanged = True + + return + def _selectedItem(self): """Extract the info of a selected item and populate the settings boxes and button. """ selItem = self._getSelectedItem() - if selItem is not None: - cols = selItem.data(self.COL_LABEL, self.COL_ROLE) - name = selItem.text(self.COL_LABEL) + if selItem is None: + return - pixmap = QPixmap(self.iPx, self.iPx) - pixmap.fill(QColor(*cols)) - self.selColour = QColor(*cols) - self.editName.setText(name) - self.colButton.setIcon(QIcon(pixmap)) - self.editName.setEnabled(True) - self.editName.selectAll() - self.editName.setFocus() + cols = selItem.data(self.COL_LABEL, self.COL_ROLE) + name = selItem.text(self.COL_LABEL) + + pixmap = QPixmap(self.iPx, self.iPx) + pixmap.fill(QColor(*cols)) + self.selColour = QColor(*cols) + self.editName.setText(name) + self.colButton.setIcon(QIcon(pixmap)) + self.editName.setEnabled(True) + self.editName.selectAll() + self.editName.setFocus() return @@ -477,12 +509,6 @@ class GuiProjectEditStatus(QWidget): return selItem[0] return None - def _rowsMoved(self): - """A row has been moved, so set the changed flag. - """ - self.colChanged = True - return - def _usageString(self, nUse): """Generate usage string. """