From 42969f6dc9924dc1b604ac163fbf29ad60e12fdd Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Wed, 26 Sep 2018 22:44:51 -0400 Subject: Refactor addtoicon and turn into more generic iconrenderer CellRenderer --- src/ui/CMakeLists.txt | 4 +- src/ui/dialog/attrdialog.cpp | 7 ++- src/ui/dialog/attrdialog.h | 2 - src/ui/dialog/cssdialog.cpp | 9 ++-- src/ui/dialog/styledialog.cpp | 10 ++-- src/ui/dialog/tags.cpp | 17 +++--- src/ui/widget/addtoicon.cpp | 119 ----------------------------------------- src/ui/widget/addtoicon.h | 79 --------------------------- src/ui/widget/iconrenderer.cpp | 116 +++++++++++++++++++++++++++++++++++++++ src/ui/widget/iconrenderer.h | 78 +++++++++++++++++++++++++++ 10 files changed, 218 insertions(+), 223 deletions(-) delete mode 100644 src/ui/widget/addtoicon.cpp delete mode 100644 src/ui/widget/addtoicon.h create mode 100644 src/ui/widget/iconrenderer.cpp create mode 100644 src/ui/widget/iconrenderer.h diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 6d1ebb785..458125c59 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -142,7 +142,7 @@ set(ui_SRC dialog/xml-tree.cpp dialog/save-template-dialog.cpp - widget/addtoicon.cpp + widget/iconrenderer.cpp widget/alignment-selector.cpp widget/anchor-selector.cpp widget/button.cpp @@ -361,7 +361,7 @@ set(ui_SRC tools/tweak-tool.h tools/zoom-tool.h - widget/addtoicon.h + widget/iconrenderer.h widget/alignment-selector.h widget/anchor-selector.h widget/attr-widget.h diff --git a/src/ui/dialog/attrdialog.cpp b/src/ui/dialog/attrdialog.cpp index 3133ff279..0c00a6b76 100644 --- a/src/ui/dialog/attrdialog.cpp +++ b/src/ui/dialog/attrdialog.cpp @@ -16,7 +16,7 @@ #include "document-undo.h" #include "helper/icon-loader.h" -#include "ui/widget/addtoicon.h" +#include "ui/widget/iconrenderer.h" #include "xml/node-event-vector.h" #include "xml/attribute-record.h" @@ -65,14 +65,13 @@ AttrDialog::AttrDialog(): _store = Gtk::ListStore::create(_attrColumns); _treeView.set_model(_store); - Inkscape::UI::Widget::AddToIcon * addRenderer = manage(new Inkscape::UI::Widget::AddToIcon()); - addRenderer->property_active() = false; + Inkscape::UI::Widget::IconRenderer * addRenderer = manage(new Inkscape::UI::Widget::IconRenderer()); + addRenderer->add_icon("edit-delete"); _treeView.append_column("", *addRenderer); Gtk::TreeViewColumn *col = _treeView.get_column(0); if (col) { auto add_icon = Gtk::manage(sp_get_icon_image("list-add", GTK_ICON_SIZE_SMALL_TOOLBAR)); - col->add_attribute(addRenderer->property_active(), _attrColumns._colUnsetAttr); col->set_clickable(true); col->set_widget(*add_icon); add_icon->set_tooltip_text(_("Add a new attribute")); diff --git a/src/ui/dialog/attrdialog.h b/src/ui/dialog/attrdialog.h index 448d8a804..c2efb66fc 100644 --- a/src/ui/dialog/attrdialog.h +++ b/src/ui/dialog/attrdialog.h @@ -43,11 +43,9 @@ public: class AttrColumns : public Gtk::TreeModel::ColumnRecord { public: AttrColumns() { - add(_colUnsetAttr); add(_attributeName); add(_attributeValue); } - Gtk::TreeModelColumn _colUnsetAttr; Gtk::TreeModelColumn _attributeName; Gtk::TreeModelColumn _attributeValue; }; diff --git a/src/ui/dialog/cssdialog.cpp b/src/ui/dialog/cssdialog.cpp index 47922fc64..13903af70 100644 --- a/src/ui/dialog/cssdialog.cpp +++ b/src/ui/dialog/cssdialog.cpp @@ -17,7 +17,7 @@ #include "selection.h" #include "helper/icon-loader.h" -#include "ui/widget/addtoicon.h" +#include "ui/widget/iconrenderer.h" #include "xml/attribute-record.h" @@ -47,14 +47,11 @@ CssDialog::CssDialog(): _store = Gtk::ListStore::create(_cssColumns); _treeView.set_model(_store); - Inkscape::UI::Widget::AddToIcon * addRenderer = manage(new Inkscape::UI::Widget::AddToIcon()); - addRenderer->property_active() = false; + Inkscape::UI::Widget::IconRenderer * addRenderer = manage(new Inkscape::UI::Widget::IconRenderer()); + addRenderer->add_icon("edit-delete"); int addCol = _treeView.append_column("", *addRenderer) - 1; Gtk::TreeViewColumn *col = _treeView.get_column(addCol); - if (col) { - col->add_attribute(addRenderer->property_active(), _cssColumns._colUnsetProp); - } _propRenderer = Gtk::manage(new Gtk::CellRendererText()); _propRenderer->property_editable() = true; diff --git a/src/ui/dialog/styledialog.cpp b/src/ui/dialog/styledialog.cpp index e978667ef..2984ae11a 100644 --- a/src/ui/dialog/styledialog.cpp +++ b/src/ui/dialog/styledialog.cpp @@ -19,7 +19,7 @@ #include "document-undo.h" #include "helper/icon-loader.h" -#include "ui/widget/addtoicon.h" +#include "ui/widget/iconrenderer.h" #include "xml/attribute-record.h" #include "xml/node-observer.h" @@ -248,8 +248,10 @@ StyleDialog::StyleDialog() : #endif // Tree - Inkscape::UI::Widget::AddToIcon * addRenderer = manage( - new Inkscape::UI::Widget::AddToIcon() ); + Inkscape::UI::Widget::IconRenderer * addRenderer = manage( + new Inkscape::UI::Widget::IconRenderer() ); + addRenderer->add_icon("edit-delete"); + addRenderer->add_icon("list-add"); _store = TreeStore::create(this); _treeView.set_model(_store); @@ -260,7 +262,7 @@ StyleDialog::StyleDialog() : int addCol = _treeView.append_column("", *addRenderer) - 1; Gtk::TreeViewColumn *col = _treeView.get_column(addCol); if ( col ) { - col->add_attribute( addRenderer->property_active(), _mColumns._colIsSelector ); + col->add_attribute( addRenderer->property_icon(), _mColumns._colIsSelector ); } _treeView.append_column("CSS Selector", _mColumns._colSelector); _treeView.set_expander_column(*(_treeView.get_column(1))); diff --git a/src/ui/dialog/tags.cpp b/src/ui/dialog/tags.cpp index d27a320a7..bf191fd3e 100644 --- a/src/ui/dialog/tags.cpp +++ b/src/ui/dialog/tags.cpp @@ -31,7 +31,7 @@ #include "helper/icon-loader.h" #include "svg/css-ostringstream.h" #include "ui/tools/tool-base.h" //"event-context.h" -#include "ui/widget/addtoicon.h" +#include "ui/widget/iconrenderer.h" #include "ui/widget/layertypeicon.h" #include "verbs.h" #include "xml/node-observer.h" @@ -286,7 +286,7 @@ public: Gtk::TreeModelColumn _colParentObject; Gtk::TreeModelColumn _colObject; Gtk::TreeModelColumn _colLabel; - Gtk::TreeModelColumn _colAddRemove; + Gtk::TreeModelColumn _colAddRemove; Gtk::TreeModelColumn _colAllowAddRemove; }; @@ -391,7 +391,7 @@ void TagsPanel::_addObject( SPDocument* doc, SPObject* obj, Gtk::TreeModel::Row* row[_model->_colObject] = &child; row[_model->_colParentObject] = NULL; row[_model->_colLabel] = child.label() ? child.label() : child.getId(); - row[_model->_colAddRemove] = true; + row[_model->_colAddRemove] = 1; row[_model->_colAllowAddRemove] = true; _tree.expand_to_path( _store->get_path(iter) ); @@ -409,7 +409,7 @@ void TagsPanel::_addObject( SPDocument* doc, SPObject* obj, Gtk::TreeModel::Row* rowitems[_model->_colObject] = NULL; rowitems[_model->_colParentObject] = obj; rowitems[_model->_colLabel] = _("Items"); - rowitems[_model->_colAddRemove] = false; + rowitems[_model->_colAddRemove] = 0; rowitems[_model->_colAllowAddRemove] = false; _tree.expand_to_path( _store->get_path(iteritems) ); @@ -423,7 +423,7 @@ void TagsPanel::_addObject( SPDocument* doc, SPObject* obj, Gtk::TreeModel::Row* row[_model->_colObject] = &child; row[_model->_colParentObject] = NULL; row[_model->_colLabel] = item ? (item->label() ? item->label() : item->getId()) : SP_TAG_USE(&child)->href; - row[_model->_colAddRemove] = false; + row[_model->_colAddRemove] = 0; row[_model->_colAllowAddRemove] = true; if (SP_TAG(obj)->expanded()) { @@ -933,11 +933,14 @@ TagsPanel::TagsPanel() : tooltip_string += (_("Remove from selection set")); _tree.set_tooltip_text( tooltip_string ); - Inkscape::UI::Widget::AddToIcon * addRenderer = manage( new Inkscape::UI::Widget::AddToIcon()); + Inkscape::UI::Widget::IconRenderer * addRenderer = manage( new Inkscape::UI::Widget::IconRenderer()); + addRenderer->add_icon("edit-delete"); + addRenderer->add_icon("list-add"); + int addColNum = _tree.append_column("type", *addRenderer) - 1; Gtk::TreeViewColumn *col = _tree.get_column(addColNum); if ( col ) { - col->add_attribute( addRenderer->property_active(), _model->_colAddRemove ); + col->add_attribute( addRenderer->property_icon(), _model->_colAddRemove ); col->add_attribute( addRenderer->property_visible(), _model->_colAllowAddRemove ); } diff --git a/src/ui/widget/addtoicon.cpp b/src/ui/widget/addtoicon.cpp deleted file mode 100644 index 4542333b5..000000000 --- a/src/ui/widget/addtoicon.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Authors: - * Theodore Janeczko - * - * Copyright (C) Theodore Janeczko 2012 - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ - - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include "ui/widget/addtoicon.h" - -#include "helper/icon-loader.h" -#include "layertypeicon.h" -#include "ui/icon-names.h" -#include "widgets/toolbox.h" - -namespace Inkscape { -namespace UI { -namespace Widget { - -AddToIcon::AddToIcon() : - Glib::ObjectBase(typeid(AddToIcon)), - Gtk::CellRendererPixbuf(), -// _pixAddName(INKSCAPE_ICON("layer-new")), - _property_active(*this, "active", false) -// _property_pixbuf_add(*this, "pixbuf_on", Glib::RefPtr(0)) -{ - property_mode() = Gtk::CELL_RENDERER_MODE_ACTIVATABLE; - - -// Glib::RefPtr icon_theme = Gtk::IconTheme::get_default(); -// -// if (!icon_theme->has_icon(_pixAddName)) { -// Inkscape::queueIconPrerender( INKSCAPE_ICON(_pixAddName.data()), Inkscape::ICON_SIZE_DECORATION ); -// } -// if (icon_theme->has_icon(_pixAddName)) { -// _property_pixbuf_add = icon_theme->load_icon(_pixAddName, phys, (Gtk::IconLookupFlags)0); -// } - - set_pixbuf(); -} - -void AddToIcon::get_preferred_height_vfunc(Gtk::Widget& widget, - int& min_h, - int& nat_h) const -{ - Gtk::CellRendererPixbuf::get_preferred_height_vfunc(widget, min_h, nat_h); - - if (min_h) { - min_h += (min_h) >> 1; - } - - if (nat_h) { - nat_h += (nat_h) >> 1; - } -} - -void AddToIcon::get_preferred_width_vfunc(Gtk::Widget& widget, - int& min_w, - int& nat_w) const -{ - Gtk::CellRendererPixbuf::get_preferred_width_vfunc(widget, min_w, nat_w); - - if (min_w) { - min_w += (min_w) >> 1; - } - - if (nat_w) { - nat_w += (nat_w) >> 1; - } -} - -void AddToIcon::render_vfunc( const Cairo::RefPtr& cr, - Gtk::Widget& widget, - const Gdk::Rectangle& background_area, - const Gdk::Rectangle& cell_area, - Gtk::CellRendererState flags ) -{ - set_pixbuf(); - - Gtk::CellRendererPixbuf::render_vfunc( cr, widget, background_area, cell_area, flags ); -} - -bool AddToIcon::activate_vfunc(GdkEvent* /*event*/, - Gtk::Widget& /*widget*/, - const Glib::ustring& /*path*/, - const Gdk::Rectangle& /*background_area*/, - const Gdk::Rectangle& /*cell_area*/, - Gtk::CellRendererState /*flags*/) -{ - return false; -} - -void AddToIcon::set_pixbuf() -{ - bool active = property_active().get_value(); - property_pixbuf() = sp_get_icon_pixbuf((active ? "list-add" : "edit-delete"), GTK_ICON_SIZE_BUTTON); -} - - -} // namespace Widget -} // namespace UI -} // namespace Inkscape - -/* - Local Variables: - mode:c++ - c-file-style:"stroustrup" - c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) - indent-tabs-mode:nil - fill-column:99 - End: -*/ -// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/ui/widget/addtoicon.h b/src/ui/widget/addtoicon.h deleted file mode 100644 index 2cfcda958..000000000 --- a/src/ui/widget/addtoicon.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef __UI_DIALOG_ADDTOICON_H__ -#define __UI_DIALOG_ADDTOICON_H__ -/* - * Authors: - * Theodore Janeczko - * - * Copyright (C) Theodore Janeczko 2012 - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ - -#include -#include -#include - -namespace Inkscape { -namespace UI { -namespace Widget { - -class AddToIcon : public Gtk::CellRendererPixbuf { -public: - AddToIcon(); - ~AddToIcon() override = default;; - - Glib::PropertyProxy property_active() { return _property_active.get_proxy(); } - Glib::PropertyProxy< Glib::RefPtr > property_pixbuf_on(); - Glib::PropertyProxy< Glib::RefPtr > property_pixbuf_off(); - -protected: - void render_vfunc( const Cairo::RefPtr& cr, - Gtk::Widget& widget, - const Gdk::Rectangle& background_area, - const Gdk::Rectangle& cell_area, - Gtk::CellRendererState flags ) override; - - void get_preferred_width_vfunc(Gtk::Widget& widget, - int& min_w, - int& nat_w) const override; - - void get_preferred_height_vfunc(Gtk::Widget& widget, - int& min_h, - int& nat_h) const override; - - bool activate_vfunc(GdkEvent *event, - Gtk::Widget &widget, - const Glib::ustring &path, - const Gdk::Rectangle &background_area, - const Gdk::Rectangle &cell_area, - Gtk::CellRendererState flags) override; - - -private: - -// Glib::ustring _pixAddName; - - Glib::Property _property_active; -// Glib::Property< Glib::RefPtr > _property_pixbuf_add; - void set_pixbuf(); -}; - - - -} // namespace Widget -} // namespace UI -} // namespace Inkscape - - -#endif /* __UI_DIALOG_IMAGETOGGLER_H__ */ - -/* - Local Variables: - mode:c++ - c-file-style:"stroustrup" - c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) - indent-tabs-mode:nil - fill-column:99 - End: -*/ -// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/ui/widget/iconrenderer.cpp b/src/ui/widget/iconrenderer.cpp new file mode 100644 index 000000000..9826d0bd3 --- /dev/null +++ b/src/ui/widget/iconrenderer.cpp @@ -0,0 +1,116 @@ +/* + * Authors: + * Theodore Janeczko + * + * Copyright (C) Theodore Janeczko 2012 + * Martin Owens 2018 + * + * Released under GNU GPLv2+, read the file 'COPYING' for more information + */ + + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "ui/widget/iconrenderer.h" + +#include "helper/icon-loader.h" +#include "layertypeicon.h" +#include "ui/icon-names.h" +#include "widgets/toolbox.h" + +namespace Inkscape { +namespace UI { +namespace Widget { + +IconRenderer::IconRenderer() : + Glib::ObjectBase(typeid(IconRenderer)), + Gtk::CellRendererPixbuf(), + _property_icon(*this, "icon", 0) +{ + property_mode() = Gtk::CELL_RENDERER_MODE_ACTIVATABLE; + set_pixbuf(); +} + +void IconRenderer::get_preferred_height_vfunc(Gtk::Widget& widget, + int& min_h, + int& nat_h) const +{ + Gtk::CellRendererPixbuf::get_preferred_height_vfunc(widget, min_h, nat_h); + + if (min_h) { + min_h += (min_h) >> 1; + } + + if (nat_h) { + nat_h += (nat_h) >> 1; + } +} + +void IconRenderer::get_preferred_width_vfunc(Gtk::Widget& widget, + int& min_w, + int& nat_w) const +{ + Gtk::CellRendererPixbuf::get_preferred_width_vfunc(widget, min_w, nat_w); + + if (min_w) { + min_w += (min_w) >> 1; + } + + if (nat_w) { + nat_w += (nat_w) >> 1; + } +} + +void IconRenderer::render_vfunc( const Cairo::RefPtr& cr, + Gtk::Widget& widget, + const Gdk::Rectangle& background_area, + const Gdk::Rectangle& cell_area, + Gtk::CellRendererState flags ) +{ + set_pixbuf(); + + Gtk::CellRendererPixbuf::render_vfunc( cr, widget, background_area, cell_area, flags ); +} + +bool IconRenderer::activate_vfunc(GdkEvent* /*event*/, + Gtk::Widget& /*widget*/, + const Glib::ustring& /*path*/, + const Gdk::Rectangle& /*background_area*/, + const Gdk::Rectangle& /*cell_area*/, + Gtk::CellRendererState /*flags*/) +{ + return false; +} + +void IconRenderer::add_icon(Glib::ustring name) +{ + _icons.push_back(name); +} + +void IconRenderer::set_pixbuf() +{ + int icon_index = property_icon().get_value(); + auto icon_name = Glib::ustring("image-missing"); + if(icon_index >= 0 && icon_index < _icons.size()) { + icon_name = _icons[icon_index]; + } + property_pixbuf() = sp_get_icon_pixbuf(icon_name.c_str(), GTK_ICON_SIZE_BUTTON); +} + + +} // namespace Widget +} // namespace UI +} // namespace Inkscape + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/ui/widget/iconrenderer.h b/src/ui/widget/iconrenderer.h new file mode 100644 index 000000000..1f5aafde9 --- /dev/null +++ b/src/ui/widget/iconrenderer.h @@ -0,0 +1,78 @@ +#ifndef __UI_DIALOG_ADDTOICON_H__ +#define __UI_DIALOG_ADDTOICON_H__ +/* + * Authors: + * Theodore Janeczko + * + * Copyright (C) Theodore Janeczko 2012 + * Martin Owens 2018 + * + * Released under GNU GPLv2+, read the file 'COPYING' for more information + */ + +#include +#include +#include + +namespace Inkscape { +namespace UI { +namespace Widget { + +class IconRenderer : public Gtk::CellRendererPixbuf { +public: + IconRenderer(); + ~IconRenderer() override = default;; + + Glib::PropertyProxy property_icon() { return _property_icon.get_proxy(); } + Glib::PropertyProxy< Glib::RefPtr > property_pixbuf_on(); + Glib::PropertyProxy< Glib::RefPtr > property_pixbuf_off(); + + void add_icon(Glib::ustring name); +protected: + void render_vfunc( const Cairo::RefPtr& cr, + Gtk::Widget& widget, + const Gdk::Rectangle& background_area, + const Gdk::Rectangle& cell_area, + Gtk::CellRendererState flags ) override; + + void get_preferred_width_vfunc(Gtk::Widget& widget, + int& min_w, + int& nat_w) const override; + + void get_preferred_height_vfunc(Gtk::Widget& widget, + int& min_h, + int& nat_h) const override; + + bool activate_vfunc(GdkEvent *event, + Gtk::Widget &widget, + const Glib::ustring &path, + const Gdk::Rectangle &background_area, + const Gdk::Rectangle &cell_area, + Gtk::CellRendererState flags) override; + +private: + + Glib::Property _property_icon; + std::vector _icons; + void set_pixbuf(); +}; + + + +} // namespace Widget +} // namespace UI +} // namespace Inkscape + + +#endif /* __UI_DIALOG_IMAGETOGGLER_H__ */ + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : -- cgit v1.2.3