File: listmodel_wrapper.h

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 (576 lines) | stat: -rw-r--r-- 20,440 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//!
//! \addtogroup linuxui Linux UI
//! @{
//! 

#ifndef __LISTMODEL_WRAPPER_H__
#define __LISTMODEL_WRAPPER_H__

//use IconManager to retrive icon file
//reuse wb_icons to get actual Pixbufs
//ListModel get_field obtains value of the given field
//NodeId is an index to a value in some data structure inside the backend
//It seems that UI model should be build in advance, as it is not possible to know 
//what types are for columns

#include <gtkmm/treemodel.h>
#include <gtkmm/treeview.h>
#include <gtkmm/iconview.h>
#include <gtkmm/liststore.h>

#include "base/trackable.h"
#include "grt/tree_model.h"
#include "image_cache.h"
#include "wbdbg.h"
#include <sstream>

//==============================================================================
//
//==============================================================================
template <int T>
struct Pow
{
  enum {Value = 256  *  Pow<T - 1>::Value};
};

template <>
struct Pow<0>
{
  enum {Value=1};
};

//==============================================================================
//
//==============================================================================
struct Index
{
    struct Info
    {
      unsigned int mode : 2;
      unsigned int stamp : 6;
    };
  public:
    typedef std::set<std::string>     ExternalMap;
    enum {K = 3, StampMax = 0x3f, ModeMask = 0xC0, MaxDepth = (sizeof(GtkTreeIter) - 1) / K, MaxValue = Pow<K>::Value - 1};
    enum Mode {Internal = 0x01, External = 0x02, ListNode = 0x03};

    Index(GtkTreeIter* it, const bec::NodeId& node);
    Index(const GtkTreeIter* it);

    int stamp() const;
    void stamp(const int st);
    bool cmp_stamp(const int st) const;

    Mode mode() const
    {
      return (Mode)(((Info*)_raw_data)->mode);
    }
    void mode(const Mode m)
    {
      ((Info*)_raw_data)->mode = m;
    }

    bec::NodeId to_node() const;
    static void reset_iter(GtkTreeIter* it);

  private:
    int  word(const int w) const;
    void word(const int w, const int v);

    char*                   _raw_data;
    static ExternalMap      _ext_map;
    std::string            *_ext;
};



//! \brief if a column created by TreeModelWrapper should be read only or editable
enum {READONLY_COLUMN = 0, EDITABLE_COLUMN = 1};

typedef Gtk::TreeModelColumn<int>            IntColumn;
typedef Gtk::TreeModelColumn<double>         DoubleColumn;
typedef Gtk::TreeModelColumn<Glib::ustring>  StringColumn;

class ListModelWrapper;

Gtk::TreeModel::Path node2path(const ::bec::NodeId& node);

//! Enums to define columns view and behaviour. These are used in various ColumnsModel::append_*_column
enum Editable {RO = 0, EDITABLE = 1, EDITABLE_WO_FIRST = 2};
//! Enums to define columns view and behaviour. These are used in various ColumnsModel::append_*_column
enum Iconic {NO_ICON = 0, WITH_ICON = 1};
enum ToggleAction {TOGGLE_BY_WRAPPER, TOGGLE_BY_GTKMM};

//! \brief generic model for a TreeModelWrapper
//!
//! Using ColumnsModel we do not need to create models for Gtk::TreeView in separate classes.
//! With ColumnsModel it is to add columns to a model.
class ColumnsModel : public Gtk::TreeModel::ColumnRecord
{
    class ComboColumns : public Gtk::TreeModel::ColumnRecord
    {
    public:
      ComboColumns()
      {
        add(name);
      }

      Gtk::TreeModelColumn<Glib::ustring> name;
    } _combo_columns;

  public:

    ColumnsModel(ListModelWrapper *tmw, Gtk::TreeView *treeview) : Gtk::TreeModel::ColumnRecord(), _tmw(tmw), _treeview(treeview) {}
    ~ColumnsModel();

    void reset(const bool cleanup_only_self = false);

