~ubuntu-branches/ubuntu/utopic/gnome-chemistry-utils/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/gcp/reaction-step.cc

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-05-10 21:30:51 UTC
  • mfrom: (1.1.15) (2.1.25 sid)
  • Revision ID: package-import@ubuntu.com-20130510213051-mswxsp3vitsgqspm
Tags: 0.14.2-1ubuntu1
* Sync with Debian. Remaining change:
  - Build-depend on firefox-dev instead of xulrunner-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// -*- C++ -*-
2
2
 
3
 
/* 
 
3
/*
4
4
 * GChemPaint library
5
 
 * reaction-step.cc 
 
5
 * reaction-step.cc
6
6
 *
7
7
 * Copyright (C) 2004-2011 Jean Bréfort <jean.brefort@normalesup.org>
8
8
 *
9
 
 * This program is free software; you can redistribute it and/or 
10
 
 * modify it under the terms of the GNU General Public License as 
11
 
 * published by the Free Software Foundation; either version 2 of the
 
9
 * This program is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU General Public License as
 
11
 * published by the Free Software Foundation; either version 3 of the
12
12
 * License, or (at your option) any later version.
13
13
 *
14
14
 * This program is distributed in the hope that it will be useful,
43
43
namespace gcp {
44
44
 
45
45
TypeId ReactionStepType = NoType;
 
46
TypeId DummyStepType = NoType;
46
47
 
47
48
ReactionStep::ReactionStep (): MechanismStep (ReactionStepType)
48
49
{
108
109
{
109
110
        if (IsLocked ())
110
111
                return;
 
112
        Lock ();
111
113
        CleanChildren ();
112
114
}
113
 
        
 
115
 
114
116
xmlNodePtr ReactionStep::Save (xmlDocPtr xml) const
115
117
{
116
118
        xmlNodePtr node;
131
133
        }
132
134
        return node;
133
135
}
134
 
        
 
136
 
135
137
bool ReactionStep::Load (xmlNodePtr node)
136
138
{
 
139
        if (!Object::Load (node))
 
140
                return false;
 
141
        Document *pDoc = dynamic_cast <Document*> (GetDocument ());
 
142
        pDoc->NotifyDirty (this);
 
143
        pDoc->ObjectLoaded (this);
 
144
        return true;
 
145
}
 
146
 
 
147
void ReactionStep::OnLoaded ()
 
148
{
137
149
        m_bLoading = true;
138
 
        if (!Object::Load (node))
139
 
                return false;
140
150
        map<Object*, gccv::Rect> Objects;
141
151
        map<double, Object*> Children;
142
152
        gccv::Rect rect;
145
155
        Document *pDoc = dynamic_cast <Document*> (GetDocument ());
146
156
        View *view = pDoc->GetView ();
147
157
        Theme *pTheme = pDoc->GetTheme ();
 
158
        view->Update (this);
148
159
        WidgetData  *pData= view->GetData ();
149
160
        map<double, Object*>::iterator im, endm;
150
161
        double x, y, x0, y0, x1, y1;
 
162
        // this method is called more than once (might consider this a bug) so we need to remove old operators
 
163
        std::list <gcu::Object *> Operators;
 
164
        // FIXME: add a dummy object if empty
151
165
        while (pObj) {
152
 
                if (pObj->GetType () != gcp::MechanismArrowType) {
 
166
                if (pObj->GetType () == ReactionOperatorType)
 
167
                        Operators.push_front (pObj);
 
168
                else if (pObj->GetType () != gcp::MechanismArrowType) {
153
169
                        pData->GetObjectBounds (pObj, &rect);
154
170
                        x = (rect.x0 + rect.x1) / 2;
155
171
                        while (Children[x] != NULL)
159
175
                }
160
176
                pObj = GetNextChild (i);
161
177
        }
 
178
        while (!Operators.empty ()) {
 
179
                delete Operators.front ();
 
180
                Operators.pop_front ();
 
181
        }
162
182
        im = Children.begin ();
163
183
        endm = Children.end ();
164
184
        rect = Objects[(*im).second];
178
198
                rect = Objects[pObj];
179
199
                x+= rect.x1 - rect.x0;
180
200
        }
181
 
        view->Update (this);
182
201
        m_bLoading = false;
183
 
        return true;
184
202
}
185
203
 
186
204
double ReactionStep::GetYAlign ()
209
227
                WidgetData  *pData= (WidgetData*) g_object_get_data (G_OBJECT (pDoc->GetWidget ()), "data");
210
228
                map<double, Object*>::iterator im, endm;
211
229
                double x, y, x0, y0, x1, y1;
 
230
                std::set <gcu::Object *> Invalid;
 
231
                if (!pObj) {
 
232
                        delete this;
 
233
                        return true;
 
234
                }
212
235
                while (pObj) {
213
236
                        if (pObj->GetType () == ReactionOperatorType)
214
237
                                Operators.push_front (pObj);
215
238
                        else if (pObj->GetType () == MechanismArrowType); // don't do anything
216
 
                        else {
217
 
                                pData->GetObjectBounds (pObj, &rect);
218
 
                                x = (rect.x0 + rect.x1) / 2;
219
 
                                while (Children[x] != NULL)
220
 
                                        x += 1e-5;
221
 
                                Children[x] = pObj;
222
 
                                Objects[pObj] = rect;
 
239
                        else if (pObj->GetType () == MesomeryArrowType) {
 
240
                                delete this;
 
241
                                return true;
 
242
                        } else {
 
243
                                if (pObj->GetChildrenNumber ()) {
 
244
                                        pData->GetObjectBounds (pObj, &rect);
 
245
                                        x = (rect.x0 + rect.x1) / 2;
 
246
                                        while (Children[x] != NULL)
 
247
                                                x += 1e-5;
 
248
                                        Children[x] = pObj;
 
249
                                        Objects[pObj] = rect;
 
250
                                } else
 
251
                                        Invalid.insert (pObj);
223
252
                        }
224
253
                        pObj = GetNextChild (i);
225
254
                }
 
255
                // delete invalid children
 
256
                // first avoid new signals
 
257
                m_bLoading = true;
 
258
                // really delete
 
259
                std::set <gcu::Object *>::iterator j, jend = Invalid.end ();
 
260
                for (j = Invalid.begin (); j != jend; j++)
 
261
                        delete *j;
 
262
                m_bLoading = false;
226
263
                while (!Operators.empty ()) {
227
264
                        pObj = Operators.front ();
228
265
                        pView->Remove (pObj);
255
292
                return true;
256
293
}
257
294
 
258
 
void ReactionStep::RemoveArrow (ReactionArrow *arrow) {
259
 
        m_Arrows.erase (arrow);
260
 
        if (m_Arrows.empty ()) {
261
 
                // if there is no more arrows this is no more a reaction step
262
 
                delete this;
263
 
        }
264
 
}
265
 
 
266
295
std::string ReactionStep::Name ()
267
296
{
268
297
        return _("Reaction step");
270
299
 
271
300
void ReactionStep::CleanChildren ()
272
301
{
273
 
        set<ReactionArrow *>::iterator i, end = m_Arrows.end();
 
302
        map < Step *, Arrow * >::iterator i, end = m_Arrows.end();
274
303
        for (i = m_Arrows.begin (); i != end; i++)
275
 
                (*i)->RemoveStep (this);
 
304
                (*i).second->RemoveStep (this);
276
305
        /* If there are no chidren, don't care and exit */
277
306
        if (!GetChildrenNumber ())
278
307
                return;
297
326
                        Child->SetParent (pObj);
298
327
                        arrows.push_back (static_cast <MechanismArrow *> (Child));
299
328
                        continue;
300
 
                } else if (Child->GetType () == MechanismStepType) {
 
329
                } else if (Child->GetType () == MechanismStepType || Child->GetType () == MesomeryArrowType) {
301
330
                        Child->SetParent (pObj);
302
331
                        if (pOp && !Group)
303
332
                                new_objects.insert (Child);
347
376
                                        step->AddChild (obj);
348
377
                                        obj = parent->GetFirstChild (it);
349
378
                                }
350
 
                                
 
379
 
351
380
                        } else
352
381
                                step->AddChild (molecule);
353
382
                }
365
394
                EmitSignal (OnChangedSignal);
366
395
}
367
396
 
 
397
bool ReactionStep::Validate ()
 
398
{
 
399
        if (m_Arrows.empty ())
 
400
                return false;
 
401
        // FIXME:
 
402
        return true;
 
403
}
 
404
 
368
405
}       //      namespace gcp