manuskript/manuskript/ui/tools/splitDialog.py

71 lines
2 KiB
Python
Raw Normal View History

2017-11-11 04:26:23 +13:00
#!/usr/bin/env python
# --!-- coding: utf8 --!--
from PyQt5.QtWidgets import QInputDialog
2017-11-11 05:21:02 +13:00
from manuskript.functions import mainWindow
2017-11-11 04:26:23 +13:00
class splitDialog(QInputDialog):
"""
Opens a dialog to split indexes.
"""
def __init__(self, parent, indexes, mark=None):
"""
@param parent: a QWidget, for the dialog.
@param indexes: a list of QModelIndex in the outlineModel
@param default: the default split mark
"""
QInputDialog.__init__(self, parent)
description = self.tr("""
<p>Split selected item(s) at the given mark.</p>
<p>If one of the selected item is a folder, it will be applied
recursively to <i>all</i> of it's children items.</p>
2017-11-11 21:55:36 +13:00
<p>The split mark can contain folling escape sequences:
2017-11-11 04:26:23 +13:00
<ul>
<li><b><code>\\n</code></b>: line break</li>
<li><b><code>\\t</code></b>: tab</li>
</ul>
</p>
<p><b>Mark:</b></p>
""")
if not mark:
mark = "\\n---\\n"
mark = mark.replace("\n", "\\n")
mark = mark.replace("\t", "\\t")
self.setLabelText(description)
self.setTextValue(mark)
2017-11-11 05:21:02 +13:00
if len(indexes) == 0:
return
if len(indexes) == 1:
idx = indexes[0]
self.setWindowTitle(
self.tr("Split '{}'").format(self.getItem(idx).title())
)
else:
self.setWindowTitle(self.tr("Split items"))
2017-11-11 04:26:23 +13:00
r = self.exec()
mark = self.textValue()
if r and mark:
mark = mark.replace("\\n", "\n")
mark = mark.replace("\\t", "\t")
for idx in indexes:
2017-11-11 05:21:02 +13:00
item = self.getItem(idx)
item.split(mark)
def getItem(self, index):
if index.isValid():
return index.internalPointer()
else:
return mainWindow().mdlOutline.rootItem