File: treemodel_wrapper.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (321 lines) | stat: -rw-r--r-- 9,589 bytes parent folder | download
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "treemodel_wrapper.h"
#include "wbdbg.h"
#include <gtkmm/icontheme.h>
#include <string.h>



//------------------------------------------------------------------------------
void TreeModelWrapper::update_root_node(const bec::NodeId &root_node)
{
  _root_node_path= root_node.toString();
  _root_node_path_dot= root_node.toString()+".";

  _stamp++;
}

//------------------------------------------------------------------------------
//bool TreeModelWrapper::init_gtktreeiter(GtkTreeIter *it, const bec::NodeId &node) const
//{
//  if ( _tm && it && node.is_valid() )
//  {
//    Index id(it, node);
//    id.stamp(_stamp);
//  }
//  return it && node.is_valid();
//}


//------------------------------------------------------------------------------
//bec::NodeId TreeModelWrapper::node_for_iter(const iterator &iter) const
//{
//  const GtkTreeIter* it = iter.gobj();
//  const Index id(it);
//  bec::NodeId  node;
//
//  if ( it )
//  {
//    if (id.cmp_stamp(_stamp))
//      node = id.to_node();
//    else
//      g_warning("reference to invalid iter in grt list/tree model");
//  }
//
//  return node;
//}

//------------------------------------------------------------------------------
bec::NodeId TreeModelWrapper::get_node_for_path(const Gtk::TreeModel::Path &path) const
{
  if (path.empty())
    return _root_node_path_dot;
  return bec::NodeId(_root_node_path_dot + path.to_string());
}

//------------------------------------------------------------------------------
bool TreeModelWrapper::get_iter_vfunc(const Path& path, iterator& iter) const
{
  if (!tm() || _invalid)
    return false;

  bool ret= false;
  GtkTreeIter       *it = iter.gobj();

  //g_message("get iter for %s + %s\n", _root_node_path_dot.c_str(), path.to_string().c_str());
  bec::NodeId node(_root_node_path_dot + path.to_string());

  try
  {
    if (node.is_valid() && tm()->count_children(tm()->get_parent(node)) > node.back())
      ret = init_gtktreeiter(it, node);
  }
  catch (...)
  {
    ret= false;
  }

  return ret;
}

Gtk::TreeModel::Path TreeModelWrapper::get_path_vfunc(const iterator& iter) const
{
  bec::NodeId node= node_for_iter(iter);
  Gtk::TreeModel::Path path;

  if ( node.is_valid() )
  {
    const int node_depth = node.depth();

    // get path from an iterator. The iterator points to the node, so
    // we have to trim the root node prefix so we have a path to the tree
    for ( int i = bec::NodeId(_root_node_path).depth(); i < node_depth; i++ )
      path.push_back(node[i]);
  }
  return path;
}

//------------------------------------------------------------------------------

void TreeModelWrapper::tree_row_collapsed(const iterator& iter, const Path &path)
{
  if (tm())
  {
    if ( _expanded_rows )
      _expanded_rows->erase(path.to_string());

    tm()->collapse_node(node_for_iter(iter));
  }
}

//------------------------------------------------------------------------------

void TreeModelWrapper::tree_row_expanded(const iterator& iter, const Path &path)
{
  if (tm())
  {
    if ( _expanded_rows )
      _expanded_rows->insert(path.to_string());
      
    tm()->expand_node(node_for_iter(iter));
  }
}


//------------------------------------------------------------------------------
void TreeModelWrapper::get_icon_value(const iterator& iter, int column, const bec::NodeId &node, Glib::ValueBase& value) const
{
  if (!tm())
    return;
    
  static ImageCache       *pixbufs      =  ImageCache::get_instance();
  static Glib::RefPtr<Gtk::IconTheme> icon_theme= Gtk::IconTheme::get_default();
  GValue *gval = value.gobj();

  g_value_init(gval, GDK_TYPE_PIXBUF);

  bec::IconId               icon_id = tm()->get_field_icon(node, column, get_icon_size());
  if (icon_id != 0)
  {
    Glib::RefPtr<Gdk::Pixbuf>  pixbuf = pixbufs->image(icon_id);
    
    if ( pixbuf )
      g_value_set_object(gval, pixbuf->gobj());
    else
    {
      if (tm()->is_expandable(node))
      {
        Glib::RefPtr<Gdk::Pixbuf>  pixbuf = icon_theme->load_icon("folder", 16, (Gtk::IconLookupFlags)0);
        
        if ( pixbuf )
          g_value_set_object(gval, pixbuf->gobj());
      }
    }
  }
  else
  {
    if (tm()->is_expandable(node))
    {
      Glib::RefPtr<Gdk::Pixbuf>  pixbuf = icon_theme->load_icon("folder", 16, (Gtk::IconLookupFlags)0);
      
      if ( pixbuf )
        g_value_set_object(gval, pixbuf->gobj());
    }
  }
}

//------------------------------------------------------------------------------

Gtk::TreeModelFlags TreeModelWrapper::get_flags_vfunc() const
{
  if (_show_as_list)
    return Gtk::TREE_MODEL_ITERS_PERSIST | Gtk::TREE_MODEL_LIST_ONLY;
  else
    return Gtk::TREE_MODEL_ITERS_PERSIST;
}

