Add support for configurable transparent background to SVGs

Authored by huoni on Mar 24 2018, 1:46 AM.



SVGs do not honor the "Transparent background" config option, and simply have no
background at all. This patch ensures this option is applied to SVGs as well as
raster images.

Unlike RasterImageView that uses a QPixmap buffer (mCurrentBuffer) for drawing
the background and the image, SVGs are rendered directly on top of SvgImageView
(QGraphicsWidget). Therefore we just paint the background in SvgImageView::paint
which happens before the SVG is rendered.

We move the checkboard texture code to AbstractImageView now that it's used by
both subclasses RasterImageView and SvgImageView.

This patch also configures kconf_update due to the moving of the above enum.
Since I didn't want to litter /app with files, I've moved the update files to

Fixes T8125



Test Plan
  • Open an SVG with solid color config option
  • Open and SVG with checkboard background config. At a high enough zoom (where the birdseye view shows up), panning/scrolling should have the checkboard pattern fixed to the image, like raster images
  • Ensure raster images with transparent backgrounds are unaffected


The script must be in
/usr/share/kconf_update in order for /usr/lib/kf5/kconf_update to find it.
If successfull, instances of RasterImageView::AlphaBackground* should change to

Diff Detail

R260 Gwenview
Automatic diff as part of commit; lint not applicable.
Automatic diff as part of commit; unit tests not applicable.
huoni requested review of this revision.Mar 24 2018, 1:46 AM
huoni created this revision.
huoni edited the summary of this revision. (Show Details)Mar 24 2018, 5:00 AM

(See for the exact syntax to use for "Depends on" and for adding tasks. Apparently it's still an open task on upstream Phab…)

Works nicely, and even for the screenshots you selected image and colour carefully πŸ‘Œ

A couple of inline comments for now, I'll continue with the code review later.


As mentioned in T8125, I'm not sure hasAlphaChannel makes sense for SVGs. Perhaps only mention that you'll draw the background for every SVG, or even remove the whole comment.


I'm afraid neither the compiler nor the machine running the code will care much about your comment ;)

In general for situations which "should never happen" you can Q_ASSERT(0); which kills Gwenview (at least in Debug mode) and thus makes the problem visible.


If you want to rename this, you'd need to provide a kconf_update script, otherwise users will lose their old settings.

huoni added inline comments.Mar 25 2018, 10:07 AM

I thought it was good practice to always have a default clause, to communicate that you've considered all cases.

Would it be preferable to put Q_ASSERT(0) here instead, or just take out default?


RasterImageView::AlphaBackgroundMode is still valid code, so old settings would probably still work. Would it be preferable to leave the kcfg file alone if it still works?

rkflx added inline comments.Mar 25 2018, 10:22 AM

Well, assume you add an entry to the enum. Now both the compiler and static analysis tools will see your default and assume this was a deliberate choice, i.e. you'll not get notified that probably you should add the new case here too.

When testing I expected to get a compiler warning when there were unhandled cases (i.e. when removing all but the first case in your example), but this somehow did not work for me (not sure why).

In other places in Gwenview the ASSERT is used inside the default, so I'd say just do it this way.


Does it still work, though? Open old Gwenview, change setting, open new Gwenview: Is the change retained?

For cat ~/.config/gwenviewrc I get:




...which in my book is a different config entry entirely ;)

Peter recently added a kconf_update script for Gwenview, I think you can piggyback on that one.

huoni updated this revision to Diff 30585.Mar 26 2018, 4:37 AM
huoni marked 3 inline comments as done.
  • Remove unneeded comment
  • Use Q_ASSERT(0) instead of empty default: clause
huoni added a comment.Mar 26 2018, 4:38 AM

See inline comment


Right, so I think I know how these update scripts work.
Would I simply edit app/gwenview.upd to add my changes, under a new Id= line?

rkflx added inline comments.Mar 26 2018, 7:43 AM

Yup, that's how I understood it too. Konversation is doing something similar.

rkflx requested changes to this revision.Mar 26 2018, 8:33 PM

Finally looked more in-depth at the code. There are still some improvements you could make, but we are getting there…


I believe const QPixmap& would avoid a bunch of useless copying.


AFAIK we can now assume usage of a C++11 compiler, so you could directly use override in new code.


You can initialize those member variables the same way it's done for mSvgItem above.


In the config dialog, Apply has an immediate effect for raster images (good), but for SVGs you have to hit OK for the background to change (bad).

Possibly setAlphaBackgroundMode is called in this case, where for RasterImageView there is update, so you'd need something similar for SVGs?


In theory, background is needed only for a single case, so it can be moved there. However, we won't need it at all ;)


As you are using this variable only a single time and it is constructed by a simple function call, you could just use the function inline, saving an extra copy.


Why write this complicated construct when you could simply write scrollPos() inline?


Why do you need the second painter? The following works just fine for me:

painter->drawTiledPixmap(imageRect, alphaBackgroundTexture(), scrollPos());

