More QVariant migration

This commit is contained in:
Kovid Goyal 2014-04-23 10:55:02 +05:30
parent a5dc23ae0f
commit 12f1e58f9f
5 changed files with 29 additions and 28 deletions

View File

@ -48,7 +48,7 @@ def all_py_files():
def detect_qvariant(): def detect_qvariant():
count = 0 count = 0
pat = re.compile(b'|'.join(br'QVariant NONE toDateTime toDate toInt toBool toString\(\) toPyObject canConvert toBitArray toByteArray toHash toFloat toMap toLine toPoint toReal toRect toTime toUInt toUrl'.split())) # noqa pat = re.compile(b'|'.join(br'QVariant NONE toDateTime toDate toInt toBool toString\(\) toPyObject canConvert toBitArray toByteArray toHash toFloat toMap toLine toPoint toReal toRect toTime\(\) toUInt toUrl'.split())) # noqa
exclusions = { exclusions = {
'src/calibre/gui2/viewer/gestures.py': {'toPoint'}, 'src/calibre/gui2/viewer/gestures.py': {'toPoint'},
'src/calibre/utils/serve_coffee.py': {'toString()'}, 'src/calibre/utils/serve_coffee.py': {'toString()'},

View File

@ -88,8 +88,9 @@ class WebView(QWebView): # {{{
@property @property
def scroll_frac(self): def scroll_frac(self):
val, ok = self.page().evaljs('window.pageYOffset/document.body.scrollHeight').toFloat() try:
if not ok: val = float(self.page().evaljs('window.pageYOffset/document.body.scrollHeight'))
except (TypeError, ValueError):
val = 0 val = 0
return val return val
# }}} # }}}
@ -190,9 +191,9 @@ class ItemEdit(QWidget):
_('No match found for: %s')%text, show=True) _('No match found for: %s')%text, show=True)
delta = 1 if forwards else -1 delta = 1 if forwards else -1
current = unicode(d.currentItem().data(Qt.DisplayRole).toString()) current = unicode(d.currentItem().data(Qt.DisplayRole) or '')
next_index = (d.currentRow() + delta)%d.count() next_index = (d.currentRow() + delta)%d.count()
next = unicode(d.item(next_index).data(Qt.DisplayRole).toString()) next = unicode(d.item(next_index).data(Qt.DisplayRole) or '')
msg = '<p>'+_('No matches for %(text)s found in the current file [%(current)s].' msg = '<p>'+_('No matches for %(text)s found in the current file [%(current)s].'
' Do you want to search in the %(which)s file [%(next)s]?') ' Do you want to search in the %(which)s file [%(next)s]?')
msg = msg%dict(text=text, current=current, next=next, msg = msg%dict(text=text, current=current, next=next,
@ -215,7 +216,7 @@ class ItemEdit(QWidget):
self.dest_list.addItems(spine_names) self.dest_list.addItems(spine_names)
def current_changed(self, item): def current_changed(self, item):
name = self.current_name = unicode(item.data(Qt.DisplayRole).toString()) name = self.current_name = unicode(item.data(Qt.DisplayRole) or '')
path = self.container.name_to_abspath(name) path = self.container.name_to_abspath(name)
# Ensure encoding map is populated # Ensure encoding map is populated
root = self.container.parsed(name) root = self.container.parsed(name)
@ -248,13 +249,13 @@ class ItemEdit(QWidget):
dest_index, frag = 0, None dest_index, frag = 0, None
if item is not None: if item is not None:
if where is None: if where is None:
self.name.setText(item.data(0, Qt.DisplayRole).toString()) self.name.setText(item.data(0, Qt.DisplayRole) or '')
self.name.setCursorPosition(0) self.name.setCursorPosition(0)
toc = item.data(0, Qt.UserRole).toPyObject() toc = item.data(0, Qt.UserRole)
if toc.dest: if toc.dest:
for i in xrange(self.dest_list.count()): for i in xrange(self.dest_list.count()):
litem = self.dest_list.item(i) litem = self.dest_list.item(i)
if unicode(litem.data(Qt.DisplayRole).toString()) == toc.dest: if unicode(litem.data(Qt.DisplayRole) or '') == toc.dest:
dest_index = i dest_index = i
frag = toc.frag frag = toc.frag
break break

View File

@ -11,7 +11,7 @@ import sys, os, textwrap
from threading import Thread from threading import Thread
from functools import partial from functools import partial
from PyQt5.Qt import (QPushButton, QFrame, QVariant, QMenu, QInputDialog, from PyQt5.Qt import (QPushButton, QFrame, QMenu, QInputDialog,
QDialog, QVBoxLayout, QDialogButtonBox, QSize, QStackedWidget, QWidget, QDialog, QVBoxLayout, QDialogButtonBox, QSize, QStackedWidget, QWidget,
QLabel, Qt, pyqtSignal, QIcon, QTreeWidget, QGridLayout, QTreeWidgetItem, QLabel, Qt, pyqtSignal, QIcon, QTreeWidget, QGridLayout, QTreeWidgetItem,
QToolButton, QItemSelectionModel, QCursor, QKeySequence) QToolButton, QItemSelectionModel, QCursor, QKeySequence)
@ -330,12 +330,12 @@ class ItemView(QFrame): # {{{
def populate_item_pane(self): def populate_item_pane(self):
item = self.current_item item = self.current_item
name = unicode(item.data(0, Qt.DisplayRole).toString()) name = unicode(item.data(0, Qt.DisplayRole) or '')
self.item_pane.heading.setText('<h2>%s</h2>'%name) self.item_pane.heading.setText('<h2>%s</h2>'%name)
self.icon_label.setPixmap(item.data(0, Qt.DecorationRole self.icon_label.setPixmap(item.data(0, Qt.DecorationRole
).toPyObject().pixmap(32, 32)) ).pixmap(32, 32))
tt = _('This entry points to an existing destination') tt = _('This entry points to an existing destination')
toc = item.data(0, Qt.UserRole).toPyObject() toc = item.data(0, Qt.UserRole)
if toc.dest_exists is False: if toc.dest_exists is False:
tt = _('The location this entry points to does not exist') tt = _('The location this entry points to does not exist')
elif toc.dest_exists is None: elif toc.dest_exists is None:
@ -487,7 +487,7 @@ class TreeWidget(QTreeWidget): # {{{
def title_case(self): def title_case(self):
from calibre.utils.titlecase import titlecase from calibre.utils.titlecase import titlecase
for item in self.selectedItems(): for item in self.selectedItems():
t = unicode(item.data(0, Qt.DisplayRole).toString()) t = unicode(item.data(0, Qt.DisplayRole) or '')
item.setData(0, Qt.DisplayRole, titlecase(t)) item.setData(0, Qt.DisplayRole, titlecase(t))
def bulk_rename(self): def bulk_rename(self):
@ -526,7 +526,7 @@ class TreeWidget(QTreeWidget): # {{{
if item is not None: if item is not None:
m = QMenu() m = QMenu()
ci = unicode(item.data(0, Qt.DisplayRole).toString()) ci = unicode(item.data(0, Qt.DisplayRole) or '')
p = item.parent() or self.invisibleRootItem() p = item.parent() or self.invisibleRootItem()
idx = p.indexOfChild(item) idx = p.indexOfChild(item)
if idx > 0: if idx > 0:
@ -622,7 +622,7 @@ class TOCView(QWidget): # {{{
return super(TOCView, self).event(e) return super(TOCView, self).event(e)
def item_title(self, item): def item_title(self, item):
return unicode(item.data(0, Qt.DisplayRole).toString()) return unicode(item.data(0, Qt.DisplayRole) or '')
def del_items(self): def del_items(self):
self.tocw.del_items() self.tocw.del_items()
@ -672,7 +672,7 @@ class TOCView(QWidget): # {{{
self.tocw.move_down() self.tocw.move_down()
def update_status_tip(self, item): def update_status_tip(self, item):
c = item.data(0, Qt.UserRole).toPyObject() c = item.data(0, Qt.UserRole)
if c is not None: if c is not None:
frag = c.frag or '' frag = c.frag or ''
if frag: if frag:
@ -683,8 +683,8 @@ class TOCView(QWidget): # {{{
def data_changed(self, top_left, bottom_right): def data_changed(self, top_left, bottom_right):
for r in xrange(top_left.row(), bottom_right.row()+1): for r in xrange(top_left.row(), bottom_right.row()+1):
idx = self.tocw.model().index(r, 0, top_left.parent()) idx = self.tocw.model().index(r, 0, top_left.parent())
new_title = unicode(idx.data(Qt.DisplayRole).toString()).strip() new_title = unicode(idx.data(Qt.DisplayRole) or '').strip()
toc = idx.data(Qt.UserRole).toPyObject() toc = idx.data(Qt.UserRole)
if toc is not None: if toc is not None:
toc.title = new_title or _('(Untitled)') toc.title = new_title or _('(Untitled)')
item = self.tocw.itemFromIndex(idx) item = self.tocw.itemFromIndex(idx)
@ -711,7 +711,7 @@ class TOCView(QWidget): # {{{
'The location this entry point to does not exist:\n%s') 'The location this entry point to does not exist:\n%s')
%child.dest_error) %child.dest_error)
else: else:
c.setData(0, Qt.ToolTipRole, QVariant()) c.setData(0, Qt.ToolTipRole, None)
self.update_status_tip(c) self.update_status_tip(c)
@ -774,8 +774,8 @@ class TOCView(QWidget): # {{{
def process_node(parent, toc_parent): def process_node(parent, toc_parent):
for i in xrange(parent.childCount()): for i in xrange(parent.childCount()):
item = parent.child(i) item = parent.child(i)
title = unicode(item.data(0, Qt.DisplayRole).toString()).strip() title = unicode(item.data(0, Qt.DisplayRole) or '').strip()
toc = item.data(0, Qt.UserRole).toPyObject() toc = item.data(0, Qt.UserRole)
dest, frag = toc.dest, toc.frag dest, frag = toc.dest, toc.frag
toc = toc_parent.add(title, dest, frag) toc = toc_parent.add(title, dest, frag)
process_node(item, toc) process_node(item, toc)

