2009-06-01 09:55:11 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2009-04-23
|
2010-01-01 20:11:34 +00:00
|
|
|
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
#
|
|
|
|
# This software is licensed under the "HS" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
|
|
|
# http://www.hardcoded.net/licenses/hs_license
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-09-27 08:44:06 +00:00
|
|
|
from PyQt4.QtCore import SIGNAL, Qt, QAbstractItemModel, QModelIndex, QRect
|
2009-06-01 09:55:11 +00:00
|
|
|
from PyQt4.QtGui import QBrush, QStyledItemDelegate, QFont, QTreeView, QColor
|
|
|
|
|
2009-09-27 08:44:06 +00:00
|
|
|
from qtlib.tree_model import TreeNode, TreeModel
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
class ResultNode(TreeNode):
|
|
|
|
def __init__(self, model, parent, row, dupe, group):
|
2009-09-27 08:44:06 +00:00
|
|
|
TreeNode.__init__(self, model, parent, row)
|
2009-06-01 09:55:11 +00:00
|
|
|
self.dupe = dupe
|
|
|
|
self.group = group
|
|
|
|
self._normalData = None
|
|
|
|
self._deltaData = None
|
|
|
|
|
2009-09-27 08:44:06 +00:00
|
|
|
def _createNode(self, ref, row):
|
|
|
|
return ResultNode(self.model, self, row, ref, self.group)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-09-27 08:44:06 +00:00
|
|
|
def _getChildren(self):
|
|
|
|
return self.group.dupes if self.dupe is self.group.ref else []
|
|
|
|
|
|
|
|
def invalidate(self):
|
2009-06-01 09:55:11 +00:00
|
|
|
self._normalData = None
|
|
|
|
self._deltaData = None
|
2009-09-27 08:44:06 +00:00
|
|
|
TreeNode.invalidate(self)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def normalData(self):
|
|
|
|
if self._normalData is None:
|
2009-09-06 13:16:52 +00:00
|
|
|
self._normalData = self.model._app._get_display_info(self.dupe, self.group, delta=False)
|
2009-06-01 09:55:11 +00:00
|
|
|
return self._normalData
|
|
|
|
|
|
|
|
@property
|
|
|
|
def deltaData(self):
|
|
|
|
if self._deltaData is None:
|
2009-09-06 13:16:52 +00:00
|
|
|
self._deltaData = self.model._app._get_display_info(self.dupe, self.group, delta=True)
|
2009-06-01 09:55:11 +00:00
|
|
|
return self._deltaData
|
|
|
|
|
|
|
|
|
|
|
|
class ResultsDelegate(QStyledItemDelegate):
|
|
|
|
def initStyleOption(self, option, index):
|
|
|
|
QStyledItemDelegate.initStyleOption(self, option, index)
|
|
|
|
node = index.internalPointer()
|
|
|
|
if node.group.ref is node.dupe:
|
|
|
|
newfont = QFont(option.font)
|
|
|
|
newfont.setBold(True)
|
|
|
|
option.font = newfont
|
|
|
|
|
|
|
|
|
|
|
|
class ResultsModel(TreeModel):
|
|
|
|
def __init__(self, app):
|
|
|
|
self._app = app
|
|
|
|
self._results = app.results
|
|
|
|
self._data = app.data
|
|
|
|
self._delta_columns = app.DELTA_COLUMNS
|
|
|
|
self.delta = False
|
|
|
|
self._power_marker = False
|
|
|
|
TreeModel.__init__(self)
|
|
|
|
|
2009-09-27 08:44:06 +00:00
|
|
|
def _createNode(self, ref, row):
|
2009-06-01 09:55:11 +00:00
|
|
|
if self.power_marker:
|
2009-09-27 08:44:06 +00:00
|
|
|
# ref is a dupe
|
|
|
|
group = self._results.get_group_of_duplicate(ref)
|
|
|
|
return ResultNode(self, None, row, ref, group)
|
2009-06-01 09:55:11 +00:00
|
|
|
else:
|
2009-09-27 08:44:06 +00:00
|
|
|
# ref is a group
|
|
|
|
return ResultNode(self, None, row, ref.ref, ref)
|
|
|
|
|
|
|
|
def _getChildren(self):
|
|
|
|
return self._results.dupes if self.power_marker else self._results.groups
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def columnCount(self, parent):
|
|
|
|
return len(self._data.COLUMNS)
|
|
|
|
|
|
|
|
def data(self, index, role):
|
|
|
|
if not index.isValid():
|
2009-09-27 08:44:06 +00:00
|
|
|
return None
|
2009-06-01 09:55:11 +00:00
|
|
|
node = index.internalPointer()
|
|
|
|
if role == Qt.DisplayRole:
|
|
|
|
data = node.deltaData if self.delta else node.normalData
|
2009-09-27 08:44:06 +00:00
|
|
|
return data[index.column()]
|
2009-06-01 09:55:11 +00:00
|
|
|
elif role == Qt.CheckStateRole:
|
|
|
|
if index.column() == 0 and node.dupe is not node.group.ref:
|
|
|
|
state = Qt.Checked if self._results.is_marked(node.dupe) else Qt.Unchecked
|
2009-09-27 08:44:06 +00:00
|
|
|
return state
|
2009-06-01 09:55:11 +00:00
|
|
|
elif role == Qt.ForegroundRole:
|
|
|
|
if node.dupe is node.group.ref or node.dupe.is_ref:
|
2009-09-27 08:44:06 +00:00
|
|
|
return QBrush(Qt.blue)
|
2009-06-01 09:55:11 +00:00
|
|
|
elif self.delta and index.column() in self._delta_columns:
|
2009-09-27 08:44:06 +00:00
|
|
|
return QBrush(QColor(255, 142, 40)) # orange
|
2009-06-01 09:55:11 +00:00
|
|
|
elif role == Qt.EditRole:
|
|
|
|
if index.column() == 0:
|
2009-09-27 08:44:06 +00:00
|
|
|
return node.normalData[index.column()]
|
|
|
|
return None
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def dupesForIndexes(self, indexes):
|
|
|
|
nodes = [index.internalPointer() for index in indexes]
|
|
|
|
return [node.dupe for node in nodes]
|
|
|
|
|
2009-06-15 07:52:14 +00:00
|
|
|
def indexesForDupes(self, dupes):
|
|
|
|
def index(dupe):
|
|
|
|
try:
|
|
|
|
if self.power_marker:
|
|
|
|
row = self._results.dupes.index(dupe)
|
2009-10-01 11:08:18 +00:00
|
|
|
node = self.subnodes[row]
|
2009-06-15 07:52:14 +00:00
|
|
|
assert node.dupe is dupe
|
|
|
|
return self.createIndex(row, 0, node)
|
|
|
|
else:
|
|
|
|
group = self._results.get_group_of_duplicate(dupe)
|
|
|
|
row = self._results.groups.index(group)
|
2009-10-01 11:08:18 +00:00
|
|
|
node = self.subnodes[row]
|
2009-06-15 07:52:14 +00:00
|
|
|
if dupe is group.ref:
|
|
|
|
assert node.dupe is dupe
|
|
|
|
return self.createIndex(row, 0, node)
|
|
|
|
subrow = group.dupes.index(dupe)
|
2009-10-03 10:48:08 +00:00
|
|
|
subnode = node.subnodes[subrow]
|
2009-06-15 07:52:14 +00:00
|
|
|
assert subnode.dupe is dupe
|
|
|
|
return self.createIndex(subrow, 0, subnode)
|
|
|
|
except ValueError: # the dupe is not there anymore
|
|
|
|
return QModelIndex()
|
|
|
|
|
|
|
|
return map(index, dupes)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def flags(self, index):
|
|
|
|
if not index.isValid():
|
|
|
|
return Qt.ItemIsEnabled
|
|
|
|
flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable
|
|
|
|
if index.column() == 0:
|
|
|
|
flags |= Qt.ItemIsUserCheckable | Qt.ItemIsEditable
|
|
|
|
return flags
|
|
|
|
|
|
|
|
def headerData(self, section, orientation, role):
|
|
|
|
if orientation == Qt.Horizontal and role == Qt.DisplayRole and section < len(self._data.COLUMNS):
|
2009-09-27 08:44:06 +00:00
|
|
|
return self._data.COLUMNS[section]['display']
|
|
|
|
return None
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def setData(self, index, value, role):
|
|
|
|
if not index.isValid():
|
|
|
|
return False
|
|
|
|
node = index.internalPointer()
|
|
|
|
if role == Qt.CheckStateRole:
|
|
|
|
if index.column() == 0:
|
|
|
|
self._app.toggle_marking_for_dupes([node.dupe])
|
|
|
|
return True
|
|
|
|
if role == Qt.EditRole:
|
|
|
|
if index.column() == 0:
|
|
|
|
value = unicode(value.toString())
|
|
|
|
if self._app.rename_dupe(node.dupe, value):
|
2009-11-02 16:43:05 +00:00
|
|
|
node.invalidate()
|
2009-06-01 09:55:11 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def sort(self, column, order):
|
|
|
|
if self.power_marker:
|
|
|
|
self._results.sort_dupes(column, order == Qt.AscendingOrder, self.delta)
|
|
|
|
else:
|
|
|
|
self._results.sort_groups(column, order == Qt.AscendingOrder)
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
def toggleMarked(self, indexes):
|
|
|
|
assert indexes
|
|
|
|
dupes = self.dupesForIndexes(indexes)
|
|
|
|
self._app.toggle_marking_for_dupes(dupes)
|
|
|
|
|
|
|
|
#--- Properties
|
|
|
|
@property
|
|
|
|
def power_marker(self):
|
|
|
|
return self._power_marker
|
|
|
|
|
|
|
|
@power_marker.setter
|
|
|
|
def power_marker(self, value):
|
|
|
|
if value == self._power_marker:
|
|
|
|
return
|
|
|
|
self._power_marker = value
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
|
|
|
|
class ResultsView(QTreeView):
|
|
|
|
#--- Override
|
|
|
|
def keyPressEvent(self, event):
|
|
|
|
if event.text() == ' ':
|
|
|
|
self.model().toggleMarked(self.selectionModel().selectedRows())
|
|
|
|
return
|
|
|
|
QTreeView.keyPressEvent(self, event)
|
|
|
|
|
2009-06-19 11:14:27 +00:00
|
|
|
def mouseDoubleClickEvent(self, event):
|
|
|
|
self.emit(SIGNAL('doubleClicked()'))
|
|
|
|
# We don't call the superclass' method because the default behavior is to rename the cell.
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def setModel(self, model):
|
|
|
|
assert isinstance(model, ResultsModel)
|
|
|
|
QTreeView.setModel(self, model)
|
|
|
|
|
|
|
|
#--- Public
|
|
|
|
def selectedDupes(self):
|
|
|
|
return self.model().dupesForIndexes(self.selectionModel().selectedRows())
|
|
|
|
|