mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 18:54:09 -04:00
Make paper size combo box re-useable
This commit is contained in:
parent
13a8f4f895
commit
aef66816a3
@ -9,8 +9,8 @@ import sys
|
||||
from threading import Thread
|
||||
|
||||
from PyQt5.Qt import (
|
||||
QCheckBox, QComboBox, QDoubleSpinBox, QFormLayout, QHBoxLayout, QIcon, QLabel,
|
||||
QLineEdit, QPageSize, QPrinter, QProgressDialog, QTimer, QToolButton, QVBoxLayout
|
||||
QCheckBox, QDoubleSpinBox, QFormLayout, QHBoxLayout, QIcon, QLabel,
|
||||
QLineEdit, QPageSize, QProgressDialog, QTimer, QToolButton, QVBoxLayout
|
||||
)
|
||||
|
||||
from calibre import sanitize_file_name
|
||||
@ -19,11 +19,11 @@ from calibre.gui2 import (
|
||||
Application, choose_save_file, dynamic, elided_text, error_dialog,
|
||||
open_local_file
|
||||
)
|
||||
from calibre.gui2.widgets import PaperSizes
|
||||
from calibre.gui2.widgets2 import Dialog
|
||||
from calibre.ptempfile import PersistentTemporaryFile
|
||||
from calibre.utils.config import JSONConfig
|
||||
from calibre.utils.filenames import expanduser
|
||||
from calibre.utils.icu import numeric_sort_key
|
||||
from calibre.utils.ipc.simple_worker import start_pipe_worker
|
||||
from calibre.utils.serialize import msgpack_dumps, msgpack_loads
|
||||
|
||||
@ -31,12 +31,6 @@ from calibre.utils.serialize import msgpack_dumps, msgpack_loads
|
||||
vprefs = JSONConfig('viewer')
|
||||
|
||||
|
||||
def format_page_size(s):
|
||||
sz = QPageSize.definitionSize(s)
|
||||
unit = {QPageSize.Millimeter: 'mm', QPageSize.Inch: 'inch'}[QPageSize.definitionUnits(s)]
|
||||
return '{} ({:g} x {:g} {})'.format(QPageSize.name(s), sz.width(), sz.height(), unit)
|
||||
|
||||
|
||||
class PrintDialog(Dialog):
|
||||
|
||||
OUTPUT_NAME = 'print-to-pdf-choose-file'
|
||||
@ -69,16 +63,9 @@ class PrintDialog(Dialog):
|
||||
w = QLabel(_('&File:'))
|
||||
l.addRow(w, h), w.setBuddy(f)
|
||||
|
||||
self.paper_size = ps = QComboBox(self)
|
||||
for a in sorted(self.paper_size_map, key=numeric_sort_key):
|
||||
qtsz = self.paper_size_map[a]
|
||||
ps.addItem(format_page_size(qtsz), a.upper())
|
||||
previous_size = vprefs.get('print-to-pdf-page-size', None)
|
||||
if previous_size not in self.paper_size_map:
|
||||
previous_size = (QPrinter().pageLayout().pageSize().name() or '').lower()
|
||||
if previous_size not in self.paper_size_map:
|
||||
previous_size = 'a4'
|
||||
ps.setCurrentIndex(ps.findData(previous_size.upper()))
|
||||
self.paper_size = ps = PaperSizes(self)
|
||||
ps.initialize()
|
||||
ps.set_value_for_config = vprefs.get('print-to-pdf-page-size', None)
|
||||
l.addRow(_('Paper &size:'), ps)
|
||||
tmap = {
|
||||
'left':_('&Left margin:'),
|
||||
@ -115,7 +102,7 @@ class PrintDialog(Dialog):
|
||||
fpath = os.path.join(head, tail)
|
||||
ans = {
|
||||
'output': fpath,
|
||||
'paper_size': self.paper_size.currentData().lower(),
|
||||
'paper_size': self.paper_size.get_value_for_config,
|
||||
'page_numbers':self.pnum.isChecked(),
|
||||
'show_file':self.show_file.isChecked(),
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ import re, os
|
||||
from PyQt5.Qt import (QIcon, QFont, QLabel, QListWidget, QAction,
|
||||
QListWidgetItem, QTextCharFormat, QApplication, QSyntaxHighlighter,
|
||||
QCursor, QColor, QWidget, QPixmap, QSplitterHandle, QToolButton,
|
||||
Qt, pyqtSignal, QRegExp, QSize, QSplitter, QPainter,
|
||||
Qt, pyqtSignal, QRegExp, QSize, QSplitter, QPainter, QPageSize, QPrinter,
|
||||
QLineEdit, QComboBox, QPen, QGraphicsScene, QMenu, QStringListModel,
|
||||
QCompleter, QTimer, QRect, QGraphicsView)
|
||||
QCompleter, QTimer, QRect, QGraphicsView, QPagedPaintDevice)
|
||||
|
||||
from calibre.gui2 import (error_dialog, pixmap_to_data, gprefs,
|
||||
warning_dialog)
|
||||
@ -958,6 +958,7 @@ class PythonHighlighter(QSyntaxHighlighter): # {{{
|
||||
|
||||
# }}}
|
||||
|
||||
|
||||
# Splitter {{{
|
||||
|
||||
|
||||
@ -1235,6 +1236,37 @@ class Splitter(QSplitter):
|
||||
# }}}
|
||||
|
||||
|
||||
class PaperSizes(QComboBox): # {{{
|
||||
|
||||
system_default_paper_size = None
|
||||
|
||||
def initialize(self, choices=None):
|
||||
from calibre.utils.icu import numeric_sort_key
|
||||
if self.system_default_paper_size is None:
|
||||
QComboBox.system_default_paper_size = 'letter' if QPrinter().pageSize() == QPagedPaintDevice.Letter else 'a4'
|
||||
if not choices:
|
||||
from calibre.ebooks.conversion.plugins.pdf_output import PAPER_SIZES
|
||||
choices = PAPER_SIZES
|
||||
for a in sorted(choices, key=numeric_sort_key):
|
||||
s = getattr(QPageSize, a.capitalize())
|
||||
sz = QPageSize.definitionSize(s)
|
||||
unit = {QPageSize.Millimeter: 'mm', QPageSize.Inch: 'inch'}[QPageSize.definitionUnits(s)]
|
||||
name = '{} ({:g} x {:g} {})'.format(QPageSize.name(s), sz.width(), sz.height(), unit)
|
||||
self.addItem(name, a)
|
||||
|
||||
@property
|
||||
def get_value_for_config(self):
|
||||
return self.currentData()
|
||||
|
||||
@get_value_for_config.setter
|
||||
def set_value_for_config(self, val):
|
||||
idx = self.findData(val or self.system_default_paper_size)
|
||||
if idx == -1:
|
||||
idx = self.findData('a4')
|
||||
self.setCurrentIndex(idx)
|
||||
# }}}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from PyQt5.Qt import QTextEdit
|
||||
app = QApplication([])
|
||||
|
Loading…
x
Reference in New Issue
Block a user