mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #7702 (Option to populate author from selected row when adding empty books)
This commit is contained in:
parent
b25ba3ab43
commit
2e7967bd6e
@ -8,11 +8,12 @@ __docformat__ = 'restructuredtext en'
|
|||||||
import os
|
import os
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from PyQt4.Qt import QInputDialog, QPixmap, QMenu
|
from PyQt4.Qt import QPixmap, QMenu
|
||||||
|
|
||||||
|
|
||||||
from calibre.gui2 import error_dialog, choose_files, \
|
from calibre.gui2 import error_dialog, choose_files, \
|
||||||
choose_dir, warning_dialog, info_dialog
|
choose_dir, warning_dialog, info_dialog
|
||||||
|
from calibre.gui2.dialogs.add_empty_book import AddEmptyBookDialog
|
||||||
from calibre.gui2.widgets import IMAGE_EXTENSIONS
|
from calibre.gui2.widgets import IMAGE_EXTENSIONS
|
||||||
from calibre.ebooks import BOOK_EXTENSIONS
|
from calibre.ebooks import BOOK_EXTENSIONS
|
||||||
from calibre.utils.filenames import ascii_filename
|
from calibre.utils.filenames import ascii_filename
|
||||||
@ -42,7 +43,7 @@ class AddAction(InterfaceAction):
|
|||||||
'ebook file is a different book)'), self.add_recursive_multiple)
|
'ebook file is a different book)'), self.add_recursive_multiple)
|
||||||
self.add_menu.addSeparator()
|
self.add_menu.addSeparator()
|
||||||
self.add_menu.addAction(_('Add Empty book. (Book entry with no '
|
self.add_menu.addAction(_('Add Empty book. (Book entry with no '
|
||||||
'formats)'), self.add_empty)
|
'formats)'), self.add_empty, _('Shift+Ctrl+E'))
|
||||||
self.add_menu.addAction(_('Add from ISBN'), self.add_from_isbn)
|
self.add_menu.addAction(_('Add from ISBN'), self.add_from_isbn)
|
||||||
self.qaction.setMenu(self.add_menu)
|
self.qaction.setMenu(self.add_menu)
|
||||||
self.qaction.triggered.connect(self.add_books)
|
self.qaction.triggered.connect(self.add_books)
|
||||||
@ -83,12 +84,21 @@ class AddAction(InterfaceAction):
|
|||||||
Add an empty book item to the library. This does not import any formats
|
Add an empty book item to the library. This does not import any formats
|
||||||
from a book file.
|
from a book file.
|
||||||
'''
|
'''
|
||||||
num, ok = QInputDialog.getInt(self.gui, _('How many empty books?'),
|
author = None
|
||||||
_('How many empty books should be added?'), 1, 1, 100)
|
index = self.gui.library_view.currentIndex()
|
||||||
if ok:
|
if index.isValid():
|
||||||
|
raw = index.model().db.authors(index.row())
|
||||||
|
if raw:
|
||||||
|
authors = [a.strip().replace('|', ',') for a in raw.split(',')]
|
||||||
|
if authors:
|
||||||
|
author = authors[0]
|
||||||
|
dlg = AddEmptyBookDialog(self.gui, self.gui.library_view.model().db, author)
|
||||||
|
if dlg.exec_() == dlg.Accepted:
|
||||||
|
num = dlg.qty_to_add
|
||||||
from calibre.ebooks.metadata import MetaInformation
|
from calibre.ebooks.metadata import MetaInformation
|
||||||
for x in xrange(num):
|
for x in xrange(num):
|
||||||
self.gui.library_view.model().db.import_book(MetaInformation(None), [])
|
mi = MetaInformation(_('Unknown'), dlg.selected_authors)
|
||||||
|
self.gui.library_view.model().db.import_book(mi, [])
|
||||||
self.gui.library_view.model().books_added(num)
|
self.gui.library_view.model().books_added(num)
|
||||||
|
|
||||||
def add_isbns(self, books, add_tags=[]):
|
def add_isbns(self, books, add_tags=[]):
|
||||||
|
85
src/calibre/gui2/dialogs/add_empty_book.py
Normal file
85
src/calibre/gui2/dialogs/add_empty_book.py
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
|
||||||
|
__docformat__ = 'restructuredtext en'
|
||||||
|
__license__ = 'GPL v3'
|
||||||
|
|
||||||
|
|
||||||
|
from PyQt4.Qt import QDialog, QGridLayout, QLabel, QDialogButtonBox, \
|
||||||
|
QApplication, QSpinBox, QToolButton, QIcon
|
||||||
|
from calibre.ebooks.metadata import authors_to_string, string_to_authors
|
||||||
|
from calibre.gui2.widgets import CompleteComboBox
|
||||||
|
from calibre.utils.icu import sort_key
|
||||||
|
|
||||||
|
class AddEmptyBookDialog(QDialog):
|
||||||
|
|
||||||
|
def __init__(self, parent, db, author):
|
||||||
|
QDialog.__init__(self, parent)
|
||||||
|
self.db = db
|
||||||
|
|
||||||
|
self.setWindowTitle(_('How many empty books?'))
|
||||||
|
|
||||||
|
self._layout = QGridLayout(self)
|
||||||
|
self.setLayout(self._layout)
|
||||||
|
|
||||||
|
self.qty_label = QLabel(_('How many empty books should be added?'))
|
||||||
|
self._layout.addWidget(self.qty_label, 0, 0, 1, 2)
|
||||||
|
|
||||||
|
self.qty_spinbox = QSpinBox(self)
|
||||||
|
self.qty_spinbox.setRange(1, 10000)
|
||||||
|
self.qty_spinbox.setValue(1)
|
||||||
|
self._layout.addWidget(self.qty_spinbox, 1, 0, 1, 2)
|
||||||
|
|
||||||
|
self.author_label = QLabel(_('Set the author of the new books to:'))
|
||||||
|
self._layout.addWidget(self.author_label, 2, 0, 1, 2)
|
||||||
|
|
||||||
|
self.authors_combo = CompleteComboBox(self)
|
||||||
|
self.authors_combo.setSizeAdjustPolicy(
|
||||||
|
self.authors_combo.AdjustToMinimumContentsLengthWithIcon)
|
||||||
|
self.authors_combo.setEditable(True)
|
||||||
|
self._layout.addWidget(self.authors_combo, 3, 0, 1, 1)
|
||||||
|
self.initialize_authors(db, author)
|
||||||
|
|
||||||
|
self.clear_button = QToolButton(self)
|
||||||
|
self.clear_button.setIcon(QIcon(I('trash.png')))
|
||||||
|
self.clear_button.setToolTip(_('Reset author to Unknown'))
|
||||||
|
self.clear_button.clicked.connect(self.reset_author)
|
||||||
|
self._layout.addWidget(self.clear_button, 3, 1, 1, 1)
|
||||||
|
|
||||||
|
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
|
||||||
|
button_box.accepted.connect(self.accept)
|
||||||
|
button_box.rejected.connect(self.reject)
|
||||||
|
self._layout.addWidget(button_box)
|
||||||
|
self.resize(self.sizeHint())
|
||||||
|
|
||||||
|
def reset_author(self, *args):
|
||||||
|
self.authors_combo.setEditText(_('Unknown'))
|
||||||
|
|
||||||
|
def initialize_authors(self, db, author):
|
||||||
|
all_authors = db.all_authors()
|
||||||
|
all_authors.sort(key=lambda x : sort_key(x[1]))
|
||||||
|
for i in all_authors:
|
||||||
|
id, name = i
|
||||||
|
name = [name.strip().replace('|', ',') for n in name.split(',')]
|
||||||
|
self.authors_combo.addItem(authors_to_string(name))
|
||||||
|
|
||||||
|
au = author
|
||||||
|
if not au:
|
||||||
|
au = _('Unknown')
|
||||||
|
self.authors_combo.setEditText(au.replace('|', ','))
|
||||||
|
|
||||||
|
self.authors_combo.set_separator('&')
|
||||||
|
self.authors_combo.set_space_before_sep(True)
|
||||||
|
self.authors_combo.update_items_cache(db.all_author_names())
|
||||||
|
|
||||||
|
@property
|
||||||
|
def qty_to_add(self):
|
||||||
|
return self.qty_spinbox.value()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def selected_authors(self):
|
||||||
|
return string_to_authors(unicode(self.authors_combo.text()))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app = QApplication([])
|
||||||
|
d = AddEmptyBookDialog()
|
||||||
|
d.exec_()
|
Loading…
x
Reference in New Issue
Block a user