Code

r16407@tres: ted | 2007-08-29 22:33:35 -0700
[inkscape.git] / src / extension / execution-env.cpp
1 /*
2  * Authors:
3  *   Ted Gould <ted@gould.cx>
4  *
5  * Copyright (C) 2007 Authors
6  *
7  * Released under GNU GPL, read the file 'COPYING' for more information
8  */
10 #include <config.h>
12 #include "gtkmm/messagedialog.h"
14 #include "execution-env.h"
15 #include "prefdialog.h"
16 #include "implementation/implementation.h"
18 #include "selection.h"
19 #include "effect.h"
20 #include "document.h"
21 #include "ui/view/view.h"
22 #include "sp-namedview.h"
23 #include "desktop-handles.h"
25 #include "util/glib-list-iterators.h"
27 namespace Inkscape {
28 namespace Extension {
31 ExecutionEnv::ExecutionEnv (Effect * effect, Inkscape::UI::View::View * doc, Gtk::Widget * controls, sigc::signal<void> * changeSignal, Gtk::Dialog * prefDialog) :
32     _visibleDialog(NULL),
33     _effect(effect),
34     _prefsVisible(false),
35     _finished(false),
36     _humanWait(false),
37     _canceled(false),
38     _prefsChanged(false),
39     _livePreview(true),
40     _selfdelete(false),
41     _changeSignal(changeSignal),
42     _doc(doc) {
44     SPDesktop *desktop = (SPDesktop *)_doc;
45     sp_namedview_document_from_window(desktop);
47     if (desktop != NULL) {
48         Inkscape::Util::GSListConstIterator<SPItem *> selected =
49              sp_desktop_selection(desktop)->itemList();
50         while ( selected != NULL ) {
51             Glib::ustring selected_id;
52             selected_id = SP_OBJECT_ID(*selected);
53             _selected.insert(_selected.end(), selected_id);
54             //std::cout << "Selected: " << selected_id << std::endl;
55             ++selected;
56         }
57     }
59     _mainloop = Glib::MainLoop::create(false);
61     if (prefDialog == NULL) {
62         if (controls != NULL) {
63             createPrefsDialog(controls);
64         } else {
65             createWorkingDialog();
66         }
67     } else {
68         _visibleDialog = prefDialog;
69         _prefsVisible = true;
70     }
71     
72     if (_changeSignal != NULL) {
73         _changesig = _changeSignal->connect(sigc::mem_fun(this, &ExecutionEnv::preferencesChange));
74     }
76     return;
77 }
79 ExecutionEnv::~ExecutionEnv (void) {
80     _dialogsig.disconnect();
81     if (_prefsVisible) {
82         _changesig.disconnect();
83     }
84     if (_visibleDialog != NULL && !_shutdown) {
85         delete _visibleDialog;
86     }
87     if (_changeSignal != NULL && !_shutdown) {
88         delete _changeSignal;
89     }
90     return;
91 }
93 void
94 ExecutionEnv::preferencesChange (void) {
95     //std::cout << "Preferences are a changin'" << std::endl;
96     _prefsChanged = true;
97     if (_humanWait) {
98         _mainloop->quit();
99         documentCancel();
100         _humanWait = false;
101     } else {
102         processingCancel();
103         documentCancel();
104     }
105     return;
108 void
109 ExecutionEnv::createPrefsDialog (Gtk::Widget * controls) {
110     _visibleDialog = new PrefDialog(_effect->get_name(), _effect->get_help(), controls, this, _effect, _changeSignal);
111     _visibleDialog->signal_response().connect(sigc::mem_fun(this, &ExecutionEnv::preferencesResponse));
112     _visibleDialog->show();
113     _dialogsig = _visibleDialog->signal_response().connect(sigc::mem_fun(this, &ExecutionEnv::preferencesResponse));
115     _prefsVisible = true;
116     return;
119 void
120 ExecutionEnv::createWorkingDialog (void) {
121     if (_visibleDialog != NULL) {
122         delete _visibleDialog;
123     }
124     if (_changeSignal != NULL) {
125         delete _changeSignal;
126         _changeSignal = NULL;
127     }
129     gchar * dlgmessage = g_strdup_printf(_("'%s' working, please wait..."), _effect->get_name());
130     _visibleDialog = new Gtk::MessageDialog(dlgmessage,
131                                false, // use markup
132                                Gtk::MESSAGE_INFO,
133                                Gtk::BUTTONS_CANCEL,
134                                true); // modal
135     _dialogsig = _visibleDialog->signal_response().connect(sigc::mem_fun(this, &ExecutionEnv::workingCanceled));
136     g_free(dlgmessage);
137     _visibleDialog->show();
139     _prefsVisible = false;
140     return;
143 void
144 ExecutionEnv::workingCanceled (const int resp) {
145     processingCancel();
146     documentCancel();
147     _finished = true;
148     return;
151 void
152 ExecutionEnv::preferencesResponse (const int resp) {
153     if (resp == Gtk::RESPONSE_OK) {
154         if (_humanWait && _livePreview) {
155             documentCommit();
156             _mainloop->quit();
157             _finished = true;
158         } else {
159             createWorkingDialog();
160             if (!_livePreview) {
161                 _mainloop->quit();
162                 _humanWait = false;
163                 _livePreview = true; // this is counter intuitive
164             }
165         }
166     } else {
167         if (_humanWait) {
168             _mainloop->quit();
169         } else {
170             processingCancel();
171         }
172         documentCancel();
173         _finished = true;
174     }
175     return;
178 void
179 ExecutionEnv::processingComplete(void) {
180     //std::cout << "Processing Complete" << std::endl;
181     if (_prefsChanged) { return; } // do it all again
182     if (_prefsVisible) {
183         _humanWait = true;
184     } else {
185         documentCommit();
186         _finished = true;
187     }
188     return;
191 void
192 ExecutionEnv::processingCancel (void) {
193     _effect->get_imp()->cancelProcessing();
194     return;
197 void
198 ExecutionEnv::documentCancel (void) {
199     _canceled = true;
200     return;
203 void
204 ExecutionEnv::documentCommit (void) {
205     sp_document_done(_doc->doc(), SP_VERB_NONE, _(_effect->get_name()));
206     Effect::set_last_effect(_effect);
207     return;
210 void
211 ExecutionEnv::reselect (void) {
212     if (_doc == NULL) { return; }
213     SPDocument * doc = _doc->doc();
214     if (doc == NULL) { return; }
216     SPDesktop *desktop = (SPDesktop *)_doc;
217     sp_namedview_document_from_window(desktop);
219     if (desktop == NULL) { return; }
221     Inkscape::Selection * selection = sp_desktop_selection(desktop);
223     for (std::list<Glib::ustring>::iterator i = _selected.begin(); i != _selected.end(); i++) {
224         SPObject * obj = doc->getObjectById(i->c_str());
225         if (obj != NULL) {
226             selection->add(obj);
227         }
228     }
230     return;
233 void
234 ExecutionEnv::run (void) {
235     while (!_finished) {
236         _canceled = false;
237         if (_humanWait || !_livePreview) {
238             _mainloop->run();
239         } else {
240             _prefsChanged = false;
241             _effect->get_imp()->effect(_effect, _doc);
242             processingComplete();
243         }
244         if (_canceled) {
245             sp_document_cancel(_doc->doc());
246             reselect();
247         }
248     }
249     if (_selfdelete) {
250         delete this;
251     }
252     return;
255 void
256 ExecutionEnv::livePreview (bool state) { 
257     _mainloop->quit();
258     if (_livePreview && !state) {
259         documentCancel();
260     }
261     if (!_livePreview && state) {
262         _humanWait = false;
263     }
264     _livePreview = state;
265     return;
268 void
269 ExecutionEnv::shutdown (bool del) { 
270     if (_humanWait) {
271         _mainloop->quit();
272     } else {
273         processingCancel();
274     }
275     documentCancel();
277     _finished = true;
278     _visibleDialog = NULL;
280     _selfdelete = del;
282     return;
285 } }  /* namespace Inkscape, Extension */
289 /*
290   Local Variables:
291   mode:c++
292   c-file-style:"stroustrup"
293   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
294   indent-tabs-mode:nil
295   fill-column:99
296   End:
297 */
298 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :