Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions qpageview/backgroundjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class SingleRun:
old one finishes.

"""
__slots__ = ("_job")

def __init__(self):
self._job = None

Expand Down
2 changes: 2 additions & 0 deletions qpageview/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@


class ImageEntry:
__slots__ = ("image", "bcount", "time")

def __init__(self, image):
self.image = image
self.bcount = image.sizeInBytes()
Expand Down
19 changes: 7 additions & 12 deletions qpageview/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,16 @@


class Link:
fileName = ""
isExternal = False
targetPage = -1
url = ""
tooltip = ""
area = Area(0, 0, 0, 0)
__slots__ = (
"fileName", "isExternal", "targetPage", "url", "tooltip", "area")

def __init__(self, left, top, right, bottom, url=None, tooltip=None):
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code here used class attributes to set defaults for their corresponding instance attributes, but class and instance attributes can't share names when using __slots__. Since these are only ever used as instance attributes in practice, the new code simply initializes them that way to start with.

self.fileName = ""
self.area = Area(left, top, right, bottom)
if url:
self.url = url
if "://" in url:
self.isExternal = True
if tooltip:
self.tooltip = tooltip
self.url = url or ""
self.isExternal = ("://" in self.url)
self.tooltip = tooltip or ""
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the most concise way to always initialize these as instance variables. (Otherwise, we would have to add else blocks to all the if statements above since they can no longer fall back on their class counterparts.)

self.targetPage = -1

def rect(self):
"""Return the area attribute as a QRectF()."""
Expand Down
2 changes: 2 additions & 0 deletions qpageview/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

class Link(link.Link):
"""A link that encapsulates QPdfLinkModel data."""
__slots__ = ("_targetPage", "_url")

def __init__(self, linkobj, index, pointSize):
self._targetPage = linkobj.data(index, QPdfLinkModel.Role.Page.value)
self._url = linkobj.data(index,
Expand Down
2 changes: 2 additions & 0 deletions qpageview/rectangles.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Rectangles:
once. x should be < x2 and y should be < y2.

"""
__slots__ = ("_items", "_index")

def __init__(self, objects=None):
"""Initializes the Rectangles object.

Expand Down
2 changes: 2 additions & 0 deletions qpageview/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def rect(self):

class MapToPage:
"""Simple class wrapping a QTransform to map rect and point to page coordinates."""
__slots__ = ("t")

def __init__(self, transform):
self.t = transform

Expand Down
Loading