]> git.lyx.org Git - lyx.git/commitdiff
#9130 Text in main work area isn't rendered with high resolution
authorStephan Witt <switt@lyx.org>
Tue, 19 May 2015 20:46:06 +0000 (22:46 +0200)
committerStephan Witt <switt@lyx.org>
Tue, 19 May 2015 20:46:26 +0000 (22:46 +0200)
Improved icon and pixmap handling with SVG images and high physical resolution displays.
This results in much better looking icons and splash banner.

src/frontends/qt4/GuiApplication.cpp
src/frontends/qt4/GuiApplication.h
src/frontends/qt4/GuiImage.cpp
src/frontends/qt4/GuiView.cpp
src/graphics/GraphicsLoader.cpp

index f03776caca6baf27483a11ac5e76262156c1832c..6eb968cdb0dbceffa81fd25943ee1a633cd5407c 100644 (file)
@@ -566,22 +566,39 @@ QString iconName(FuncRequest const & f, bool unknown)
        return QString();
 }
 
+
+bool getPixmap(QPixmap & pixmap, QString const & path)
+{
+       if (pixmap.load(path)) {
+               if (path.endsWith(".svgz") || path.endsWith(".svg") ) {
+                       GuiApplication const * guiApp = theGuiApp();
+                       if (guiApp != 0) {
+                               pixmap.setDevicePixelRatio(guiApp->pixelRatio());
+                       }
+               }
+               return true;
+       }
+       return false;
+}
+
+
 QPixmap getPixmap(QString const & path, QString const & name, QString const & ext)
 {
-       QPixmap pixmap;
        QString imagedir = path;
        FileName fname = imageLibFileSearch(imagedir, name, ext, theGuiApp()->imageSearchMode());
        QString fpath = toqstr(fname.absFileName());
+       QPixmap pixmap = QPixmap();
 
-       if (pixmap.load(fpath)) {
+       if (getPixmap(pixmap, fpath)) {
                return pixmap;
-       } else {
-           QStringList exts = ext.split(",");
-           fpath = ":/" + path + name + ".";
-           for (int i = 0; i < exts.size(); ++i) {
-               if (pixmap.load(fpath + exts.at(i)))
+       }
+       
+       QStringList exts = ext.split(",");
+       fpath = ":/" + path + name + ".";
+       for (int i = 0; i < exts.size(); ++i) {
+               if (getPixmap(pixmap, fpath + exts.at(i))) {
                        return pixmap;
-           }
+               }
        }
 
        bool const list = ext.contains(",");
@@ -592,6 +609,7 @@ QPixmap getPixmap(QString const & path, QString const & name, QString const & ex
        return QPixmap();
 }
 
+
 QIcon getIcon(FuncRequest const & f, bool unknown)
 {
 #if (QT_VERSION >= 0x040600)
@@ -610,13 +628,13 @@ QIcon getIcon(FuncRequest const & f, bool unknown)
                return QIcon();
 
        //LYXERR(Debug::GUI, "Found icon: " << icon);
-       QPixmap pm;
-       if (!pm.load(icon)) {
+       QPixmap pixmap = QPixmap();
+       if (!getPixmap(pixmap,icon)) {
                LYXERR0("Cannot load icon " << icon << " please verify resource system!");
                return QIcon();
        }
 
-       return QIcon(pm);
+       return QIcon(pixmap);
 }
 
 
@@ -2423,6 +2441,9 @@ void GuiApplication::execBatchCommands()
 #ifdef Q_OS_MAC
 #if QT_VERSION > 0x040600
        setAttribute(Qt::AA_MacDontSwapCtrlAndMeta,lyxrc.mac_dontswap_ctrl_meta);
+#endif
+#if QT_VERSION > 0x050100
+       setAttribute(Qt::AA_UseHighDpiPixmaps,true);
 #endif
        // Create the global default menubar which is shown for the dialogs
        // and if no GuiView is visible.
index c6258eaeb692c60004950061733d9433f133fc4f..bfc242ccde8713ebd0d314f9c64fdd4699084d23 100644 (file)
@@ -247,7 +247,12 @@ extern GuiApplication * guiApp;
 QString iconName(FuncRequest const & f, bool unknown);
 
 /// \return the pixmap for the given path, name and extension.
+/// in case of errors a warning is produced and an empty pixmap is returned.
 QPixmap getPixmap(QString const & path, QString const & name, QString const & ext);
+/// Load the file at \param path and convert it to a pixmap.
+/// \return true on success otherwise invalidate the pixmap and return false.
+/// The caller is responsible for error reporting.
+bool getPixmap(QPixmap & pixmap, QString const & path);
 
 /// \return an icon for the given action.
 QIcon getIcon(FuncRequest const & f, bool unknown);
index f8e5261858a135449db4ce33c38d7683e9d9f697..589511cb90b3819eed547d8d005087e5d00bb820 100644 (file)
@@ -141,8 +141,9 @@ bool GuiImage::clip(Params const & params)
                // No clipping is necessary.
                return false;
 
-       int const new_width  = params.bb.xr - params.bb.xl;
-       int const new_height = params.bb.yt - params.bb.yb;
+       double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
+       int const new_width  = static_cast<int>((params.bb.xr - params.bb.xl) * pixelRatio);
+       int const new_height = static_cast<int>((params.bb.yt - params.bb.yb) * pixelRatio);
 
        QImage const & image = is_transformed_ ? transformed_ : original_;
 
@@ -185,7 +186,8 @@ bool GuiImage::scale(Params const & params)
        if (params.scale == 100)
                return false;
 
-       qreal scale = qreal(params.scale) / 100.0;
+       double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
+       qreal scale = qreal(params.scale) / 100.0 * pixelRatio;
 
 #if (QT_VERSION >= 0x040500) && (QT_VERSION <= 0x040502)
        // Due to a bug in Qt, LyX will crash for certain
index 52fadcf3ef231d7619ae709f5dee7e8474008747..e41a4514f26c973e9c33f6b888d00b89796e1aad 100644 (file)
@@ -32,6 +32,7 @@
 #include "TocModel.h"
 
 #include "qt_helpers.h"
+#include "support/filetools.h"
 
 #include "frontends/alert.h"
 #include "frontends/KeySymbol.h"
 #include <QSplitter>
 #include <QStackedWidget>
 #include <QStatusBar>
+#include <QSvgRenderer>
 #include <QtConcurrentRun>
 #include <QTime>
 #include <QTimer>
@@ -149,7 +151,8 @@ namespace {
 class BackgroundWidget : public QWidget
 {
 public:
-       BackgroundWidget()
+       BackgroundWidget(int width, int height)
+               : width_(width), height_(height)
        {
                LYXERR(Debug::GUI, "show banner: " << lyxrc.show_banner);
                if (!lyxrc.show_banner)
@@ -157,32 +160,40 @@ public:
                /// The text to be written on top of the pixmap
                QString const text = lyx_version ?
                        qt_("version ") + lyx_version : qt_("unknown version");
-               splash_ = getPixmap("images/", "banner", "svgz,png");
+               QString imagedir = "images/";
+               FileName fname = imageLibFileSearch(imagedir, "banner", "svgz");
+               QSvgRenderer svgRenderer(toqstr(fname.absFileName()));
+               if (svgRenderer.isValid()) {
+                       splash_ = QPixmap(splashSize());
+                       QPainter painter(&splash_);
+                       svgRenderer.render(&painter);
+                       splash_.setDevicePixelRatio(pixelRatio());
+               } else {
+                       splash_ = getPixmap("images/", "banner", "png");
+               }
 
                QPainter pain(&splash_);
                pain.setPen(QColor(0, 0, 0));
-               double const multiplier = splashPixelRatio() / pixelRatio();
-               int const size = static_cast<int>(toqstr(lyxrc.font_sizes[FONT_SIZE_LARGE]).toDouble() * multiplier);
-               int const x = static_cast<int>(190 * multiplier);
-               int const y = static_cast<int>(225 * multiplier);
+               qreal const fsize = fontSize();
+               QPointF const position = textPosition();
                LYXERR(Debug::GUI,
                        "widget pixel ratio: " << pixelRatio() <<
                        " splash pixel ratio: " << splashPixelRatio() <<
-                       " version text size,position: " << size << "@" << x << "+" << y);
+                       " version text size,position: " << fsize << "@" << position.x() << "+" << position.y());
                QFont font;
                // The font used to display the version info
                font.setStyleHint(QFont::SansSerif);
                font.setWeight(QFont::Bold);
-               font.setPointSize(size);
+               font.setPointSizeF(fsize);
                pain.setFont(font);
-               pain.drawText(x, y, text);
+               pain.drawText(position, text);
                setFocusPolicy(Qt::StrongFocus);
        }
 
        void paintEvent(QPaintEvent *)
        {
-               int const w = static_cast<int>(splash_.width() / splashPixelRatio());
-               int const h = static_cast<int>(splash_.height() / splashPixelRatio());
+               int const w = width_;
+               int const h = height_;
                int const x = (width() - w) / 2;
                int const y = (height() - h) / 2;
                LYXERR(Debug::GUI,
@@ -207,6 +218,8 @@ public:
 
 private:
        QPixmap splash_;
+       int const width_;
+       int const height_;
 
        /// Current ratio between physical pixels and device-independent pixels
        double pixelRatio() const {
@@ -217,6 +230,26 @@ private:
 #endif
        }
 
+       qreal fontSize() {
+               return toqstr(lyxrc.font_sizes[FONT_SIZE_LARGE]).toDouble();
+       }
+
+       QPointF textPosition() {
+               return QPointF(splashWidth()/2 - 16, splashHeigth() - 40);
+       }
+
+       QSize splashSize() {
+               return  QSize(width_ * pixelRatio(),height_ * pixelRatio());
+       }
+
+       double splashWidth() {
+               return splash_.width()/splashPixelRatio();
+       }
+
+       double splashHeigth() {
+               return splash_.height()/splashPixelRatio();
+       }
+
        /// Ratio between physical pixels and device-independent pixels of splash image
        double splashPixelRatio() const {
 #if QT_VERSION >= 0x050000
@@ -264,7 +297,7 @@ struct GuiView::GuiViewPrivate
                }
 
                splitter_ = new QSplitter;
-               bg_widget_ = new BackgroundWidget;
+               bg_widget_ = new BackgroundWidget(400, 250);
                stack_widget_ = new QStackedWidget;
                stack_widget_->addWidget(bg_widget_);
                stack_widget_->addWidget(splitter_);
index 23ebb623948f9fee8bc9b6c27951fe92e28b1f33..498394e421f09a9a7bb9aec16f45945d4270d8e9 100644 (file)
@@ -460,6 +460,10 @@ void Loader::Impl::createPixmap()
                if (idx != string::npos && idx > 3) {
                        if (filename.substr(idx - 3, 3) == "@2x") {
                                params_.pixel_ratio = 2.0;
+                       } else if (cached_item_->filename().extension() == "svgz") {
+                               params_.pixel_ratio = 4.0;
+                       } else if (cached_item_->filename().extension() == "svg") {
+                               params_.pixel_ratio = 4.0;
                        }
                }
        }