Clean up column handling in urlmodel.

This commit is contained in:
Florian Bruhin 2015-07-26 18:23:12 +02:00
parent aaa523ce7c
commit 2d2779d6f3

View File

@ -23,7 +23,7 @@ import datetime
from PyQt5.QtCore import pyqtSlot, Qt from PyQt5.QtCore import pyqtSlot, Qt
from qutebrowser.utils import message, objreg, utils from qutebrowser.utils import message, objreg, utils, qtutils
from qutebrowser.completion.models import base from qutebrowser.completion.models import base
from qutebrowser.config import config from qutebrowser.config import config
@ -36,10 +36,14 @@ class UrlCompletionModel(base.BaseCompletionModel):
# pylint: disable=abstract-method # pylint: disable=abstract-method
URL_COLUMN = 0
TEXT_COLUMN = 1
TIME_COLUMN = 2
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
self.columns_to_filter = [0, 1] self.columns_to_filter = [self.URL_COLUMN, self.TEXT_COLUMN]
self._quickmark_cat = self.new_category("Quickmarks") self._quickmark_cat = self.new_category("Quickmarks")
self._bookmark_cat = self.new_category("Bookmarks") self._bookmark_cat = self.new_category("Bookmarks")
@ -104,21 +108,21 @@ class UrlCompletionModel(base.BaseCompletionModel):
def reformat_timestamps(self): def reformat_timestamps(self):
"""Reformat the timestamps if the config option was changed.""" """Reformat the timestamps if the config option was changed."""
for i in range(self._history_cat.rowCount()): for i in range(self._history_cat.rowCount()):
name_item = self._history_cat.child(i, 0) url_item = self._history_cat.child(i, self.URL_COLUMN)
atime_item = self._history_cat.child(i, 2) atime_item = self._history_cat.child(i, self.TIME_COLUMN)
atime = name_item.data(base.Role.sort) atime = url_item.data(base.Role.sort)
atime_item.setText(self._fmt_atime(atime)) atime_item.setText(self._fmt_atime(atime))
@pyqtSlot(object) @pyqtSlot(object)
def on_history_item_added(self, entry): def on_history_item_added(self, entry):
"""Slot called when a new history item was added.""" """Slot called when a new history item was added."""
for i in range(self._history_cat.rowCount()): for i in range(self._history_cat.rowCount()):
name_item = self._history_cat.child(i, 0) url_item = self._history_cat.child(i, self.URL_COLUMN)
atime_item = self._history_cat.child(i, 2) atime_item = self._history_cat.child(i, self.TIME_COLUMN)
url = name_item.data(base.Role.userdata) url = url_item.data(base.Role.userdata)
if url == entry.url: if url == entry.url:
atime_item.setText(self._fmt_atime(entry.atime)) atime_item.setText(self._fmt_atime(entry.atime))
name_item.setData(int(entry.atime), base.Role.sort) url_item.setData(int(entry.atime), base.Role.sort)
break break
else: else:
self._add_history_entry(entry) self._add_history_entry(entry)
@ -141,7 +145,7 @@ class UrlCompletionModel(base.BaseCompletionModel):
name: The name of the quickmark which has been removed. name: The name of the quickmark which has been removed.
""" """
for i in range(self._quickmark_cat.rowCount()): for i in range(self._quickmark_cat.rowCount()):
name_item = self._quickmark_cat.child(i, 1) name_item = self._quickmark_cat.child(i, self.TEXT_COLUMN)
if name_item.data(Qt.DisplayRole) == name: if name_item.data(Qt.DisplayRole) == name:
self._quickmark_cat.removeRow(i) self._quickmark_cat.removeRow(i)
break break
@ -164,7 +168,7 @@ class UrlCompletionModel(base.BaseCompletionModel):
url: The url of the bookmark which has been removed. url: The url of the bookmark which has been removed.
""" """
for i in range(self._bookmark_cat.rowCount()): for i in range(self._bookmark_cat.rowCount()):
url_item = self._bookmark_cat.child(i, 0) url_item = self._bookmark_cat.child(i, self.URL_COLUMN)
if url_item.data(Qt.DisplayRole) == url: if url_item.data(Qt.DisplayRole) == url:
self._bookmark_cat.removeRow(i) self._bookmark_cat.removeRow(i)
break break
@ -188,7 +192,8 @@ class UrlCompletionModel(base.BaseCompletionModel):
message.info(win_id, "Bookmarks deleted") message.info(win_id, "Bookmarks deleted")
elif category.data() == 'Quickmarks': elif category.data() == 'Quickmarks':
quickmark_manager = objreg.get('quickmark-manager') quickmark_manager = objreg.get('quickmark-manager')
name = model.data(index.sibling(index.row(), sibling = index.sibling(index.row(), self.NAME_COLUMN)
index.column() + 1)) qtutils.ensure_valid(sibling)
name = model.data(sibling)
quickmark_manager.quickmark_del(name) quickmark_manager.quickmark_del(name)
message.info(win_id, "Quickmarks deleted") message.info(win_id, "Quickmarks deleted")