Changeset View
Changeset View
Standalone View
Standalone View
documentation/standarddocumentationview.cpp
Show All 17 Lines | |||||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "standarddocumentationview.h" | 21 | #include "standarddocumentationview.h" | ||
22 | #include "documentationfindwidget.h" | 22 | #include "documentationfindwidget.h" | ||
23 | #include "debug.h" | 23 | #include "debug.h" | ||
24 | 24 | | |||
25 | #include <QFontDatabase> | 25 | #include <QFontDatabase> | ||
26 | #include <QWebFrame> | ||||
kfunk: Unused include | |||||
26 | 27 | | |||
27 | using namespace KDevelop; | 28 | using namespace KDevelop; | ||
28 | 29 | | |||
29 | StandardDocumentationView::StandardDocumentationView(DocumentationFindWidget* findWidget, QWidget* parent) | 30 | StandardDocumentationView::StandardDocumentationView(DocumentationFindWidget* findWidget, QWidget* parent) | ||
30 | : QWebView (parent) | 31 | : QWebView(parent) | ||
31 | { | 32 | { | ||
32 | findWidget->setEnabled(true); | 33 | findWidget->setEnabled(true); | ||
33 | connect(findWidget, &DocumentationFindWidget::newSearch, this, &StandardDocumentationView::search); | 34 | connect(findWidget, &DocumentationFindWidget::newSearch, this, &StandardDocumentationView::search); | ||
34 | 35 | | |||
35 | QFont sansSerifFont = QFontDatabase::systemFont(QFontDatabase::GeneralFont); | 36 | QFont sansSerifFont = QFontDatabase::systemFont(QFontDatabase::GeneralFont); | ||
36 | QFont monospaceFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); | 37 | QFont monospaceFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); | ||
37 | QFont minimalFont = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont); | 38 | QFont minimalFont = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont); | ||
38 | 39 | | |||
39 | QWebSettings* s = settings(); | 40 | QWebSettings* s = settings(); | ||
40 | 41 | | |||
41 | s->setFontFamily(QWebSettings::StandardFont, sansSerifFont.family()); | 42 | s->setFontFamily(QWebSettings::StandardFont, sansSerifFont.family()); | ||
42 | s->setFontFamily(QWebSettings::SerifFont, "Serif"); | 43 | s->setFontFamily(QWebSettings::SerifFont, "Serif"); | ||
43 | s->setFontFamily(QWebSettings::SansSerifFont, sansSerifFont.family()); | 44 | s->setFontFamily(QWebSettings::SansSerifFont, sansSerifFont.family()); | ||
44 | s->setFontFamily(QWebSettings::FixedFont, monospaceFont.family()); | 45 | s->setFontFamily(QWebSettings::FixedFont, monospaceFont.family()); | ||
45 | 46 | | |||
46 | s->setFontSize(QWebSettings::DefaultFontSize, QFontInfo(sansSerifFont).pixelSize()); | 47 | s->setFontSize(QWebSettings::DefaultFontSize, QFontInfo(sansSerifFont).pixelSize()); | ||
47 | s->setFontSize(QWebSettings::DefaultFixedFontSize, QFontInfo(monospaceFont).pixelSize()); | 48 | s->setFontSize(QWebSettings::DefaultFixedFontSize, QFontInfo(monospaceFont).pixelSize()); | ||
48 | s->setFontSize(QWebSettings::MinimumFontSize, QFontInfo(minimalFont).pixelSize()); | 49 | s->setFontSize(QWebSettings::MinimumFontSize, QFontInfo(minimalFont).pixelSize()); | ||
50 | | ||||
51 | // Fixes for correct positioning. The problem looks like the following: | ||||
No longer needed afaics, has been introduced with a108066e644861b2756009b57c02d4b42792fead. kfunk: No longer needed afaics, has been introduced with a108066e644861b2756009b57c02d4b42792fead. | |||||
antonanikin: Fixed, now we don't use JS for page scrolling. | |||||
52 | // | ||||
53 | // 1) Some page is loaded and loadFinished() signal is emitted, | ||||
54 | // after this QWebView set right position inside page. | ||||
55 | // | ||||
56 | // 2) After loadFinished() emitting, page JS code finishes it's work and changes | ||||
57 | // font settings (size). This leads to page contents "moving" inside view widget | ||||
58 | // and as a result we have wrong position. | ||||
59 | // | ||||
60 | // Such behavior occurs for example with QtHelp pages. | ||||
61 | // | ||||
62 | // To fix the problem, first, we disable view painter updates during load to avoid content | ||||
63 | // "flickering" and also to hide font size "jumping". Secondly, we reset position inside page | ||||
64 | // after loading with using standard QWebFrame method scrollToAnchor(). | ||||
65 | | ||||
66 | connect(this, &QWebView::loadStarted, this, [this]() { | ||||
67 | setUpdatesEnabled(false); | ||||
68 | }); | ||||
69 | | ||||
70 | connect(this, &QWebView::loadFinished, this, [this](bool) { | ||||
71 | if (url().isValid()) { | ||||
72 | page()->mainFrame()->scrollToAnchor(url().fragment()); | ||||
73 | } | ||||
74 | setUpdatesEnabled(true); | ||||
75 | }); | ||||
76 | | ||||
49 | } | 77 | } | ||
50 | 78 | | |||
51 | void StandardDocumentationView::search ( const QString& text, DocumentationFindWidget::FindOptions options ) | 79 | void StandardDocumentationView::search ( const QString& text, DocumentationFindWidget::FindOptions options ) | ||
52 | { | 80 | { | ||
53 | //Highlighting has been commented because it doesn't let me jump around all occurrences | 81 | //Highlighting has been commented because it doesn't let me jump around all occurrences | ||
54 | // page()->findText(QString(), QWebPage::HighlightAllOccurrences); | 82 | // page()->findText(QString(), QWebPage::HighlightAllOccurrences); | ||
55 | 83 | | |||
I'm not sure we want to adjust the position on resize. I would just drop that whole event filter. Steps to reproduce where this feature looks odd:
Actual:
Expected:
kfunk: I'm not sure we want to adjust the position on resize. I would just drop that whole event… | |||||
56 | QWebPage::FindFlags ff=QWebPage::FindWrapsAroundDocument /*| QWebPage::HighlightAllOccurrences*/; | 84 | QWebPage::FindFlags ff=QWebPage::FindWrapsAroundDocument /*| QWebPage::HighlightAllOccurrences*/; | ||
57 | if(options & DocumentationFindWidget::Previous) | 85 | if(options & DocumentationFindWidget::Previous) | ||
58 | ff |= QWebPage::FindBackward; | 86 | ff |= QWebPage::FindBackward; | ||
59 | 87 | | |||
60 | if(options & DocumentationFindWidget::MatchCase) | 88 | if(options & DocumentationFindWidget::MatchCase) | ||
61 | ff |= QWebPage::FindCaseSensitively; | 89 | ff |= QWebPage::FindCaseSensitively; | ||
62 | 90 | | |||
63 | page()->findText(text, ff); | 91 | page()->findText(text, ff); | ||
64 | } | 92 | } | ||
65 | 93 | | |||
66 | void StandardDocumentationView::setDocumentation(const IDocumentation::Ptr& doc) | 94 | void StandardDocumentationView::setDocumentation(const IDocumentation::Ptr& doc) | ||
67 | { | 95 | { | ||
68 | if(m_doc) | 96 | if(m_doc) | ||
69 | disconnect(m_doc.data()); | 97 | disconnect(m_doc.data()); | ||
70 | m_doc = doc; | 98 | m_doc = doc; | ||
71 | update(); | 99 | update(); | ||
72 | if(m_doc) | 100 | if(m_doc) | ||
73 | connect(m_doc.data(), &IDocumentation::descriptionChanged, this, &StandardDocumentationView::update); | 101 | connect(m_doc.data(), &IDocumentation::descriptionChanged, this, &StandardDocumentationView::update); | ||
74 | } | 102 | } | ||
75 | 103 | | |||
76 | void StandardDocumentationView::update() | 104 | void StandardDocumentationView::update() | ||
http://doc.qt.io/archives/qt-5.5/qwebframe.html#scrollToAnchor instead? Does that work? kfunk: http://doc.qt.io/archives/qt-5.5/qwebframe.html#scrollToAnchor instead? Does that work? | |||||
antonanikin: Damn! How could I not notice this method??!
Thanks a lot! | |||||
77 | { | 105 | { | ||
78 | if(m_doc) | 106 | if(m_doc) | ||
79 | setHtml(m_doc->description()); | 107 | setHtml(m_doc->description()); | ||
80 | else | 108 | else | ||
81 | qCDebug(DOCUMENTATION) << "calling StandardDocumentationView::update() on an uninitialized view"; | 109 | qCDebug(DOCUMENTATION) << "calling StandardDocumentationView::update() on an uninitialized view"; | ||
82 | } | 110 | } |
Unused include