diff options
| author | Thomas Holder <thomas@thomas-holder.de> | 2018-12-14 10:05:39 +0000 |
|---|---|---|
| committer | Thomas Holder <thomas@thomas-holder.de> | 2018-12-14 10:05:39 +0000 |
| commit | 91112f9d55017b82caa02a3c4673a8420a361801 (patch) | |
| tree | 6f72e874054ed63bfbab5a2b9e1f34b56d842c53 /src | |
| parent | extract_uri: fix, test, document (diff) | |
| download | inkscape-91112f9d55017b82caa02a3c4673a8420a361801.tar.gz inkscape-91112f9d55017b82caa02a3c4673a8420a361801.zip | |
DocumentUndo::ScopedInsensitive
Diffstat (limited to 'src')
| -rw-r--r-- | src/conn-avoid-ref.cpp | 5 | ||||
| -rw-r--r-- | src/document-undo.cpp | 7 | ||||
| -rw-r--r-- | src/document-undo.h | 37 | ||||
| -rw-r--r-- | src/document.cpp | 5 | ||||
| -rw-r--r-- | src/extension/system.cpp | 12 | ||||
| -rw-r--r-- | src/widgets/gradient-vector.cpp | 4 | ||||
| -rw-r--r-- | src/widgets/toolbox.cpp | 5 |
7 files changed, 32 insertions, 43 deletions
diff --git a/src/conn-avoid-ref.cpp b/src/conn-avoid-ref.cpp index 087bd93fa..32d564fe6 100644 --- a/src/conn-avoid-ref.cpp +++ b/src/conn-avoid-ref.cpp @@ -364,8 +364,7 @@ void init_avoided_shape_geometry(SPDesktop *desktop) // Don't count this as changes to the document, // it is basically just late initialisation. SPDocument *document = desktop->getDocument(); - bool saved = DocumentUndo::getUndoSensitive(document); - DocumentUndo::setUndoSensitive(document, false); + DocumentUndo::ScopedInsensitive _no_undo(document); bool initialised = false; std::vector<SPItem *> tmp; @@ -376,8 +375,6 @@ void init_avoided_shape_geometry(SPDesktop *desktop) SPItem *item = *iter; item->avoidRef->handleSettingChange(); } - - DocumentUndo::setUndoSensitive(document, saved); } diff --git a/src/document-undo.cpp b/src/document-undo.cpp index 9753c4970..d41771f06 100644 --- a/src/document-undo.cpp +++ b/src/document-undo.cpp @@ -78,13 +78,6 @@ void Inkscape::DocumentUndo::setUndoSensitive(SPDocument *doc, bool sensitive) doc->sensitive = sensitive; } -/*TODO: Throughout the inkscape code tree set/get_undo_sensitive are used for - * as is shown above. Perhaps it makes sense to create new functions, - * undo_ignore, and undo_recall to replace the start and end parts of the above. - * The main complexity with this is that they have to nest, so you have to store - * the saved bools in a stack. Perhaps this is why the above solution is better. - */ - bool Inkscape::DocumentUndo::getUndoSensitive(SPDocument const *document) { g_assert(document != nullptr); diff --git a/src/document-undo.h b/src/document-undo.h index 1651bcfba..5ed9db8f6 100644 --- a/src/document-undo.h +++ b/src/document-undo.h @@ -14,11 +14,7 @@ namespace Glib { class ustring; } -typedef struct _GObject GObject; - -class SPDesktop; class SPDocument; -struct InkscapeApplication; namespace Inkscape { @@ -29,14 +25,7 @@ public: /** * Set undo sensitivity. * - * \note - * Since undo sensitivity needs to be nested, setting undo sensitivity - * should be done like this: - *\verbatim - bool saved = DocumentUndo::getUndoSensitive(document); - DocumentUndo::setUndoSensitive(document, false); - ... do stuff ... - DocumentUndo::setUndoSensitive(document, saved); \endverbatim + * Don't use this to temporarily turn sensitivity off, use ScopedInsensitive instead. */ static void setUndoSensitive(SPDocument *doc, bool sensitive); @@ -57,6 +46,30 @@ public: static gboolean undo(SPDocument *document); static gboolean redo(SPDocument *document); + + /** + * RAII-style mechanism for creating a temporary undo-insensitive context. + * + * \verbatim + { + DocumentUndo::ScopedInsensitive tmp(document); + ... do stuff ... + // "tmp" goes out of scope here and automatically restores undo-sensitivity + } \endverbatim + */ + class ScopedInsensitive { + SPDocument * m_doc; + bool m_saved; + + public: + ScopedInsensitive(SPDocument *doc) + : m_doc(doc) + { + m_saved = getUndoSensitive(doc); + setUndoSensitive(doc, false); + } + ~ScopedInsensitive() { setUndoSensitive(m_doc, m_saved); } + }; }; } // namespace Inkscape diff --git a/src/document.cpp b/src/document.cpp index a351957fd..753de44da 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -1276,12 +1276,9 @@ SPDocument::_updateDocument() SPItemCtx ctx; setupViewport(&ctx); - bool saved = DocumentUndo::getUndoSensitive(this); - DocumentUndo::setUndoSensitive(this, false); + DocumentUndo::ScopedInsensitive _no_undo(this); this->root->updateDisplay((SPCtx *)&ctx, 0); - - DocumentUndo::setUndoSensitive(this, saved); } this->_emitModified(); } diff --git a/src/extension/system.cpp b/src/extension/system.cpp index 02f6f8177..ea486ed80 100644 --- a/src/extension/system.cpp +++ b/src/extension/system.cpp @@ -301,9 +301,8 @@ save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, // Update attributes: { - bool const saved = DocumentUndo::getUndoSensitive(doc); - DocumentUndo::setUndoSensitive(doc, false); { + DocumentUndo::ScopedInsensitive _no_undo(doc); // also save the extension for next use store_file_extension_in_prefs (omod->get_id(), save_method); // set the "dataloss" attribute if the chosen extension is lossy @@ -312,7 +311,6 @@ save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, repr->setAttribute("inkscape:dataloss", "true"); } } - DocumentUndo::setUndoSensitive(doc, saved); doc->setModifiedSinceSave(false); } @@ -322,13 +320,11 @@ save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, catch(...) { // revert attributes in case of official and overwrite if(check_overwrite && official) { - bool const saved = DocumentUndo::getUndoSensitive(doc); - DocumentUndo::setUndoSensitive(doc, false); { + DocumentUndo::ScopedInsensitive _no_undo(doc); store_file_extension_in_prefs (saved_output_extension, save_method); repr->setAttribute("inkscape:dataloss", saved_dataloss); } - DocumentUndo::setUndoSensitive(doc, saved); doc->changeUriAndHrefs(saved_uri); } doc->setModifiedSinceSave(saved_modified); @@ -344,13 +340,11 @@ save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, // If it is an unofficial save, set the modified attributes back to what they were. if ( !official) { - bool const saved = DocumentUndo::getUndoSensitive(doc); - DocumentUndo::setUndoSensitive(doc, false); { + DocumentUndo::ScopedInsensitive _no_undo(doc); store_file_extension_in_prefs (saved_output_extension, save_method); repr->setAttribute("inkscape:dataloss", saved_dataloss); } - DocumentUndo::setUndoSensitive(doc, saved); doc->setModifiedSinceSave(saved_modified); g_free(saved_output_extension); diff --git a/src/widgets/gradient-vector.cpp b/src/widgets/gradient-vector.cpp index 038835956..d1315f4c5 100644 --- a/src/widgets/gradient-vector.cpp +++ b/src/widgets/gradient-vector.cpp @@ -1095,10 +1095,8 @@ static void sp_gradient_vector_widget_load_gradient(GtkWidget *widget, SPGradien // Once the user edits a gradient, it stops being auto-collectable if (gradient->getRepr()->attribute("inkscape:collect")) { SPDocument *document = gradient->document; - bool saved = DocumentUndo::getUndoSensitive(document); - DocumentUndo::setUndoSensitive(document, false); + DocumentUndo::ScopedInsensitive _no_undo(document); gradient->getRepr()->setAttribute("inkscape:collect", nullptr); - DocumentUndo::setUndoSensitive(document, saved); } } else { // no gradient, disable everything gtk_widget_set_sensitive(widget, FALSE); diff --git a/src/widgets/toolbox.cpp b/src/widgets/toolbox.cpp index 0a7c4c79c..38ab84771 100644 --- a/src/widgets/toolbox.cpp +++ b/src/widgets/toolbox.cpp @@ -1136,8 +1136,7 @@ static void toggle_snap_callback(GtkToggleAction *act, gpointer data) //data poi return; } - bool saved = DocumentUndo::getUndoSensitive(doc); - DocumentUndo::setUndoSensitive(doc, false); + DocumentUndo::ScopedInsensitive _no_undo(doc); bool v = false; SPAttributeEnum attr = (SPAttributeEnum) GPOINTER_TO_INT(g_object_get_data(G_OBJECT(act), "SP_ATTR_INKSCAPE")); @@ -1233,8 +1232,6 @@ static void toggle_snap_callback(GtkToggleAction *act, gpointer data) //data poi // The snapping preferences are stored in the document, and therefore toggling makes the document dirty doc->setModifiedSinceSave(); - - DocumentUndo::setUndoSensitive(doc, saved); } void setup_snap_toolbox(GtkWidget *toolbox, SPDesktop *desktop) |
