Changeset View
Changeset View
Standalone View
Standalone View
documentation/standarddocumentationview.cpp
Show All 16 Lines | |||||
17 | * Free Software Foundation, Inc., | 17 | * Free Software Foundation, Inc., | ||
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 <QEvent> | ||||
25 | #include <QFontDatabase> | 26 | #include <QFontDatabase> | ||
27 | #include <QWebFrame> | ||||
kfunk: Unused include | |||||
26 | 28 | | |||
27 | using namespace KDevelop; | 29 | using namespace KDevelop; | ||
28 | 30 | | |||
31 | class StandardDocumentationViewHelper : public QObject | ||||
32 | { | ||||
33 | Q_OBJECT | ||||
34 | public: | ||||
35 | StandardDocumentationViewHelper(StandardDocumentationView* view); | ||||
36 | | ||||
37 | bool eventFilter(QObject* target, QEvent* event); | ||||
38 | | ||||
39 | private: | ||||
40 | void adjustPosition(); | ||||
41 | | ||||
42 | StandardDocumentationView* m_view; | ||||
43 | }; | ||||
44 | | ||||
45 | StandardDocumentationViewHelper::StandardDocumentationViewHelper(StandardDocumentationView* view) | ||||
46 | : QObject(view) | ||||
47 | , m_view(view) | ||||
48 | { | ||||
49 | Q_ASSERT(view); | ||||
50 | | ||||
51 | m_view->installEventFilter(this); | ||||
52 | | ||||
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. | |||||
53 | // First, we disable view painter updates during load to avoid content "flickering" and | ||||
54 | // also to hide font size "jumping", which happens for example for Qt help pages with | ||||
55 | // JavaScript enabled. This JS also breaks positioning inside page: | ||||
56 | // | ||||
57 | // 1) Some page is loaded (for example QWebView::setUrl()) and loadFinished() signal | ||||
58 | // emitted, then view set right position inside page. | ||||
59 | // | ||||
60 | // 2) After this, page JS finishes it's work and change font settings (size). | ||||
61 | // This leads to contents "moving" inside view and as a result we have wrong position. | ||||
62 | // | ||||
63 | // Therefore secondly, we fix position after loading with using standard QWebFrame method | ||||
64 | // scrollToAnchor() to avoid page reloading which happens if view's methods like | ||||
65 | // load() or setUrl() are used. | ||||
66 | // | ||||
67 | // Such positioning fixing also applied on resize event - standard view's behavior breaks | ||||
68 | // position during widget resize. | ||||
69 | | ||||
70 | connect(m_view, &QWebView::loadStarted, this, [this]() { | ||||
71 | m_view->setUpdatesEnabled(false); | ||||
72 | }); | ||||
73 | | ||||
74 | connect(m_view, &QWebView::loadFinished, this, [this](bool) { | ||||
75 | adjustPosition(); | ||||
76 | m_view->setUpdatesEnabled(true); | ||||
77 | }); | ||||
78 | } | ||||
79 | | ||||
80 | bool StandardDocumentationViewHelper::eventFilter(QObject* target, QEvent* event) | ||||
81 | { | ||||
82 | bool result = QObject::eventFilter(target, event); | ||||
83 | | ||||
84 | if (event->type() == QEvent::Resize) { | ||||
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… | |||||
85 | adjustPosition(); | ||||
86 | } | ||||
87 | | ||||
88 | return result; | ||||
89 | } | ||||
90 | | ||||
91 | void StandardDocumentationViewHelper::adjustPosition() | ||||
92 | { | ||||
93 | if (!m_view->url().isValid()) { | ||||
94 | return; | ||||
95 | } | ||||
96 | | ||||
97 | m_view->page()->mainFrame()->scrollToAnchor(m_view->url().fragment()); | ||||
98 | } | ||||
99 | | ||||
29 | StandardDocumentationView::StandardDocumentationView(DocumentationFindWidget* findWidget, QWidget* parent) | 100 | StandardDocumentationView::StandardDocumentationView(DocumentationFindWidget* findWidget, QWidget* parent) | ||
30 | : QWebView (parent) | 101 | : QWebView(parent) | ||
31 | { | 102 | { | ||
32 | findWidget->setEnabled(true); | 103 | findWidget->setEnabled(true); | ||
33 | connect(findWidget, &DocumentationFindWidget::newSearch, this, &StandardDocumentationView::search); | 104 | connect(findWidget, &DocumentationFindWidget::newSearch, this, &StandardDocumentationView::search); | ||
34 | 105 | | |||
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! | |||||
35 | QFont sansSerifFont = QFontDatabase::systemFont(QFontDatabase::GeneralFont); | 106 | QFont sansSerifFont = QFontDatabase::systemFont(QFontDatabase::GeneralFont); | ||
36 | QFont monospaceFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); | 107 | QFont monospaceFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); | ||
37 | QFont minimalFont = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont); | 108 | QFont minimalFont = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont); | ||
38 | 109 | | |||
39 | QWebSettings* s = settings(); | 110 | QWebSettings* s = settings(); | ||
40 | 111 | | |||
41 | s->setFontFamily(QWebSettings::StandardFont, sansSerifFont.family()); | 112 | s->setFontFamily(QWebSettings::StandardFont, sansSerifFont.family()); | ||
42 | s->setFontFamily(QWebSettings::SerifFont, "Serif"); | 113 | s->setFontFamily(QWebSettings::SerifFont, "Serif"); | ||
43 | s->setFontFamily(QWebSettings::SansSerifFont, sansSerifFont.family()); | 114 | s->setFontFamily(QWebSettings::SansSerifFont, sansSerifFont.family()); | ||
44 | s->setFontFamily(QWebSettings::FixedFont, monospaceFont.family()); | 115 | s->setFontFamily(QWebSettings::FixedFont, monospaceFont.family()); | ||
45 | 116 | | |||
46 | s->setFontSize(QWebSettings::DefaultFontSize, QFontInfo(sansSerifFont).pixelSize()); | 117 | s->setFontSize(QWebSettings::DefaultFontSize, QFontInfo(sansSerifFont).pixelSize()); | ||
47 | s->setFontSize(QWebSettings::DefaultFixedFontSize, QFontInfo(monospaceFont).pixelSize()); | 118 | s->setFontSize(QWebSettings::DefaultFixedFontSize, QFontInfo(monospaceFont).pixelSize()); | ||
48 | s->setFontSize(QWebSettings::MinimumFontSize, QFontInfo(minimalFont).pixelSize()); | 119 | s->setFontSize(QWebSettings::MinimumFontSize, QFontInfo(minimalFont).pixelSize()); | ||
120 | | ||||
121 | new StandardDocumentationViewHelper(this); | ||||
49 | } | 122 | } | ||
50 | 123 | | |||
51 | void StandardDocumentationView::search ( const QString& text, DocumentationFindWidget::FindOptions options ) | 124 | void StandardDocumentationView::search ( const QString& text, DocumentationFindWidget::FindOptions options ) | ||
52 | { | 125 | { | ||
53 | //Highlighting has been commented because it doesn't let me jump around all occurrences | 126 | //Highlighting has been commented because it doesn't let me jump around all occurrences | ||
54 | // page()->findText(QString(), QWebPage::HighlightAllOccurrences); | 127 | // page()->findText(QString(), QWebPage::HighlightAllOccurrences); | ||
55 | 128 | | |||
56 | QWebPage::FindFlags ff=QWebPage::FindWrapsAroundDocument /*| QWebPage::HighlightAllOccurrences*/; | 129 | QWebPage::FindFlags ff=QWebPage::FindWrapsAroundDocument /*| QWebPage::HighlightAllOccurrences*/; | ||
Show All 18 Lines | |||||
75 | 148 | | |||
76 | void StandardDocumentationView::update() | 149 | void StandardDocumentationView::update() | ||
77 | { | 150 | { | ||
78 | if(m_doc) | 151 | if(m_doc) | ||
79 | setHtml(m_doc->description()); | 152 | setHtml(m_doc->description()); | ||
80 | else | 153 | else | ||
81 | qCDebug(DOCUMENTATION) << "calling StandardDocumentationView::update() on an uninitialized view"; | 154 | qCDebug(DOCUMENTATION) << "calling StandardDocumentationView::update() on an uninitialized view"; | ||
82 | } | 155 | } | ||
156 | | ||||
157 | #include "standarddocumentationview.moc" |
Unused include