diff --git a/src/calibre/gui2/keyboard.py b/src/calibre/gui2/keyboard.py index e38a2a0c4b..7af4089d4c 100644 --- a/src/calibre/gui2/keyboard.py +++ b/src/calibre/gui2/keyboard.py @@ -19,7 +19,7 @@ from calibre.utils.config import JSONConfig from calibre.constants import DEBUG from calibre import prints from calibre.utils.icu import sort_key, lower -from calibre.gui2 import NONE, error_dialog, info_dialog +from calibre.gui2 import error_dialog, info_dialog from calibre.utils.search_query_parser import SearchQueryParser, ParseException from calibre.gui2.search_box import SearchBox2 @@ -224,7 +224,7 @@ class ConfigModel(QAbstractItemModel, SearchQueryParser): ip = index.internalPointer() if ip is not None and role == Qt.UserRole: return ip - return NONE + return None def flags(self, index): ans = QAbstractItemModel.flags(self, index) @@ -498,7 +498,7 @@ class Delegate(QStyledItemDelegate): # {{{ self.closeEditor.connect(self.editing_done) def to_doc(self, index): - data = index.data(Qt.UserRole).toPyObject() + data = index.data(Qt.UserRole) if data is None: html = _('This shortcut no longer exists') elif data.is_shortcut: diff --git a/src/calibre/gui2/tweak_book/char_select.py b/src/calibre/gui2/tweak_book/char_select.py index c8de3a8508..5b1cc22dd1 100644 --- a/src/calibre/gui2/tweak_book/char_select.py +++ b/src/calibre/gui2/tweak_book/char_select.py @@ -12,13 +12,12 @@ from functools import partial from collections import defaultdict from PyQt5.Qt import ( - QAbstractItemModel, QModelIndex, Qt, QVariant, pyqtSignal, QApplication, + QAbstractItemModel, QModelIndex, Qt, pyqtSignal, QApplication, QTreeView, QSize, QGridLayout, QAbstractListModel, QListView, QPen, QMenu, QStyledItemDelegate, QSplitter, QLabel, QSizePolicy, QIcon, QMimeData, QPushButton, QToolButton, QInputMethodEvent) from calibre.constants import plugins, cache_dir -from calibre.gui2 import NONE from calibre.gui2.widgets2 import HistoryLineEdit2 from calibre.gui2.tweak_book import tprefs from calibre.gui2.tweak_book.widgets import Dialog, BusyCursor @@ -435,20 +434,20 @@ class CategoryModel(QAbstractItemModel): def data(self, index, role=Qt.DisplayRole): if not index.isValid(): - return NONE + return None pid = index.internalId() if pid == 0: if role == Qt.DisplayRole: - return QVariant(self.categories[index.row()][0]) + return self.categories[index.row()][0] if role == Qt.FontRole: - return QVariant(self.bold_font) + return self.bold_font if role == Qt.DecorationRole and index.row() == 0: - return QVariant(self.fav_icon) + return self.fav_icon else: if role == Qt.DisplayRole: item = self.categories[pid - 1][1][index.row()] - return QVariant(item[0]) - return NONE + return item[0] + return None def get_range(self, index): if index.isValid(): @@ -532,8 +531,8 @@ class CharModel(QAbstractListModel): def data(self, index, role): if role == Qt.UserRole and -1 < index.row() < len(self.chars): - return QVariant(self.chars[index.row()]) - return NONE + return self.chars[index.row()] + return None def flags(self, index): ans = Qt.ItemIsEnabled @@ -581,8 +580,9 @@ class CharDelegate(QStyledItemDelegate): def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) - charcode, ok = index.data(Qt.UserRole).toInt() - if not ok: + try: + charcode = int(index.data(Qt.UserRole)) + except (TypeError, ValueError): return painter.save() try: @@ -632,8 +632,11 @@ class CharView(QListView): self.clicked.connect(self.item_activated) def item_activated(self, index): - char_code, ok = self.model().data(index, Qt.UserRole).toInt() - if ok: + try: + char_code = int(self.model().data(index, Qt.UserRole)) + except (TypeError, ValueError): + pass + else: self.char_selected.emit(chr(char_code)) def set_allow_drag_and_drop(self, enabled): @@ -663,8 +666,11 @@ class CharView(QListView): row = index.row() if row != self.last_mouse_idx: self.last_mouse_idx = row - char_code, ok = self.model().data(index, Qt.UserRole).toInt() - if ok: + try: + char_code = int(self.model().data(index, Qt.UserRole)) + except (TypeError, ValueError): + pass + else: self.show_name.emit(char_code) self.setCursor(Qt.PointingHandCursor) else: @@ -676,8 +682,11 @@ class CharView(QListView): def context_menu(self, pos): index = self.indexAt(pos) if index.isValid(): - char_code, ok = self.model().data(index, Qt.UserRole).toInt() - if ok: + try: + char_code = int(self.model().data(index, Qt.UserRole)) + except (TypeError, ValueError): + pass + else: m = QMenu(self) m.addAction(QIcon(I('edit-copy.png')), _('Copy %s to clipboard') % chr(char_code), partial(self.copy_to_clipboard, char_code)) m.addAction(QIcon(I('rating.png')), diff --git a/src/calibre/gui2/tweak_book/check.py b/src/calibre/gui2/tweak_book/check.py index bd2f6cb408..f381ef13cf 100644 --- a/src/calibre/gui2/tweak_book/check.py +++ b/src/calibre/gui2/tweak_book/check.py @@ -9,7 +9,7 @@ __copyright__ = '2013, Kovid Goyal ' import sys from PyQt5.Qt import ( - QIcon, Qt, QSplitter, QListWidget, QTextBrowser, QPalette, QMenu, + QIcon, Qt, QSplitter, QListWidget, QTextBrowser, QPalette, QUrl, QMenu, QListWidgetItem, pyqtSignal, QApplication, QStyledItemDelegate) from calibre.ebooks.oeb.polish.check.base import WARN, INFO, DEBUG, ERROR, CRITICAL @@ -111,17 +111,17 @@ class Check(QSplitter): msg, _('Click to run a check on the book'), _('Run check'))) def link_clicked(self, url): - url = unicode(url.toString()) + url = unicode(url.toString(QUrl.None)) if url == 'activate:item': self.current_item_activated() elif url == 'run:check': self.check_requested.emit() elif url == 'fix:errors': - errors = [self.items.item(i).data(Qt.UserRole).toPyObject() for i in xrange(self.items.count())] + errors = [self.items.item(i).data(Qt.UserRole) for i in xrange(self.items.count())] self.fix_requested.emit(errors) elif url.startswith('fix:error,'): num = int(url.rpartition(',')[-1]) - errors = [self.items.item(num).data(Qt.UserRole).toPyObject()] + errors = [self.items.item(num).data(Qt.UserRole)] self.fix_requested.emit(errors) elif url.startswith('activate:item:'): index = int(url.rpartition(':')[-1]) @@ -138,7 +138,7 @@ class Check(QSplitter): def current_item_activated(self, *args): i = self.items.currentItem() if i is not None: - err = i.data(Qt.UserRole).toPyObject() + err = i.data(Qt.UserRole) if err.has_multiple_locations: self.location_activated(0) else: @@ -147,7 +147,7 @@ class Check(QSplitter): def location_activated(self, index): i = self.items.currentItem() if i is not None: - err = i.data(Qt.UserRole).toPyObject() + err = i.data(Qt.UserRole) err.current_location_index = index self.item_activated.emit(err) @@ -165,7 +165,7 @@ class Check(QSplitter): return loc if i is not None: - err = i.data(Qt.UserRole).toPyObject() + err = i.data(Qt.UserRole) header = {DEBUG:_('Debug'), INFO:_('Information'), WARN:_('Warning'), ERROR:_('Error'), CRITICAL:_('Error')}[err.level] ifix = '' loc = loc_to_string(err.line, err.col) diff --git a/src/calibre/gui2/tweak_book/editor/insert_resource.py b/src/calibre/gui2/tweak_book/editor/insert_resource.py index d5b3ffb150..baf146c485 100644 --- a/src/calibre/gui2/tweak_book/editor/insert_resource.py +++ b/src/calibre/gui2/tweak_book/editor/insert_resource.py @@ -11,7 +11,7 @@ from functools import partial from PyQt5.Qt import ( QGridLayout, QSize, QListView, QStyledItemDelegate, QLabel, QPixmap, - QApplication, QSizePolicy, QAbstractListModel, QVariant, Qt, QRect, + QApplication, QSizePolicy, QAbstractListModel, Qt, QRect, QPainter, QModelIndex, QSortFilterProxyModel, QLineEdit, QToolButton, QIcon, QFormLayout, pyqtSignal, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QMenu, QInputDialog) @@ -20,7 +20,7 @@ from calibre import fit_image from calibre.constants import plugins from calibre.ebooks.metadata import string_to_authors from calibre.ebooks.metadata.book.base import Metadata -from calibre.gui2 import NONE, choose_files, error_dialog +from calibre.gui2 import choose_files, error_dialog from calibre.gui2.languages import LanguagesEdit from calibre.gui2.tweak_book import current_container, tprefs from calibre.gui2.tweak_book.widgets import Dialog @@ -98,7 +98,7 @@ class ImageDelegate(QStyledItemDelegate): def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, QModelIndex()) # draw the hover and selection highlights - name = unicode(index.data(Qt.DisplayRole).toString()) + name = unicode(index.data(Qt.DisplayRole) or '') cover = self.cover_cache.get(name, None) if cover is None: cover = self.cover_cache[name] = QPixmap() @@ -161,10 +161,10 @@ class Images(QAbstractListModel): try: name = self.image_names[index.row()] except IndexError: - return NONE + return None if role in (Qt.DisplayRole, Qt.ToolTipRole): - return QVariant(name) - return NONE + return name + return None class InsertImage(Dialog): @@ -259,12 +259,12 @@ class InsertImage(Dialog): def activated(self, index): if self.for_browsing: - return self.image_activated.emit(unicode(index.data().toString())) + return self.image_activated.emit(unicode(index.data() or '')) self.chosen_image_is_external = False self.accept() def accept(self): - self.chosen_image = unicode(self.view.currentIndex().data().toString()) + self.chosen_image = unicode(self.view.currentIndex().data() or '') super(InsertImage, self).accept() def filter_changed(self, *args): diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index 5dc004128f..6b120691d3 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -602,7 +602,7 @@ class TextEdit(PlainTextEdit): return pos = cursor.positionInBlock() for r in cursor.block().layout().additionalFormats(): - if r.start <= pos < r.start + r.length and r.format.property(SYNTAX_PROPERTY).toBool(): + if r.start <= pos < r.start + r.length and r.format.property(SYNTAX_PROPERTY): return r def syntax_format_for_cursor(self, cursor): @@ -617,6 +617,7 @@ class TextEdit(PlainTextEdit): QToolTip.setFont(self.tooltip_font) QToolTip.setPalette(self.tooltip_palette) QToolTip.showText(ev.globalPos(), textwrap.fill(tt)) + return QToolTip.hideText() ev.ignore() # }}} diff --git a/src/calibre/gui2/tweak_book/file_list.py b/src/calibre/gui2/tweak_book/file_list.py index 492405ce1f..44a7612f0d 100644 --- a/src/calibre/gui2/tweak_book/file_list.py +++ b/src/calibre/gui2/tweak_book/file_list.py @@ -87,7 +87,7 @@ class ItemDelegate(QStyledItemDelegate): # {{{ rename_requested = pyqtSignal(object, object) def setEditorData(self, editor, index): - name = unicode(index.data(NAME_ROLE).toString()) + name = unicode(index.data(NAME_ROLE) or '') # We do this because Qt calls selectAll() unconditionally on the # editor, and we want only a part of the file name to be selected QTimer.singleShot(0, partial(self.set_editor_data, name, editor)) @@ -105,7 +105,7 @@ class ItemDelegate(QStyledItemDelegate): # {{{ def setModelData(self, editor, model, index): newname = unicode(editor.text()) - oldname = unicode(index.data(NAME_ROLE).toString()) + oldname = unicode(index.data(NAME_ROLE) or '') if newname != oldname: self.rename_requested.emit(oldname, newname) @@ -123,7 +123,7 @@ class ItemDelegate(QStyledItemDelegate): # {{{ suffix = '%s(%d)' % (NBSP, index.model().rowCount(index)) else: try: - suffix = NBSP + human_readable(current_container().filesize(unicode(index.data(NAME_ROLE).toString()))) + suffix = NBSP + human_readable(current_container().filesize(unicode(index.data(NAME_ROLE) or ''))) except EnvironmentError: suffix = NBSP + human_readable(0) br = painter.boundingRect(option.rect, Qt.AlignRight|Qt.AlignVCenter, suffix) @@ -195,7 +195,7 @@ class FileList(QTreeWidget): def get_state(self): s = {'pos':self.verticalScrollBar().value()} s['expanded'] = {c for c, item in self.categories.iteritems() if item.isExpanded()} - s['selected'] = {unicode(i.data(0, NAME_ROLE).toString()) for i in self.selectedItems()} + s['selected'] = {unicode(i.data(0, NAME_ROLE) or '') for i in self.selectedItems()} return s def set_state(self, state): @@ -204,21 +204,21 @@ class FileList(QTreeWidget): self.verticalScrollBar().setValue(state['pos']) for parent in self.categories.itervalues(): for c in (parent.child(i) for i in xrange(parent.childCount())): - name = unicode(c.data(0, NAME_ROLE).toString()) + name = unicode(c.data(0, NAME_ROLE) or '') if name in state['selected']: c.setSelected(True) def item_from_name(self, name): for parent in self.categories.itervalues(): for c in (parent.child(i) for i in xrange(parent.childCount())): - q = unicode(c.data(0, NAME_ROLE).toString()) + q = unicode(c.data(0, NAME_ROLE) or '') if q == name: return c def select_name(self, name): for parent in self.categories.itervalues(): for c in (parent.child(i) for i in xrange(parent.childCount())): - q = unicode(c.data(0, NAME_ROLE).toString()) + q = unicode(c.data(0, NAME_ROLE) or '') c.setSelected(q == name) if q == name: self.scrollToItem(c) @@ -421,9 +421,9 @@ class FileList(QTreeWidget): container = current_container() ci = self.currentItem() if ci is not None: - cn = unicode(ci.data(0, NAME_ROLE).toString()) - mt = unicode(ci.data(0, MIME_ROLE).toString()) - cat = unicode(ci.data(0, CATEGORY_ROLE).toString()) + cn = unicode(ci.data(0, NAME_ROLE) or '') + mt = unicode(ci.data(0, MIME_ROLE) or '') + cat = unicode(ci.data(0, CATEGORY_ROLE) or '') n = elided_text(cn.rpartition('/')[-1]) m.addAction(QIcon(I('save.png')), _('Export %s') % n, partial(self.export, cn)) if cn not in container.names_that_must_not_be_changed and cn not in container.names_that_must_not_be_removed and mt not in OEB_FONTS: @@ -446,7 +446,7 @@ class FileList(QTreeWidget): selected_map = defaultdict(list) for item in sel: - selected_map[unicode(item.data(0, CATEGORY_ROLE).toString())].append(unicode(item.data(0, NAME_ROLE).toString())) + selected_map[unicode(item.data(0, CATEGORY_ROLE) or '')].append(unicode(item.data(0, NAME_ROLE) or '')) for items in selected_map.itervalues(): items.sort(key=self.index_of_name) @@ -466,7 +466,7 @@ class FileList(QTreeWidget): for category, parent in self.categories.iteritems(): for i in xrange(parent.childCount()): item = parent.child(i) - if unicode(item.data(0, NAME_ROLE).toString()) == name: + if unicode(item.data(0, NAME_ROLE) or '') == name: return (category, i) return (None, -1) @@ -483,7 +483,7 @@ class FileList(QTreeWidget): self.mark_requested.emit(name, 'cover') def mark_as_titlepage(self, name): - first = unicode(self.categories['text'].child(0).data(0, NAME_ROLE).toString()) == name + first = unicode(self.categories['text'].child(0).data(0, NAME_ROLE) or '') == name move_to_start = False if not first: move_to_start = question_dialog(self, _('Not first item'), _( @@ -500,7 +500,7 @@ class FileList(QTreeWidget): return QTreeWidget.keyPressEvent(self, ev) def request_bulk_rename(self): - names = {unicode(item.data(0, NAME_ROLE).toString()) for item in self.selectedItems()} + names = {unicode(item.data(0, NAME_ROLE) or '') for item in self.selectedItems()} bad = names & current_container().names_that_must_not_be_changed if bad: return error_dialog(self, _('Cannot rename'), @@ -517,7 +517,7 @@ class FileList(QTreeWidget): self.bulk_rename_requested.emit(name_map) def request_delete(self): - names = {unicode(item.data(0, NAME_ROLE).toString()) for item in self.selectedItems()} + names = {unicode(item.data(0, NAME_ROLE) or '') for item in self.selectedItems()} bad = names & current_container().names_that_must_not_be_removed if bad: return error_dialog(self, _('Cannot delete'), @@ -525,9 +525,9 @@ class FileList(QTreeWidget): text = self.categories['text'] children = (text.child(i) for i in xrange(text.childCount())) - spine_removals = [(unicode(item.data(0, NAME_ROLE).toString()), item.isSelected()) for item in children] - other_removals = {unicode(item.data(0, NAME_ROLE).toString()) for item in self.selectedItems() - if unicode(item.data(0, CATEGORY_ROLE).toString()) != 'text'} + spine_removals = [(unicode(item.data(0, NAME_ROLE) or ''), item.isSelected()) for item in children] + other_removals = {unicode(item.data(0, NAME_ROLE) or '') for item in self.selectedItems() + if unicode(item.data(0, CATEGORY_ROLE) or '') != 'text'} self.delete_requested.emit(spine_removals, other_removals) def delete_done(self, spine_removals, other_removals): @@ -539,7 +539,7 @@ class FileList(QTreeWidget): if category != 'text': for i in xrange(parent.childCount()): child = parent.child(i) - if unicode(child.data(0, NAME_ROLE).toString()) in other_removals: + if unicode(child.data(0, NAME_ROLE) or '') in other_removals: removals.append(child) # The sorting by index is necessary otherwise Qt crashes with recursive @@ -563,8 +563,8 @@ class FileList(QTreeWidget): if current_order != pre_drop_order: order = [] for child in (text.child(i) for i in xrange(text.childCount())): - name = unicode(child.data(0, NAME_ROLE).toString()) - linear = child.data(0, LINEAR_ROLE).toBool() + name = unicode(child.data(0, NAME_ROLE) or '') + linear = bool(child.data(0, LINEAR_ROLE)) order.append([name, linear]) # Ensure that all non-linear items are at the end, any non-linear # items not at the end will be made linear @@ -579,9 +579,9 @@ class FileList(QTreeWidget): self._request_edit(item) def _request_edit(self, item): - category = unicode(item.data(0, CATEGORY_ROLE).toString()) - mime = unicode(item.data(0, MIME_ROLE).toString()) - name = unicode(item.data(0, NAME_ROLE).toString()) + category = unicode(item.data(0, CATEGORY_ROLE) or '') + mime = unicode(item.data(0, MIME_ROLE) or '') + name = unicode(item.data(0, NAME_ROLE) or '') syntax = {'text':'html', 'styles':'css'}.get(category, None) self.edit_file.emit(name, syntax, mime) @@ -601,9 +601,9 @@ class FileList(QTreeWidget): def searchable_names(self): ans = {'text':OrderedDict(), 'styles':OrderedDict(), 'selected':OrderedDict()} for item in self.all_files: - category = unicode(item.data(0, CATEGORY_ROLE).toString()) - mime = unicode(item.data(0, MIME_ROLE).toString()) - name = unicode(item.data(0, NAME_ROLE).toString()) + category = unicode(item.data(0, CATEGORY_ROLE) or '') + mime = unicode(item.data(0, MIME_ROLE) or '') + name = unicode(item.data(0, NAME_ROLE) or '') ok = category in {'text', 'styles'} if ok: ans[category][name] = syntax_from_mime(name, mime) @@ -646,7 +646,7 @@ class FileList(QTreeWidget): def link_stylesheets(self, names): s = self.categories['styles'] - sheets = [unicode(s.child(i).data(0, NAME_ROLE).toString()) for i in xrange(s.childCount())] + sheets = [unicode(s.child(i).data(0, NAME_ROLE) or '') for i in xrange(s.childCount())] if not sheets: return error_dialog(self, _('No stylesheets'), _( 'This book currently has no stylesheets. You must first create a stylesheet' diff --git a/src/calibre/gui2/tweak_book/preferences.py b/src/calibre/gui2/tweak_book/preferences.py index 1f9262d71c..9b0637c564 100644 --- a/src/calibre/gui2/tweak_book/preferences.py +++ b/src/calibre/gui2/tweak_book/preferences.py @@ -72,7 +72,7 @@ class BasicSettings(QWidget): # {{{ widget.addItem(human or key, key) def getter(w): - ans = unicode(w.itemData(w.currentIndex()).toString()) + ans = unicode(w.itemData(w.currentIndex()) or '') return {none_val:None}.get(ans, ans) def setter(w, val): diff --git a/src/calibre/gui2/tweak_book/preview.py b/src/calibre/gui2/tweak_book/preview.py index d6999153e5..cc9eae3b9d 100644 --- a/src/calibre/gui2/tweak_book/preview.py +++ b/src/calibre/gui2/tweak_book/preview.py @@ -212,7 +212,7 @@ class NetworkAccessManager(QNetworkAccessManager): self.cache.setMaximumCacheSize(0) def createRequest(self, operation, request, data): - url = unicode(request.url().toString()) + url = unicode(request.url().toString(QUrl.None)) if operation == self.GetOperation and url.startswith('file://'): path = url[7:] if iswindows and path.startswith('/'): @@ -320,12 +320,13 @@ class WebPage(QWebPage): def line_numbers(self): if self._line_numbers is None: def atoi(x): - ans, ok = x.toUInt() - if not ok: + try: + ans = int(x) + except (TypeError, ValueError): ans = None return ans - self._line_numbers = sorted(uniq(filter(lambda x:x is not None, map(atoi, self.mainFrame().evaluateJavaScript( - 'window.calibre_preview_integration.line_numbers()').toStringList())))) + val = self.mainFrame().evaluateJavaScript('window.calibre_preview_integration.line_numbers()') + self._line_numbers = sorted(uniq(filter(lambda x:x is not None, map(atoi, val)))) return self._line_numbers def go_to_line(self, lnum): diff --git a/src/calibre/gui2/tweak_book/search.py b/src/calibre/gui2/tweak_book/search.py index a3e8fd0756..23fc0b4c33 100644 --- a/src/calibre/gui2/tweak_book/search.py +++ b/src/calibre/gui2/tweak_book/search.py @@ -14,12 +14,12 @@ from PyQt5.Qt import ( QWidget, QToolBar, Qt, QHBoxLayout, QSize, QIcon, QGridLayout, QLabel, QTimer, QPushButton, pyqtSignal, QComboBox, QCheckBox, QSizePolicy, QVBoxLayout, QFont, QLineEdit, QToolButton, QListView, QFrame, QApplication, QStyledItemDelegate, - QAbstractListModel, QVariant, QFormLayout, QModelIndex, QMenu, QItemSelection) + QAbstractListModel, QFormLayout, QModelIndex, QMenu, QItemSelection) import regex from calibre import prepare_string_for_xml -from calibre.gui2 import NONE, error_dialog, info_dialog, choose_files, choose_save_file +from calibre.gui2 import error_dialog, info_dialog, choose_files, choose_save_file from calibre.gui2.dialogs.message_box import MessageBox from calibre.gui2.widgets2 import HistoryComboBox from calibre.gui2.tweak_book import tprefs, editors, current_container @@ -403,17 +403,17 @@ class SearchesModel(QAbstractListModel): try: if role == Qt.DisplayRole: search = self.searches[self.filtered_searches[index.row()]] - return QVariant(search['name']) + return search['name'] if role == Qt.ToolTipRole: search = self.searches[self.filtered_searches[index.row()]] tt = '\n'.join((search['find'], search['replace'])) - return QVariant(tt) + return tt if role == Qt.UserRole: search = self.searches[self.filtered_searches[index.row()]] - return QVariant((self.filtered_searches[index.row()], search)) + return (self.filtered_searches[index.row()], search) except IndexError: pass - return NONE + return None def do_filter(self, text): text = unicode(text) @@ -696,7 +696,7 @@ class SavedSearches(Dialog): seen.add(index.row()) search = SearchWidget.DEFAULT_STATE.copy() del search['mode'] - search_index, s = index.data(Qt.UserRole).toPyObject() + search_index, s = index.data(Qt.UserRole) search.update(s) search['wrap'] = self.wrap search['direction'] = self.direction @@ -722,7 +722,7 @@ class SavedSearches(Dialog): def edit_search(self): index = self.searches.currentIndex() if index.isValid(): - search_index, search = index.data(Qt.UserRole).toPyObject() + search_index, search = index.data(Qt.UserRole) d = EditSearch(search=search, search_index=search_index, parent=self) if d.exec_() == d.Accepted: self.model.dataChanged.emit(index, index) @@ -753,7 +753,7 @@ class SavedSearches(Dialog): self.description.setText(' \n \n ') i = self.searches.currentIndex() if i.isValid(): - search_index, search = i.data(Qt.UserRole).toPyObject() + search_index, search = i.data(Qt.UserRole) cs = '✓' if search.get('case_sensitive', SearchWidget.DEFAULT_STATE['case_sensitive']) else '✗' da = '✓' if search.get('dot_all', SearchWidget.DEFAULT_STATE['dot_all']) else '✗' if search.get('mode', SearchWidget.DEFAULT_STATE['mode']) == 'regex': @@ -799,7 +799,7 @@ class SavedSearches(Dialog): else: searches = [] for index in self.searches.selectionModel().selectedIndexes(): - search = index.data(Qt.UserRole).toPyObject()[-1] + search = index.data(Qt.UserRole)[-1] searches.append(search.copy()) if not searches: return error_dialog(self, _('No searches'), _( diff --git a/src/calibre/gui2/tweak_book/spell.py b/src/calibre/gui2/tweak_book/spell.py index 4cc6492dab..061e42b016 100644 --- a/src/calibre/gui2/tweak_book/spell.py +++ b/src/calibre/gui2/tweak_book/spell.py @@ -275,7 +275,7 @@ class ManageUserDictionaries(Dialog): d = self.dictionaries.currentItem() if d is None: return - return d.data(Qt.UserRole).toPyObject() + return d.data(Qt.UserRole) def active_toggled(self): d = self.current_dictionary @@ -283,7 +283,7 @@ class ManageUserDictionaries(Dialog): dictionaries.mark_user_dictionary_as_active(d.name, self.is_active.isChecked()) self.dictionaries_changed = True for item in (self.dictionaries.item(i) for i in xrange(self.dictionaries.count())): - d = item.data(Qt.UserRole).toPyObject() + d = item.data(Qt.UserRole) item.setData(Qt.FontRole, self.emph_font if d.is_active else None) def show_current_dictionary(self, *args): @@ -364,7 +364,7 @@ class ManageUserDictionaries(Dialog): self.dictionaries_changed = True def remove_word(self): - words = {i.data(Qt.UserRole).toPyObject() for i in self.words.selectedItems()} + words = {i.data(Qt.UserRole) for i in self.words.selectedItems()} if words: kwords = [(w, DictionaryLocale(l, None)) for w, l in words] d = self.current_dictionary @@ -376,7 +376,7 @@ class ManageUserDictionaries(Dialog): def find_word(self, word, lang): key = (word, lang) for i in xrange(self.words.count()): - if self.words.item(i).data(Qt.UserRole).toPyObject() == key: + if self.words.item(i).data(Qt.UserRole) == key: return i return -1 @@ -454,7 +454,7 @@ class ManageDictionaries(Dialog): # {{{ def data_changed(self, item, column): if column == 0 and item.type() == DICTIONARY: - d = item.data(0, Qt.UserRole).toPyObject() + d = item.data(0, Qt.UserRole) if not d.builtin and unicode(item.text(0)) != d.name: rename_dictionary(d, unicode(item.text(0))) @@ -504,7 +504,7 @@ class ManageDictionaries(Dialog): # {{{ def remove_dictionary(self): item = self.dictionaries.currentItem() if item is not None and item.type() == DICTIONARY: - dic = item.data(0, Qt.UserRole).toPyObject() + dic = item.data(0, Qt.UserRole) if not dic.builtin: remove_dictionary(dic) self.build_dictionaries(reread=True) @@ -534,7 +534,7 @@ class ManageDictionaries(Dialog): # {{{ def init_country(self, item): pc = self.pcb - font = item.data(0, Qt.FontRole).toPyObject() + font = item.data(0, Qt.FontRole) preferred = bool(font and font.bold()) pc.setText((_( 'This is already the preferred variant for the {1} language') if preferred else _( @@ -548,20 +548,20 @@ class ManageDictionaries(Dialog): # {{{ bf.setBold(True) for x in (item.parent().child(i) for i in xrange(item.parent().childCount())): x.setData(0, Qt.FontRole, bf if x is item else None) - lc = unicode(item.parent().data(0, Qt.UserRole).toPyObject()) + lc = unicode(item.parent().data(0, Qt.UserRole)) pl = dprefs['preferred_locales'] - pl[lc] = '%s-%s' % (lc, unicode(item.data(0, Qt.UserRole).toPyObject())) + pl[lc] = '%s-%s' % (lc, unicode(item.data(0, Qt.UserRole))) dprefs['preferred_locales'] = pl def init_dictionary(self, item): saf = self.fb - font = item.data(0, Qt.FontRole).toPyObject() + font = item.data(0, Qt.FontRole) preferred = bool(font and font.italic()) saf.setText((_( 'This is already the preferred dictionary') if preferred else _('Use this as the preferred dictionary'))) saf.setEnabled(not preferred) - self.remove_dictionary_button.setEnabled(not item.data(0, Qt.UserRole).toPyObject().builtin) + self.remove_dictionary_button.setEnabled(not item.data(0, Qt.UserRole).builtin) def set_favorite(self): item = self.dictionaries.currentItem() @@ -569,9 +569,9 @@ class ManageDictionaries(Dialog): # {{{ bf.setItalic(True) for x in (item.parent().child(i) for i in xrange(item.parent().childCount())): x.setData(0, Qt.FontRole, bf if x is item else None) - cc = unicode(item.parent().data(0, Qt.UserRole).toPyObject()) - lc = unicode(item.parent().parent().data(0, Qt.UserRole).toPyObject()) - d = item.data(0, Qt.UserRole).toPyObject() + cc = unicode(item.parent().data(0, Qt.UserRole)) + lc = unicode(item.parent().parent().data(0, Qt.UserRole)) + d = item.data(0, Qt.UserRole) locale = '%s-%s' % (lc, cc) pl = dprefs['preferred_dictionaries'] pl[locale] = d.id diff --git a/src/calibre/gui2/tweak_book/toc.py b/src/calibre/gui2/tweak_book/toc.py index 434ac542cf..250ddb3731 100644 --- a/src/calibre/gui2/tweak_book/toc.py +++ b/src/calibre/gui2/tweak_book/toc.py @@ -182,8 +182,8 @@ class TOCViewer(QWidget): def emit_navigate(self, *args): item = self.view.currentItem() if item is not None: - dest = unicode(item.data(0, DEST_ROLE).toString()) - frag = unicode(item.data(0, FRAG_ROLE).toString()) + dest = unicode(item.data(0, DEST_ROLE) or '') + frag = unicode(item.data(0, FRAG_ROLE) or '') if not frag: frag = TOP self.navigate_requested.emit(dest, frag) diff --git a/src/calibre/gui2/tweak_book/undo.py b/src/calibre/gui2/tweak_book/undo.py index 27507cee79..15d2e9ae9d 100644 --- a/src/calibre/gui2/tweak_book/undo.py +++ b/src/calibre/gui2/tweak_book/undo.py @@ -9,10 +9,10 @@ __copyright__ = '2013, Kovid Goyal ' import shutil from PyQt5.Qt import ( - QAbstractListModel, Qt, QModelIndex, QVariant, QApplication, QWidget, + QAbstractListModel, Qt, QModelIndex, QApplication, QWidget, QGridLayout, QListView, QStyledItemDelegate, pyqtSignal, QPushButton, QIcon) -from calibre.gui2 import NONE, error_dialog +from calibre.gui2 import error_dialog ROOT = QModelIndex() @@ -44,14 +44,14 @@ class GlobalUndoHistory(QAbstractListModel): def data(self, index, role=Qt.DisplayRole): if role == Qt.DisplayRole: - return QVariant(self.label_for_row(index.row())) + return self.label_for_row(index.row()) if role == Qt.FontRole and index.row() == self.pos: f = QApplication.instance().font() f.setBold(True) - return QVariant(f) + return f if role == Qt.UserRole: - return QVariant(self.states[index.row()]) - return NONE + return self.states[index.row()] + return None def label_for_row(self, row): msg = self.states[row].message diff --git a/src/calibre/gui2/tweak_book/widgets.py b/src/calibre/gui2/tweak_book/widgets.py index 0146cec789..60026a74da 100644 --- a/src/calibre/gui2/tweak_book/widgets.py +++ b/src/calibre/gui2/tweak_book/widgets.py @@ -14,9 +14,9 @@ from PyQt5.Qt import ( QDialog, QDialogButtonBox, QGridLayout, QLabel, QLineEdit, QVBoxLayout, QFormLayout, QHBoxLayout, QToolButton, QIcon, QApplication, Qt, QWidget, QPoint, QSizePolicy, QPainter, QStaticText, pyqtSignal, QTextOption, - QAbstractListModel, QModelIndex, QVariant, QStyledItemDelegate, QStyle, - QListView, QTextDocument, QSize, QComboBox, QFrame, QCursor, QCheckBox, - QSplitter, QPixmap, QRect, QGroupBox) + QAbstractListModel, QModelIndex, QStyledItemDelegate, QStyle, QCheckBox, + QListView, QTextDocument, QSize, QComboBox, QFrame, QCursor, QGroupBox, + QSplitter, QPixmap, QRect) from calibre import prepare_string_for_xml, human_readable from calibre.ebooks.oeb.polish.utils import lead_text, guess_type @@ -502,7 +502,7 @@ class NamesDelegate(QStyledItemDelegate): def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) - text, positions = index.data(Qt.UserRole).toPyObject() + text, positions = index.data(Qt.UserRole) self.initStyleOption(option, index) painter.save() painter.setFont(option.font) @@ -551,9 +551,9 @@ class NamesModel(QAbstractListModel): def data(self, index, role): if role == Qt.UserRole: - return QVariant(self.items[index.row()]) + return self.items[index.row()] if role == Qt.DisplayRole: - return QVariant('\xa0' * 20) + return '\xa0' * 20 def filter(self, query): query = unicode(query or '') @@ -684,7 +684,7 @@ class InsertLink(Dialog): if not rows: self.anchor_names.model().set_names([]) else: - name, positions = self.file_names.model().data(rows[0], Qt.UserRole).toPyObject() + name, positions = self.file_names.model().data(rows[0], Qt.UserRole) self.populate_anchors(name) def populate_anchors(self, name): @@ -704,7 +704,7 @@ class InsertLink(Dialog): rows = list(self.file_names.selectionModel().selectedRows()) if not rows: return - name = self.file_names.model().data(rows[0], Qt.UserRole).toPyObject()[0] + name = self.file_names.model().data(rows[0], Qt.UserRole)[0] if name == self.source_name: href = '' else: @@ -843,7 +843,7 @@ class InsertSemantics(Dialog): d.exec_() def semantic_type_changed(self): - item_type = unicode(self.semantic_type.itemData(self.semantic_type.currentIndex()).toString()) + item_type = unicode(self.semantic_type.itemData(self.semantic_type.currentIndex()) or '') name, frag = self.final_type_map.get(item_type, (None, None)) self.show_type(name, frag) @@ -869,7 +869,7 @@ class InsertSemantics(Dialog): def target_text_changed(self): name, frag = unicode(self.target.text()).partition('#')[::2] - item_type = unicode(self.semantic_type.itemData(self.semantic_type.currentIndex()).toString()) + item_type = unicode(self.semantic_type.itemData(self.semantic_type.currentIndex()) or '') self.final_type_map[item_type] = (name, frag or None) def selected_file_changed(self, *args): @@ -877,7 +877,7 @@ class InsertSemantics(Dialog): if not rows: self.anchor_names.model().set_names([]) else: - name, positions = self.file_names.model().data(rows[0], Qt.UserRole).toPyObject() + name, positions = self.file_names.model().data(rows[0], Qt.UserRole) self.populate_anchors(name) def populate_anchors(self, name): @@ -893,12 +893,12 @@ class InsertSemantics(Dialog): rows = list(self.file_names.selectionModel().selectedRows()) if not rows: return - name = self.file_names.model().data(rows[0], Qt.UserRole).toPyObject()[0] + name = self.file_names.model().data(rows[0], Qt.UserRole)[0] href = name frag = '' rows = list(self.anchor_names.selectionModel().selectedRows()) if rows: - anchor = self.anchor_names.model().data(rows[0], Qt.UserRole).toPyObject()[0] + anchor = self.anchor_names.model().data(rows[0], Qt.UserRole)[0] if anchor: frag = '#' + anchor href += frag