mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add search to the font family chooser dialog and get rid of the obsolete fontfamilymodel
This commit is contained in:
parent
570b2e5824
commit
d1cf14703d
@ -13,7 +13,7 @@ from PyQt4.Qt import (QFontInfo, QFontMetrics, Qt, QFont, QFontDatabase, QPen,
|
|||||||
QStyledItemDelegate, QSize, QStyle, QStringListModel, pyqtSignal,
|
QStyledItemDelegate, QSize, QStyle, QStringListModel, pyqtSignal,
|
||||||
QDialog, QVBoxLayout, QApplication, QFontComboBox, QPushButton,
|
QDialog, QVBoxLayout, QApplication, QFontComboBox, QPushButton,
|
||||||
QToolButton, QGridLayout, QListView, QWidget, QDialogButtonBox, QIcon,
|
QToolButton, QGridLayout, QListView, QWidget, QDialogButtonBox, QIcon,
|
||||||
QHBoxLayout, QLabel, QModelIndex)
|
QHBoxLayout, QLabel, QModelIndex, QLineEdit)
|
||||||
|
|
||||||
from calibre.constants import config_dir
|
from calibre.constants import config_dir
|
||||||
from calibre.gui2 import choose_files, error_dialog, info_dialog
|
from calibre.gui2 import choose_files, error_dialog, info_dialog
|
||||||
@ -197,15 +197,56 @@ class FontFamilyDialog(QDialog):
|
|||||||
afb.setIcon(QIcon(I('plus.png')))
|
afb.setIcon(QIcon(I('plus.png')))
|
||||||
afb.clicked.connect(self.add_fonts)
|
afb.clicked.connect(self.add_fonts)
|
||||||
self.ml = QLabel(_('Choose a font family from the list below:'))
|
self.ml = QLabel(_('Choose a font family from the list below:'))
|
||||||
|
self.search = QLineEdit(self)
|
||||||
|
self.search.setPlaceholderText(_('Search'))
|
||||||
|
self.search.returnPressed.connect(self.find)
|
||||||
|
self.nb = QToolButton(self)
|
||||||
|
self.nb.setIcon(QIcon(I('arrow-down.png')))
|
||||||
|
self.nb.setToolTip(_('Find Next'))
|
||||||
|
self.pb = QToolButton(self)
|
||||||
|
self.pb.setIcon(QIcon(I('arrow-up.png')))
|
||||||
|
self.pb.setToolTip(_('Find Previous'))
|
||||||
|
self.nb.clicked.connect(self.find_next)
|
||||||
|
self.pb.clicked.connect(self.find_previous)
|
||||||
|
|
||||||
l.addWidget(self.ml, 0, 0, 1, 2)
|
l.addWidget(self.ml, 0, 0, 1, 4)
|
||||||
l.addWidget(self.view, 1, 0, 1, 1)
|
l.addWidget(self.search, 1, 0, 1, 1)
|
||||||
l.addWidget(self.faces, 1, 1, 1, 1)
|
l.addWidget(self.nb, 1, 1, 1, 1)
|
||||||
l.addWidget(self.bb, 2, 0, 1, 2)
|
l.addWidget(self.pb, 1, 2, 1, 1)
|
||||||
|
l.addWidget(self.view, 2, 0, 1, 3)
|
||||||
|
l.addWidget(self.faces, 1, 3, 2, 1)
|
||||||
|
l.addWidget(self.bb, 3, 0, 1, 4)
|
||||||
l.setAlignment(self.faces, Qt.AlignTop)
|
l.setAlignment(self.faces, Qt.AlignTop)
|
||||||
|
|
||||||
self.resize(800, 600)
|
self.resize(800, 600)
|
||||||
|
|
||||||
|
def set_current(self, i):
|
||||||
|
self.view.setCurrentIndex(self.m.index(i))
|
||||||
|
|
||||||
|
def keyPressEvent(self, e):
|
||||||
|
if e.key() == Qt.Key_Return:
|
||||||
|
return
|
||||||
|
return QDialog.keyPressEvent(self, e)
|
||||||
|
|
||||||
|
def find(self, backwards=False):
|
||||||
|
i = self.view.currentIndex().row()
|
||||||
|
if i < 0: i = 0
|
||||||
|
q = icu_lower(unicode(self.search.text())).strip()
|
||||||
|
if not q: return
|
||||||
|
r = (xrange(i-1, -1, -1) if backwards else xrange(i+1,
|
||||||
|
len(self.families)))
|
||||||
|
for j in r:
|
||||||
|
f = self.families[j]
|
||||||
|
if q in icu_lower(f):
|
||||||
|
self.set_current(j)
|
||||||
|
return
|
||||||
|
|
||||||
|
def find_next(self):
|
||||||
|
self.find()
|
||||||
|
|
||||||
|
def find_previous(self):
|
||||||
|
self.find(backwards=True)
|
||||||
|
|
||||||
def build_font_list(self):
|
def build_font_list(self):
|
||||||
try:
|
try:
|
||||||
self.families = list(self.font_scanner.find_font_families())
|
self.families = list(self.font_scanner.find_font_families())
|
||||||
|
@ -3,18 +3,16 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
'''
|
'''
|
||||||
Miscellaneous widgets used in the GUI
|
Miscellaneous widgets used in the GUI
|
||||||
'''
|
'''
|
||||||
import re, traceback, os
|
import re, os
|
||||||
|
|
||||||
from PyQt4.Qt import (QIcon, QFont, QLabel, QListWidget, QAction,
|
from PyQt4.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,
|
||||||
QAbstractListModel, QVariant, Qt, SIGNAL, pyqtSignal, QRegExp, QSize,
|
QVariant, Qt, SIGNAL, pyqtSignal, QRegExp, QSize, QSplitter, QPainter,
|
||||||
QSplitter, QPainter, QLineEdit, QComboBox, QPen, QGraphicsScene, QMenu,
|
QLineEdit, QComboBox, QPen, QGraphicsScene, QMenu, QStringListModel,
|
||||||
QStringListModel, QCompleter, QStringList, QTimer, QRect,
|
QCompleter, QStringList, QTimer, QRect, QGraphicsView, QByteArray)
|
||||||
QGraphicsView, QByteArray)
|
|
||||||
|
|
||||||
from calibre.constants import iswindows
|
from calibre.gui2 import (error_dialog, pixmap_to_data, gprefs,
|
||||||
from calibre.gui2 import (NONE, error_dialog, pixmap_to_data, gprefs,
|
|
||||||
warning_dialog)
|
warning_dialog)
|
||||||
from calibre.gui2.filename_pattern_ui import Ui_Form
|
from calibre.gui2.filename_pattern_ui import Ui_Form
|
||||||
from calibre import fit_image
|
from calibre import fit_image
|
||||||
@ -348,43 +346,6 @@ class CoverView(QGraphicsView, ImageDropMixin): # {{{
|
|||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
class FontFamilyModel(QAbstractListModel): # {{{
|
|
||||||
|
|
||||||
def __init__(self, *args):
|
|
||||||
QAbstractListModel.__init__(self, *args)
|
|
||||||
from calibre.utils.fonts.scanner import font_scanner
|
|
||||||
try:
|
|
||||||
self.families = font_scanner.find_font_families()
|
|
||||||
except:
|
|
||||||
self.families = []
|
|
||||||
print 'WARNING: Could not load fonts'
|
|
||||||
traceback.print_exc()
|
|
||||||
# Restrict to Qt families as Qt tends to crash
|
|
||||||
self.families[:0] = [_('None')]
|
|
||||||
self.font = QFont('Arial' if iswindows else 'sansserif')
|
|
||||||
|
|
||||||
def rowCount(self, *args):
|
|
||||||
return len(self.families)
|
|
||||||
|
|
||||||
def data(self, index, role):
|
|
||||||
try:
|
|
||||||
family = self.families[index.row()]
|
|
||||||
except:
|
|
||||||
traceback.print_exc()
|
|
||||||
return NONE
|
|
||||||
if role == Qt.DisplayRole:
|
|
||||||
return QVariant(family)
|
|
||||||
if role == Qt.FontRole:
|
|
||||||
# If a user chooses some non standard font as the interface font,
|
|
||||||
# rendering some font names causes Qt to crash, so return what is
|
|
||||||
# hopefully a "safe" font
|
|
||||||
return QVariant(self.font)
|
|
||||||
return NONE
|
|
||||||
|
|
||||||
def index_of(self, family):
|
|
||||||
return self.families.index(family.strip())
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
# BasicList {{{
|
# BasicList {{{
|
||||||
class BasicListItem(QListWidgetItem):
|
class BasicListItem(QListWidgetItem):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user