Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame^] | 1 | // |
| 2 | // HistoryTreeController.m |
| 3 | // Ring |
| 4 | // |
| 5 | // Created by Alexandre Lision on 2015-01-28. |
| 6 | // |
| 7 | // |
| 8 | |
| 9 | #import "QNSTreeController.h" |
| 10 | #import "MinimalHistoryBackend.h" |
| 11 | |
| 12 | @interface Node : NSObject { |
| 13 | NSMutableArray *children; |
| 14 | } |
| 15 | @end |
| 16 | |
| 17 | @implementation Node |
| 18 | - (id) init |
| 19 | { |
| 20 | if (self = [super init]) { |
| 21 | children = [[NSMutableArray alloc] init]; |
| 22 | } |
| 23 | return self; |
| 24 | } |
| 25 | |
| 26 | - (void) addChild:(Node*) child |
| 27 | { |
| 28 | [children addObject:child]; |
| 29 | } |
| 30 | |
| 31 | - (void) setName: (NSString*) newName |
| 32 | { |
| 33 | #ifdef REARRANGE_FROM_SETNAME |
| 34 | [treeController rearrangeObjects]; |
| 35 | #endif |
| 36 | } |
| 37 | |
| 38 | @end |
| 39 | |
| 40 | |
| 41 | @implementation QNSTreeController |
| 42 | |
| 43 | - (id) initWithQModel:(QAbstractItemModel*) model |
| 44 | { |
| 45 | [super init]; |
| 46 | NSLog(@"init Tree..."); |
| 47 | self->privateQModel = model; |
| 48 | |
| 49 | topNodes = [[NSMutableArray alloc] init]; |
| 50 | [self connect]; |
| 51 | |
| 52 | return [self initWithContent:topNodes]; |
| 53 | } |
| 54 | |
| 55 | - (BOOL)isEditable |
| 56 | { |
| 57 | return self->privateQModel->flags(self->privateQModel->index(0, 0)) | Qt::ItemIsEditable; |
| 58 | } |
| 59 | |
| 60 | - (void)connect |
| 61 | { |
| 62 | QObject::connect(self->privateQModel, |
| 63 | &QAbstractItemModel::rowsInserted, |
| 64 | [=](const QModelIndex & parent, int first, int last) { |
| 65 | for( int row = first; row <= last; row++) { |
| 66 | if(!parent.isValid()) { |
| 67 | //Inserting topnode |
| 68 | Node* n = [[Node alloc] init]; |
| 69 | [self insertObject:n atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]]; |
| 70 | } else { |
| 71 | Node* child = [[Node alloc] init]; |
| 72 | NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row}; |
| 73 | [self insertObject:child atArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]]; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | ); |
| 78 | |
| 79 | QObject::connect(self->privateQModel, |
| 80 | &QAbstractItemModel::rowsAboutToBeMoved, |
| 81 | [=](const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) { |
| 82 | NSLog(@"rows about to be moved, start: %d, end: %d, moved to: %d", sourceStart, sourceEnd, destinationRow); |
| 83 | /* first remove the row from old location |
| 84 | * then insert them at the new location on the "rowsMoved signal */ |
| 85 | for( int row = sourceStart; row <= sourceEnd; row++) { |
| 86 | //TODO |
| 87 | } |
| 88 | } |
| 89 | ); |
| 90 | |
| 91 | QObject::connect(self->privateQModel, |
| 92 | &QAbstractItemModel::rowsMoved, |
| 93 | [=](const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) { |
| 94 | //NSLog(@"rows moved, start: %d, end: %d, moved to: %d", sourceStart, sourceEnd, destinationRow); |
| 95 | /* these rows should have been removed in the "rowsAboutToBeMoved" handler |
| 96 | * now insert them in the new location */ |
| 97 | for( int row = sourceStart; row <= sourceEnd; row++) { |
| 98 | //TODO |
| 99 | } |
| 100 | } |
| 101 | ); |
| 102 | |
| 103 | QObject::connect(self->privateQModel, |
| 104 | &QAbstractItemModel::rowsAboutToBeRemoved, |
| 105 | [=](const QModelIndex & parent, int first, int last) { |
| 106 | //NSLog(@"rows about to be removed"); |
| 107 | for( int row = first; row <= last; row++) { |
| 108 | if(topNodes.count <= parent.row()) |
| 109 | { |
| 110 | NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row}; |
| 111 | [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]]; |
| 112 | } else { |
| 113 | NSLog(@"Removing rows not in tree!"); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | ); |
| 118 | |
| 119 | QObject::connect(self->privateQModel, |
| 120 | &QAbstractItemModel::rowsAboutToBeRemoved, |
| 121 | [=](const QModelIndex & parent, int first, int last) { |
| 122 | NSLog(@"rows about to be removed"); |
| 123 | // for( int row = first; row <= last; row++) { |
| 124 | // if(topNodes.count <= parent.row()) |
| 125 | // { |
| 126 | // NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row}; |
| 127 | // [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]]; |
| 128 | // } else { |
| 129 | // NSLog(@"Removing rows not in tree!"); |
| 130 | // } |
| 131 | // } |
| 132 | } |
| 133 | ); |
| 134 | |
| 135 | QObject::connect(self->privateQModel, |
| 136 | &QAbstractItemModel::rowsRemoved, |
| 137 | [=](const QModelIndex & parent, int first, int last) { |
| 138 | NSLog(@"rows removed"); |
| 139 | for( int row = first; row <= last; row++) { |
| 140 | if(parent.isValid()) |
| 141 | { |
| 142 | //Removing leaf |
| 143 | NSUInteger indexes[] = { (NSUInteger)parent.row(), (NSUInteger)row}; |
| 144 | [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndexes:indexes length:2]]; |
| 145 | } else |
| 146 | { |
| 147 | [self removeObjectAtArrangedObjectIndexPath:[[NSIndexPath alloc] initWithIndex:row]]; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | ); |
| 152 | |
| 153 | QObject::connect(self->privateQModel, |
| 154 | &QAbstractItemModel::layoutChanged, |
| 155 | [=]() { |
| 156 | //NSLog(@"layout changed"); |
| 157 | } |
| 158 | ); |
| 159 | |
| 160 | } |
| 161 | |
| 162 | @end |