2015-07-01 01:38:14 +12:00
|
|
|
#!/usr/bin/env python
|
2016-02-07 00:34:22 +13:00
|
|
|
# --!-- coding: utf8 --!--
|
|
|
|
from PyQt5.QtCore import Qt, QRect
|
|
|
|
from PyQt5.QtGui import QPalette, QFontMetrics
|
|
|
|
from PyQt5.QtWidgets import QWidget, QMenu, QAction, qApp, QListWidgetItem, QStyledItemDelegate, QStyle
|
|
|
|
|
|
|
|
from manuskript.enums import Outline
|
|
|
|
from manuskript.functions import mainWindow
|
2016-04-12 09:54:47 +12:00
|
|
|
from manuskript.ui import style
|
2016-02-07 00:34:22 +13:00
|
|
|
from manuskript.ui.search_ui import Ui_search
|
|
|
|
from manuskript.models import references as Ref
|
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
|
|
|
|
class search(QWidget, Ui_search):
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
QWidget.__init__(self, parent)
|
|
|
|
self.setupUi(self)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
self.options = {
|
|
|
|
"All": True,
|
|
|
|
"Title": True,
|
|
|
|
"Text": True,
|
|
|
|
"Summary": False,
|
|
|
|
"Notes": False,
|
|
|
|
"POV": False,
|
|
|
|
"Status": False,
|
|
|
|
"Label": False,
|
|
|
|
"CS": True
|
2016-02-07 00:34:22 +13:00
|
|
|
}
|
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
self.text.returnPressed.connect(self.search)
|
|
|
|
self.generateOptionMenu()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
self.delegate = listResultDelegate(self)
|
|
|
|
self.result.setItemDelegate(self.delegate)
|
2017-03-24 23:33:35 +13:00
|
|
|
self.result.itemClicked.connect(self.openItem)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-04-12 09:54:47 +12:00
|
|
|
self.result.setStyleSheet(style.searchResultSS())
|
|
|
|
self.text.setStyleSheet(style.lineEditSS())
|
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
def generateOptionMenu(self):
|
|
|
|
self.menu = QMenu(self)
|
|
|
|
a = QAction(self.tr("Search in:"), self.menu)
|
|
|
|
a.setEnabled(False)
|
|
|
|
self.menu.addAction(a)
|
|
|
|
for i, d in [
|
2016-02-07 00:34:22 +13:00
|
|
|
(self.tr("All"), "All"),
|
|
|
|
(self.tr("Title"), "Title"),
|
|
|
|
(self.tr("Text"), "Text"),
|
2015-07-01 01:38:14 +12:00
|
|
|
(self.tr("Summary"), "Summary"),
|
2016-02-07 00:34:22 +13:00
|
|
|
(self.tr("Notes"), "Notes"),
|
|
|
|
(self.tr("POV"), "POV"),
|
|
|
|
(self.tr("Status"), "Status"),
|
|
|
|
(self.tr("Label"), "Label"),
|
|
|
|
]:
|
2015-07-01 01:38:14 +12:00
|
|
|
a = QAction(i, self.menu)
|
|
|
|
a.setCheckable(True)
|
|
|
|
a.setChecked(self.options[d])
|
|
|
|
a.setData(d)
|
|
|
|
a.triggered.connect(self.updateOptions)
|
|
|
|
self.menu.addAction(a)
|
|
|
|
self.menu.addSeparator()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
a = QAction(self.tr("Options:"), self.menu)
|
|
|
|
a.setEnabled(False)
|
|
|
|
self.menu.addAction(a)
|
|
|
|
for i, d in [
|
2016-02-07 00:34:22 +13:00
|
|
|
(self.tr("Case sensitive"), "CS"),
|
|
|
|
]:
|
2015-07-01 01:38:14 +12:00
|
|
|
a = QAction(i, self.menu)
|
|
|
|
a.setCheckable(True)
|
|
|
|
a.setChecked(self.options[d])
|
|
|
|
a.setData(d)
|
|
|
|
a.triggered.connect(self.updateOptions)
|
|
|
|
self.menu.addAction(a)
|
|
|
|
self.menu.addSeparator()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
self.btnOptions.setMenu(self.menu)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
def updateOptions(self):
|
|
|
|
a = self.sender()
|
|
|
|
self.options[a.data()] = a.isChecked()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
def search(self):
|
|
|
|
text = self.text.text()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
# Chosing the right columns
|
|
|
|
lstColumns = [
|
2017-11-16 08:58:12 +13:00
|
|
|
("Title", Outline.title),
|
|
|
|
("Text", Outline.text),
|
|
|
|
("Summary", Outline.summarySentence),
|
|
|
|
("Summary", Outline.summaryFull),
|
|
|
|
("Notes", Outline.notes),
|
|
|
|
("POV", Outline.POV),
|
|
|
|
("Status", Outline.status),
|
|
|
|
("Label", Outline.label),
|
2016-02-07 00:34:22 +13:00
|
|
|
]
|
2015-07-01 01:38:14 +12:00
|
|
|
columns = [c[1] for c in lstColumns if self.options[c[0]] or self.options["All"]]
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
# Setting override cursor
|
|
|
|
qApp.setOverrideCursor(Qt.WaitCursor)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
# Searching
|
|
|
|
model = mainWindow().mdlOutline
|
|
|
|
results = model.findItemsContaining(text, columns, self.options["CS"])
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
# Showing results
|
|
|
|
self.result.clear()
|
|
|
|
for r in results:
|
|
|
|
index = model.getIndexByID(r)
|
|
|
|
if not index.isValid():
|
|
|
|
continue
|
|
|
|
item = index.internalPointer()
|
|
|
|
i = QListWidgetItem(item.title(), self.result)
|
|
|
|
i.setData(Qt.UserRole, r)
|
|
|
|
i.setData(Qt.UserRole + 1, item.path())
|
|
|
|
self.result.addItem(i)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
# Removing override cursor
|
|
|
|
qApp.restoreOverrideCursor()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
def openItem(self, item):
|
2015-07-11 03:43:12 +12:00
|
|
|
r = Ref.textReference(item.data(Qt.UserRole))
|
|
|
|
Ref.open(r)
|
2016-02-07 00:34:22 +13:00
|
|
|
# mw = mainWindow()
|
|
|
|
# index = mw.mdlOutline.getIndexByID(item.data(Qt.UserRole))
|
|
|
|
# mw.mainEditor.setCurrentModelIndex(index, newTab=True)
|
|
|
|
|
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
class listResultDelegate(QStyledItemDelegate):
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
QStyledItemDelegate.__init__(self, parent)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
def paint(self, painter, option, index):
|
2016-02-07 00:34:22 +13:00
|
|
|
extra = index.data(Qt.UserRole + 1)
|
2015-07-01 01:38:14 +12:00
|
|
|
if not extra:
|
|
|
|
return QStyledItemDelegate.paint(self, painter, option, index)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
else:
|
|
|
|
if option.state & QStyle.State_Selected:
|
|
|
|
painter.fillRect(option.rect, option.palette.color(QPalette.Highlight))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
title = index.data()
|
|
|
|
extra = " - {}".format(extra)
|
|
|
|
painter.drawText(option.rect.adjusted(2, 1, 0, 0), Qt.AlignLeft, title)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-01 01:38:14 +12:00
|
|
|
fm = QFontMetrics(option.font)
|
|
|
|
w = fm.width(title)
|
|
|
|
r = QRect(option.rect)
|
|
|
|
r.setLeft(r.left() + w)
|
|
|
|
painter.save()
|
|
|
|
if option.state & QStyle.State_Selected:
|
|
|
|
painter.setPen(Qt.white)
|
|
|
|
else:
|
|
|
|
painter.setPen(Qt.gray)
|
|
|
|
painter.drawText(r.adjusted(2, 1, 0, 0), Qt.AlignLeft, extra)
|
2016-02-07 00:34:22 +13:00
|
|
|
painter.restore()
|