Generate CSV catalog: Add a button to sort the fields by the order in which they appear in the book list. Fixes #2068070 [[enhancement] allow catalogs to be automatically set to columns in booklist order](https://bugs.launchpad.net/calibre/+bug/2068070)

This commit is contained in:
Kovid Goyal 2024-06-26 19:38:56 +05:30
parent 26e67c96ed
commit 218ca903d6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -5,6 +5,8 @@ __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import sys
from qt.core import QAbstractItemView, QHBoxLayout, QLabel, QListWidget, QListWidgetItem, QPushButton, Qt, QVBoxLayout, QWidget from qt.core import QAbstractItemView, QHBoxLayout, QLabel, QListWidget, QListWidgetItem, QPushButton, Qt, QVBoxLayout, QWidget
from calibre.constants import ismacos from calibre.constants import ismacos
@ -31,6 +33,20 @@ def set_saved_field_data(name, fields, sort_order):
gprefs.set(name + '_db_fields_sort_order', sort_order) gprefs.set(name + '_db_fields_sort_order', sort_order)
class ListWidgetItem(QListWidgetItem):
def __init__(self, colname, human_name, position_in_booklist, parent):
super().__init__(human_name, parent)
self.setData(Qt.ItemDataRole.UserRole, colname)
self.position_in_booklist = position_in_booklist
def __lt__(self, other):
try:
return self.position_in_booklist < getattr(other, 'position_in_booklist', sys.maxsize)
except TypeError:
return False
class PluginWidget(QWidget): class PluginWidget(QWidget):
TITLE = _('CSV/XML options') TITLE = _('CSV/XML options')
@ -66,6 +82,10 @@ class PluginWidget(QWidget):
b.clicked.connect(self.select_visible) b.clicked.connect(self.select_visible)
b.setToolTip(_('Select the fields currently shown in the book list')) b.setToolTip(_('Select the fields currently shown in the book list'))
h.addWidget(b) h.addWidget(b)
self.order_button = b = QPushButton(_('&Sort by booklist'))
b.clicked.connect(self.order_by_booklist)
b.setToolTip(_('Sort the fields by their position in the book list'))
h.addWidget(b)
def select_all(self): def select_all(self):
for row in range(self.db_fields.count()): for row in range(self.db_fields.count()):
@ -85,6 +105,9 @@ class PluginWidget(QWidget):
field = item.data(Qt.ItemDataRole.UserRole) field = item.data(Qt.ItemDataRole.UserRole)
item.setCheckState(Qt.CheckState.Unchecked if field in hidden else Qt.CheckState.Checked) item.setCheckState(Qt.CheckState.Unchecked if field in hidden else Qt.CheckState.Checked)
def order_by_booklist(self):
self.db_fields.sortItems()
def initialize(self, catalog_name, db): def initialize(self, catalog_name, db):
self.name = catalog_name self.name = catalog_name
from calibre.library.catalogs import FIELDS from calibre.library.catalogs import FIELDS
@ -102,15 +125,18 @@ class PluginWidget(QWidget):
return name(x[:-len('_index')]) + ' ' + _('Number') return name(x[:-len('_index')]) + ' ' + _('Number')
return fm[x].get('name') or x return fm[x].get('name') or x
state = get_gui().library_view.get_state()
cpos = state['column_positions']
def key(x): def key(x):
return (sort_order.get(x, 10000), name(x)) return (sort_order.get(x, 10000), name(x))
self.db_fields.clear() self.db_fields.clear()
for x in sorted(self.all_fields, key=key): for x in sorted(self.all_fields, key=key):
QListWidgetItem(name(x) + ' (%s)' % x, self.db_fields).setData(Qt.ItemDataRole.UserRole, x) i = ListWidgetItem(x, name(x) + ' (%s)' % x, cpos.get(x, sys.maxsize), self.db_fields)
if x.startswith('#') and fm[x]['datatype'] == 'series': if x.startswith('#') and fm[x]['datatype'] == 'series':
x += '_index' x += '_index'
QListWidgetItem(name(x) + ' (%s)' % x, self.db_fields).setData(Qt.ItemDataRole.UserRole, x) ListWidgetItem(x, name(x) + ' (%s)' % x, i.position_in_booklist, self.db_fields)
# Restore the activated fields from last use # Restore the activated fields from last use
for x in range(self.db_fields.count()): for x in range(self.db_fields.count()):