diff options
author | Tobias Markmann <tm@ayena.de> | 2016-10-19 13:56:14 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2016-10-19 13:56:14 (GMT) |
commit | bcd3660a3527327117b46e104d16204a64fe0b9f (patch) | |
tree | 79136e46ff58f5a83bc268642f455283591bfb0e | |
parent | 02e7140ee68229dcd439f7e333cd3858265cab08 (diff) | |
download | swift-bcd3660a3527327117b46e104d16204a64fe0b9f.zip swift-bcd3660a3527327117b46e104d16204a64fe0b9f.tar.bz2 |
Scale QWebView DPI resolution to match desktop DPI resolution
Test-Information:
Tested that at 1em scaling, the default, the main text in the
chat style has the same height as the nicknames in the roster.
Tested with Qt 5.4.2 on macOS 10.12 on a retina system,
with Qt 5.4.2 on macOS 10.12 on a non-retina system, and
Windows 8 with Qt 5.7.0 with QtWebKit Technology Preview 4.
Change-Id: I10701c411d9f787bf497eb6aab208a0f3fda621c
-rw-r--r-- | Swift/QtUI/QtWebKitChatView.cpp | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/Swift/QtUI/QtWebKitChatView.cpp b/Swift/QtUI/QtWebKitChatView.cpp index c3320d8..414f098 100644 --- a/Swift/QtUI/QtWebKitChatView.cpp +++ b/Swift/QtUI/QtWebKitChatView.cpp @@ -1,40 +1,41 @@ /* * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swift/QtUI/QtWebKitChatView.h> #include <QApplication> #include <QDesktopServices> +#include <QDesktopWidget> #include <QEventLoop> #include <QFile> #include <QFileDialog> #include <QFileInfo> #include <QInputDialog> #include <QKeyEvent> #include <QMessageBox> #include <QStackedWidget> #include <QTimer> #include <QVBoxLayout> #include <QWebFrame> #include <QWebSettings> #include <QtDebug> #include <Swiften/Base/FileSize.h> #include <Swiften/Base/Log.h> #include <Swiften/StringCodecs/Base64.h> #include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> #include <Swift/Controllers/UIEvents/UIEventStream.h> #include <Swift/QtUI/MessageSnippet.h> #include <Swift/QtUI/QtChatWindow.h> #include <Swift/QtUI/QtChatWindowJSBridge.h> #include <Swift/QtUI/QtScaledAvatarCache.h> #include <Swift/QtUI/QtSwiftUtil.h> #include <Swift/QtUI/QtUtilities.h> #include <Swift/QtUI/QtWebView.h> #include <Swift/QtUI/SystemMessageSnippet.h> @@ -320,61 +321,65 @@ void QtWebKitChatView::resetView() { lastElement_ = QWebElement(); firstElement_ = lastElement_; topMessageAdded_ = false; scrollBarMaximum_ = 0; QString pageHTML = theme_->getTemplate(); pageHTML.replace("==bodyBackground==", "background-color:#e3e3e3"); pageHTML.replace(pageHTML.indexOf("%@"), 2, theme_->getBase()); if (pageHTML.count("%@") > 3) { pageHTML.replace(pageHTML.indexOf("%@"), 2, theme_->getMainCSS()); } pageHTML.replace(pageHTML.indexOf("%@"), 2, "Variants/Blue on Green.css"); pageHTML.replace(pageHTML.indexOf("%@"), 2, ""/*headerSnippet.getContent()*/); pageHTML.replace(pageHTML.indexOf("%@"), 2, ""/*footerSnippet.getContent()*/); QEventLoop syncLoop; connect(webView_, SIGNAL(loadFinished(bool)), &syncLoop, SLOT(quit())); webPage_->mainFrame()->setHtml(pageHTML); while (!viewReady_) { syncLoop.processEvents(QEventLoop::AllEvents, 50); } document_ = webPage_->mainFrame()->documentElement(); scrollToBottom(); connect(webPage_->mainFrame(), SIGNAL(contentsSizeChanged(const QSize&)), this, SLOT(handleFrameSizeChanged()), Qt::UniqueConnection); // Hooking up to scroll bar update, because Qt does not provide a way to retrieve accurate scroll bar updates from C++ directly. QWebElement body = document_.findFirst("body"); assert(!body.isNull()); body.setAttribute("onscroll", "chatwindow.verticalScrollBarPositionChanged(document.body.scrollTop / (document.body.scrollHeight - window.innerHeight))"); - webView_->settings()->setFontSize(QWebSettings::DefaultFontSize, QApplication::font().pointSize()); + // Adjust web view default 96 DPI setting to screen DPI. + // For more information see https://webkit.org/blog/57/css-units/ + webView_->setZoomFactor(QApplication::desktop()->screen()->logicalDpiX() / 96.0); + + body.setStyleProperty("font-size", QString("%1pt").arg(QApplication::font().pointSize())); } static QWebElement findElementWithID(QWebElement document, QString elementName, QString id) { QWebElementCollection elements = document.findAll(elementName); Q_FOREACH(QWebElement element, elements) { if (element.attribute("id") == id) { return element; } } return QWebElement(); } void QtWebKitChatView::setFileTransferProgress(QString id, const int percentageDone) { rememberScrolledToBottom(); QWebElement ftElement = findElementWithID(document_, "div", id); if (ftElement.isNull()) { SWIFT_LOG(debug) << "Tried to access FT UI via invalid id!" << std::endl; return; } QWebElement progressBar = ftElement.findFirst("div.progressbar"); progressBar.setStyleProperty("width", QString::number(percentageDone) + "%"); QWebElement progressBarValue = ftElement.findFirst("div.progressbar-value"); progressBarValue.setInnerXml(QString::number(percentageDone) + " %"); } void QtWebKitChatView::setFileTransferStatus(QString id, const ChatWindow::FileTransferState state, const QString& /* msg */) { rememberScrolledToBottom(); QWebElement ftElement = findElementWithID(document_, "div", id); if (ftElement.isNull()) { |