mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
26e67c96ed
commit
218ca903d6
@ -5,6 +5,8 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import sys
|
||||
|
||||
from qt.core import QAbstractItemView, QHBoxLayout, QLabel, QListWidget, QListWidgetItem, QPushButton, Qt, QVBoxLayout, QWidget
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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):
|
||||
|
||||
TITLE = _('CSV/XML options')
|
||||
@ -66,6 +82,10 @@ class PluginWidget(QWidget):
|
||||
b.clicked.connect(self.select_visible)
|
||||
b.setToolTip(_('Select the fields currently shown in the book list'))
|
||||
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):
|
||||
for row in range(self.db_fields.count()):
|
||||
@ -85,6 +105,9 @@ class PluginWidget(QWidget):
|
||||
field = item.data(Qt.ItemDataRole.UserRole)
|
||||
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):
|
||||
self.name = catalog_name
|
||||
from calibre.library.catalogs import FIELDS
|
||||
@ -102,15 +125,18 @@ class PluginWidget(QWidget):
|
||||
return name(x[:-len('_index')]) + ' ' + _('Number')
|
||||
return fm[x].get('name') or x
|
||||
|
||||
state = get_gui().library_view.get_state()
|
||||
cpos = state['column_positions']
|
||||
|
||||
def key(x):
|
||||
return (sort_order.get(x, 10000), name(x))
|
||||
|
||||
self.db_fields.clear()
|
||||
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':
|
||||
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
|
||||
for x in range(self.db_fields.count()):
|
||||
|
Loading…
x
Reference in New Issue
Block a user