def search_epub_content(self): ''' Search epub content ''' # reset results layout self.search_results.clear() # get search input keyword = self.search_input.text() # search in each book db = self.db.new_api matched_ids = {book_id for book_id in db.all_book_ids() if 'EPUB' in db.formats(book_id)} for book_id in matched_ids: mi = db.get_metadata(book_id, get_cover=False, cover_as_data=False) if prefs['tags'] in mi.tags: title = mi.title filepath = fmt_path = self.gui.current_db.format_abspath(book_id, 'EPUB', index_is_id=True) grep = EpubGrep(keyword) grep.setPreview(True) grep_result = grep.searchin(filepath) #print(grep_result) if grep_result != None and len(grep_result) != 0: widgetLayout = QVBoxLayout() book_button = QPushButton(title, self) book_button.clicked.connect(partial(self.view, book_id, 0)) widgetLayout.addWidget(book_button) widgetLayout.setSizeConstraint(QLayout.SetFixedSize) widget = QWidget() widget.setLayout(widgetLayout) widgetItem = QListWidgetItem() widgetItem.setSizeHint(widget.sizeHint()) self.search_results.addItem(widgetItem) self.search_results.setItemWidget(widgetItem, widget) #matched = result.stdout.decode('utf-8') matched = grep_result search_result_count = 0 if prefs['search_result_count'] == '' else int(prefs['search_result_count']) count = 0 for lines in matched.split('--\n'): # only handles results within limit if search_result_count != 0 and count >= search_result_count: break widgetLayout = QVBoxLayout() qLabel = QLabel(lines.replace(keyword, '<font color=yellow>' + keyword + '</font>').replace('\n\n', '\n').replace('\n','<br/>') + '<br/>') qLabel.setTextFormat(Qt.RichText) qLabel.setStyleSheet(''' font-size: 24px; ''') widgetLayout.addWidget(qLabel) widgetLayout.setSizeConstraint(QLayout.SetFixedSize) widget = QWidget() widget.setLayout(widgetLayout) widgetItem = QListWidgetItem() widgetItem.setSizeHint(widget.sizeHint()) widgetItem.setData(Qt.ItemDataRole.UserRole, (book_id, count)) self.search_results.addItem(widgetItem) self.search_results.setItemWidget(widgetItem, widget) count += 1 self.search_results.itemClicked.connect(self.view_search_result)
def test(scale=0.25): from PyQt5.Qt import QLabel, QPixmap, QMainWindow, QWidget, QScrollArea, QGridLayout from calibre.gui2 import Application app = Application([]) mi = Metadata('Unknown', ['Kovid Goyal', 'John & Doe', 'Author']) mi.series = 'A series & styles' m = QMainWindow() sa = QScrollArea(m) w = QWidget(m) sa.setWidget(w) l = QGridLayout(w) w.setLayout(l), l.setSpacing(30) scale *= w.devicePixelRatioF() labels = [] for r, color in enumerate(sorted(default_color_themes)): for c, style in enumerate(sorted(all_styles())): mi.series_index = c + 1 mi.title = 'An algorithmic cover [%s]' % color prefs = override_prefs(cprefs, override_color_theme=color, override_style=style) scale_cover(prefs, scale) img = generate_cover(mi, prefs=prefs, as_qimage=True) img.setDevicePixelRatio(w.devicePixelRatioF()) la = QLabel() la.setPixmap(QPixmap.fromImage(img)) l.addWidget(la, r, c) labels.append(la) m.setCentralWidget(sa) w.resize(w.sizeHint()) m.show() app.exec_()
def test(scale=0.25): from PyQt5.Qt import QLabel, QApplication, QPixmap, QMainWindow, QWidget, QScrollArea, QGridLayout app = QApplication([]) mi = Metadata('xxx', ['Kovid Goyal', 'John & Doe', 'Author']) mi.series = 'A series of styles' m = QMainWindow() sa = QScrollArea(m) w = QWidget(m) sa.setWidget(w) l = QGridLayout(w) w.setLayout(l), l.setSpacing(30) labels = [] for r, color in enumerate(sorted(default_color_themes)): for c, style in enumerate(sorted(all_styles())): mi.series_index = c + 1 mi.title = 'An algorithmic cover [%s]' % color prefs = override_prefs(cprefs, override_color_theme=color, override_style=style) scale_cover(prefs, scale) img = generate_cover(mi, prefs=prefs, as_qimage=True) la = QLabel() la.setPixmap(QPixmap.fromImage(img)) l.addWidget(la, r, c) labels.append(la) m.setCentralWidget(sa) w.resize(w.sizeHint()) m.show() app.exec_()
def test(scale=0.5): from PyQt5.Qt import QLabel, QApplication, QPixmap, QMainWindow, QWidget, QScrollArea, QGridLayout app = QApplication([]) mi = Metadata('xxx', ['Kovid Goyal', 'John Q. Doe', 'Author']) mi.series = 'A series of styles' m = QMainWindow() sa = QScrollArea(m) w = QWidget(m) sa.setWidget(w) l = QGridLayout(w) w.setLayout(l), l.setSpacing(30) labels = [] for r, color in enumerate(sorted(default_color_themes)): for c, style in enumerate(sorted(all_styles())): mi.series_index = c + 1 mi.title = 'An algorithmic cover [%s]' % color prefs = override_prefs(cprefs, override_color_theme=color, override_style=style) for x in ('cover_width', 'cover_height', 'title_font_size', 'subtitle_font_size', 'footer_font_size'): prefs[x] = int(scale * prefs[x]) img = generate_cover(mi, prefs=prefs, as_qimage=True) la = QLabel() la.setPixmap(QPixmap.fromImage(img)) l.addWidget(la, r, c) labels.append(la) m.setCentralWidget(sa) w.resize(w.sizeHint()) m.show() app.exec_()
def sizeHint(self): a = QWidget.sizeHint(self) a.setHeight(a.height() + 75) a.setWidth(a.width() + 100) return a
def sizeHint(self): a = QWidget.sizeHint(self) a.setHeight(a.height() + 75) a.setWidth(a.width() + 100) return a
def sizeHint(self): ans = QWidget.sizeHint(self) ans.setWidth(400) return ans
def sizeHint(self): return QWidget.sizeHint(self)
def sizeHint(self): ans = QWidget.sizeHint(self) if not self.has_widget: ans = default_size_hint(ans) return ans
class DemoDialog(QDialog): def __init__(self, gui, icon, do_user_config): QDialog.__init__(self, gui) self.gui = gui self.do_user_config = do_user_config self.db = gui.current_db self.l = QVBoxLayout() self.setLayout(self.l) self.setWindowTitle('Wiki Reader') self.setWindowIcon(icon) self.helpl = QLabel( 'Enter the URL of a wikipedia article below. ' 'It will be downloaded and converted into an ebook.') self.helpl.setWordWrap(True) self.l.addWidget(self.helpl) self.w = QWidget(self) self.sa = QScrollArea(self) self.l.addWidget(self.sa) self.w.l = QVBoxLayout() self.w.setLayout(self.w.l) self.sa.setWidget(self.w) self.sa.setWidgetResizable(True) self.title = Title(self) self.w.l.addWidget(self.title) self.urls = [URL(self)] self.w.l.addWidget(self.urls[0]) self.add_more_button = QPushButton( QIcon(I('plus.png')), 'Add another URL') self.l.addWidget(self.add_more_button) self.bb = QDialogButtonBox(self) self.bb.setStandardButtons(self.bb.Ok | self.bb.Cancel) self.bb.accepted.connect(self.accept) self.bb.rejected.connect(self.reject) self.l.addWidget(self.bb) self.book_button = b = self.bb.addButton( 'Convert a Wiki&Book', self.bb.ActionRole) b.clicked.connect(self.wiki_book) self.add_more_button.clicked.connect(self.add_more) self.finished.connect(self.download) self.setMinimumWidth(500) self.resize(self.sizeHint()) self.single_url = None def wiki_book(self): d = QDialog(self) d.l = l = QFormLayout(d) l.setFieldGrowthPolicy(l.ExpandingFieldsGrow) d.setWindowTitle('Enter WikiBook URL') d.la = la = QLabel( '<p>You can convert a pre-made WikiBook into a book here. ' 'Simply enter the URL to the WikiBook page. For a list of ' 'WikiBooks, see ' '<a href="https://en.wikipedia.org/wiki/Special:PrefixIndex/Book:' '">here</a>.' ) la.setMinimumWidth(400) la.setWordWrap(True) la.setOpenExternalLinks(True) l.addRow(la) d.le = le = QLineEdit(self) l.addRow('WikiBook &URL:', le) le.setText('https://') le.selectAll() d.bb = bb = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, d) l.addRow(bb) bb.accepted.connect(d.accept), bb.rejected.connect(d.reject) if d.exec_() == d.Accepted: self.single_url = le.text() self.accept() def do_resize(self): a = self.sizeHint() b = self.w.sizeHint() h = min(400, b.height()) self.resize(QSize(a.width(), h + 200)) def scroll_to_bottom(self): v = self.sa.verticalScrollBar() v.setValue(v.maximum()) def add_more(self): url = URL(self) self.urls.append(url) self.w.l.addWidget(url) QTimer.singleShot(0, self.do_resize) QTimer.singleShot(10, self.scroll_to_bottom) def about(self): # Get the about text from a file inside the plugin zip file # The get_resources function is a builtin function defined for all your # plugin code. It loads files from the plugin zip file. It returns # the bytes from the specified file. # # Note that if you are loading more than one file, for performance, you # should pass a list of names to get_resources. In this case, # get_resources will return a dictionary mapping names to bytes. Names # that are not found in the zip file will not be in the returned # dictionary. text = get_resources('about.txt') QMessageBox.about(self, 'About the Wiki Reader', text.decode('utf-8')) # def config(self): # self.do_user_config(parent=self) # Apply the changes # self.label.setText(prefs['hello_world_msg']) def download(self, retcode): if retcode != self.Accepted: return if self.single_url is None: urls = [x.url for x in self.urls] urls = [x.strip() for x in urls if x.strip()] urls = [ ('http://' + x) if not urlparse(x).scheme else x for x in urls] else: urls = self.single_url self.single_url = None args, fmt, temp_files = get_recipe(urls, self.title.title) job = self.gui.job_manager.run_job( Dispatcher(self.fetched), 'gui_convert', args=args, description='Fetch article from Wikipedia') job.extra_conversion_args = (temp_files, fmt) # don't prompt if all OK # if isinstance(urls, list): # info_dialog( # self, 'Downloading', # 'Downloading %d article(s) from Wikipedia. When the download' # ' completes the book will be added to your calibre library.' # % len(urls), show=True, show_copy_button=False) # else: # info_dialog( # self, 'Downloading', # 'Downloading book from Wikipedia. When the download' # ' completes the book will be added to your calibre library.', # show=True, show_copy_button=False) def fetched(self, job): if job.failed: return self.gui.job_exception(job) temp_files, fmt = job.extra_conversion_args fname = temp_files[0].name self.gui.iactions['Add Books']._add_books([fname], False) for f in temp_files[1:]: try: os.remove(f.name) except: pass
def sizeHint(self): return QWidget.sizeHint(self)
def sizeHint(self): ans = QWidget.sizeHint(self) ans.setWidth(400) return ans