diff options
| author | Patrick Storz <eduard.braun2@gmx.de> | 2019-08-11 20:12:00 +0000 |
|---|---|---|
| committer | Patrick Storz <eduard.braun2@gmx.de> | 2019-08-31 14:50:39 +0000 |
| commit | f7414de1bfeb11007f6cb02ce730c38b660bcc73 (patch) | |
| tree | 2258e60484b5a8cde5095d149b6d152b17085426 /src/extension | |
| parent | Add base_directory for extensions loaded from a file. (diff) | |
| download | inkscape-f7414de1bfeb11007f6cb02ce730c38b660bcc73.tar.gz inkscape-f7414de1bfeb11007f6cb02ce730c38b660bcc73.zip | |
Add new widget "image" which allows to display an image file
* The node's content is the file path.
Absolute paths should work, the preferred way is to specify a
relative path, though, which will be interpreted relatively to the
.inx file's location
* The attributes "width/height" allows to override the native size
of the image.
Diffstat (limited to 'src/extension')
| -rw-r--r-- | src/extension/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/extension/prefdialog/widget-image.cpp | 87 | ||||
| -rw-r--r-- | src/extension/prefdialog/widget-image.h | 61 | ||||
| -rw-r--r-- | src/extension/prefdialog/widget.cpp | 6 |
4 files changed, 155 insertions, 1 deletions
diff --git a/src/extension/CMakeLists.txt b/src/extension/CMakeLists.txt index 032b9cd4c..be8ee38f1 100644 --- a/src/extension/CMakeLists.txt +++ b/src/extension/CMakeLists.txt @@ -63,6 +63,7 @@ set(extension_SRC prefdialog/parameter-string.cpp prefdialog/widget.cpp prefdialog/widget-box.cpp + prefdialog/widget-image.cpp prefdialog/widget-label.cpp prefdialog/widget-separator.cpp prefdialog/widget-spacer.cpp @@ -141,6 +142,7 @@ set(extension_SRC prefdialog/parameter-string.h prefdialog/widget.h prefdialog/widget-box.h + prefdialog/widget-image.h prefdialog/widget-label.h prefdialog/widget-separator.h prefdialog/widget-spacer.h diff --git a/src/extension/prefdialog/widget-image.cpp b/src/extension/prefdialog/widget-image.cpp new file mode 100644 index 000000000..4f5e11dec --- /dev/null +++ b/src/extension/prefdialog/widget-image.cpp @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * Image widget for extensions + *//* + * Authors: + * Patrick Storz <eduard.braun2@gmx.de> + * + * Copyright (C) 2019 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "widget-image.h" + +#include <glibmm/fileutils.h> +#include <glibmm/miscutils.h> +#include <gtkmm/image.h> + +#include "xml/node.h" +#include "extension/extension.h" + +namespace Inkscape { +namespace Extension { + + +WidgetImage::WidgetImage(Inkscape::XML::Node *xml, Inkscape::Extension::Extension *ext) + : InxWidget(xml, ext) +{ + std::string image_path; + + // get path to image + const char *content = nullptr; + if (xml->firstChild()) { + content = xml->firstChild()->content(); + } + if (content) { + image_path = content; + } else { + g_warning("Missing path for image widget in extension '%s'.", _extension->get_id()); + return; + } + + // make sure path is absolute (relative paths are relative to .inx file's location) + if (!Glib::path_is_absolute(image_path)) { + image_path = Glib::build_filename(_extension->get_base_directory(), image_path); + } + + // check if image exists + if (Glib::file_test(image_path, Glib::FILE_TEST_IS_REGULAR)) { + _image_path = image_path; + } else { + g_warning("Image file ('%s') not found for image widget in extension '%s'.", + image_path.c_str(), _extension->get_id()); + } + + // parse width/height attributes + const char *width = xml->attribute("width"); + const char *height = xml->attribute("height"); + if (width && height) { + _width = strtoul(width, nullptr, 0); + _height = strtoul(height, nullptr, 0); + } +} + +/** \brief Create a label for the description */ +Gtk::Widget *WidgetImage::get_widget(sigc::signal<void> * /*changeSignal*/) +{ + if (_hidden || _image_path.empty()) { + return nullptr; + } + + Gtk::Image *image = Gtk::manage(new Gtk::Image(_image_path)); + + // resize if requested + if (_width && _height) { + Glib::RefPtr<Gdk::Pixbuf> pixbuf = image->get_pixbuf(); + pixbuf = pixbuf->scale_simple(_width, _height, Gdk::INTERP_BILINEAR); + image->set(pixbuf); + } + + image->show(); + + return dynamic_cast<Gtk::Widget *>(image); +} + +} /* namespace Extension */ +} /* namespace Inkscape */ diff --git a/src/extension/prefdialog/widget-image.h b/src/extension/prefdialog/widget-image.h new file mode 100644 index 000000000..95dd4d5d1 --- /dev/null +++ b/src/extension/prefdialog/widget-image.h @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * Image widget for extensions + *//* + * Authors: + * Patrick Storz <eduard.braun2@gmx.de> + * + * Copyright (C) 2019 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INK_EXTENSION_WIDGET_IMAGE_H +#define SEEN_INK_EXTENSION_WIDGET_IMAGE_H + +#include "widget.h" + +#include <string> + +namespace Gtk { + class Widget; +} + +namespace Inkscape { +namespace Xml { + class Node; +} + +namespace Extension { + +/** \brief A label widget */ +class WidgetImage : public InxWidget { +public: + WidgetImage(Inkscape::XML::Node *xml, Inkscape::Extension::Extension *ext); + + Gtk::Widget *get_widget(sigc::signal<void> *changeSignal) override; +private: + /** \brief Path to image file (relative paths are relative to the .inx file location). */ + std::string _image_path; + + /** desired width of image when rendered on screen (in px) */ + unsigned int _width = 0; + /** desired height of image when rendered on screen (in px) */ + unsigned int _height = 0; +}; + +} /* namespace Extension */ +} /* namespace Inkscape */ + +#endif /* SEEN_INK_EXTENSION_WIDGET_IMAGE_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 : diff --git a/src/extension/prefdialog/widget.cpp b/src/extension/prefdialog/widget.cpp index da4b6545d..d4f33f7af 100644 --- a/src/extension/prefdialog/widget.cpp +++ b/src/extension/prefdialog/widget.cpp @@ -13,6 +13,7 @@ #include "parameter.h" #include "widget.h" #include "widget-box.h" +#include "widget-image.h" #include "widget-label.h" #include "widget-separator.h" #include "widget-spacer.h" @@ -48,6 +49,8 @@ InxWidget *InxWidget::make(Inkscape::XML::Node *in_repr, Inkscape::Extension::Ex g_warning("InxWidget without name in extension '%s'.", in_ext->get_id()); } else if (!strcmp(name, "hbox") || !strcmp(name, "vbox")) { widget = new WidgetBox(in_repr, in_ext); + } else if (!strcmp(name, "image")) { + widget = new WidgetImage(in_repr, in_ext); } else if (!strcmp(name, "label")) { widget = new WidgetLabel(in_repr, in_ext); } else if (!strcmp(name, "separator")) { @@ -67,7 +70,8 @@ InxWidget *InxWidget::make(Inkscape::XML::Node *in_repr, Inkscape::Extension::Ex bool InxWidget::is_valid_widget_name(const char *name) { // keep in sync with names supported in InxWidget::make() above - static const std::vector<std::string> valid_names = {"hbox", "vbox", "label", "separator", "spacer", "param"}; + static const std::vector<std::string> valid_names = + {"hbox", "vbox", "image", "label", "separator", "spacer", "param"}; if (std::find(valid_names.begin(), valid_names.end(), name) != valid_names.end()) { return true; |
