mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Enhancement #2062434: Add option to show/edit current column definition to the booklist header context menu
This commit is contained in:
parent
589d926507
commit
04e50fa395
@ -42,7 +42,8 @@ from qt.core import (
|
|||||||
|
|
||||||
from calibre import force_unicode
|
from calibre import force_unicode
|
||||||
from calibre.constants import filesystem_encoding, islinux
|
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.dialogs.enum_values_edit import EnumValuesEdit
|
||||||
from calibre.gui2.gestures import GestureManager
|
from calibre.gui2.gestures import GestureManager
|
||||||
from calibre.gui2.library import DEFAULT_SORT
|
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.library.models import BooksModel, DeviceBooksModel
|
||||||
from calibre.gui2.pin_columns import PinTableView
|
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.config import prefs, tweaks
|
||||||
from calibre.utils.icu import primary_sort_key
|
from calibre.utils.icu import primary_sort_key
|
||||||
from polyglot.builtins import iteritems
|
from polyglot.builtins import iteritems
|
||||||
@ -567,6 +569,21 @@ class BooksView(QTableView): # {{{
|
|||||||
view.apply_state(view.get_default_state())
|
view.apply_state(view.get_default_state())
|
||||||
elif action == 'addcustcol':
|
elif action == 'addcustcol':
|
||||||
self.add_column_signal.emit()
|
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_'):
|
elif action.startswith('align_'):
|
||||||
alignment = action.partition('_')[-1]
|
alignment = action.partition('_')[-1]
|
||||||
self._model.change_alignment(column, alignment)
|
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),
|
ans.addAction(QIcon.ic('width.png'), _('Adjust width of column {0}').format(name),
|
||||||
partial(self.manually_adjust_column_size, view, col, 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.is_library_view:
|
||||||
if self._model.db.field_metadata[col]['is_category']:
|
if self._model.db.field_metadata[col]['is_category']:
|
||||||
act = ans.addAction(QIcon.ic('quickview.png'), _('Quickview column %s') % name,
|
act = ans.addAction(QIcon.ic('quickview.png'), _('Quickview column %s') % name,
|
||||||
@ -664,8 +691,10 @@ class BooksView(QTableView): # {{{
|
|||||||
partial(handler, action='reset_ondevice_width'))
|
partial(handler, action='reset_ondevice_width'))
|
||||||
ans.addAction(_('Restore default layout'), partial(handler, action='defaults'))
|
ans.addAction(_('Restore default layout'), partial(handler, action='defaults'))
|
||||||
if self.can_add_columns:
|
if self.can_add_columns:
|
||||||
ans.addAction(
|
act = ans.addAction(QIcon.ic('column.png'), _('Add your own columns'),
|
||||||
QIcon.ic('column.png'), _('Add your own columns'), partial(handler, action='addcustcol'))
|
partial(handler, action='addcustcol'))
|
||||||
|
col_manager = CreateNewCustomColumn(self.gui)
|
||||||
|
act.setEnabled(not col_manager.must_restart())
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def show_row_header_context_menu(self, pos):
|
def show_row_header_context_menu(self, pos):
|
||||||
|
@ -106,10 +106,11 @@ class CreateCustomColumn(QDialog):
|
|||||||
)))
|
)))
|
||||||
column_types_map = {k['datatype']:idx for idx, k in iteritems(column_types)}
|
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)
|
QDialog.__init__(self, gui)
|
||||||
self.orig_column_number = -1
|
self.orig_column_number = -1
|
||||||
self.gui = gui
|
self.gui = gui
|
||||||
|
self.view_only = view_only
|
||||||
self.setup_ui()
|
self.setup_ui()
|
||||||
self.setWindowTitle(_('Create a custom column'))
|
self.setWindowTitle(_('Create a custom column'))
|
||||||
self.heading_label.setText('<b>' + _('Create a custom column'))
|
self.heading_label.setText('<b>' + _('Create a custom column'))
|
||||||
@ -138,8 +139,12 @@ class CreateCustomColumn(QDialog):
|
|||||||
self.exec()
|
self.exec()
|
||||||
return
|
return
|
||||||
|
|
||||||
self.setWindowTitle(_('Edit custom column'))
|
if view_only:
|
||||||
self.heading_label.setText('<b>' + _('Edit custom column'))
|
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 definition'))
|
||||||
self.shortcuts.setVisible(False)
|
self.shortcuts.setVisible(False)
|
||||||
col = current_key
|
col = current_key
|
||||||
if col not in caller.custcols:
|
if col not in caller.custcols:
|
||||||
@ -292,7 +297,11 @@ class CreateCustomColumn(QDialog):
|
|||||||
self.g = g = QGridLayout()
|
self.g = g = QGridLayout()
|
||||||
l.addLayout(g)
|
l.addLayout(g)
|
||||||
l.addStretch(10)
|
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)
|
bb.accepted.connect(self.accept), bb.rejected.connect(self.reject)
|
||||||
l.addWidget(bb)
|
l.addWidget(bb)
|
||||||
|
|
||||||
@ -932,6 +941,7 @@ class CreateNewCustomColumn:
|
|||||||
INVALID_DISPLAY = 7
|
INVALID_DISPLAY = 7
|
||||||
EXCEPTION_RAISED = 8
|
EXCEPTION_RAISED = 8
|
||||||
MUST_RESTART = 9
|
MUST_RESTART = 9
|
||||||
|
COLUMN_EDITED = 11
|
||||||
|
|
||||||
def __init__(self, gui):
|
def __init__(self, gui):
|
||||||
self.gui = gui
|
self.gui = gui
|
||||||
@ -991,20 +1001,46 @@ class CreateNewCustomColumn:
|
|||||||
'colnum': self.created_count,
|
'colnum': self.created_count,
|
||||||
'is_multiple': is_multiple,
|
'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:
|
try:
|
||||||
dialog = CreateCustomColumn(self.gui, self, lookup_name,
|
dialog = CreateCustomColumn(self.gui, self, lookup_name,
|
||||||
self.gui.library_view.model().orig_headers,
|
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:
|
if dialog.result() == QDialog.DialogCode.Accepted and self.cc_column_key is not None:
|
||||||
cc = self.custcols[lookup_name]
|
cc = self.custcols[lookup_name]
|
||||||
self.db.create_custom_column(
|
if operation == 'create':
|
||||||
label=cc['label'],
|
self.db.create_custom_column(
|
||||||
name=cc['name'],
|
label=cc['label'],
|
||||||
datatype=cc['datatype'],
|
name=cc['name'],
|
||||||
is_multiple=cc['is_multiple'],
|
datatype=cc['datatype'],
|
||||||
display=cc['display'])
|
is_multiple=bool(cc['is_multiple']),
|
||||||
self.gui.must_restart_before_config = True
|
display=cc['display'])
|
||||||
return (self.Result.COLUMN_ADDED, self.cc_column_key)
|
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:
|
except Exception as e:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
@ -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):
|
class Preferences(QDialog):
|
||||||
|
|
||||||
run_wizard_requested = pyqtSignal()
|
run_wizard_requested = pyqtSignal()
|
||||||
@ -394,13 +400,11 @@ class Preferences(QDialog):
|
|||||||
do_restart = False
|
do_restart = False
|
||||||
if must_restart:
|
if must_restart:
|
||||||
self.must_restart = True
|
self.must_restart = True
|
||||||
msg = _('Some of the changes you made require a restart.'
|
|
||||||
' Please restart calibre as soon as possible.')
|
|
||||||
if rc:
|
if rc:
|
||||||
msg = _('The changes you have made require calibre be '
|
msg = must_restart_message
|
||||||
'restarted immediately. You will not be allowed to '
|
else:
|
||||||
'set any more preferences, until you restart.')
|
msg = _('Some of the changes you made require a restart.'
|
||||||
|
' Please restart calibre as soon as possible.')
|
||||||
do_restart = show_restart_warning(msg, parent=self)
|
do_restart = show_restart_warning(msg, parent=self)
|
||||||
|
|
||||||
self.showing_widget.refresh_gui(self.gui)
|
self.showing_widget.refresh_gui(self.gui)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user