File: dia-diagramdata.cpp

package info (click to toggle)
dia 0.97.3%2Bgit20160930-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 52,952 kB
  • ctags: 17,146
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,826; sh: 540; perl: 137; sed: 19
file content (177 lines) | stat: -rw-r--r-- 5,110 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * C++ wrapper of DiagramData - just to have something clean to wrap
 *
 * Copyright (C) 2007, Hans Breuer, <Hans@Breuer.Org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include "object.h"
#include "diagramdata.h"
#include <assert.h>

#include "dia-object.h"

#include "dia-diagramdata.h"

//! how many contained
int 
dia::Objects::len () const
{
    if (list)
        return g_list_length(*list);
    return 0;
}
//! \brief return back a single one
//! Together with len() getitem() allows list access. The code generator needs some
//! extra help to not making it an endless list. At least for Python the NULL return
//! was turned into None objects, we want to have only as much list elements 
//! as there are real objects.
dia::Object* 
dia::Objects::getitem (int n) const
{
    if (list && n >= 0 && n < g_list_length(*list))
        return new dia::Object((::DiaObject*)g_list_nth_data(*list, n));
    return 0;
}

//! construct with underlying list of objects
//! \todo check ownership/lifetime of that list!
dia::Layer::Layer (::Layer* layer) : self(layer), _found(0) 
{
    assert (self);
    // todo: when we allow to change the name this needs to change
    name = g_strdup (self->name);
    objects = new dia::Objects(&(layer->objects));
}
dia::Layer::~Layer ()
{
    if (_found)
        delete _found;
    if (name)
        g_free (const_cast<gchar*>(name));
    if (objects)
      delete const_cast<dia::Objects*>(objects);
}
void
dia::Layer::add_object (Object* o)
{
    g_return_if_fail (self != NULL);
    layer_add_object (self, o->Self());
}
void
dia::Layer::update_extents ()
{
    g_return_if_fail (self != NULL);
    layer_update_extents(self);
}
//! the object next to given point but within maxdist
dia::Object* 
dia::Layer::find_closest_object (::Point* pos, double maxdist) const
{
    g_return_val_if_fail (self != NULL, 0);
    ::DiaObject* o = layer_find_closest_object (self, pos, maxdist);
    if (o)
      return new dia::Object (o);
    return 0;
}
//! a list of Object in the given rectangle
dia::Objects& 
dia::Layer::find_objects_in_rectangle (::Rectangle* rect) const
{
    static GList* list = layer_find_objects_in_rectangle (self, rect);
    if (_found)
        delete _found;
    const_cast<Layer*>(this)->_found = new dia::Objects (&list);
    return *_found;
}
 //! objects are kept in an ordered list, this is the index of the given object
int 
dia::Layer::object_index (Object* o) const
{
    g_return_val_if_fail (self != NULL, -1);
    return layer_object_get_index (self, o->Self());
}

int
dia::Layers::len () const
{
    if (list)
        return (*list)->len;
    return 0;
}
dia::Layer*
dia::Layers::getitem (int n) const
{
  if (list && n >= 0 && n < (*list)->len)
    return new dia::Layer(static_cast< ::Layer* >(g_ptr_array_index (*list, n)));
  return 0;
}

/*!
 * DiagramData is the low level Diagram, i.e. everything without an UI
 */
dia::DiagramData::DiagramData () : self(0), active_layer(0), _selected(0)
{
    // the usual GObjetc cast DIA_DIAGRAM_DATA does not work when playing games with namespaces ;)
    self = static_cast< ::DiagramData* > (g_object_new (DIA_TYPE_DIAGRAM_DATA, NULL));
    //FIXME: grumpf
    if (self->active_layer)
      active_layer = new Layer(self->active_layer);
    else if (self->layers->len > 0)
      active_layer = new Layer((::Layer*)g_ptr_array_index(self->layers, 0));
    layers = new Layers(&self->layers);
}
dia::DiagramData::~DiagramData ()
{
    g_object_unref (self);
    if (_selected)
        delete _selected;
    if (layers)
      delete const_cast< dia::Layers* >(layers);
}

dia::Layer*
dia::DiagramData::add_layer (const char* name)
{
    int pos = -1; //TODO: make this a parameter
    g_return_val_if_fail (self != NULL, 0);
    ::Layer* layer = new_layer(g_strdup(name),self);
    if (pos != -1)
	data_add_layer_at(self, layer, pos);
    else
	data_add_layer(self, layer);

    dia::Layer* dl = new dia::Layer (layer);
    return dl;
}
void 
dia::DiagramData::update_extents ()
{
    g_return_if_fail(self != NULL);
    data_update_extents(self);
    // conceptionally const, i.e. read-only
    *const_cast< ::Rectangle* >(&extents) = self->extents;
}
dia::Objects&
dia::DiagramData::get_sorted_selected () const
{
    if (_selected)
        delete _selected;

    static GList* list = data_get_sorted_selected(self);
    const_cast< DiagramData* >(this)->_selected = new dia::Objects (&list);
    return *_selected;
}