    void add_model_column(Gtk::TreeModelColumnBase *col, int bec_tm_idx);

    const StringColumn& set_text_column(const int bec_tm_idx, const bool editable, Gtk::IconView *iv);

    //! \brief adds a column that will contain the description for the column index
    void add_tooltip_column(int bec_tm_idx);

    //! \brief appends integer column named @name
    //!
    //! append_int_column adds column which can store integers. Parameter bec_tm_idx is used to map gtk's indices
    //! to a bec::TreeModel indices. E.g. when adding combo column two
    //!
    const IntColumn&        append_int_column(const int bec_tm_idx, const std::string& name, const Editable editable= RO);
    const DoubleColumn&  append_double_column(const int bec_tm_idx, const std::string& name, const Editable editable= RO);
    void disable_edit_first_row(Gtk::CellRenderer* cell, const Gtk::TreeIter& iter);
    const StringColumn&  append_string_column(const int bec_tm_idx, const std::string& name, const Editable editable= RO, const Iconic have_icon= NO_ICON);
    const StringColumn&  append_markup_column(const int bec_tm_idx, const std::string& name, const Iconic have_icon= NO_ICON);
    const StringColumn& append_combo_column(const int bec_tm_idx, const std::string& name, Glib::RefPtr<Gtk::ListStore> list_w, const Editable editable= RO, bool popup_only=false);
    //! TOGGLE_BY_* are temp workaround for gtkmm's _connect_auto_store_editable_signal_handler
    //! not always working with our ListModelWrapper. Sometimes we have double setting(reverting) of a value in
    //! a BE model thus leaving value the same ( !(!value)). Needs further digging
    int append_check_column(const int bec_tm_idx, const std::string& name, const Editable Editable= RO, const ToggleAction action = TOGGLE_BY_WRAPPER);

    int add_generic_column(const int bec_tm_idx, Gtk::TreeModelColumnBase *column,
                           Gtk::TreeViewColumn *vcolumn);

    int ui2bec(const int index_of_ui_column) const;

    Glib::RefPtr<Gtk::ListStore> create_model(const grt::StringListRef& source)
    {
      Glib::RefPtr<Gtk::ListStore> list_w(Gtk::ListStore::create(_combo_columns));

      grt::StringListRef::const_iterator last = source.end();
      for (grt::StringListRef::const_iterator iter= source.begin(); iter != last; ++iter)
      {
        Gtk::TreeRow row= *list_w->append();
        row[_combo_columns.name]= **iter;
      }

      return list_w;
    }

    Glib::RefPtr<Gtk::ListStore> create_model(const std::vector<std::string>& source)
    {
      Glib::RefPtr<Gtk::ListStore> list_w(Gtk::ListStore::create(_combo_columns));

      std::vector<std::string>::const_iterator last = source.end();
      for (std::vector<std::string>::const_iterator iter= source.begin(); iter != last; ++iter)
      {
        Gtk::TreeRow row= *list_w->append();
        row[_combo_columns.name]= *iter;
      }

      return list_w;
    }
  private:
    std::list<Gtk::TreeModelColumnBase*> _columns;
    ListModelWrapper      *_tmw;
    Gtk::TreeView         *_treeview;
    std::vector<int>       _ui2bec; //! map ui indices to a bec ones
    void add_bec_index_mapping(const int bec_tm_index);

//    void toggled(const Glib::ustring& path, Gtk::CellRendererToggle* cell);
};

template <typename T>
inline T convert(const Glib::ustring& str)
{
  throw std::logic_error(std::string("Add conversion for ") + typeid(T).name());
}

template <>
inline int convert(const Glib::ustring& str)
{
  return atoi(str.c_str());
}

template <>
inline Glib::ustring convert(const Glib::ustring& str)
{
  return str;
}

template <typename T>
inline bool can_convert(const Glib::ustring& str, T &val)
{
  std::istringstream ss(str);
  ss >> val;
  return (bool)(ss);
}

template <>
inline bool can_convert(const Glib::ustring& str, Glib::ustring &val)
{
  val = str;
  return true;
}

