Bug correction (might have solved a troubling segfault issue)

This commit is contained in:
Olivier Keshavjee 2016-02-04 17:20:44 +01:00
parent d67e48854c
commit da5c70154a

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
#--!-- coding: utf8 --!--
# --!-- coding: utf8 --!--
from qt import *
from enums import *
@ -8,11 +8,12 @@ from lxml import etree as ET
from functions import *
import settings
import locale
locale.setlocale(locale.LC_ALL, '')
import time
class outlineModel(QAbstractItemModel):
class outlineModel(QAbstractItemModel):
def __init__(self, parent):
QAbstractItemModel.__init__(self, parent)
@ -45,7 +46,7 @@ class outlineModel(QAbstractItemModel):
if len(parent.children()) == 0:
return None
#print(item.title(), [i.title() for i in parent.children()])
# print(item.title(), [i.title() for i in parent.children()])
row = parent.children().index(item)
col = column
@ -67,6 +68,7 @@ class outlineModel(QAbstractItemModel):
def getIndexByID(self, ID):
"Returns the index of item whose ID is ``ID``. If none, returns QModelIndex()."
def search(item):
if item.ID() == ID:
return item
@ -74,6 +76,7 @@ class outlineModel(QAbstractItemModel):
r = search(c)
if r:
return r
item = search(self.rootItem)
if not item:
return QModelIndex()
@ -85,13 +88,15 @@ class outlineModel(QAbstractItemModel):
return QModelIndex()
childItem = index.internalPointer()
try:
# print(childItem.title())
parentItem = childItem.parent()
except AttributeError:
import traceback, sys
print(traceback.print_exc())
print(sys.exc_info()[0])
return QModelIndex()
# try:
# parentItem = childItem.parent()
# except AttributeError:
# import traceback, sys
# print(traceback.print_exc())
# print(sys.exc_info()[0])
# return QModelIndex()
if parentItem == self.rootItem:
return QModelIndex()
@ -128,9 +133,9 @@ class outlineModel(QAbstractItemModel):
item.setData(index.column(), value, role)
#self.dataChanged.emit(index.sibling(index.row(), 0),
#index.sibling(index.row(), max([i.value for i in Outline])))
#print("Model emit", index.row(), index.column())
# self.dataChanged.emit(index.sibling(index.row(), 0),
# index.sibling(index.row(), max([i.value for i in Outline])))
# print("Model emit", index.row(), index.column())
self.dataChanged.emit(index, index)
if index.column() == Outline.type.value:
@ -141,7 +146,6 @@ class outlineModel(QAbstractItemModel):
return True
def headerData(self, section, orientation, role=Qt.DisplayRole):
if orientation == Qt.Horizontal and role in [Qt.DisplayRole, Qt.ToolTipRole]:
if section == Outline.title.value:
@ -173,15 +177,13 @@ class outlineModel(QAbstractItemModel):
else:
return QVariant()
return True
#################### DRAG AND DROP ########################
# http://doc.qt.io/qt-5/model-view-programming.html#using-drag-and-drop-with-item-views
def flags(self, index):
#FIXME when dragging folders, sometimes flags is not called
# FIXME when dragging folders, sometimes flags is not called
flags = QAbstractItemModel.flags(self, index) | Qt.ItemIsEditable
@ -225,14 +227,14 @@ class outlineModel(QAbstractItemModel):
return Qt.CopyAction | Qt.MoveAction
#def canDropMimeData(self, data, action, row, column, parent):
#if not data.hasFormat("application/xml"):
#return False
# def canDropMimeData(self, data, action, row, column, parent):
# if not data.hasFormat("application/xml"):
# return False
#if column > 0:
#return False
# if column > 0:
# return False
#return True
# return True
def dropMimeData(self, data, action, row, column, parent):
@ -319,7 +321,7 @@ class outlineModel(QAbstractItemModel):
# If parent is not folder, write next to
else:
self.insertItem(item, parent.row()+1, parent.parent())
self.insertItem(item, parent.row() + 1, parent.parent())
def removeIndex(self, index):
item = index.internalPointer()
@ -338,7 +340,7 @@ class outlineModel(QAbstractItemModel):
for l in reversed(sorted(levels.keys())):
rows = levels[l]
rows = list(reversed(sorted(rows, key=lambda x:x[0])))
rows = list(reversed(sorted(rows, key=lambda x: x[0])))
for r in rows:
self.removeIndex(r[1])
@ -358,18 +360,17 @@ class outlineModel(QAbstractItemModel):
self.endRemoveRows()
return True
#def insertRow(self, row, item, parent=QModelIndex()):
#self.beginInsertRows(parent, row, row)
# def insertRow(self, row, item, parent=QModelIndex()):
# self.beginInsertRows(parent, row, row)
#if not parent.isValid():
#parentItem = self.rootItem
#else:
#parentItem = parent.internalPointer()
# if not parent.isValid():
# parentItem = self.rootItem
# else:
# parentItem = parent.internalPointer()
#parentItem.insertChild(row, item)
#self.endInsertRows()
# parentItem.insertChild(row, item)
# self.endInsertRows()
################# XML / saving / loading #################
@ -412,8 +413,8 @@ class outlineModel(QAbstractItemModel):
item = item.child(int(p))
return self.indexFromItem(item)
class outlineItem():
class outlineItem():
def __init__(self, model=None, title="", _type="folder", xml=None, parent=None, ID=None):
self._data = {}
@ -438,7 +439,6 @@ class outlineItem():
if ID:
self._data[Outline.ID] = ID
def child(self, row):
return self.childItems[row]
@ -453,11 +453,11 @@ class outlineItem():
def data(self, column, role=Qt.DisplayRole):
#print("Data: ", column, role)
# print("Data: ", column, role)
if role == Qt.DisplayRole or role == Qt.EditRole:
#if column == Outline.compile.value:
#return self.data(column, Qt.CheckStateRole)
# if column == Outline.compile.value:
# return self.data(column, Qt.CheckStateRole)
if Outline(column) in self._data:
return self._data[Outline(column)]
@ -478,15 +478,15 @@ class outlineItem():
elif self.isHTML():
return QIcon.fromTheme("text-html")
#elif role == Qt.ForegroundRole:
#if self.isCompile() in [0, "0"]:
#return QBrush(Qt.gray)
# elif role == Qt.ForegroundRole:
# if self.isCompile() in [0, "0"]:
# return QBrush(Qt.gray)
elif role == Qt.CheckStateRole and column == Outline.compile.value:
#print(self.title(), self.compile())
#if self._data[Outline(column)] and not self.compile():
#return Qt.PartiallyChecked
#else:
# print(self.title(), self.compile())
# if self._data[Outline(column)] and not self.compile():
# return Qt.PartiallyChecked
# else:
return self._data[Outline(column)]
elif role == Qt.FontRole:
@ -544,7 +544,9 @@ class outlineItem():
if updateWordCount:
self.updateWordCount()
def updateWordCount(self):
def updateWordCount(self, emit=True):
"""Update word count for item and parents.
If emit is False, no signal is emitted (sometimes cause segfault)"""
if not self.isFolder():
setGoal = toInt(self.data(Outline.setGoal.value))
goal = toInt(self.data(Outline.goal.value))
@ -579,11 +581,12 @@ class outlineItem():
else:
self.setData(Outline.goalPercentage.value, "")
if emit:
self.emitDataChanged([Outline.goal.value, Outline.setGoal.value,
Outline.wordCount.value, Outline.goalPercentage.value])
if self.parent():
self.parent().updateWordCount()
self.parent().updateWordCount(emit)
def row(self):
if self.parent:
@ -629,7 +632,8 @@ class outlineItem():
def removeChild(self, row):
self.childItems.pop(row)
self.updateWordCount()
# Might be causing segfault when updateWordCount emits dataChanged
self.updateWordCount(emit=False)
def parent(self):
return self._parent
@ -714,9 +718,9 @@ class outlineItem():
locale.format("%d", wc, grouping=True))
###############################################################################
# XML
###############################################################################
###############################################################################
# XML
###############################################################################
def toXML(self):
item = ET.Element("outlineItem")
@ -750,9 +754,9 @@ class outlineItem():
for k in root.attrib:
if k in Outline.__members__:
#if k == Outline.compile:
#self.setData(Outline.__members__[k].value, unicode(root.attrib[k]), Qt.CheckStateRole)
#else:
# if k == Outline.compile:
# self.setData(Outline.__members__[k].value, unicode(root.attrib[k]), Qt.CheckStateRole)
# else:
self.setData(Outline.__members__[k].value, str(root.attrib[k]))
for child in root:
@ -762,9 +766,9 @@ class outlineItem():
self.appendRevision(child.attrib["timestamp"], child.attrib["text"])
###############################################################################
# IDS
###############################################################################
###############################################################################
# IDS
###############################################################################
def getUniqueID(self):
self.setData(Outline.ID.value, self._model.rootItem.findUniqueID())
@ -788,7 +792,6 @@ class outlineItem():
checkChildren(self)
def listAllIDs(self):
IDs = [self.data(Outline.ID.value)]
for c in self.children():
@ -851,9 +854,9 @@ class outlineItem():
return lst
###############################################################################
# REVISIONS
###############################################################################
###############################################################################
# REVISIONS
###############################################################################
def revisions(self):
return self.data(Outline.revisions.value)
@ -904,13 +907,13 @@ class outlineItem():
for r in rev:
# Have to put the lambda key otherwise cannot order when one element is None
for span in sorted(rule, key=lambda x:x if x else 60*60*24*30*365):
for span in sorted(rule, key=lambda x: x if x else 60 * 60 * 24 * 30 * 365):
if not span or now - r[0] < span:
revs[span].append(r)
break
for span in revs:
sortedRev = sorted(revs[span], key=lambda x:x[0])
sortedRev = sorted(revs[span], key=lambda x: x[0])
last = None
for r in sortedRev:
if not last: