blob: eca4884808c02cde1dbd492513d435d738c5fd3d [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
2 * Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
3 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
Alexandre Lision5855b6a2015-02-03 11:31:05 -050030#import "QNSTreeController.h"
Alexandre Lision5855b6a2015-02-03 11:31:05 -050031
32@interface Node : NSObject {
33 NSMutableArray *children;
34}
35@end
36
37@implementation Node
38- (id) init
39{
40 if (self = [super init]) {
41 children = [[NSMutableArray alloc] init];
42 }
43 return self;
44}
45
46- (void) addChild:(Node*) child
47{
48 [children addObject:child];
49}
50
Alexandre Lision5855b6a2015-02-03 11:31:05 -050051@end
52
53
54@implementation QNSTreeController
55
56- (id) initWithQModel:(QAbstractItemModel*) model
57{
58 [super init];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050059 self->privateQModel = model;
60
61 topNodes = [[NSMutableArray alloc] init];
62 [self connect];
63
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040064 [self populate];
65
Alexandre Lision5855b6a2015-02-03 11:31:05 -050066 return [self initWithContent:topNodes];
67}
68
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040069-(void) populate
70{
71 for (int i =0 ; i < self->privateQModel->rowCount() ; ++i){
72 [topNodes insertObject:[[Node alloc] init] atIndex:i];
73 }
74}
75
Alexandre Lision5855b6a2015-02-03 11:31:05 -050076- (BOOL)isEditable
77{
78 return self->privateQModel->flags(self->privateQModel->index(0, 0)) | Qt::ItemIsEditable;
79}
80
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040081- (QModelIndex) toQIdx:(NSTreeNode*) node
82{
83 NSIndexPath* idx = node.indexPath;
84 NSUInteger myArray[[idx length]];
85 [idx getIndexes:myArray];
86 QModelIndex toReturn;
Alexandre Lision3b0bd332015-03-15 18:43:07 -040087
88 for (int i = 0; i < idx.length; ++i) {
89 toReturn = self->privateQModel->index(myArray[i], 0, toReturn);
90 }
91
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040092 return toReturn;
93}
94
Alexandre Lision3b0bd332015-03-15 18:43:07 -040095- (void) insertChildAtQIndex:(QModelIndex) qIdx
96{
97 Node* child = [[Node alloc] init];
98
99 QModelIndex tmp = qIdx.parent();
100 NSMutableArray* allIndexes = [NSMutableArray array];
101 while (tmp.isValid()) {
102 [allIndexes insertObject:@(tmp.row()) atIndex:0];
103 tmp = tmp.parent();
104 }
105 [allIndexes insertObject:@(qIdx.row()) atIndex:allIndexes.count];
106
107 NSUInteger indexes[allIndexes.count];
108 for (int i = 0 ; i < allIndexes.count ; ++i) {
109 indexes[i] = [[allIndexes objectAtIndex:i] intValue];
110 }
111 [self insertObject:child atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:allIndexes.count]];
112}
113
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500114- (void)connect
115{
116 QObject::connect(self->privateQModel,
117 &QAbstractItemModel::rowsInserted,
118 [=](const QModelIndex & parent, int first, int last) {
119 for( int row = first; row <= last; row++) {
120 if(!parent.isValid()) {
121 //Inserting topnode
122 Node* n = [[Node alloc] init];
123 [self insertObject:n atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]];
124 } else {
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400125 [self insertChildAtQIndex:self->privateQModel->index(row, 0, parent)];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500126 }
127 }
128 }
129 );
130
131 QObject::connect(self->privateQModel,
132 &QAbstractItemModel::rowsAboutToBeMoved,
133 [=](const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) {
134 NSLog(@"rows about to be moved, start: %d, end: %d, moved to: %d", sourceStart, sourceEnd, destinationRow);
135 /* first remove the row from old location
136 * then insert them at the new location on the "rowsMoved signal */
137 for( int row = sourceStart; row <= sourceEnd; row++) {
138 //TODO
139 }
140 }
141 );
142
143 QObject::connect(self->privateQModel,
144 &QAbstractItemModel::rowsMoved,
145 [=](const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) {
146 //NSLog(@"rows moved, start: %d, end: %d, moved to: %d", sourceStart, sourceEnd, destinationRow);
147 /* these rows should have been removed in the "rowsAboutToBeMoved" handler
148 * now insert them in the new location */
149 for( int row = sourceStart; row <= sourceEnd; row++) {
150 //TODO
151 }
152 }
153 );
154
155 QObject::connect(self->privateQModel,
156 &QAbstractItemModel::rowsAboutToBeRemoved,
157 [=](const QModelIndex & parent, int first, int last) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400158 NSLog(@"rows about to be removed");
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500159 }
160 );
161
162 QObject::connect(self->privateQModel,
163 &QAbstractItemModel::rowsRemoved,
164 [=](const QModelIndex & parent, int first, int last) {
Alexandre Lisionf5b90da2015-03-20 18:13:54 -0400165 //NSLog(@"rows removed");
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500166 for( int row = first; row <= last; row++) {
167 if(parent.isValid())
168 {
169 //Removing leaf
170 NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row};
171 [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]];
172 } else
173 {
174 [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]];
175 }
176 }
177 }
178 );
179
180 QObject::connect(self->privateQModel,
181 &QAbstractItemModel::layoutChanged,
182 [=]() {
Alexandre Lisionf5b90da2015-03-20 18:13:54 -0400183 //NSLog(@"layout changed");
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500184 }
185 );
186
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400187 QObject::connect(self->privateQModel,
188 &QAbstractItemModel::dataChanged,
189 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionf5b90da2015-03-20 18:13:54 -0400190 //NSLog(@"data changed");
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400191 for(int row = topLeft.row() ; row <= bottomRight.row() ; ++row)
192 {
193 QModelIndex tmpIdx = self->privateQModel->index(row, 0);
194 if(tmpIdx.row() >= [self.arrangedObjects count]) {
195 Node* n = [[Node alloc] init];
196 if(tmpIdx.isValid())
197 [self insertObject:n atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]];
198 }
199 }
200 });
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500201}
202
203@end