def paintPageNumbers(self): # Page numbers are drawn only when the editor is in page mode, # if fields are set and the option to display numbers is enabled if (self.m_usePageMode and not self.m_pageMetrics.pxPageMargins().isNull() and self.m_showPageNumbers): pageWidth = self.m_pageMetrics.pxPageSize().width() pageHeight = self.m_pageMetrics.pxPageSize().height() pageMargins = QMarginsF(self.m_pageMetrics.pxPageMargins()) p = QPainter(self.viewport()) font = QFont() font.setPointSize(10) font.setFamily('Calibri') p.setFont(font) p.setPen(QPen(self.palette().text(), 1)) # The current height and width are displayed on the screen. curHeight = pageHeight - (self.verticalScrollBar().value() % pageHeight) # The start of the field must take into account the scrollbar offset. leftMarginPosition = pageMargins.left() - self.horizontalScrollBar( ).value() # Total field width marginWidth = pageWidth - pageMargins.left() - pageMargins.right() # The number of the first page to see. pageNumber = self.verticalScrollBar().value() / pageHeight + 1 # Add 0.3 to pageNumber before emitting # to announce page when it is about halfway up the screen self.pageInfo.emit( (int(pageNumber + 0.3), int(self.verticalScrollBar().maximum() / pageHeight + 1))) # Paint page numbers while there are remotely more visible pages while (curHeight < pageHeight + self.height()): # Define the space for top page number; paint number topMarginRect = QRectF(leftMarginPosition, curHeight - pageHeight, marginWidth, pageMargins.top()) self.paintPageNumber(p, topMarginRect, True, pageNumber) # Define the space for bottom page number; paint number bottomMarginRect = QRectF(leftMarginPosition, curHeight - pageMargins.bottom(), marginWidth, pageMargins.bottom()) self.paintPageNumber(p, bottomMarginRect, False, pageNumber) # Go to next page pageNumber += 1 curHeight += pageHeight
class PageMetrics(): m_pageFormat = QPageSize.PageSizeId() m_mmPageSize = QSizeF() m_mmPageMargins = QMarginsF() m_pxPageSize = QSizeF() m_pxPageMargins = QMarginsF() def __init__(self): super(PageMetrics, self).__init__() def mmToInches(self, mm): return mm * 0.039370147 def mmToPx(self, _mm, _x): return self.mmToInches(_mm) * (qApp.desktop().logicalDpiX() if _x else qApp.desktop().logicalDpiY()) def pageSizeIdFromString(self, _from): result = QPageSize.A4 if (_from == "A0"): result = QPageSize.A0 elif (_from == "A1"): result = QPageSize.A1 elif (_from == "A2"): result = QPageSize.A2 elif (_from == "A3"): result = QPageSize.A3 elif (_from == "A4"): result = QPageSize.A4 elif (_from == "A5"): result = QPageSize.A5 elif (_from == "A6"): result = QPageSize.A6 else: Q_ASSERT_X(0, Q_FUNC_INFO, qPrintable("Undefined page size: " + _from)) return result def stringFromPageSizeId(self, _pageSize): result = "" if(_pageSize == QPageSize.A0): result = "A0" elif (_pageSize == QPageSize.A1): result = "A1" elif (_pageSize == QPageSize.A2): result = "A2" elif (_pageSize == QPageSize.A3): result = "A3" elif (_pageSize == QPageSize.A4): result = "A4" elif (_pageSize == QPageSize.A5): result = "A5" elif (_pageSize == QPageSize.A6): result = "A6" else: Q_ASSERT_X(0, Q_FUNC_INFO, qPrintable("Undefined page size: " + QString.number(_pageSize))) return result def update(self, _pageFormat, _mmPageMargins=QMarginsF()): self.m_pageFormat = _pageFormat self.m_mmPageSize = QPageSize(self.m_pageFormat).rect(QPageSize.Millimeter).size() self.m_mmPageMargins = _mmPageMargins # Calculate values in pixels x = True; y = False self.m_pxPageSize = QSizeF(self.mmToPx(self.m_mmPageSize.width(), x), self.mmToPx(self.m_mmPageSize.height(), y)) self.m_pxPageMargins = QMarginsF(self.mmToPx(self.m_mmPageMargins.left(), x), self.mmToPx(self.m_mmPageMargins.top(), y), self.mmToPx(self.m_mmPageMargins.right(), x), self.mmToPx(self.m_mmPageMargins.bottom(), y)) def pageFormat(self): return self.m_pageFormat def mmPageSize(self): return self.m_mmPageSize def mmPageMargins(self): return self.m_mmPageMargins def pxPageSize(self): return QSizeF(self.m_pxPageSize.width(), self.m_pxPageSize.height()) def pxPageMargins(self): return QMarginsF(self.m_pxPageMargins.left(), self.m_pxPageMargins.top(), self.m_pxPageMargins.right(), self.m_pxPageMargins.bottom())