From 656587ca6393663c8d652a63df7d8393b4adaac7 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Thu, 25 Aug 2022 23:38:42 +0200 Subject: [PATCH 1/3] epub: Improve TableOfContents for some files The link can be percent encoded so try it like that if not found in the normal way, also if the text overflows the page, it's in the next page BUGS: 458289 --- core/textdocumentgenerator_p.h | 8 +++-- generators/epub/converter.cpp | 61 ++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/core/textdocumentgenerator_p.h b/core/textdocumentgenerator_p.h index 955ea303f..fb1a5e395 100644 --- a/core/textdocumentgenerator_p.h +++ b/core/textdocumentgenerator_p.h @@ -136,8 +136,12 @@ static Okular::DocumentViewport calculateViewport(QTextDocument *document, const const QSizeF pageSize = document->pageSize(); const QRectF rect = document->documentLayout()->blockBoundingRect(block); - const int page = qRound(rect.y()) / qRound(pageSize.height()); - const int offset = qRound(rect.y()) % qRound(pageSize.height()); + int page = qRound(rect.y()) / qRound(pageSize.height()); + int offset = qRound(rect.y()) % qRound(pageSize.height()); + if (rect.y() + rect.height() > pageSize.height()) { + page = page + 1; + offset = 0; + } Okular::DocumentViewport viewport(page); viewport.rePos.normalizedX = (double)rect.x() / (double)pageSize.width(); diff --git a/generators/epub/converter.cpp b/generators/epub/converter.cpp index 78d3b9424..b48c5d850 100644 --- a/generators/epub/converter.cpp +++ b/generators/epub/converter.cpp @@ -380,37 +380,42 @@ QTextDocument *Converter::convert(const QString &fileName) if (mSectionMap.contains(link)) { block = mSectionMap.value(link); - } else { // load missing resource - char *data = nullptr; - // epub_get_data can't handle whitespace url encodings - QByteArray ba = link.replace(QLatin1String("%20"), QLatin1String(" ")).toLatin1(); - const char *clinkClean = ba.data(); - int size = epub_get_data(mTextDocument->getEpub(), clinkClean, &data); - - if (data) { - _cursor->insertBlock(); - - // try to load as image and if not load as html - block = _cursor->block(); - QImage image; - mSectionMap.insert(link, block); - if (image.loadFromData((unsigned char *)data, size)) { - mTextDocument->addResource(QTextDocument::ImageResource, QUrl(link), image); - _cursor->insertImage(link); - } else { - _cursor->insertHtml(QString::fromUtf8(data)); - // Add anchors to hashes - _handle_anchors(block, link); + } else { + const QString percentDecodedLink = QUrl::fromPercentEncoding(link.toUtf8()); + if (mSectionMap.contains(percentDecodedLink)) { + block = mSectionMap.value(percentDecodedLink); + } else { // load missing resource + char *data = nullptr; + // epub_get_data can't handle whitespace url encodings + QByteArray ba = link.replace(QLatin1String("%20"), QLatin1String(" ")).toLatin1(); + const char *clinkClean = ba.data(); + int size = epub_get_data(mTextDocument->getEpub(), clinkClean, &data); + + if (data) { + _cursor->insertBlock(); + + // try to load as image and if not load as html + block = _cursor->block(); + QImage image; + mSectionMap.insert(link, block); + if (image.loadFromData((unsigned char *)data, size)) { + mTextDocument->addResource(QTextDocument::ImageResource, QUrl(link), image); + _cursor->insertImage(link); + } else { + _cursor->insertHtml(QString::fromUtf8(data)); + // Add anchors to hashes + _handle_anchors(block, link); + } + + // Start new file in a new page + int page = mTextDocument->pageCount(); + while (mTextDocument->pageCount() == page) { + _cursor->insertText(QStringLiteral("\n")); + } } - // Start new file in a new page - int page = mTextDocument->pageCount(); - while (mTextDocument->pageCount() == page) { - _cursor->insertText(QStringLiteral("\n")); - } + free(data); } - - free(data); } if (block.isValid()) { // be sure we actually got a block From 01871fdfff9363af3a90959650ef20ab9276c464 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Sun, 21 Aug 2022 19:17:26 +0200 Subject: [PATCH 2/3] Even more tweaks to opening "text" files BUGS: 430538 BUGS: 456434 --- part/part.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/part/part.cpp b/part/part.cpp index 0ac6aca04..0a5aaf336 100644 --- a/part/part.cpp +++ b/part/part.cpp @@ -1578,10 +1578,19 @@ bool Part::openFile() mimes << pathMime << argMime; } + // text is super annoying because it always succeeds when opening so try to make sure + // that we don't set it as first mime unless we're really sure it is that. + // If it could be something else based on the content we try that first but only if that content itself + // is not text or if it's a supported text child like markdown if (mimes[0].inherits(QStringLiteral("text/plain"))) { const QMimeType contentMime = db.mimeTypeForFile(fileNameToOpen, QMimeDatabase::MatchContent); - if (contentMime.name() != QLatin1String("text/plain")) { + if (!contentMime.inherits(QStringLiteral("text/plain"))) { mimes.prepend(contentMime); + } else if (contentMime.name() != QLatin1String("text/plain")) { + const QStringList supportedMimes = m_document->supportedMimeTypes(); + if (supportedMimes.contains(contentMime.name())) { + mimes.prepend(contentMime); + } } } } else { From 3304038d1de0bead5e67275a7a6a9885afe0332d Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Sat, 13 Aug 2022 23:36:04 +0200 Subject: [PATCH 3/3] ToggleActions: Obey "don't show text" for a particular action toolbar setting BUGS: 457322 --- part/toggleactionmenu.cpp | 15 +++++++++++++++ part/toggleactionmenu.h | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/part/toggleactionmenu.cpp b/part/toggleactionmenu.cpp index 0ccff3fb2..453c67b25 100644 --- a/part/toggleactionmenu.cpp +++ b/part/toggleactionmenu.cpp @@ -52,6 +52,7 @@ QWidget *ToggleActionMenu::createWidget(QWidget *parent) // END QToolButton hack m_buttons.append(button); + m_originalToolButtonStyle[button] = button->toolButtonStyle(); // Apply other properties to the button. updateButtons(); @@ -74,11 +75,25 @@ void ToggleActionMenu::setDefaultAction(QAction *action) updateButtons(); } +Qt::ToolButtonStyle ToggleActionMenu::styleFor(QToolButton *button) const +{ + Qt::ToolButtonStyle style = m_originalToolButtonStyle[button]; + + if (style == Qt::ToolButtonTextBesideIcon && priority() < QAction::NormalPriority) { + style = Qt::ToolButtonIconOnly; + } + + return style; +} + void ToggleActionMenu::updateButtons() { for (QToolButton *button : qAsConst(m_buttons)) { if (button) { button->setDefaultAction(this->defaultAction()); + // If *this action* is low priority we need to tell the button + // so that it hides the text + button->setToolButtonStyle(styleFor(button)); if (delayed()) { // TODO deprecated interface. button->setPopupMode(QToolButton::DelayedPopup); diff --git a/part/toggleactionmenu.h b/part/toggleactionmenu.h index 1a7c56263..54eabb976 100644 --- a/part/toggleactionmenu.h +++ b/part/toggleactionmenu.h @@ -88,6 +88,14 @@ protected: QPointer m_defaultAction; QList> m_buttons; + QHash m_originalToolButtonStyle; + + /** + * Returns the aproppriate style for @p button. + * Respects both toolbar settings and settings for this menu action. + */ + Qt::ToolButtonStyle styleFor(QToolButton *button) const; + /** * Updates the toolbar buttons by setting the current defaultAction() on them. *