View File

@ -101,14 +101,14 @@ def download_resources(browser, resource_cache, output_dir):
for img in browser.css_select('img[src]', all=True): for img in browser.css_select('img[src]', all=True):
# Using javascript ensures that absolute URLs are returned, direct # Using javascript ensures that absolute URLs are returned, direct
# attribute access does not do that # attribute access does not do that
src = unicode(img.evaluateJavaScript('this.src').toString()).strip() src = unicode(img.evaluateJavaScript('this.src') or '').strip()
if src: if src:
resources[src].append(img) resources[src].append(img)
for link in browser.css_select('link[href]', all=True): for link in browser.css_select('link[href]', all=True):
lt = unicode(link.attribute('type')).strip() or 'text/css' lt = unicode(link.attribute('type')).strip() or 'text/css'
rel = unicode(link.attribute('rel')).strip() or 'stylesheet' rel = unicode(link.attribute('rel')).strip() or 'stylesheet'
if lt == 'text/css' and rel == 'stylesheet': if lt == 'text/css' and rel == 'stylesheet':
href = unicode(link.evaluateJavaScript('this.href').toString()).strip() href = unicode(link.evaluateJavaScript('this.href') or '').strip()
if href: if href:
resources[href].append(link) resources[href].append(link)
else: else:
@ -165,7 +165,7 @@ def links_from_selectors(selectors, recursions, browser, url, recursion_level):
if recursions > recursion_level: if recursions > recursion_level:
for selector in selectors: for selector in selectors:
for a in browser.css_select(selector, all=True): for a in browser.css_select(selector, all=True):
href = unicode(a.evaluateJavaScript('this.href').toString()).strip() href = unicode(a.evaluateJavaScript('this.href') or '').strip()
if href: if href:
ans.append(href) ans.append(href)
return ans return ans

