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