Enhancement #2062434: Add option to show/edit current column definition to the booklist header context menu

This commit is contained in:
Charles Haley 2024-04-19 15:41:23 +01:00
parent 589d926507
commit 04e50fa395
3 changed files with 91 additions and 22 deletions

View File

@ -42,7 +42,8 @@ from qt.core import (
from calibre import force_unicode
from calibre.constants import filesystem_encoding, islinux
from calibre.gui2 import BOOK_DETAILS_DISPLAY_DEBOUNCE_DELAY, FunctionDispatcher, error_dialog, gprefs
from calibre.gui2 import (BOOK_DETAILS_DISPLAY_DEBOUNCE_DELAY, FunctionDispatcher,
error_dialog, gprefs, show_restart_warning)
from calibre.gui2.dialogs.enum_values_edit import EnumValuesEdit
from calibre.gui2.gestures import GestureManager
from calibre.gui2.library import DEFAULT_SORT
@ -68,6 +69,7 @@ from calibre.gui2.library.delegates import (
)
from calibre.gui2.library.models import BooksModel, DeviceBooksModel
from calibre.gui2.pin_columns import PinTableView
from calibre.gui2.preferences.create_custom_column import CreateNewCustomColumn
from calibre.utils.config import prefs, tweaks
from calibre.utils.icu import primary_sort_key
from polyglot.builtins import iteritems
@ -567,6 +569,21 @@ class BooksView(QTableView): # {{{
view.apply_state(view.get_default_state())
elif action == 'addcustcol':
self.add_column_signal.emit()
elif action == 'viewcustcol':
col_manager = CreateNewCustomColumn(self.gui)
col_manager.view_existing_column(column)
elif action == 'editcustcol':
def show_restart_dialog():
from calibre.gui2.preferences.main import must_restart_message
if show_restart_warning(must_restart_message):
self.gui.quit(restart=True)
col_manager = CreateNewCustomColumn(self.gui)
if col_manager.must_restart():
show_restart_dialog()
else:
res = col_manager.edit_existing_column(column)
if res[0] == CreateNewCustomColumn.Result.COLUMN_EDITED:
show_restart_dialog()
elif action.startswith('align_'):
alignment = action.partition('_')[-1]
self._model.change_alignment(column, alignment)
@ -631,6 +648,16 @@ class BooksView(QTableView): # {{{
ans.addAction(QIcon.ic('width.png'), _('Adjust width of column {0}').format(name),
partial(self.manually_adjust_column_size, view, col, name))
if not isinstance(view, DeviceBooksView):
col_manager = CreateNewCustomColumn(self.gui)
if self.can_add_columns and self.model().is_custom_column(col):
actv = ans.addAction(QIcon.ic('view-image.png'), _('View column definition for %s') % name,
partial(handler, action='viewcustcol'))
acte = ans.addAction(QIcon.ic('edit_input.png'), _('Edit column definition for %s') % name,
partial(handler, action='editcustcol'))
if col_manager.must_restart():
actv.setEnabled(False)
acte.setEnabled(False)
if self.is_library_view:
if self._model.db.field_metadata[col]['is_category']:
act = ans.addAction(QIcon.ic('quickview.png'), _('Quickview column %s') % name,
@ -664,8 +691,10 @@ class BooksView(QTableView): # {{{
partial(handler, action='reset_ondevice_width'))
ans.addAction(_('Restore default layout'), partial(handler, action='defaults'))
if self.can_add_columns:
ans.addAction(
QIcon.ic('column.png'), _('Add your own columns'), partial(handler, action='addcustcol'))
act = ans.addAction(QIcon.ic('column.png'), _('Add your own columns'),
partial(handler, action='addcustcol'))
col_manager = CreateNewCustomColumn(self.gui)
act.setEnabled(not col_manager.must_restart())
return ans
def show_row_header_context_menu(self, pos):

View File

@ -106,10 +106,11 @@ class CreateCustomColumn(QDialog):
)))
column_types_map = {k['datatype']:idx for idx, k in iteritems(column_types)}
def __init__(self, gui, caller, current_key, standard_colheads, freeze_lookup_name=False):
def __init__(self, gui, caller, current_key, standard_colheads, freeze_lookup_name=False, view_only=False):
QDialog.__init__(self, gui)
self.orig_column_number = -1
self.gui = gui
self.view_only = view_only
self.setup_ui()
self.setWindowTitle(_('Create a custom column'))
self.heading_label.setText('<b>' + _('Create a custom column'))
@ -138,8 +139,12 @@ class CreateCustomColumn(QDialog):
self.exec()
return
if view_only:
self.setWindowTitle(_('View custom column'))
self.heading_label.setText('<b>' + _('View custom column definition. Changes will be ignored'))
else:
self.setWindowTitle(_('Edit custom column'))
self.heading_label.setText('<b>' + _('Edit custom column'))
self.heading_label.setText('<b>' + _('Edit custom column definition'))
self.shortcuts.setVisible(False)
col = current_key
if col not in caller.custcols:
@ -292,7 +297,11 @@ class CreateCustomColumn(QDialog):
self.g = g = QGridLayout()
l.addLayout(g)
l.addStretch(10)
self.button_box = bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel, self)
if self.view_only:
self.button_box = bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Close, self)
else:
self.button_box = bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok |
QDialogButtonBox.StandardButton.Cancel, self)
bb.accepted.connect(self.accept), bb.rejected.connect(self.reject)
l.addWidget(bb)
@ -932,6 +941,7 @@ class CreateNewCustomColumn:
INVALID_DISPLAY = 7
EXCEPTION_RAISED = 8
MUST_RESTART = 9
COLUMN_EDITED = 11
def __init__(self, gui):
self.gui = gui
@ -991,20 +1001,46 @@ class CreateNewCustomColumn:
'colnum': self.created_count,
'is_multiple': is_multiple,
}
return self._create_or_edit_column(lookup_name, freeze_lookup_name=freeze_lookup_name,
operation='create')
def edit_existing_column(self, lookup_name):
if lookup_name not in self.custcols:
return self.Result.INVALID_KEY
return self._create_or_edit_column(lookup_name, freeze_lookup_name=False, operation='edit')
def view_existing_column(self, lookup_name):
if lookup_name not in self.custcols:
return self.Result.INVALID_KEY
return self._create_or_edit_column(lookup_name, freeze_lookup_name=True, operation='view')
def _create_or_edit_column(self, lookup_name, freeze_lookup_name, operation=None):
try:
dialog = CreateCustomColumn(self.gui, self, lookup_name,
self.gui.library_view.model().orig_headers,
freeze_lookup_name=freeze_lookup_name)
freeze_lookup_name=freeze_lookup_name,
view_only=operation=='view')
if dialog.result() == QDialog.DialogCode.Accepted and self.cc_column_key is not None:
cc = self.custcols[lookup_name]
if operation == 'create':
self.db.create_custom_column(
label=cc['label'],
name=cc['name'],
datatype=cc['datatype'],
is_multiple=cc['is_multiple'],
is_multiple=bool(cc['is_multiple']),
display=cc['display'])
self.gui.must_restart_before_config = True
return (self.Result.COLUMN_ADDED, self.cc_column_key)
# editing/viewing
if operation == 'edit':
self.db.set_custom_column_metadata(cc['colnum'], name=cc['name'],
label=cc['label'], display=cc['display'],
notify=False)
if '*must_restart' in cc:
self.gui.must_restart_before_config = True
return (self.Result.COLUMN_EDITED, self.cc_column_key)
return (self.Result.CANCELED, self.cc_column_key)
except Exception as e:
import traceback
traceback.print_exc()

View File

@ -218,6 +218,12 @@ class Browser(QScrollArea): # {{{
# }}}
must_restart_message = _('The changes you have made require calibre be '
'restarted immediately. You will not be allowed to '
'set any more preferences, until you restart.')
class Preferences(QDialog):
run_wizard_requested = pyqtSignal()
@ -394,13 +400,11 @@ class Preferences(QDialog):
do_restart = False
if must_restart:
self.must_restart = True
if rc:
msg = must_restart_message
else:
msg = _('Some of the changes you made require a restart.'
' Please restart calibre as soon as possible.')
if rc:
msg = _('The changes you have made require calibre be '
'restarted immediately. You will not be allowed to '
'set any more preferences, until you restart.')
do_restart = show_restart_warning(msg, parent=self)
self.showing_widget.refresh_gui(self.gui)