Convert split and merge dialogs into class method dialogs
This commit is contained in:
@@ -27,7 +27,6 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
from PyQt5.QtCore import Qt, pyqtSlot
|
||||
from PyQt5.QtGui import QCloseEvent
|
||||
from PyQt5.QtWidgets import (
|
||||
QAbstractItemView, QDialogButtonBox, QGridLayout, QLabel, QListWidget,
|
||||
QListWidgetItem, QVBoxLayout, QWidget
|
||||
@@ -37,7 +36,7 @@ from novelwriter import CONFIG, SHARED
|
||||
from novelwriter.extensions.configlayout import NColourLabel
|
||||
from novelwriter.extensions.modified import NDialog
|
||||
from novelwriter.extensions.switch import NSwitch
|
||||
from novelwriter.types import QtDialogCancel, QtDialogOk, QtDialogReset, QtUserRole
|
||||
from novelwriter.types import QtAccepted, QtDialogCancel, QtDialogOk, QtDialogReset, QtUserRole
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -118,7 +117,7 @@ class GuiDocMerge(NDialog):
|
||||
logger.debug("Delete: GuiDocMerge")
|
||||
return
|
||||
|
||||
def getData(self) -> dict:
|
||||
def data(self) -> dict:
|
||||
"""Return the user's choices."""
|
||||
finalItems = []
|
||||
for i in range(self.listBox.count()):
|
||||
@@ -131,15 +130,15 @@ class GuiDocMerge(NDialog):
|
||||
|
||||
return self._data
|
||||
|
||||
##
|
||||
# Events
|
||||
##
|
||||
|
||||
def closeEvent(self, event: QCloseEvent) -> None:
|
||||
"""Capture the close event and perform cleanup."""
|
||||
event.accept()
|
||||
self.deleteLater()
|
||||
return
|
||||
@classmethod
|
||||
def getData(cls, parent: QWidget, handle: str, items: list[str]) -> tuple[dict, bool]:
|
||||
"""Pop the dialog and return the result."""
|
||||
cls = GuiDocMerge(parent, handle, items)
|
||||
cls.exec()
|
||||
data = cls.data()
|
||||
accepted = cls.result() == QtAccepted
|
||||
cls.deleteLater()
|
||||
return data, accepted
|
||||
|
||||
##
|
||||
# Private Slots
|
||||
|
||||
@@ -27,7 +27,6 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
from PyQt5.QtCore import pyqtSlot
|
||||
from PyQt5.QtGui import QCloseEvent
|
||||
from PyQt5.QtWidgets import (
|
||||
QAbstractItemView, QComboBox, QDialogButtonBox, QGridLayout, QLabel,
|
||||
QListWidget, QListWidgetItem, QVBoxLayout, QWidget
|
||||
@@ -37,7 +36,7 @@ from novelwriter import CONFIG, SHARED
|
||||
from novelwriter.extensions.configlayout import NColourLabel
|
||||
from novelwriter.extensions.modified import NDialog
|
||||
from novelwriter.extensions.switch import NSwitch
|
||||
from novelwriter.types import QtDialogCancel, QtDialogOk, QtUserRole
|
||||
from novelwriter.types import QtAccepted, QtDialogCancel, QtDialogOk, QtUserRole
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -146,7 +145,7 @@ class GuiDocSplit(NDialog):
|
||||
logger.debug("Delete: GuiDocSplit")
|
||||
return
|
||||
|
||||
def getData(self) -> tuple[dict, list]:
|
||||
def data(self) -> tuple[dict, list[str]]:
|
||||
"""Return the user's choices. Also save the users options for
|
||||
the next time the dialog is used.
|
||||
"""
|
||||
@@ -179,15 +178,15 @@ class GuiDocSplit(NDialog):
|
||||
|
||||
return self._data, self._text
|
||||
|
||||
##
|
||||
# Events
|
||||
##
|
||||
|
||||
def closeEvent(self, event: QCloseEvent) -> None:
|
||||
"""Capture the close event and perform cleanup."""
|
||||
event.accept()
|
||||
self.deleteLater()
|
||||
return
|
||||
@classmethod
|
||||
def getData(cls, parent: QWidget, handle: str) -> tuple[dict, list[str], bool]:
|
||||
"""Pop the dialog and return the result."""
|
||||
cls = GuiDocSplit(parent, handle)
|
||||
cls.exec()
|
||||
data, text = cls.data()
|
||||
accepted = cls.result() == QtAccepted
|
||||
cls.deleteLater()
|
||||
return data, text, accepted
|
||||
|
||||
##
|
||||
# Private Slots
|
||||
|
||||
@@ -25,14 +25,11 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from PyQt5.QtWidgets import (
|
||||
QDialog, QDialogButtonBox, QHBoxLayout, QLabel, QLineEdit, QVBoxLayout,
|
||||
QWidget
|
||||
)
|
||||
from PyQt5.QtWidgets import QDialogButtonBox, QHBoxLayout, QLabel, QLineEdit, QVBoxLayout, QWidget
|
||||
|
||||
from novelwriter import CONFIG
|
||||
from novelwriter.extensions.modified import NDialog
|
||||
from novelwriter.types import QtDialogCancel, QtDialogOk
|
||||
from novelwriter.types import QtAccepted, QtDialogCancel, QtDialogOk
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -92,6 +89,6 @@ class GuiEditLabel(NDialog):
|
||||
cls = GuiEditLabel(parent, text=text)
|
||||
cls.exec()
|
||||
label = cls.itemLabel
|
||||
accepted = cls.result() == QDialog.DialogCode.Accepted
|
||||
accepted = cls.result() == QtAccepted
|
||||
cls.deleteLater()
|
||||
return label, accepted
|
||||
|
||||
@@ -28,14 +28,17 @@ import logging
|
||||
from PyQt5.QtCore import QSize, pyqtSlot
|
||||
from PyQt5.QtGui import QFontMetrics
|
||||
from PyQt5.QtWidgets import (
|
||||
QDialog, QDialogButtonBox, QFrame, QHBoxLayout, QLabel, QListWidget,
|
||||
QDialogButtonBox, QFrame, QHBoxLayout, QLabel, QListWidget,
|
||||
QListWidgetItem, QVBoxLayout, QWidget
|
||||
)
|
||||
|
||||
from novelwriter import CONFIG
|
||||
from novelwriter.constants import nwQuotes, trConst
|
||||
from novelwriter.extensions.modified import NDialog
|
||||
from novelwriter.types import QtAlignCenter, QtAlignTop, QtDialogCancel, QtDialogOk, QtUserRole
|
||||
from novelwriter.types import (
|
||||
QtAccepted, QtAlignCenter, QtAlignTop, QtDialogCancel, QtDialogOk,
|
||||
QtUserRole
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -127,7 +130,7 @@ class GuiQuoteSelect(NDialog):
|
||||
cls = GuiQuoteSelect(parent, current=current)
|
||||
cls.exec()
|
||||
quote = cls._selected
|
||||
accepted = cls.result() == QDialog.DialogCode.Accepted
|
||||
accepted = cls.result() == QtAccepted
|
||||
cls.deleteLater()
|
||||
return quote, accepted
|
||||
|
||||
|
||||
+16
-26
@@ -34,9 +34,8 @@ from time import time
|
||||
from PyQt5.QtCore import QPoint, Qt, QTimer, pyqtSignal, pyqtSlot
|
||||
from PyQt5.QtGui import QDragEnterEvent, QDragMoveEvent, QDropEvent, QIcon, QMouseEvent, QPalette
|
||||
from PyQt5.QtWidgets import (
|
||||
QAbstractItemView, QAction, QDialog, QFrame, QHBoxLayout, QHeaderView,
|
||||
QLabel, QMenu, QShortcut, QTreeWidget, QTreeWidgetItem, QVBoxLayout,
|
||||
QWidget
|
||||
QAbstractItemView, QAction, QFrame, QHBoxLayout, QHeaderView, QLabel,
|
||||
QMenu, QShortcut, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget
|
||||
)
|
||||
|
||||
from novelwriter import CONFIG, SHARED
|
||||
@@ -1397,14 +1396,10 @@ class GuiProjectTree(QTreeWidget):
|
||||
if not newFile:
|
||||
itemList.remove(tHandle)
|
||||
|
||||
dlgMerge = GuiDocMerge(SHARED.mainGui, tHandle, itemList)
|
||||
dlgMerge.exec()
|
||||
|
||||
if dlgMerge.result() == QDialog.DialogCode.Accepted:
|
||||
|
||||
mrgData = dlgMerge.getData()
|
||||
mrgList = mrgData.get("finalItems", [])
|
||||
if not mrgList:
|
||||
data, status = GuiDocMerge.getData(SHARED.mainGui, tHandle, itemList)
|
||||
if status:
|
||||
items = data.get("finalItems", [])
|
||||
if not items:
|
||||
SHARED.info(self.tr("No documents selected for merging."))
|
||||
return False
|
||||
|
||||
@@ -1424,7 +1419,7 @@ class GuiProjectTree(QTreeWidget):
|
||||
else:
|
||||
return False
|
||||
|
||||
for sHandle in mrgList:
|
||||
for sHandle in items:
|
||||
docMerger.appendText(sHandle, True, mLabel)
|
||||
|
||||
if not docMerger.writeTargetDoc():
|
||||
@@ -1441,8 +1436,8 @@ class GuiProjectTree(QTreeWidget):
|
||||
self.projView.openDocumentRequest.emit(mHandle, nwDocMode.EDIT, "", False)
|
||||
self.projView.setSelectedHandle(mHandle, doScroll=True)
|
||||
|
||||
if mrgData.get("moveToTrash", False):
|
||||
for sHandle in reversed(mrgData.get("finalItems", [])):
|
||||
if data.get("moveToTrash", False):
|
||||
for sHandle in reversed(data.get("finalItems", [])):
|
||||
trItem = self._getTreeItem(sHandle)
|
||||
if isinstance(trItem, QTreeWidgetItem) and trItem.childCount() == 0:
|
||||
self.moveItemToTrash(sHandle, askFirst=False, flush=False)
|
||||
@@ -1468,16 +1463,11 @@ class GuiProjectTree(QTreeWidget):
|
||||
logger.error("Only valid document items can be split")
|
||||
return False
|
||||
|
||||
dlgSplit = GuiDocSplit(SHARED.mainGui, tHandle)
|
||||
dlgSplit.exec()
|
||||
|
||||
if dlgSplit.result() == QDialog.DialogCode.Accepted:
|
||||
|
||||
splitData, splitText = dlgSplit.getData()
|
||||
|
||||
headerList = splitData.get("headerList", [])
|
||||
intoFolder = splitData.get("intoFolder", False)
|
||||
docHierarchy = splitData.get("docHierarchy", False)
|
||||
data, text, status = GuiDocSplit.getData(SHARED.mainGui, tHandle)
|
||||
if status:
|
||||
headerList = data.get("headerList", [])
|
||||
intoFolder = data.get("intoFolder", False)
|
||||
docHierarchy = data.get("docHierarchy", False)
|
||||
|
||||
docSplit = DocSplitter(SHARED.project, tHandle)
|
||||
if intoFolder:
|
||||
@@ -1487,7 +1477,7 @@ class GuiProjectTree(QTreeWidget):
|
||||
else:
|
||||
docSplit.setParentItem(tItem.itemParent)
|
||||
|
||||
docSplit.splitDocument(headerList, splitText)
|
||||
docSplit.splitDocument(headerList, text)
|
||||
for writeOk, dHandle, nHandle in docSplit.writeDocuments(docHierarchy):
|
||||
SHARED.project.index.reIndexHandle(dHandle)
|
||||
self.revealNewTreeItem(dHandle, nHandle=nHandle, wordCount=True)
|
||||
@@ -1498,7 +1488,7 @@ class GuiProjectTree(QTreeWidget):
|
||||
info=docSplit.getError()
|
||||
)
|
||||
|
||||
if splitData.get("moveToTrash", False):
|
||||
if data.get("moveToTrash", False):
|
||||
self.moveItemToTrash(tHandle, askFirst=False, flush=True)
|
||||
|
||||
self.saveTreeOrder()
|
||||
|
||||
@@ -25,7 +25,7 @@ from __future__ import annotations
|
||||
|
||||
from PyQt5.QtCore import QRegularExpression, Qt
|
||||
from PyQt5.QtGui import QColor, QFont, QPainter, QTextCharFormat, QTextCursor, QTextFormat
|
||||
from PyQt5.QtWidgets import QDialogButtonBox, QSizePolicy, QStyle
|
||||
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QSizePolicy, QStyle
|
||||
|
||||
# Qt Alignment Flags
|
||||
|
||||
@@ -80,6 +80,9 @@ QtMouseMiddle = Qt.MouseButton.MiddleButton
|
||||
|
||||
# Dialog Button Box Types
|
||||
|
||||
QtAccepted = QDialog.DialogCode.Accepted
|
||||
QtRejected = QDialog.DialogCode.Rejected
|
||||
|
||||
QtDialogApply = QDialogButtonBox.StandardButton.Apply
|
||||
QtDialogCancel = QDialogButtonBox.StandardButton.Cancel
|
||||
QtDialogClose = QDialogButtonBox.StandardButton.Close
|
||||
|
||||
Reference in New Issue
Block a user