Adding qDebug(), this method and thus SvgImageView::drawAlphaBackground is called a crazy number of times compared to RasterImageView::updateFromScaler, i.e. very often when displaying the image for the first time and also each time the cursor enters the viewport.

I wonder what's done differently for raster images to prevent this (perhaps via QGraphicsView's architecture?). Also, glancing at the API docs I discovered paint and setCacheMode for QGraphicsItem, not sure if this will help.


I would make both functions private.


int β†’ AbstractImageView::AlphaBackgroundMode

This revision now requires changes to proceed.Mar 26 2018, 8:33 PM
huoni updated this revision to Diff 30670.Mar 26 2018, 10:29 PM
huoni marked 11 inline comments as done.
  • Code cleanup/improvements
  • Use graphics cache to prevent unnecessary repaints of background

Thanks again for the in depth review, I'm learning a lot!


It's hard to know when to conform to surrounding code, and when to break that consistency. In hindsight, a QPixmap is going to be more expensive to copy than QRect and the like. so this makes sense.


TIL, thanks.
In these situations, is it beneficial to submit a patch changing all the old code? In this case, essentially replace all Q_DECL_OVERRIDE with override? Or is it preferred to use the new style and leave old code as is (and end up with a mix like connect()).


All the above was a result of copying a similar structure from I believe RasterImageView. At the time of course I'm just trying to get things to work, and not thinking too deeply. Luckily your awesome reviews catch it!
I think I'll try to review my own work so to speak before submitting in future, to hopefully catch a lot of these things.


Looks like caching works here, as long as we manually call update() when the background needs changing (on zoom/pan).

Note that RasterImageView::paint is also called numerous times, but it's only drawing mCurrentBuffer. We might be able to copy that behaviour, and implement a buffer here, but I think caching is the better option.

huoni updated this revision to Diff 30688.Mar 27 2018, 5:54 AM
huoni edited the summary of this revision. (Show Details)
huoni edited the test plan for this revision. (Show Details)
  • Add kconf_update script
rkflx added a comment.Mar 27 2018, 9:06 PM

Nearly there, so I can (hopefully) move to Peter's and your other reviews next…


Why not name the directory kconf_update?


That's probably not needed anymore.


At some point we could run tools like Clazy and clang-tidy over the whole code base and apply their automated fix-ups (and replace Q_DECL_OVERRIDE manually as well). Of course this makes git blame harder to use, which is also the reason to prefer the new style for new code righ away or when a line of code is touched anyway.


You missed a spot ;)


After you latest changes you can finally drop those curly braces, which are quite uncommon for switch blocks.


I'd assume that drawTilePixmap is not that much more expensive than drawPixmap of a potential SvgImageView::mCurrentBuffer, so the current version with single occasional repaints should be fine. Great setCacheMode is working out for us, it was quite a long shot…

6 β†—(On Diff #30688)

FYI: Unless someone manually edited the config file, the default value will not be specified explicitly so your condition will never trigger. This trick will allow us to change the default to a different value, BTW.

huoni updated this revision to Diff 30767.Mar 27 2018, 10:37 PM
huoni marked 5 inline comments as done.
  • Rename updaters/ -> kconf_update/
  • Code cleanup

Because Konversation used updaters (was looking at other examples), but kconf_update is better I agree.

rkflx accepted this revision.Mar 27 2018, 10:43 PM


This revision is now accepted and ready to land.Mar 27 2018, 10:43 PM



huoni edited the summary of this revision. (Show Details)Mar 27 2018, 11:58 PM
ngraham added inline comments.Mar 30 2018, 4:03 PM

Is there ever going to be an environment where this could run that doesn't have Perl? I'm not one of those Perl haters, but it seems like this could be done more easily and in a more guaranteed-bulletproof manner with sed in a shell script instead.

rkflx added inline comments.Mar 30 2018, 9:03 PM

The docs say the following on that topic:

stick with interpreted scripts like shell or perl. To make your scripts compatible with Windows, you should not use shell scripts.

Also, lots of similar kconf_update scripts are written in Perl, so I guess this is fine.

huoni added inline comments.Mar 30 2018, 10:10 PM

Thanks for answering for me :)
Those are indeed the reasons I chose Perl.

ngraham accepted this revision.Mar 30 2018, 10:24 PM
rkflx added a comment.Apr 3 2018, 11:57 PM

One more thing…


This path does not exist on some systems I tested. Konversation uses /usr/bin/perl, but I guess the "official" way would be #!/usr/bin/env perl.

huoni updated this revision to Diff 31246.Apr 4 2018, 12:12 AM
huoni marked an inline comment as done.
  • Rebase
  • Change update script shebang to use env
huoni updated this revision to Diff 31442.Apr 6 2018, 12:36 AM
  • Fix SVG background being drawn in wrong position for split second
rkflx accepted this revision.Apr 6 2018, 9:57 AM

Still LGTM ;)

This revision was automatically updated to reflect the committed changes.