//------------------------------------------------------------------------------
bool TreeModelWrapper::iter_children_vfunc(const iterator& parent, iterator& iter) const
{
  //dprint("%s\n", __FUNCTION__);
  return _show_as_list ? false : iter_nth_child_vfunc(parent, 0, iter);
}

//------------------------------------------------------------------------------
bool TreeModelWrapper::iter_parent_vfunc(const iterator& child, iterator& iter) const
{
  //dprint("%s\n", __FUNCTION__);
  bool ret                    = false;
  if (tm())
  {
    bec::NodeId node(node_for_iter(child));
    
    if ( node.is_valid() )
    {
      // Make iter (parent would be iterator) invalid from the start, just in case the code below
      // will fail
      reset_iter(iter);
  
      bec::NodeId would_be_parent(tm()->get_parent(node));
      if ( would_be_parent.is_valid() )
      {
        init_gtktreeiter(iter.gobj(), would_be_parent);
        ret = true;
        //dprint("%s(child->'%s', parent->'%s')\n", __FUNCTION__, bec::NodeId(tm->nodeuid_to_path(uid)).repr().c_str(), would_be_parent.repr().c_str());
      }
    }
  }
  return ret;
}

//------------------------------------------------------------------------------
bool TreeModelWrapper::iter_nth_child_vfunc(const iterator& parent, int n, iterator& iter) const
{
  //dprint("%s\n", __FUNCTION__);
  //Sets @a iter to be the child of @a parent using the given index.  The first
  //index is 0.  If @a n is too big, or @a parent has no children, @a iter is set
  //to an invalid iterator and false is returned.
  //See also iter_nth_root_child_vfunc()
  bool ret = false;
  bec::NodeId parent_node(node_for_iter(parent));

  // Invalidate iter_next
  reset_iter(iter);
  if (tm())
  {  
    if ( parent_node.is_valid() )
    {
      const int children_count = tm()->count_children(parent_node);
  
      if ( n >= 0 && children_count > 0 && n < children_count )
      {
        bec::NodeId child(tm()->get_child(parent_node, n));
        if ( child.is_valid() )
        {
          init_gtktreeiter(iter.gobj(), child);
          ret = true;
          //dprint("%s(parent->'%s', int n = %i, iter->'%s')\n", __FUNCTION__, parent_node.repr().c_str(), n, child.repr().c_str());
        }
      }
    }
  }
  return ret;
}

//------------------------------------------------------------------------------
bool TreeModelWrapper::iter_nth_root_child_vfunc(int n, iterator& iter) const
{
  //dprint("%s(n = %i), _tm->count() = %i   (as list=%i)\n", __FUNCTION__, n, _tm->count(), _show_as_list);
  bool ret = false;
  bec::NodeId root_node(_root_node_path);
  //Sets @a iter to be the child of at the root level using the given index.  The first
  //index is 0.  If @a n is too big, or if there are no children, @a iter is set 
  //to an invalid iterator and false is returned.
  //See also iter_nth_child_vfunc().
  if ( tm() && n >= 0 && n < (int)tm()->count_children(root_node) )
  {
    bec::NodeId node = tm()->get_child(root_node, n);
    init_gtktreeiter(iter.gobj(), node);
    ret = true;
    //dprint("%i = %s(n = %i, iter& -> '%s')\n", ret, __FUNCTION__, n, node.repr().c_str());
  }
  
  return ret;
}

//------------------------------------------------------------------------------
bool TreeModelWrapper::iter_has_child_vfunc(const iterator& iter) const
{
  if (_invalid)
    return false;

  //dprint("has child? %i (cont= %i)", _show_as_list ? false : iter_n_children_vfunc(iter) != 0, iter_n_children_vfunc(iter));
  return _show_as_list ? false : iter_n_children_vfunc(iter) != 0;
}

//------------------------------------------------------------------------------
int TreeModelWrapper::iter_n_children_vfunc(const iterator& iter) const
{
  if (!_children_count_enabled)
    return 1;

  int ret = 0;

  bec::NodeId node(node_for_iter(iter));
  
  if ( tm() && node.is_valid() )
  {
    // can't expand here otherwise the live tree will pre-fetch everything when the tree is shown
    if (!_delay_expanding_nodes)
    // need to expand here because otherwise the backend won't know how many children the node has
      tm()->expand_node(node);
    
    ret = tm()->count_children(node);
  }
    
  return ret;
}

//------------------------------------------------------------------------------
int TreeModelWrapper::iter_n_root_children_vfunc() const
{
  const bec::NodeId  parent(_root_node_path);
  const int ret = tm() ? tm()->count_children(parent) : 0;

  //dprint("%i = %s() at path '%s'\n", ret, __FUNCTION__, _root_node_path.c_str());
  
  return ret;
}

//------------------------------------------------------------------------------
void TreeModelWrapper::block_expand_collapse_signals()
{
  _expand_signal.block();
  _collapse_signal.block();
}

//------------------------------------------------------------------------------
void TreeModelWrapper::unblock_expand_collapse_signals()
{
  _expand_signal.unblock();
  _collapse_signal.unblock();
}