Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame^] | 1 | /* |
| 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 Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 30 | #import "QNSTreeController.h" |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 31 | |
| 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 | |
| 51 | - (void) setName: (NSString*) newName |
| 52 | { |
| 53 | #ifdef REARRANGE_FROM_SETNAME |
| 54 | [treeController rearrangeObjects]; |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | @end |
| 59 | |
| 60 | |
| 61 | @implementation QNSTreeController |
| 62 | |
| 63 | - (id) initWithQModel:(QAbstractItemModel*) model |
| 64 | { |
| 65 | [super init]; |
| 66 | NSLog(@"init Tree..."); |
| 67 | self->privateQModel = model; |
| 68 | |
| 69 | topNodes = [[NSMutableArray alloc] init]; |
| 70 | [self connect]; |
| 71 | |
| 72 | return [self initWithContent:topNodes]; |
| 73 | } |
| 74 | |
| 75 | - (BOOL)isEditable |
| 76 | { |
| 77 | return self->privateQModel->flags(self->privateQModel->index(0, 0)) | Qt::ItemIsEditable; |
| 78 | } |
| 79 | |
| 80 | - (void)connect |
| 81 | { |
| 82 | QObject::connect(self->privateQModel, |
| 83 | &QAbstractItemModel::rowsInserted, |
| 84 | [=](const QModelIndex & parent, int first, int last) { |
| 85 | for( int row = first; row <= last; row++) { |
| 86 | if(!parent.isValid()) { |
| 87 | //Inserting topnode |
| 88 | Node* n = [[Node alloc] init]; |
| 89 | [self insertObject:n atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]]; |
| 90 | } else { |
| 91 | Node* child = [[Node alloc] init]; |
| 92 | NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row}; |
| 93 | [self insertObject:child atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]]; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | ); |
| 98 | |
| 99 | QObject::connect(self->privateQModel, |
| 100 | &QAbstractItemModel::rowsAboutToBeMoved, |
| 101 | [=](const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) { |
| 102 | NSLog(@"rows about to be moved, start: %d, end: %d, moved to: %d", sourceStart, sourceEnd, destinationRow); |
| 103 | /* first remove the row from old location |
| 104 | * then insert them at the new location on the "rowsMoved signal */ |
| 105 | for( int row = sourceStart; row <= sourceEnd; row++) { |
| 106 | //TODO |
| 107 | } |
| 108 | } |
| 109 | ); |
| 110 | |
| 111 | QObject::connect(self->privateQModel, |
| 112 | &QAbstractItemModel::rowsMoved, |
| 113 | [=](const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) { |
| 114 | //NSLog(@"rows moved, start: %d, end: %d, moved to: %d", sourceStart, sourceEnd, destinationRow); |
| 115 | /* these rows should have been removed in the "rowsAboutToBeMoved" handler |
| 116 | * now insert them in the new location */ |
| 117 | for( int row = sourceStart; row <= sourceEnd; row++) { |
| 118 | //TODO |
| 119 | } |
| 120 | } |
| 121 | ); |
| 122 | |
| 123 | QObject::connect(self->privateQModel, |
| 124 | &QAbstractItemModel::rowsAboutToBeRemoved, |
| 125 | [=](const QModelIndex & parent, int first, int last) { |
| 126 | //NSLog(@"rows about to be removed"); |
| 127 | for( int row = first; row <= last; row++) { |
| 128 | if(topNodes.count <= parent.row()) |
| 129 | { |
| 130 | NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row}; |
| 131 | [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]]; |
| 132 | } else { |
| 133 | NSLog(@"Removing rows not in tree!"); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | ); |
| 138 | |
| 139 | QObject::connect(self->privateQModel, |
| 140 | &QAbstractItemModel::rowsAboutToBeRemoved, |
| 141 | [=](const QModelIndex & parent, int first, int last) { |
| 142 | NSLog(@"rows about to be removed"); |
| 143 | // for( int row = first; row <= last; row++) { |
| 144 | // if(topNodes.count <= parent.row()) |
| 145 | // { |
| 146 | // NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row}; |
| 147 | // [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]]; |
| 148 | // } else { |
| 149 | // NSLog(@"Removing rows not in tree!"); |
| 150 | // } |
| 151 | // } |
| 152 | } |
| 153 | ); |
| 154 | |
| 155 | QObject::connect(self->privateQModel, |
| 156 | &QAbstractItemModel::rowsRemoved, |
| 157 | [=](const QModelIndex & parent, int first, int last) { |
| 158 | NSLog(@"rows removed"); |
| 159 | for( int row = first; row <= last; row++) { |
| 160 | if(parent.isValid()) |
| 161 | { |
| 162 | //Removing leaf |
| 163 | NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row}; |
| 164 | [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]]; |
| 165 | } else |
| 166 | { |
| 167 | [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]]; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | ); |
| 172 | |
| 173 | QObject::connect(self->privateQModel, |
| 174 | &QAbstractItemModel::layoutChanged, |
| 175 | [=]() { |
| 176 | //NSLog(@"layout changed"); |
| 177 | } |
| 178 | ); |
| 179 | |
| 180 | } |
| 181 | |
| 182 | @end |