Improve column text alignment for some dialogs in the editor

This commit is contained in:
Kovid Goyal 2021-05-31 11:28:14 +05:30
parent d6840d6d55
commit 28b551080b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 31 additions and 9 deletions

View File

@ -7,6 +7,7 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import time, textwrap, os import time, textwrap, os
from threading import Thread from threading import Thread
from contextlib import suppress
from operator import itemgetter from operator import itemgetter
from functools import partial from functools import partial
from collections import defaultdict from collections import defaultdict
@ -94,6 +95,7 @@ class ProxyModel(QSortFilterProxyModel):
class FileCollection(QAbstractTableModel): class FileCollection(QAbstractTableModel):
COLUMN_HEADERS = () COLUMN_HEADERS = ()
alignments = ()
def __init__(self, parent=None): def __init__(self, parent=None):
self.files = self.sort_keys = () self.files = self.sort_keys = ()
@ -107,11 +109,13 @@ class FileCollection(QAbstractTableModel):
return len(self.files) return len(self.files)
def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole): def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole):
if role == Qt.ItemDataRole.DisplayRole and orientation == Qt.Orientation.Horizontal: if orientation == Qt.Orientation.Horizontal:
try: if role == Qt.ItemDataRole.DisplayRole:
return self.COLUMN_HEADERS[section] with suppress(IndexError):
except IndexError: return self.COLUMN_HEADERS[section]
pass elif role == Qt.ItemDataRole.TextAlignmentRole:
with suppress(IndexError):
return self.alignments[section]
return QAbstractTableModel.headerData(self, section, orientation, role) return QAbstractTableModel.headerData(self, section, orientation, role)
def location(self, index): def location(self, index):
@ -229,6 +233,7 @@ class FilesView(QTableView):
class FilesModel(FileCollection): class FilesModel(FileCollection):
COLUMN_HEADERS = (_('Folder'), _('Name'), _('Size (KB)'), _('Type')) COLUMN_HEADERS = (_('Folder'), _('Name'), _('Size (KB)'), _('Type'))
alignments = Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignRight, Qt.AlignmentFlag.AlignLeft
CATEGORY_NAMES = { CATEGORY_NAMES = {
'image':_('Image'), 'image':_('Image'),
'text': _('Text'), 'text': _('Text'),
@ -270,9 +275,11 @@ class FilesModel(FileCollection):
return entry.basename return entry.basename
if col == 2: if col == 2:
sz = entry.size / 1024. sz = entry.size / 1024.
return ('%.2f' % sz if int(sz) != sz else unicode_type(sz)) return '%.2f ' % sz
if col == 3: if col == 3:
return self.CATEGORY_NAMES.get(entry.category) return self.CATEGORY_NAMES.get(entry.category)
elif role == Qt.ItemDataRole.TextAlignmentRole:
return Qt.AlignVCenter | self.alignments[index.column()]
class FilesWidget(QWidget): class FilesWidget(QWidget):
@ -423,6 +430,7 @@ class ImagesDelegate(QStyledItemDelegate):
class ImagesModel(FileCollection): class ImagesModel(FileCollection):
COLUMN_HEADERS = [_('Image'), _('Size (KB)'), _('Times used'), _('Resolution')] COLUMN_HEADERS = [_('Image'), _('Size (KB)'), _('Times used'), _('Resolution')]
alignments = Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignRight, Qt.AlignmentFlag.AlignRight, Qt.AlignmentFlag.AlignRight
def __init__(self, parent=None): def __init__(self, parent=None):
FileCollection.__init__(self, parent) FileCollection.__init__(self, parent)
@ -461,6 +469,9 @@ class ImagesModel(FileCollection):
return self.files[index.row()] return self.files[index.row()]
except IndexError: except IndexError:
pass pass
elif role == Qt.TextAlignmentRole:
with suppress(IndexError):
return self.alignments[index.column()]
class ImagesWidget(QWidget): class ImagesWidget(QWidget):
@ -682,6 +693,7 @@ class LinksWidget(QWidget):
class WordsModel(FileCollection): class WordsModel(FileCollection):
COLUMN_HEADERS = (_('Word'), _('Language'), _('Times used')) COLUMN_HEADERS = (_('Word'), _('Language'), _('Times used'))
alignments = Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignRight
total_words = 0 total_words = 0
def __call__(self, data): def __call__(self, data):
@ -726,6 +738,9 @@ class WordsModel(FileCollection):
return self.files[index.row()] return self.files[index.row()]
except IndexError: except IndexError:
pass pass
elif role == Qt.TextAlignmentRole:
with suppress(IndexError):
return self.alignments[index.column()]
def location(self, index): def location(self, index):
return None return None
@ -778,6 +793,7 @@ class WordsWidget(QWidget):
class CharsModel(FileCollection): class CharsModel(FileCollection):
COLUMN_HEADERS = (_('Character'), _('Name'), _('Codepoint'), _('Times used')) COLUMN_HEADERS = (_('Character'), _('Name'), _('Codepoint'), _('Times used'))
alignments = Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignRight
all_chars = () all_chars = ()
def __call__(self, data): def __call__(self, data):
@ -814,6 +830,9 @@ class CharsModel(FileCollection):
return self.files[index.row()] return self.files[index.row()]
except IndexError: except IndexError:
pass pass
elif role == Qt.TextAlignmentRole:
with suppress(IndexError):
return self.alignments[index.column()]
def location(self, index): def location(self, index):
return None return None

View File

@ -621,6 +621,7 @@ class WordsModel(QAbstractTableModel):
self.filter_expression = None self.filter_expression = None
self.show_only_misspelt = True self.show_only_misspelt = True
self.headers = (_('Word'), _('Count'), _('Language'), _('Misspelled?')) self.headers = (_('Word'), _('Count'), _('Language'), _('Misspelled?'))
self.alignments = Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignRight, Qt.AlignmentFlag.AlignLeft, Qt.AlignmentFlag.AlignHCenter
def rowCount(self, parent=QModelIndex()): def rowCount(self, parent=QModelIndex()):
return len(self.items) return len(self.items)
@ -644,6 +645,8 @@ class WordsModel(QAbstractTableModel):
pass pass
elif role == Qt.ItemDataRole.InitialSortOrderRole: elif role == Qt.ItemDataRole.InitialSortOrderRole:
return Qt.SortOrder.DescendingOrder if section == 1 else Qt.SortOrder.AscendingOrder return Qt.SortOrder.DescendingOrder if section == 1 else Qt.SortOrder.AscendingOrder
elif role == Qt.ItemDataRole.TextAlignmentRole:
return Qt.AlignVCenter | self.alignments[section]
def misspelled_text(self, w): def misspelled_text(self, w):
if self.spell_map[w]: if self.spell_map[w]:
@ -660,17 +663,17 @@ class WordsModel(QAbstractTableModel):
if col == 0: if col == 0:
return word return word
if col == 1: if col == 1:
return '%d' % len(self.words[(word, locale)]) return '{} '.format(len(self.words[(word, locale)]))
if col == 2: if col == 2:
pl = calibre_langcode_to_name(locale.langcode) pl = calibre_langcode_to_name(locale.langcode)
countrycode = locale.countrycode countrycode = locale.countrycode
if countrycode: if countrycode:
pl = '%s (%s)' % (pl, countrycode) pl = ' %s (%s)' % (pl, countrycode)
return pl return pl
if col == 3: if col == 3:
return self.misspelled_text((word, locale)) return self.misspelled_text((word, locale))
if role == Qt.ItemDataRole.TextAlignmentRole: if role == Qt.ItemDataRole.TextAlignmentRole:
return Qt.AlignmentFlag.AlignVCenter | (Qt.AlignmentFlag.AlignLeft if index.column() == 0 else Qt.AlignmentFlag.AlignHCenter) return Qt.AlignmentFlag.AlignVCenter | self.alignments[index.column()]
def sort(self, column, order=Qt.SortOrder.AscendingOrder): def sort(self, column, order=Qt.SortOrder.AscendingOrder):
reverse = order != Qt.SortOrder.AscendingOrder reverse = order != Qt.SortOrder.AscendingOrder