template <>
inline bool can_convert(const Glib::ustring& str, std::string &val)
{
  val = str;
  return true;
}

//==============================================================================
#if GLIB_CHECK_VERSION(2, 44, 1)
class ListModelWrapper : public Gtk::TreeModel, public Gtk::TreeDragDest, public Gtk::TreeDragSource,
                         public Glib::Object,
                         public base::trackable
#elif GLIB_CHECK_VERSION(2, 42, 0)
class ListModelWrapper : public Gtk::TreeModel, public Glib::Object,
                         public Gtk::TreeDragDest, public Gtk::TreeDragSource,
                         public base::trackable
#elif GLIB_CHECK_VERSION(2, 40, 2)
class ListModelWrapper : public Gtk::TreeModel, public Gtk::TreeDragDest, public Gtk::TreeDragSource,
                         public Glib::Object,
                         public base::trackable
#else
class ListModelWrapper : public Glib::Object, public Gtk::TreeModel, public Gtk::TreeDragDest,
                         public Gtk::TreeDragSource,
                         public base::trackable

#endif

{
     friend class ColumnsModel;
  protected:
     void model_changed(const bec::NodeId &, int)
     {
       ++_stamp;
     }

    //! root_node_path is a prefix for a subtree, also if you want to hide dummy root node '0'
    //! you may pass NodeId(0) to ListModelWrapper
    ListModelWrapper(bec::ListModel* tm, Gtk::TreeView *treeview, const std::string& name);

  public:
    ~ListModelWrapper();

    static Glib::RefPtr<ListModelWrapper> create(bec::ListModel* tm, Gtk::TreeView *treeview, const std::string& name)
    {
      return Glib::RefPtr<ListModelWrapper>(new ListModelWrapper(tm, treeview, name));
    }

   /**
    Marks the tree as invalid.
    Methods like get_iter_vfunc will return false and the backend tree model
    will be treated as no longer valid.
    */
    void invalidate();

    void set_iconview(Gtk::IconView* iv);

    template <class T>
    void after_cell_edit(const Glib::ustring& path_string
                        ,const Glib::ustring& new_text
                        ,const typename Gtk::TreeModelColumn<T> &column
                        )
    {
      Gtk::TreeModel::iterator iter = get_iter(Gtk::TreePath(path_string));
      if ( iter )
      {
        Gtk::TreeRow row = *iter;
        T val;
        if (can_convert<T>(new_text, val))
          row[column] = val;
      }
    }

    void after_cell_toggle(const Glib::ustring              &path_string
                          ,const Gtk::TreeModelColumn<bool> &column
                          )
    {
      Gtk::TreeModel::iterator iter = get_iter(Gtk::TreePath(path_string));
      if ( iter )
      {
        Gtk::TreeRow row = *iter;
        const bool new_value = !row[column];
        row[column] = new_value;
      }
    }

    ColumnsModel& model()
    {
      return _columns;
    }

    void set_icon_size(const bec::IconSize icon_size)
    {
      _icon_size = icon_size;
    }

    bec::IconSize get_icon_size() const
    {
      return _icon_size;
    }

    bec::NodeId node_for_iter(const iterator &iter) const;

    virtual bec::NodeId get_node_for_path(const Gtk::TreeModel::Path &path) const;
  
    bec::ListModel *get_be_model() const { return *_tm; }
    void set_be_model(bec::ListModel* tm);

    void set_name(const std::string& nm) { _name = nm; }
    const std::string& name() const { return _name; }
    void refresh();
    void note_row_added();

    typedef sigc::slot<void, const iterator&, int, GType, Glib::ValueBase&>       FakeColumnValueGetter;
    typedef sigc::slot<void, const iterator&, int, GType, const Glib::ValueBase&> FakeColumnValueSetter;

    void set_fake_column_value_getter(FakeColumnValueGetter fake_getter);
    void set_fake_column_value_setter(FakeColumnValueSetter fake_setter);

    void set_row_draggable_slot(const sigc::slot<bool, Gtk::TreeModel::Path> &slot);

    typedef std::vector<bec::NodeId> NodeIdArray;
    NodeIdArray get_selection() const;
  #ifndef NO_MENU_MANAGER
    void handle_popup(const int x, const int y, const int time, GdkEventButton* evb);
    virtual bool handle_popup_event(GdkEvent* event);
    void set_fe_menu_handler(const sigc::slot<void, const std::string&, const std::vector<bec::NodeId>&> &slot)
    {
      _fe_menu_handler = slot;
    }
  private:
    sigc::slot<void, const std::string&, const std::vector<bec::NodeId>&>  _fe_menu_handler;
  #endif
  protected:
    virtual Gtk::TreeModelFlags get_flags_vfunc() const;
    virtual int get_n_columns_vfunc() const;
    virtual GType get_column_type_vfunc(int index) const;

    /**
    Sets @a iter_next to refer to the node following @a iter it at the current level.
    If there is no next iter, false is returned and iter_next is set to be invalid.

    @param iter An iterator.
    @param iter_next An iterator that will be set to refer to the next node, or will be set as invalid.
    @result true if the operation was possible.
    */
    virtual bool iter_next_vfunc(const iterator& iter, iterator& iter_next) const;

    /**
    Sets @a iter to a valid iterator pointing to @a path

    @param path An path to a node.
    @param iter An iterator that will be set to refer to a node to the path, or will be set as invalid.
    @result true if the operation was possible.
    */
    virtual bool get_iter_vfunc(const Path& path, iterator& iter) const;

    /**
    Sets @a iter to refer to the first child of @a parent. If @a parent has no children,
    false is returned and @a iter is set to be invalid.

    @param parent An iterator.
    @param iter An iterator that will be set to refer to the firt child node, or will be set as invalid.
    @result true if the operation was possible.
    */
    virtual bool iter_children_vfunc(const iterator& parent, iterator& iter) const;

    /**
    Sets @a iter to be the parent of @a child. If @a child is at the toplevel, and
    doesn't have a parent, then @a iter is set to an invalid iterator and false
    is returned.

    @param child An iterator.
    @param iter An iterator that will be set to refer to the parent node, or will be set as invalid.
    @result true if the operation was possible.
    */
    virtual bool iter_parent_vfunc(const iterator& child, iterator& iter) const;

    /**
    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()

    @param parent An iterator.
    @param n The index of the child node to which @a iter should be set.
    @param iter An iterator that will be set to refer to the nth node, or will be set as invalid.
    @result true if the operation was possible.
    */
    virtual bool iter_nth_child_vfunc(const iterator& parent, int n, iterator& iter) const;

    /**
    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().

    @param n The index of the child node to which @a iter should be set.
    @param iter An iterator that will be set to refer to the nth node, or will be set as invalid.
    @result true if the operation was possible.
    */
    virtual bool iter_nth_root_child_vfunc(int n, iterator& iter) const;

    /**
    Returns true if @a iter has children, false otherwise.

    @param iter The iterator to test for children.
    @result true if @a iter has children.
    */
    virtual bool iter_has_child_vfunc(const iterator& iter) const;

    /**
    Returns the number of children that @a iter has.
    See also iter_n_root_children_vfunc().

    @param iter The iterator to test for children.
    @result The number of children of @a iter.
    */
    virtual int iter_n_children_vfunc(const iterator& iter) const;

    /**
    Returns the number of toplevel nodes.
    See also iter_n_children().

    @result The number of children at the root level.
    */
    virtual int iter_n_root_children_vfunc() const;

    /**
    Lets the tree ref the node.  This is an optional method for models to
    implement.  To be more specific, models may ignore this call as it exists
    primarily for performance reasons.

    This function is primarily meant as a way for views to let caching model know
    when nodes are being displayed (and hence, whether or not to cache that
    node.)  For example, a file-system based model would not want to keep the
    entire file-hierarchy in memory, just the sections that are currently being
    displayed by every current view.

    A model should be expected to be able to get an iter independent of its
    reffed state.

    @param iter the iterator.
    */
    //virtual void ref_node_vfunc(const iterator& iter) const;

    /**
    Lets the tree unref the node.  This is an optional method for models to
    implement.  To be more specific, models may ignore this call as it exists
    primarily for performance reasons.

    For more information on what this means, see unref_node_vfunc().
    Please note that nodes that are deleted are not unreffed.

    @param iter the iterator.
   */
    //virtual void unref_node_vfunc(const iterator& iter) const;

    /** Override and implement this in a derived TreeModel class.
    Returns a Path referenced by @a iter.

    @param iter The iterator.
    @result The path.
    */
    virtual TreeModel::Path get_path_vfunc(const iterator& iter) const;

    /**
    Initializes and sets @a value to that at @a column.

    @param iter The iterator.
    @param column The column to lookup the value at.
    @param value An empty Glib:Value to set.
    */
    virtual void get_value_vfunc(const iterator& iter, int column, Glib::ValueBase& value) const;

    /**
    Initializes and sets @a icon value to that at @a column.

    @param iter The iterator.
    @param column The column to lookup the value at.
    @param node The grt tree node at the iter.
    @param value An empty Glib:Value to set.
    */
    virtual void get_icon_value(const iterator& iter, int column, const bec::NodeId &node, Glib::ValueBase& value) const;

    /**
    @note This virtual method is not recommended.  To check
    whether an iterator is valid, call TreeStore::iter_is_valid(),
    ListStore::iter_is_valid() or TreeModelSort::iter_is_valid() directly
    instead.  Because these methods are intended to be used only for debugging
    and/or testing purposes, it doesn't make sense to provide an abstract
    interface to them.

    @result true if the iterator is valid.

    @deprecated Use iter_is_valid() in the derived class.
    */
    virtual bool iter_is_valid(const iterator& iter) const;

    //Called by TreeRow, which is a friend class:
    //The comment about set_row_changed() in the documentation is based on my reading of the source of
    //gtk_list_store_set_value() and gtk_tree_store_set_value().
    /**
    Row::set_value() work.
    You can probably just implement this by calling set_value_vfunc().
    Your implementation of set_value_impl() should also call set_row_changed() after changing the value.
    */
    virtual void set_value_impl(const iterator& row, int column, const Glib::ValueBase& value);

    //This might not need to be virtual, but it's not a big deal. murrayc.
    virtual void get_value_impl(const iterator& row, int column, Glib::ValueBase& value) const;


    // From TreeDragSource
    virtual bool drag_data_get_vfunc (const Gtk::TreeModel::Path& path, Gtk::SelectionData& selection_data) const;
    virtual bool row_draggable_vfunc(const TreeModel::Path &path) const;
    virtual bool drag_data_delete_vfunc (const Gtk::TreeModel::Path& path);
    
    // From TreeDragDest
    virtual bool row_drop_possible_vfunc(const Gtk::TreeModel::Path& dest, const Gtk::SelectionData& selection_data) const;
    virtual bool drag_data_received_vfunc(const Gtk::TreeModel::Path& dest, const Gtk::SelectionData& selection_data);

  protected:
    virtual bool init_gtktreeiter(GtkTreeIter* it, const bec::NodeId &uid= bec::NodeId()) const;
    void reset_iter(iterator& iter) const throw();

  private:
    bec::ListModel  **_tm;
  protected:
    Gtk::TreeView   *_treeview;
    Gtk::IconView   *_iconview;
    Gtk::Menu       *_context_menu;
    int              _stamp;              //!< stamp to check if an iterator is invalid
    ColumnsModel     _columns;            //!< Generic model of columns
    bec::IconSize    _icon_size;          //!< icon size to set by mode view changed buttons
    struct LMPtr    *_self_ref; // hack to workaround a crash.. 
    bool             _invalid;

    std::string      _name;

    FakeColumnValueGetter _fake_column_value_getter;
    FakeColumnValueSetter _fake_column_value_setter;

    sigc::slot<bool, TreeModel::Path> _row_draggable;
    static void* on_bec_model_destroyed(void*);
};

#endif

//!
//! @}
//!