From c3a160589a9cb41c70a56e5e7b66a65857a0d10e Mon Sep 17 00:00:00 2001 From: Eric Greveson Date: Mon, 1 Jul 2013 21:04:32 +0100 Subject: Factored layer model out into new Inkscape::LayerModel class. This allows Inkscape::Selection to use a LayerModel that is not associated with a UI. Changed the interface of verbs (SPAction) to use a new ActionContext rather than UI::View::View, again so that verbs may be used in a console mode. Modified boolean operation verbs to work in console-only mode. Fixed up DESKTOP_IS_ACTIVE macro to work in the case of no desktops. Modified main.cpp to process selections and verbs in no-GUI mode. Other changes are all consequences of the SPDesktop, Selection and LayerModel interface changes. (bzr r12387.1.1) --- src/helper/CMakeLists.txt | 2 ++ src/helper/Makefile_insert | 2 ++ src/helper/action.cpp | 36 +++++++++++++++++++++++++++--------- src/helper/action.h | 11 +++++++++-- 4 files changed, 40 insertions(+), 11 deletions(-) (limited to 'src/helper') diff --git a/src/helper/CMakeLists.txt b/src/helper/CMakeLists.txt index f709c7386..2e1e724f5 100644 --- a/src/helper/CMakeLists.txt +++ b/src/helper/CMakeLists.txt @@ -10,6 +10,7 @@ set(sp_marshal_SRC set(helper_SRC action.cpp + action-context.cpp geom.cpp geom-nodetype.cpp gnome-utils.cpp @@ -29,6 +30,7 @@ set(helper_SRC # ------- # Headers action.h + action-context.h geom-curves.h geom-nodetype.h geom.h diff --git a/src/helper/Makefile_insert b/src/helper/Makefile_insert index 2be40e0e0..790d87b14 100644 --- a/src/helper/Makefile_insert +++ b/src/helper/Makefile_insert @@ -5,6 +5,8 @@ helper/unit-menu.$(OBJEXT): helper/sp-marshal.h ink_common_sources += \ helper/action.cpp \ helper/action.h \ + helper/action-context.cpp \ + helper/action-context.h \ helper/geom.cpp \ helper/geom.h \ helper/geom-curves.h \ diff --git a/src/helper/action.cpp b/src/helper/action.cpp index 0e9957ca3..107d0179b 100644 --- a/src/helper/action.cpp +++ b/src/helper/action.cpp @@ -41,7 +41,7 @@ sp_action_init (SPAction *action) { action->sensitive = 0; action->active = 0; - action->view = NULL; + action->context = Inkscape::ActionContext(); action->id = action->name = action->tip = NULL; action->image = NULL; @@ -76,7 +76,7 @@ sp_action_finalize (GObject *object) * Create new SPAction object and set its properties. */ SPAction * -sp_action_new(Inkscape::UI::View::View *view, +sp_action_new(Inkscape::ActionContext const &context, const gchar *id, const gchar *name, const gchar *tip, @@ -85,7 +85,7 @@ sp_action_new(Inkscape::UI::View::View *view, { SPAction *action = (SPAction *)g_object_new(SP_TYPE_ACTION, NULL); - action->view = view; + action->context = context; action->sensitive = TRUE; action->id = g_strdup (id); action->name = g_strdup (name); @@ -111,11 +111,9 @@ public: : ActionEventBase(share_static_string("action")) { _addProperty(share_static_string("timestamp"), timestamp()); - if (action->view) { - SPDocument *document = action->view->doc(); - if (document) { - _addProperty(share_static_string("document"), document->serial()); - } + SPDocument *document = action->context.getDocument(); + if (document) { + _addProperty(share_static_string("document"), document->serial()); } _addProperty(share_static_string("verb"), action->id); } @@ -169,6 +167,26 @@ sp_action_set_name (SPAction *action, Glib::ustring const &name) action->signal_set_name.emit(name); } +/** + * Return Document associated with the action. + */ +SPDocument * +sp_action_get_document (SPAction *action) +{ + g_return_val_if_fail (SP_IS_ACTION (action), NULL); + return action->context.getDocument(); +} + +/** + * Return Selection associated with the action + */ +Inkscape::Selection * +sp_action_get_selection (SPAction *action) +{ + g_return_val_if_fail (SP_IS_ACTION (action), NULL); + return action->context.getSelection(); +} + /** * Return View associated with the action. */ @@ -176,7 +194,7 @@ Inkscape::UI::View::View * sp_action_get_view (SPAction *action) { g_return_val_if_fail (SP_IS_ACTION (action), NULL); - return action->view; + return action->context.getView(); } /* diff --git a/src/helper/action.h b/src/helper/action.h index 0cd010b34..49816e3c9 100644 --- a/src/helper/action.h +++ b/src/helper/action.h @@ -12,6 +12,7 @@ #ifndef SEEN_INKSCAPE_SP_ACTION_H #define SEEN_INKSCAPE_SP_ACTION_H +#include "helper/action-context.h" #include #include #include @@ -24,8 +25,12 @@ struct SPActionClass; #define SP_ACTION_CLASS(o) (G_TYPE_CHECK_CLASS_CAST((o), SP_TYPE_ACTION, SPActionClass)) #define SP_IS_ACTION(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), SP_TYPE_ACTION)) +class SPDocument; namespace Inkscape { + +class Selection; class Verb; + namespace UI { namespace View { class View; @@ -39,7 +44,7 @@ class View; struct SPAction : public GObject { unsigned sensitive : 1; /**< Value to track whether the action is sensitive */ unsigned active : 1; /**< Value to track whether the action is active */ - Inkscape::UI::View::View *view; /**< The View to which this action is attached */ + Inkscape::ActionContext context; /**< The context (doc/view) to which this action is attached */ gchar *id; /**< The identifier for the action */ gchar *name; /**< Full text name of the action */ gchar *tip; /**< A tooltip to describe the action */ @@ -59,7 +64,7 @@ struct SPActionClass { GType sp_action_get_type(); -SPAction *sp_action_new(Inkscape::UI::View::View *view, +SPAction *sp_action_new(Inkscape::ActionContext const &context, gchar const *id, gchar const *name, gchar const *tip, @@ -70,6 +75,8 @@ void sp_action_perform(SPAction *action, void *data); void sp_action_set_active(SPAction *action, unsigned active); void sp_action_set_sensitive(SPAction *action, unsigned sensitive); void sp_action_set_name(SPAction *action, Glib::ustring const &name); +SPDocument *sp_action_get_document(SPAction *action); +Inkscape::Selection *sp_action_get_selection(SPAction *action); Inkscape::UI::View::View *sp_action_get_view(SPAction *action); #endif -- cgit v1.2.3 From 6fe6a37589cbd6e53bf763879503ace16260e7a2 Mon Sep 17 00:00:00 2001 From: Eric Greveson Date: Mon, 1 Jul 2013 21:09:53 +0100 Subject: Added new files referenced in previous commit message (forgot to add!) (bzr r12387.1.2) --- src/helper/action-context.cpp | 74 ++++++++++++++++++++++++++++++++++++++++ src/helper/action-context.h | 78 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 src/helper/action-context.cpp create mode 100644 src/helper/action-context.h (limited to 'src/helper') diff --git a/src/helper/action-context.cpp b/src/helper/action-context.cpp new file mode 100644 index 000000000..c88086f7f --- /dev/null +++ b/src/helper/action-context.cpp @@ -0,0 +1,74 @@ +/* + * ActionContext implementation. + * + * Author: + * Eric Greveson + * + * Copyright (C) 2013 Eric Greveson + * + * This code is in public domain + */ + +#include "desktop.h" +#include "document.h" +#include "layer-model.h" +#include "selection.h" +#include "helper/action-context.h" +#include "ui/view/view.h" + +namespace Inkscape { + +ActionContext::ActionContext() + : _selection(NULL) + , _view(NULL) +{ +} + +ActionContext::ActionContext(Selection *selection) + : _selection(selection) + , _view(NULL) +{ +} + +ActionContext::ActionContext(UI::View::View *view) + : _selection(NULL) + , _view(view) +{ + SPDesktop *desktop = static_cast(view); + if (desktop) { + _selection = desktop->selection; + } +} + +SPDocument *ActionContext::getDocument() const +{ + if (_selection == NULL) { + return NULL; + } + + // Should be the same as the view's document, if view is non-NULL + return _selection->layerModel()->getDocument(); +} + +Selection *ActionContext::getSelection() const +{ + return _selection; +} + +UI::View::View *ActionContext::getView() const +{ + return _view; +} + +} // 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 : diff --git a/src/helper/action-context.h b/src/helper/action-context.h new file mode 100644 index 000000000..de09f94f4 --- /dev/null +++ b/src/helper/action-context.h @@ -0,0 +1,78 @@ +/** \file + * Inkscape UI action context implementation + *//* + * Author: + * Eric Greveson + * + * Copyright (C) 2013 Eric Greveson + * + * This code is in public domain + */ + +#ifndef SEEN_INKSCAPE_ACTION_CONTEXT_H +#define SEEN_INKSCAPE_ACTION_CONTEXT_H + +class SPDocument; + +namespace Inkscape { + +class Selection; + +namespace UI { +namespace View { +class View; +} // namespace View +} // namespace UI + +/** This structure contains all the document/view context required + for an action. Some actions may be executed on a document without + requiring a GUI, hence not providing the info directly through + Inkscape::UI::View::View. Actions that do require GUI objects should + check to see if the relevant pointers are NULL before attempting to + use them. + + ActionContext is designed to be copyable, so it may be used with stack + storage if required. */ +class ActionContext { + // NB: Only one of these is typically set - selection model if in console mode, view if in GUI mode + Selection *_selection; /**< The selection model to which this action applies, if running in console mode. May be NULL. */ + UI::View::View *_view; /**< The view to which this action applies. May be NULL (e.g. if running in console mode). */ + +public: + /** Construct without any document or GUI */ + ActionContext(); + + /** Construct an action context for when the app is being run without + any GUI, i.e. in console mode */ + ActionContext(Selection *selection); + + /** Construct an action context for when the app is being run in GUI mode */ + ActionContext(UI::View::View *view); + + /** Get the document for the action context. May be NULL. Prefer this + function to getView()->doc() if the action doesn't require a GUI. */ + SPDocument *getDocument() const; + + /** Get the selection for the action context. May be NULL. Should be + non-NULL if getDocument() is non-NULL. */ + Selection *getSelection() const; + + /** Get the view for the action context. May be NULL. Guaranteed to be + NULL if running in console mode. */ + UI::View::View *getView() const; +}; + +} // namespace Inkscape + +#endif + +/* + 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 From 09ce234c1fc367a2607936e6cf106cb24c60e94f Mon Sep 17 00:00:00 2001 From: Eric Greveson Date: Wed, 3 Jul 2013 20:06:11 +0100 Subject: Modified dbus interface so that it works in console mode (--dbus-listen) Modified action context setup so that in console mode, when a document is added to the main inkscape app instance, it gets a selection model and layer model automatically set up for it Made a couple more verbs work in console mode (bzr r12387.1.4) --- src/helper/action-context.cpp | 10 ++++++++++ src/helper/action-context.h | 11 +++++++++++ src/helper/action.cpp | 17 ++++++++++++++++- src/helper/action.h | 2 ++ 4 files changed, 39 insertions(+), 1 deletion(-) (limited to 'src/helper') diff --git a/src/helper/action-context.cpp b/src/helper/action-context.cpp index c88086f7f..f211d775d 100644 --- a/src/helper/action-context.cpp +++ b/src/helper/action-context.cpp @@ -60,6 +60,16 @@ UI::View::View *ActionContext::getView() const return _view; } +SPDesktop *ActionContext::getDesktop() const +{ + // TODO: this slightly horrible storage of a UI::View::View*, and + // casting to an SPDesktop*, is only done because that's what was + // already the norm in the Inkscape codebase. This seems wrong. Surely + // we should store an SPDesktop* in the first place? Is there a case + // of actions being carried out on a View that is not an SPDesktop? + return static_cast(_view); +} + } // namespace Inkscape /* diff --git a/src/helper/action-context.h b/src/helper/action-context.h index de09f94f4..3a7a19112 100644 --- a/src/helper/action-context.h +++ b/src/helper/action-context.h @@ -12,6 +12,7 @@ #ifndef SEEN_INKSCAPE_ACTION_CONTEXT_H #define SEEN_INKSCAPE_ACTION_CONTEXT_H +class SPDesktop; class SPDocument; namespace Inkscape { @@ -30,6 +31,12 @@ class View; Inkscape::UI::View::View. Actions that do require GUI objects should check to see if the relevant pointers are NULL before attempting to use them. + + TODO: we store a UI::View::View* because that's what the actions and verbs + used to take as parameters in their methods. Why is this? They almost + always seemed to cast straight to an SPDesktop* - so shouldn't we actually + be storing an SPDesktop*? Is there a case where a non-SPDesktop + UI::View::View is used by the actions? ActionContext is designed to be copyable, so it may be used with stack storage if required. */ @@ -60,6 +67,10 @@ public: /** Get the view for the action context. May be NULL. Guaranteed to be NULL if running in console mode. */ UI::View::View *getView() const; + + /** Get the desktop for the action context. May be NULL. Guaranteed to be + NULL if running in console mode. */ + SPDesktop *getDesktop() const; }; } // namespace Inkscape diff --git a/src/helper/action.cpp b/src/helper/action.cpp index 107d0179b..28cb40334 100644 --- a/src/helper/action.cpp +++ b/src/helper/action.cpp @@ -16,6 +16,7 @@ #include "debug/simple-event.h" #include "debug/event-tracker.h" #include "ui/view/view.h" +#include "desktop.h" #include "document.h" #include "helper/action.h" @@ -188,7 +189,7 @@ sp_action_get_selection (SPAction *action) } /** - * Return View associated with the action. + * Return View associated with the action, if any. */ Inkscape::UI::View::View * sp_action_get_view (SPAction *action) @@ -197,6 +198,20 @@ sp_action_get_view (SPAction *action) return action->context.getView(); } +/** + * Return Desktop associated with the action, if any. + */ +SPDesktop * +sp_action_get_desktop (SPAction *action) +{ + // TODO: this slightly horrible storage of a UI::View::View*, and + // casting to an SPDesktop*, is only done because that's what was + // already the norm in the Inkscape codebase. This seems wrong. Surely + // we should store an SPDesktop* in the first place? Is there a case + // of actions being carried out on a View that is not an SPDesktop? + return static_cast(sp_action_get_view(action)); +} + /* Local Variables: mode:c++ diff --git a/src/helper/action.h b/src/helper/action.h index 49816e3c9..1f2de87b4 100644 --- a/src/helper/action.h +++ b/src/helper/action.h @@ -25,6 +25,7 @@ struct SPActionClass; #define SP_ACTION_CLASS(o) (G_TYPE_CHECK_CLASS_CAST((o), SP_TYPE_ACTION, SPActionClass)) #define SP_IS_ACTION(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), SP_TYPE_ACTION)) +class SPDesktop; class SPDocument; namespace Inkscape { @@ -78,6 +79,7 @@ void sp_action_set_name(SPAction *action, Glib::ustring const &name); SPDocument *sp_action_get_document(SPAction *action); Inkscape::Selection *sp_action_get_selection(SPAction *action); Inkscape::UI::View::View *sp_action_get_view(SPAction *action); +SPDesktop *sp_action_get_desktop(SPAction *action); #endif -- cgit v1.2.3 From 104efe4e3ecadc975ab76748c66f041abf8ee7b1 Mon Sep 17 00:00:00 2001 From: Eric Greveson Date: Thu, 4 Jul 2013 15:01:44 +0100 Subject: Code readability improvements and licence changes for action-context.* based on merge request code review and feedback (bzr r12387.1.7) --- src/helper/action-context.cpp | 4 ++-- src/helper/action-context.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/helper') diff --git a/src/helper/action-context.cpp b/src/helper/action-context.cpp index f211d775d..d52e43d96 100644 --- a/src/helper/action-context.cpp +++ b/src/helper/action-context.cpp @@ -6,7 +6,7 @@ * * Copyright (C) 2013 Eric Greveson * - * This code is in public domain + * Released under GNU GPL, read the file 'COPYING' for more information */ #include "desktop.h" @@ -47,7 +47,7 @@ SPDocument *ActionContext::getDocument() const } // Should be the same as the view's document, if view is non-NULL - return _selection->layerModel()->getDocument(); + return _selection->layers()->getDocument(); } Selection *ActionContext::getSelection() const diff --git a/src/helper/action-context.h b/src/helper/action-context.h index 3a7a19112..bb538f413 100644 --- a/src/helper/action-context.h +++ b/src/helper/action-context.h @@ -6,7 +6,7 @@ * * Copyright (C) 2013 Eric Greveson * - * This code is in public domain + * Released under GNU GPL, read the file 'COPYING' for more information */ #ifndef SEEN_INKSCAPE_ACTION_CONTEXT_H -- cgit v1.2.3