~ubuntu-app-review-contributors/ubuntu-app-reviews/beatbox

« back to all changes in this revision

Viewing changes to src/Grids/FastGridModel.vala

  • Committer: App Bot
  • Author(s): Scott Ringwelski
  • Date: 2012-07-08 12:10:10 UTC
  • Revision ID: appbot@holba.ch-20120708121010-6bhuqykl5wo3pq8t
Tags: 0.5+r675-0~quantal1
Auto build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-
 
2
 * Copyright (c) 2011-2012       Scott Ringwelski <sgringwe@mtu.edu>
 
3
 *
 
4
 * Originally Written by Scott Ringwelski for BeatBox Music Player
 
5
 * BeatBox Music Player: http://www.launchpad.net/beat-box
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with this library; if not, write to the
 
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
using Gtk;
 
24
 
 
25
/** Since this class is not publicly facing (the FastView is public part),
 
26
 * this model is low level and optimized. We are not worried about stupid
 
27
 * users here.
 
28
**/
 
29
public class BeatBox.FastGridModel : GLib.Object, TreeModel, TreeDragSource {
 
30
        int stamp; // all iters must match this
 
31
        
 
32
        /* data storage variables */
 
33
        HashTable<int, GLib.Object> rows; // internal id -> user specified object
 
34
        GLib.Object default_value; // Used when calling append
 
35
        
 
36
        /* user specific function for get_value() */
 
37
        public delegate Value ValueReturnFunc (int row, int column, GLib.Object o);
 
38
        private unowned ValueReturnFunc value_func;
 
39
        
 
40
        /** Initialize data storage, columns, etc. **/
 
41
        public FastGridModel(GLib.Object default_value) {
 
42
                rows = new HashTable<int, GLib.Object>(null, null);
 
43
                this.default_value = default_value;
 
44
                
 
45
                stamp = (int)GLib.Random.next_int();
 
46
        }
 
47
 
 
48
        public Type get_column_type (int col) {
 
49
                if(col == 0)
 
50
                        return typeof(Gdk.Pixbuf);
 
51
                else if(col == 1)
 
52
                        return typeof(string);
 
53
                else if(col == 2)
 
54
                        return typeof(string);
 
55
                else
 
56
                        return typeof(GLib.Object);
 
57
        }
 
58
 
 
59
        public TreeModelFlags get_flags () {
 
60
                return TreeModelFlags.LIST_ONLY;
 
61
        }
 
62
 
 
63
        public bool get_iter (out TreeIter iter, TreePath path) {
 
64
                iter = TreeIter();
 
65
                int path_index = path.get_indices()[0];
 
66
                if(rows.size() == 0 || path_index < 0 || path_index >= rows.size() || rows.get(path_index) == null)
 
67
                        return false;
 
68
 
 
69
                iter.stamp = this.stamp;
 
70
                iter.user_data = (void*)path_index;
 
71
 
 
72
                return true;
 
73
        }
 
74
 
 
75
        public int get_n_columns () {
 
76
                return 4;
 
77
        }
 
78
 
 
79
        public TreePath? get_path (TreeIter iter) {
 
80
                return new TreePath.from_string (((int)iter.user_data).to_string());
 
81
        }
 
82
        
 
83
        public void get_value (TreeIter iter, int column, out Value val) {
 
84
                val = Value(get_column_type(column));
 
85
                
 
86
                if(iter.stamp != this.stamp || column < 0 || column >= get_n_columns()) {
 
87
                        return;
 
88
                }
 
89
                
 
90
                int row = (int)iter.user_data;
 
91
                if(!(row >= rows.size())) {
 
92
                        var object = rows.get(row);
 
93
                        val = value_func(row, column, object);
 
94
                }
 
95
        }
 
96
 
 
97
        public bool iter_children (out TreeIter iter, TreeIter? parent) {
 
98
                iter = TreeIter();
 
99
                return false;
 
100
        }
 
101
 
 
102
        public bool iter_has_child (TreeIter iter) {
 
103
 
 
104
                return false;
 
105
        }
 
106
 
 
107
        public int iter_n_children (TreeIter? iter) {
 
108
                if(iter == null)
 
109
                        return (int)rows.size();
 
110
 
 
111
                return 0;
 
112
        }
 
113
 
 
114
        public bool iter_next (ref TreeIter iter) {
 
115
                if(iter.stamp != this.stamp)
 
116
                        return false;
 
117
 
 
118
                iter.user_data = (void*)(((int)iter.user_data) + 1);
 
119
 
 
120
                if(((int)iter.user_data) >= rows.size())
 
121
                        return false;
 
122
 
 
123
                return true;
 
124
        }
 
125
 
 
126
        public bool iter_nth_child (out TreeIter iter, TreeIter? parent, int n) {
 
127
                iter = TreeIter();
 
128
 
 
129
                if(n < 0 || n >= rows.size() || parent != null)
 
130
                        return false;
 
131
 
 
132
                iter.stamp = this.stamp;
 
133
                iter.user_data = (void*)n;
 
134
 
 
135
                return true;
 
136
        }
 
137
 
 
138
        public bool iter_parent (out TreeIter iter, TreeIter child) {
 
139
                iter = TreeIter();
 
140
 
 
141
                return false;
 
142
        }
 
143
        
 
144
        public void append (out TreeIter iter) {
 
145
                iter = TreeIter();
 
146
                
 
147
                TreePath path = new TreePath.from_string(((int)rows.size()).to_string());
 
148
                rows.set((int)rows.size(), default_value);
 
149
                iter.stamp = this.stamp;
 
150
                iter.user_data = (void*)rows.size;
 
151
                
 
152
                row_inserted(path, iter);
 
153
        }
 
154
        
 
155
        public void remove (TreeIter iter) {
 
156
                if(iter.stamp != this.stamp)
 
157
                        return;
 
158
 
 
159
                var path = new TreePath.from_string(((int)iter.user_data).to_string());
 
160
                rows.remove((int)iter.user_data);
 
161
                row_deleted(path);
 
162
                
 
163
                // TODO: swap all indices > this iter's index down to maintain that
 
164
                // the table has row ids 0..n where n is rows.size (consecutive ids)
 
165
        }
 
166
        
 
167
        // Not applicable to this custom treemodel
 
168
        public new void set (TreeIter iter, ...) {
 
169
                return;
 
170
        }
 
171
        
 
172
        public void ref_node (TreeIter iter) {}
 
173
        public void unref_node (TreeIter iter) {}
 
174
 
 
175
        /** The beauty of this custom model. This tree model is simply a visual
 
176
         * representation of a HashTable of objects. Before calling this
 
177
         * method, the user should set tree_view.set_model(null). After
 
178
         * calling this, set the tree_view.set_model(fast_model). By doing this
 
179
         * the treeview will not listen for append events and will recalculate
 
180
         * and draw when the model is re-added.
 
181
         * 
 
182
         * @objects Must be a consecutive ordered hash table with indexes 
 
183
         * 0-n where n is size of the hashtable (no gaps).
 
184
        **/
 
185
        public void set_table (HashTable<int, GLib.Object> table) {
 
186
                rows.remove_all();
 
187
                for(int i = 0; i < table.size(); ++i)
 
188
                        rows.set(i, table.get(i));
 
189
        }
 
190
        
 
191
        /** Crucial. Must be set by user. Allows for this model to be abstract
 
192
         * by allowing the user to specify the function that returns values
 
193
         * based on the object (row) and column. **/
 
194
        public void set_value_func (ValueReturnFunc func) {
 
195
                value_func = func;
 
196
        }
 
197
        
 
198
        public void update_row (int index) {
 
199
                TreePath path = new TreePath.from_string(index.to_string());
 
200
                TreeIter iter = TreeIter();
 
201
                iter.stamp = this.stamp;
 
202
                iter.user_data = (void*)index;
 
203
                
 
204
                row_changed(path, iter);
 
205
        }
 
206
        
 
207
        /************************************
 
208
         * Drag'n'drop
 
209
         ************************************/
 
210
        bool drag_data_delete(TreePath path) {
 
211
                return false;
 
212
        }
 
213
        
 
214
        bool drag_data_get(TreePath path, SelectionData data) {
 
215
                /*string[] old = data.get_uris();
 
216
                string[] cp = new string[old.length + 1];
 
217
                for(int i = 0; i < old.length; ++i)
 
218
                        cp[i] = old[i];
 
219
                
 
220
                cp[cp.length - 1] = rows[int.parse(path.to_string())].uri;*/
 
221
                
 
222
                return true;
 
223
        }
 
224
        
 
225
        bool row_draggable(TreePath path) {
 
226
                return true;
 
227
        }
 
228
}