blob: 75533c25b9a676f57cabeda803a54e305b61b846 [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;
87 if(idx.length == 2)
88 toReturn = self->privateQModel->index(myArray[1], 0, self->privateQModel->index(myArray[0], 0));
89 else
90 toReturn = self->privateQModel->index(myArray[0], 0);
91 return toReturn;
92}
93
Alexandre Lision5855b6a2015-02-03 11:31:05 -050094- (void)connect
95{
96 QObject::connect(self->privateQModel,
97 &QAbstractItemModel::rowsInserted,
98 [=](const QModelIndex & parent, int first, int last) {
99 for( int row = first; row <= last; row++) {
100 if(!parent.isValid()) {
101 //Inserting topnode
102 Node* n = [[Node alloc] init];
103 [self insertObject:n atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]];
104 } else {
105 Node* child = [[Node alloc] init];
106 NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row};
107 [self insertObject:child atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]];
108 }
109 }
110 }
111 );
112
113 QObject::connect(self->privateQModel,
114 &QAbstractItemModel::rowsAboutToBeMoved,
115 [=](const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) {
116 NSLog(@"rows about to be moved, start: %d, end: %d, moved to: %d", sourceStart, sourceEnd, destinationRow);
117 /* first remove the row from old location
118 * then insert them at the new location on the "rowsMoved signal */
119 for( int row = sourceStart; row <= sourceEnd; row++) {
120 //TODO
121 }
122 }
123 );
124
125 QObject::connect(self->privateQModel,
126 &QAbstractItemModel::rowsMoved,
127 [=](const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) {
128 //NSLog(@"rows moved, start: %d, end: %d, moved to: %d", sourceStart, sourceEnd, destinationRow);
129 /* these rows should have been removed in the "rowsAboutToBeMoved" handler
130 * now insert them in the new location */
131 for( int row = sourceStart; row <= sourceEnd; row++) {
132 //TODO
133 }
134 }
135 );
136
137 QObject::connect(self->privateQModel,
138 &QAbstractItemModel::rowsAboutToBeRemoved,
139 [=](const QModelIndex & parent, int first, int last) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400140 NSLog(@"rows about to be removed");
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500141 }
142 );
143
144 QObject::connect(self->privateQModel,
145 &QAbstractItemModel::rowsRemoved,
146 [=](const QModelIndex & parent, int first, int last) {
Alexandre Lisionf5b90da2015-03-20 18:13:54 -0400147 //NSLog(@"rows removed");
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500148 for( int row = first; row <= last; row++) {
149 if(parent.isValid())
150 {
151 //Removing leaf
152 NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row};
153 [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]];
154 } else
155 {
156 [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]];
157 }
158 }
159 }
160 );
161
162 QObject::connect(self->privateQModel,
163 &QAbstractItemModel::layoutChanged,
164 [=]() {
Alexandre Lisionf5b90da2015-03-20 18:13:54 -0400165 //NSLog(@"layout changed");
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500166 }
167 );
168
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400169 QObject::connect(self->privateQModel,
170 &QAbstractItemModel::dataChanged,
171 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionf5b90da2015-03-20 18:13:54 -0400172 //NSLog(@"data changed");
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400173 for(int row = topLeft.row() ; row <= bottomRight.row() ; ++row)
174 {
175 QModelIndex tmpIdx = self->privateQModel->index(row, 0);
176 if(tmpIdx.row() >= [self.arrangedObjects count]) {
177 Node* n = [[Node alloc] init];
178 if(tmpIdx.isValid())
179 [self insertObject:n atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]];
180 }
181 }
182 });
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500183}
184
185@end