View File

@ -125,7 +125,7 @@ class WebPage(QWebPage): # {{{
@property @property
def ready_state(self): def ready_state(self):
return unicode(self.mainFrame().evaluateJavaScript('document.readyState').toString()) return unicode(self.mainFrame().evaluateJavaScript('document.readyState') or '')
@pyqtSlot(QPixmap) @pyqtSlot(QPixmap)
def transfer_image(self, img): def transfer_image(self, img):
@ -210,7 +210,7 @@ class NetworkAccessManager(QNetworkAccessManager): # {{{
reply.ignoreSslErrors() reply.ignoreSslErrors()
def createRequest(self, operation, request, data): def createRequest(self, operation, request, data):
url = unicode(request.url().toString()) url = unicode(request.url().toString(QUrl.None))
operation_name = self.OPERATION_NAMES[operation] operation_name = self.OPERATION_NAMES[operation]
debug = [] debug = []
debug.append(('Request: %s %s' % (operation_name, url))) debug.append(('Request: %s %s' % (operation_name, url)))
@ -245,7 +245,7 @@ class NetworkAccessManager(QNetworkAccessManager): # {{{
self.report_reply(reply) self.report_reply(reply)
def report_reply(self, reply): def report_reply(self, reply):
reply_url = unicode(reply.url().toString()) reply_url = unicode(reply.url().toString(QUrl.None))
self.reply_count += 1 self.reply_count += 1
err = reply.error() err = reply.error()