mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -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 threading import Thread
|
||||||
|
|
||||||
from PyQt5.Qt import (
|
from PyQt5.Qt import (
|
||||||
QCheckBox, QComboBox, QDoubleSpinBox, QFormLayout, QHBoxLayout, QIcon, QLabel,
|
QCheckBox, QDoubleSpinBox, QFormLayout, QHBoxLayout, QIcon, QLabel,
|
||||||
QLineEdit, QPageSize, QPrinter, QProgressDialog, QTimer, QToolButton, QVBoxLayout
|
QLineEdit, QPageSize, QProgressDialog, QTimer, QToolButton, QVBoxLayout
|
||||||
)
|
)
|
||||||
|
|
||||||
from calibre import sanitize_file_name
|
from calibre import sanitize_file_name
|
||||||
@ -19,11 +19,11 @@ from calibre.gui2 import (
|
|||||||
Application, choose_save_file, dynamic, elided_text, error_dialog,
|
Application, choose_save_file, dynamic, elided_text, error_dialog,
|
||||||
open_local_file
|
open_local_file
|
||||||
)
|
)
|
||||||
|
from calibre.gui2.widgets import PaperSizes
|
||||||
from calibre.gui2.widgets2 import Dialog
|
from calibre.gui2.widgets2 import Dialog
|
||||||
from calibre.ptempfile import PersistentTemporaryFile
|
from calibre.ptempfile import PersistentTemporaryFile
|
||||||
from calibre.utils.config import JSONConfig
|
from calibre.utils.config import JSONConfig
|
||||||
from calibre.utils.filenames import expanduser
|
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.ipc.simple_worker import start_pipe_worker
|
||||||
from calibre.utils.serialize import msgpack_dumps, msgpack_loads
|
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')
|
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):
|
class PrintDialog(Dialog):
|
||||||
|
|
||||||
OUTPUT_NAME = 'print-to-pdf-choose-file'
|
OUTPUT_NAME = 'print-to-pdf-choose-file'
|
||||||
@ -69,16 +63,9 @@ class PrintDialog(Dialog):
|
|||||||
w = QLabel(_('&File:'))
|
w = QLabel(_('&File:'))
|
||||||
l.addRow(w, h), w.setBuddy(f)
|
l.addRow(w, h), w.setBuddy(f)
|
||||||
|
|
||||||
self.paper_size = ps = QComboBox(self)
|
self.paper_size = ps = PaperSizes(self)
|
||||||
for a in sorted(self.paper_size_map, key=numeric_sort_key):
|
ps.initialize()
|
||||||
qtsz = self.paper_size_map[a]
|
ps.set_value_for_config = vprefs.get('print-to-pdf-page-size', None)
|
||||||
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()))
|
|
||||||
l.addRow(_('Paper &size:'), ps)
|
l.addRow(_('Paper &size:'), ps)
|
||||||
tmap = {
|
tmap = {
|
||||||
'left':_('&Left margin:'),
|
'left':_('&Left margin:'),
|
||||||
@ -115,7 +102,7 @@ class PrintDialog(Dialog):
|
|||||||
fpath = os.path.join(head, tail)
|
fpath = os.path.join(head, tail)
|
||||||
ans = {
|
ans = {
|
||||||
'output': fpath,
|
'output': fpath,
|
||||||
'paper_size': self.paper_size.currentData().lower(),
|
'paper_size': self.paper_size.get_value_for_config,
|
||||||
'page_numbers':self.pnum.isChecked(),
|
'page_numbers':self.pnum.isChecked(),
|
||||||
'show_file':self.show_file.isChecked(),
|
'show_file':self.show_file.isChecked(),
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,9 @@ import re, os
|
|||||||
from PyQt5.Qt import (QIcon, QFont, QLabel, QListWidget, QAction,
|
from PyQt5.Qt import (QIcon, QFont, QLabel, QListWidget, QAction,
|
||||||
QListWidgetItem, QTextCharFormat, QApplication, QSyntaxHighlighter,
|
QListWidgetItem, QTextCharFormat, QApplication, QSyntaxHighlighter,
|
||||||
QCursor, QColor, QWidget, QPixmap, QSplitterHandle, QToolButton,
|
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,
|
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,
|
from calibre.gui2 import (error_dialog, pixmap_to_data, gprefs,
|
||||||
warning_dialog)
|
warning_dialog)
|
||||||
@ -958,6 +958,7 @@ class PythonHighlighter(QSyntaxHighlighter): # {{{
|
|||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
# Splitter {{{
|
# 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__':
|
if __name__ == '__main__':
|
||||||
from PyQt5.Qt import QTextEdit
|
from PyQt5.Qt import QTextEdit
|
||||||
app = QApplication([])
|
app = QApplication([])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user