linkchecker/linkcheck/gui/urlmodel.py

197 lines
6.7 KiB
Python
Raw Normal View History

# -*- coding: iso-8859-1 -*-
# Copyright (C) 2010-2014 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2010-11-23 22:56:03 +00:00
import operator
from PyQt4 import QtCore, QtGui
from .. import strformat
Headers = [_(u"Parent"), _(u"URL"), _(u"Name"), _(u"Result")]
2010-11-26 19:26:31 +00:00
EmptyQVariant = QtCore.QVariant()
2010-11-06 07:40:46 +00:00
class UrlItem (object):
"""URL item storing info to be displayed."""
def __init__ (self, url_data):
2011-02-17 18:59:02 +00:00
"""Save given URL data and initialize display and tooltip texts."""
2010-11-06 07:40:46 +00:00
# url_data is of type CompactUrlData
self.url_data = url_data
# format display and tooltips
self.init_display()
self.init_tooltips()
2010-11-23 22:56:03 +00:00
def __getitem__ (self, key):
"""Define easy index access (used for sorting):
0: Parent URL
1: URL
2: URL name
3: Result
2010-11-23 22:56:03 +00:00
"""
if not isinstance(key, int):
raise TypeError("invalid index %r" % key)
if key == 0:
2010-11-24 20:35:50 +00:00
return (self.url_data.parent_url, self.url_data.line,
self.url_data.column)
elif key == 1:
2010-11-23 22:56:03 +00:00
return self.url_data.url
elif key == 2:
2010-11-23 22:56:03 +00:00
return self.url_data.name
elif key == 3:
2010-11-24 20:35:50 +00:00
return (self.url_data.valid, self.url_data.result)
2010-11-23 22:56:03 +00:00
raise IndexError("invalid index %d" % key)
def init_display (self):
2011-02-17 18:59:02 +00:00
"""Store formatted display texts from URL data."""
2010-11-24 20:35:50 +00:00
# result
2010-11-06 07:40:46 +00:00
if self.url_data.valid:
if self.url_data.warnings:
self.result_color = QtCore.Qt.darkYellow
2011-05-14 18:15:37 +00:00
text = u"\n".join(x[1] for x in self.url_data.warnings)
result = u"Warning: %s" % strformat.limit(text, length=25)
else:
2010-11-06 07:40:46 +00:00
self.result_color = QtCore.Qt.darkGreen
2011-05-14 18:15:37 +00:00
result = u"Valid"
if self.url_data.result:
result += u": %s" % self.url_data.result
else:
2010-11-06 07:40:46 +00:00
self.result_color = QtCore.Qt.darkRed
2010-11-24 20:35:50 +00:00
result = u"Error"
2011-05-14 18:15:37 +00:00
if self.url_data.result:
result += u": %s" % self.url_data.result
2010-11-23 22:56:03 +00:00
# Parent URL
if self.url_data.parent_url:
parent = u"%s%s%s" % (self.url_data.parent_url,
(_(", line %d") % self.url_data.line),
(_(", col %d") % self.url_data.column))
else:
parent = u""
# display values
self.display = [
# Parent URL
parent,
# URL
2010-11-06 07:40:46 +00:00
unicode(self.url_data.url),
# Name
2010-11-06 07:40:46 +00:00
self.url_data.name,
# Result
2010-11-24 20:35:50 +00:00
result,
]
def init_tooltips (self):
2011-02-17 18:59:02 +00:00
"""Store formatted tooltip texts from URL data."""
2010-11-06 07:40:46 +00:00
# Display warnings in result tooltip
if self.url_data.warnings:
text = u"\n".join(x[1] for x in self.url_data.warnings)
result = strformat.wrap(text, 60)
else:
result = u""
self.tooltips = [
# Parent URL
u"",
# URL
2010-11-06 07:40:46 +00:00
unicode(self.url_data.url),
# Name
2010-11-06 07:40:46 +00:00
self.url_data.name,
# Result
result,
]
class UrlItemModel(QtCore.QAbstractItemModel):
"""Model class for list of URL items."""
def __init__ (self, parent=None):
2011-02-17 18:59:02 +00:00
"""Set empty URL item list."""
super(UrlItemModel, self).__init__(parent)
# list of UrlItem objects
self.urls = []
def rowCount (self, parent=QtCore.QModelIndex()):
2011-02-17 18:59:02 +00:00
"""Return number of URL items."""
return len(self.urls)
def columnCount (self, parent=QtCore.QModelIndex()):
2011-02-17 18:59:02 +00:00
"""Return number of header columns."""
return len(Headers)
def parent (self, child=QtCore.QModelIndex()):
2011-02-17 18:59:02 +00:00
"""Return empty QModelIndex since the URL list is not hierarchical."""
return QtCore.QModelIndex()
def index (self, row, column, parent=QtCore.QModelIndex()):
2011-02-17 18:59:02 +00:00
"""Return index of URL item in given row and column."""
return self.createIndex(row, column)
def data (self, index, role=QtCore.Qt.DisplayRole):
2011-02-17 18:59:02 +00:00
"""Return URL item data at given index for given role."""
V = QtCore.QVariant
if not index.isValid() or \
not (0 <= index.row() < len(self.urls)):
2010-11-26 19:26:31 +00:00
return EmptyQVariant
2010-11-06 07:40:46 +00:00
urlitem = self.urls[index.row()]
column = index.column()
if role == QtCore.Qt.DisplayRole:
return V(urlitem.display[column])
elif role == QtCore.Qt.ToolTipRole:
return V(urlitem.tooltips[column])
elif role == QtCore.Qt.TextColorRole and column == 3:
return QtGui.QColor(urlitem.result_color)
else:
2010-11-26 19:26:31 +00:00
return EmptyQVariant
def headerData (self, section, orientation, role):
2011-02-17 18:59:02 +00:00
"""Return header column data for given parameters."""
if orientation == QtCore.Qt.Horizontal and \
role == QtCore.Qt.DisplayRole:
return Headers[section]
2010-11-26 19:26:31 +00:00
return EmptyQVariant
def flags (self, index):
2011-02-17 18:59:02 +00:00
"""Return flags that given valid item index is enabled and
selected."""
if not index.isValid():
return 0
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def clear (self):
2011-02-17 18:59:02 +00:00
"""Empty the URL item list."""
self.beginResetModel()
self.urls = []
self.endResetModel()
2010-11-26 19:29:33 +00:00
def log_url (self, url_data):
"""Add URL data to tree model."""
2010-11-06 07:40:46 +00:00
row = self.rowCount()
self.beginInsertRows(QtCore.QModelIndex(), row, row)
self.urls.append(UrlItem(url_data))
2010-11-06 07:40:46 +00:00
self.endInsertRows()
return True
def getUrlItem (self, index):
2011-02-17 18:59:02 +00:00
"""Get URL item object at given index."""
if not index.isValid() or \
2010-11-06 07:40:46 +00:00
not (0 <= index.row() < len(self.urls)):
return None
return self.urls[index.row()]
2010-11-23 22:56:03 +00:00
def sort (self, column, order=QtCore.Qt.AscendingOrder):
2011-02-17 18:59:02 +00:00
"""Sort URL items by given column and order."""
2010-11-23 22:56:03 +00:00
self.layoutAboutToBeChanged.emit()
reverse = (order == QtCore.Qt.DescendingOrder)
self.urls.sort(key=operator.itemgetter(column), reverse=reverse)
self.layoutChanged.emit()