Add Gio::Icon based RTImages
This commit is contained in:
parent
d2a280fbf3
commit
afe20fee0e
102
rtgui/rtimage.cc
102
rtgui/rtimage.cc
@ -22,12 +22,40 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <gtkmm/icontheme.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "../rtengine/settings.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct GIconKey {
|
||||
Glib::RefPtr<const Gio::Icon> icon;
|
||||
/**
|
||||
* Icon size in pixels.
|
||||
*/
|
||||
int icon_size;
|
||||
|
||||
GIconKey() {}
|
||||
GIconKey(const Glib::RefPtr<const Gio::Icon> &icon, int icon_size): icon(icon), icon_size(icon_size) {}
|
||||
|
||||
bool operator==(const GIconKey &other) const
|
||||
{
|
||||
bool icons_match = (icon.get() == nullptr && other.icon.get() == nullptr) || (icon.get() != nullptr && icon->equal(Glib::RefPtr<Gio::Icon>::cast_const(other.icon)));
|
||||
return icons_match && icon_size == other.icon_size;
|
||||
}
|
||||
};
|
||||
|
||||
struct GIconKeyHash {
|
||||
size_t operator()(const GIconKey &key) const
|
||||
{
|
||||
const size_t icon_hash = key.icon ? key.icon->hash() : 0;
|
||||
return icon_hash ^ std::hash<int>()(key.icon_size);
|
||||
}
|
||||
};
|
||||
|
||||
std::unordered_map<GIconKey, Glib::RefPtr<Gdk::Pixbuf>, GIconKeyHash> gIconPixbufCache;
|
||||
std::map<std::string, Glib::RefPtr<Gdk::Pixbuf> > pixbufCache;
|
||||
std::map<std::string, Cairo::RefPtr<Cairo::ImageSurface> > surfaceCache;
|
||||
|
||||
@ -44,6 +72,8 @@ RTImage::RTImage (RTImage &other) : surface(other.surface), pixbuf(other.pixbuf)
|
||||
set(pixbuf);
|
||||
} else if (surface) {
|
||||
set(surface);
|
||||
} else if (other.gIcon) {
|
||||
changeImage(other.gIcon, other.gIconSize);
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,13 +110,27 @@ RTImage::RTImage (Glib::RefPtr<RTImage> &other)
|
||||
if (other->get_surface()) {
|
||||
surface = other->get_surface();
|
||||
set(surface);
|
||||
} else {
|
||||
} else if (other->pixbuf) {
|
||||
pixbuf = other->get_pixbuf();
|
||||
set(pixbuf);
|
||||
} else if (other->gIcon) {
|
||||
changeImage(other->gIcon, other->gIconSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RTImage::RTImage(const Glib::RefPtr<const Gio::Icon> &gIcon, Gtk::IconSize size)
|
||||
{
|
||||
changeImage(gIcon, size);
|
||||
}
|
||||
|
||||
int RTImage::iconSizeToPixels(Gtk::IconSize size) const
|
||||
{
|
||||
int width, height;
|
||||
Gtk::IconSize::lookup(size, width, height);
|
||||
return std::round(getTweakedDPI() / baseDPI * std::max(width, height));
|
||||
}
|
||||
|
||||
void RTImage::setImage (const Glib::ustring& fileName, const Glib::ustring& rtlFileName)
|
||||
{
|
||||
Glib::ustring imageName;
|
||||
@ -113,10 +157,41 @@ void RTImage::setDPInScale (const double newDPI, const int newScale)
|
||||
}
|
||||
}
|
||||
|
||||
void RTImage::changeImage(const Glib::RefPtr<const Gio::Icon> &gIcon, int size)
|
||||
{
|
||||
clear();
|
||||
|
||||
pixbuf.reset();
|
||||
surface.clear();
|
||||
this->gIcon = gIcon;
|
||||
|
||||
if (!gIcon) {
|
||||
return;
|
||||
}
|
||||
|
||||
gIconSize = size;
|
||||
GIconKey key(gIcon, gIconSize);
|
||||
auto iterator = gIconPixbufCache.find(key);
|
||||
|
||||
if (iterator == gIconPixbufCache.end()) {
|
||||
auto icon_pixbuf = createPixbufFromGIcon(gIcon, gIconSize);
|
||||
iterator = gIconPixbufCache.emplace(key, icon_pixbuf).first;
|
||||
}
|
||||
|
||||
set(iterator->second);
|
||||
}
|
||||
|
||||
void RTImage::changeImage(const Glib::RefPtr<const Gio::Icon> &gIcon, Gtk::IconSize size)
|
||||
{
|
||||
changeImage(gIcon, iconSizeToPixels(size));
|
||||
}
|
||||
|
||||
void RTImage::changeImage (const Glib::ustring& imageName)
|
||||
{
|
||||
clear ();
|
||||
|
||||
gIcon.reset();
|
||||
|
||||
if (imageName.empty()) {
|
||||
return;
|
||||
}
|
||||
@ -150,6 +225,11 @@ int RTImage::get_width()
|
||||
if (pixbuf) {
|
||||
return pixbuf->get_width();
|
||||
}
|
||||
|
||||
if (gIcon) {
|
||||
return this->get_pixbuf()->get_width();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -161,6 +241,11 @@ int RTImage::get_height()
|
||||
if (pixbuf) {
|
||||
return pixbuf->get_height();
|
||||
}
|
||||
|
||||
if (gIcon) {
|
||||
return this->get_pixbuf()->get_height();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -178,6 +263,11 @@ void RTImage::cleanup(bool all)
|
||||
for (auto& entry : surfaceCache) {
|
||||
entry.second.clear();
|
||||
}
|
||||
|
||||
for (auto& entry : gIconPixbufCache) {
|
||||
entry.second.reset();
|
||||
}
|
||||
|
||||
RTScalable::cleanup(all);
|
||||
}
|
||||
|
||||
@ -189,6 +279,10 @@ void RTImage::updateImages()
|
||||
for (auto& entry : surfaceCache) {
|
||||
entry.second = createImgSurfFromFile(entry.first);
|
||||
}
|
||||
|
||||
for (auto& entry : gIconPixbufCache) {
|
||||
entry.second = createPixbufFromGIcon(entry.first.icon, entry.first.icon_size);
|
||||
}
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gdk::Pixbuf> RTImage::createPixbufFromFile (const Glib::ustring& fileName)
|
||||
@ -197,6 +291,12 @@ Glib::RefPtr<Gdk::Pixbuf> RTImage::createPixbufFromFile (const Glib::ustring& fi
|
||||
return Gdk::Pixbuf::create(imgSurf, 0, 0, imgSurf->get_width(), imgSurf->get_height());
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gdk::Pixbuf> RTImage::createPixbufFromGIcon(const Glib::RefPtr<const Gio::Icon> &icon, int size)
|
||||
{
|
||||
// TODO: Listen for theme changes and update icon, remove from cache.
|
||||
return Gtk::IconTheme::get_default()->lookup_icon(icon, size, Gtk::ICON_LOOKUP_FORCE_SIZE).load_icon();
|
||||
}
|
||||
|
||||
Cairo::RefPtr<Cairo::ImageSurface> RTImage::createImgSurfFromFile (const Glib::ustring& fileName)
|
||||
{
|
||||
Cairo::RefPtr<Cairo::ImageSurface> surf;
|
||||
|
@ -34,6 +34,11 @@ class RTImage final : public Gtk::Image, public RTScalable
|
||||
protected:
|
||||
Cairo::RefPtr<Cairo::ImageSurface> surface;
|
||||
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
|
||||
Glib::RefPtr<const Gio::Icon> gIcon;
|
||||
int gIconSize;
|
||||
|
||||
void changeImage(const Glib::RefPtr<const Gio::Icon> &gIcon, int size);
|
||||
int iconSizeToPixels(Gtk::IconSize size) const;
|
||||
|
||||
public:
|
||||
RTImage ();
|
||||
@ -42,9 +47,11 @@ public:
|
||||
explicit RTImage (Cairo::RefPtr<Cairo::ImageSurface> &surf);
|
||||
explicit RTImage(Cairo::RefPtr<Cairo::ImageSurface> other);
|
||||
explicit RTImage (Glib::RefPtr<RTImage> &other);
|
||||
explicit RTImage(const Glib::RefPtr<const Gio::Icon> &gIcon, Gtk::IconSize size);
|
||||
explicit RTImage (const Glib::ustring& fileName, const Glib::ustring& rtlFileName = Glib::ustring());
|
||||
|
||||
void setImage (const Glib::ustring& fileName, const Glib::ustring& rtlFileName = Glib::ustring());
|
||||
void changeImage(const Glib::RefPtr<const Gio::Icon> &gIcon, Gtk::IconSize size);
|
||||
void changeImage (const Glib::ustring& imageName);
|
||||
Cairo::RefPtr<Cairo::ImageSurface> get_surface();
|
||||
int get_width();
|
||||
@ -58,6 +65,7 @@ public:
|
||||
static void setScale (const int newScale);
|
||||
|
||||
static Glib::RefPtr<Gdk::Pixbuf> createPixbufFromFile (const Glib::ustring& fileName);
|
||||
static Glib::RefPtr<Gdk::Pixbuf> createPixbufFromGIcon(const Glib::RefPtr<const Gio::Icon> &icon, int size);
|
||||
static Cairo::RefPtr<Cairo::ImageSurface> createImgSurfFromFile (const Glib::ustring& fileName);
|
||||
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user