Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// MasterViewController.m
// Onelab
//
// Created by Maxime Graulich on 08/04/13.
// Copyright (c) 2013 Maxime Graulich. All rights reserved.
//
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController () {
}
@end
@implementation MasterViewController
- (void)awakeFromNib
{
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshObject:) name:@"refreshParameters" object:nil];
runButton = [[UIBarButtonItem alloc] initWithTitle:@"Run" style:UIBarButtonItemStyleBordered target:self action:@selector(runWithNewParameter)];
[runButton setTitle:@"Run"]; // ?? do nothink ??
self.navigationItem.leftBarButtonItem = runButton;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
_sections = [[NSMutableArray alloc] init];
_sectionstitle = [[NSMutableArray alloc] init];
}
- (void) getAvailableParam
{
std::vector<onelab::number> number;
onelab::server::instance()->get(number);
for(int i=0;i<number.size();i++)
{
if(!number[i].getVisible()) continue;
NSString *name=[NSString stringWithCString:number[i].getName().c_str() encoding:[NSString defaultCStringEncoding]];
NSArray *splitedname = [name componentsSeparatedByString:@"/"];
NSString * sectiontitle = [splitedname objectAtIndex:0];
bool found = false;
for(int j=0;j<[_sectionstitle count];j++)
{
NSString *title = [_sectionstitle objectAtIndex:j];
if([title isEqualToString:@"Post proccessing"])
{
[_sections removeObjectAtIndex:j];
[_sectionstitle removeObjectAtIndex:j];
}
if([title isEqualToString:sectiontitle]) // the section already exists
{
NSMutableArray *section = [_sections objectAtIndex:j];
for(int k=0; k<[section count];k++)
if([[section objectAtIndex: k] isEqualToString:name]){found = true; break;}
if(!found)
[section addObject:name];
found = true;
break;
}
}
if(found) continue;
// we have to create the section
NSMutableArray *newSection = [[NSMutableArray alloc] initWithObjects:name, nil];
[_sections addObject:newSection];
[_sectionstitle addObject:sectiontitle];
}
std::vector<onelab::string> string;
onelab::server::instance()->get(string);
for(int i=0;i<string.size();i++)
{
if(!string[i].getVisible()) continue;
if(string[i].getKind() == "file") continue;
NSString *name=[NSString stringWithCString:string[i].getName().c_str() encoding:[NSString defaultCStringEncoding]];
NSArray *splitedname = [name componentsSeparatedByString:@"/"];
NSString * sectiontitle = [splitedname objectAtIndex:0];
bool found = false;
for(int j=0;j<[_sectionstitle count];j++)
{
NSString *title = [_sectionstitle objectAtIndex:j];
if([title isEqualToString:sectiontitle]) // the section already exists
{
NSMutableArray *section = [_sections objectAtIndex:j];
for(int k=0; k<[section count];k++)
if([[section objectAtIndex: k] isEqualToString:name]){found = true; break;} // the parameters is already in the section
if(!found)
[section addObject:name];
found = true;
break;
}
}
if(found) continue;
// we have to create the section
NSMutableArray *newSection = [[NSMutableArray alloc] initWithObjects:name, nil];
[_sections addObject:newSection];
[_sectionstitle addObject:sectiontitle];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self getAvailableParam];
[self.tableView reloadData];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)refreshObject:(id)sender
{
onelab_cb("reset");
[_sections removeAllObjects];
[_sectionstitle removeAllObjects];
[self getAvailableParam]; // Get the param
for(int i=0; i<_sections.count;i++) // Refresh they values
{
NSMutableArray *mSection = [_sections objectAtIndex:i];
for(int n=0; n <mSection.count;n++)
{
NSString *paramName = [mSection objectAtIndex:n];
std::vector<onelab::number> number;
onelab::server::instance()->get(number,[paramName UTF8String]);
if(number.size() > 0){
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:n inSection:i]];
UISlider *slider;
for(UIView *v in cell.subviews)
{
if ([v isKindOfClass:UISlider.class]){
UISlider *slider = (UISlider*)v;
[slider setValue:number[0].getValue()];
break;
}
else if([v isKindOfClass:UIPickerView.class]){
UIView *picker = (UISlider*)v;
int index=0;
for(int i=0;i<number[0].getChoices().size();i++)
if(number[0].getChoices()[i] == number[0].getValue()){index = i; break;}
[(UIPickerView *)picker selectRow:index inComponent:0 animated:YES];
break;
}
else if([v isKindOfClass:UITextField.class])
{
UITextField *text = (UITextField*) v;
[text setText:[NSString stringWithFormat:@"%f", number[0].getValue()]];
break;
}
}
}
std::vector<onelab::string> string;
onelab::server::instance()->get(string,[paramName UTF8String]);
if(string.size() > 0) {
UIView *picker = [self.tableView viewWithTag:(1000*i+n)];
if([picker isKindOfClass:UIPickerView.class]){
int index = -1;
for(int i=0;i<string[0].getChoices().size();i++)
if(string[0].getChoices()[i] == string[0].getValue()){index = i; break;}
if(index >= 0)
[(UIPickerView *)picker selectRow:index inComponent:0 animated:YES];
}
}
}
}
[self.tableView reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName:@"requestRender" object:nil];
}
- (void)runWithNewParameter
{
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[runButton setTitle:@"Stop"];
[self refreshControl];
[runButton setAction:@selector(stopRunning)];
onelab_cb("compute");
[runButton setTitle:@"Run"];
[self refreshControl];
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
[runButton setAction:@selector(runWithNewParameter)];
[self getAvailableParam];
});
}
- (void)stopRunning
{
onelab_cb("stop");
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_sections count];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([_sections count] < section)
return 0;
NSMutableArray *mSection = [_sections objectAtIndex:section];
return [mSection count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// get the param with his name
NSMutableArray *sectionContent = [_sections objectAtIndex:[indexPath section]];
NSString *paramName = [sectionContent objectAtIndex:[indexPath row]];
std::vector<onelab::number> number;
std::vector<onelab::string> string;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:paramName];
if(cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:paramName];
else
return cell;
onelab::server::instance()->get(number,[paramName UTF8String]);
onelab::server::instance()->get(string,[paramName UTF8String]);
if(number.size() > 0)
{
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, cell.frame.size.width - 40, cell.frame.size.height/2)];
if(number[0].getReadOnly())
{
[cell setUserInteractionEnabled:NO];
lbl.alpha = 0.439216f;
}
if(number[0].getLabel() != "")
[lbl setText:[NSString stringWithCString:number[0].getLabel().c_str() encoding:[NSString defaultCStringEncoding]]];
else
[lbl setText:[NSString stringWithCString:number[0].getName().c_str() encoding:[NSString defaultCStringEncoding]]];
[lbl setBackgroundColor:[UIColor clearColor]];
lbl.tag = -(1000 * indexPath.section + indexPath.row);
[cell addSubview:lbl];
if(number[0].getChoices().size() && number[0].getChoices().size() == number[0].getValueLabels().size()) // enumeration
{
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(20, cell.frame.size.height/2+5, cell.frame.size.width - 40, cell.frame.size.height/2)];
picker.showsSelectionIndicator = YES;
picker.tag = 1000 * indexPath.section + indexPath.row;
[picker setDataSource:self];
[picker setDelegate:self];
for(int row=0;row<number[0].getChoices().size();row++)
if(number[0].getValue() == number[0].getChoices()[row])[picker selectRow:row inComponent:0 animated:NO];
[cell addSubview:picker];
}
else if(number[0].getChoices().size() == 2 && number[0].getChoices()[0] == 0 && number[0].getChoices()[1] == 1) // check box
{
UISwitch *swtch = [[UISwitch alloc] initWithFrame:CGRectMake(10, 5, cell.frame.size.width - 40, cell.frame.size.height)];
lbl.tag = 1000 * indexPath.section + indexPath.row;
[swtch setSelected:(number[0].getValue() == 1)];
[lbl setFrame:CGRectMake(100, 5, cell.frame.size.width - 110, cell.frame.size.height)];
[cell addSubview:swtch];
}
else if(number[0].getStep() == 0) // text box
{
UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(20, cell.frame.size.height/2+5, cell.frame.size.width - 40, cell.frame.size.height/2)];
textfield.tag = 1000 * indexPath.section + indexPath.row;
[textfield setBorderStyle:UITextBorderStyleRoundedRect];
[textfield setText:[NSString stringWithFormat:@"%f",number[0].getValue()]];
[textfield setKeyboardType:UIKeyboardTypeNumberPad];
[textfield addTarget:self action:@selector(PViewNbIso:) forControlEvents:UIControlEventValueChanged];
[textfield setDelegate:self];
[cell addSubview:textfield];
}
else // slider with range
{
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20, cell.frame.size.height/2+5, cell.frame.size.width - 40, cell.frame.size.height/2)];
slider.tag = 1000 * indexPath.section + indexPath.row;
[slider setMaximumValue:number[0].getMax()];
[slider setMinimumValue:number[0].getMin()];
[slider setValue:number[0].getValue()];
//TODO add step ?
[slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventTouchUpOutside];
[slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventTouchUpInside];
[lbl setText:[NSString stringWithFormat:@"%@ %f" ,[NSString stringWithCString:number[0].getLabel().c_str() encoding:[NSString defaultCStringEncoding]], number[0].getValue()]];
[cell addSubview:slider];
}
}
else if(string.size() > 0)
{
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, cell.frame.size.width - 40, cell.frame.size.height/2)];
if(string[0].getReadOnly())
{
[cell setUserInteractionEnabled:NO];
lbl.alpha = 0.439216f;
}
[lbl setText:[NSString stringWithCString:string[0].getLabel().c_str() encoding:[NSString defaultCStringEncoding]]];
[lbl setBackgroundColor:[UIColor clearColor]];
lbl.tag = -(1000 * indexPath.section + indexPath.row);
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(20, cell.frame.size.height/2+5, cell.frame.size.width - 40, cell.frame.size.height/2)];
picker.showsSelectionIndicator = YES;
picker.tag = 1000 * indexPath.section + indexPath.row;
[picker setDataSource:self];
[picker setDelegate:self];
for(int row=0;row<string[0].getChoices().size();row++)
if(string[0].getValue() == string[0].getChoices()[row])[picker selectRow:row inComponent:0 animated:NO];
[cell addSubview:picker];
[cell addSubview:lbl];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *paramName = [[_sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
std::vector<onelab::number> number;
onelab::server::instance()->get(number,[paramName UTF8String]);
if(number.size() > 0){
if(number[0].getChoices().size() && number[0].getChoices().size() == number[0].getValueLabels().size()) // enumeration
return 200.f;
if(number[0].getChoices().size() == 2 && number[0].getChoices()[0] == 0 && number[0].getChoices()[1] == 1) // check box
return 40.f;
return 60.f;
}
std::vector<onelab::string> string;
onelab::server::instance()->get(string,[paramName UTF8String]);
if(string.size() > 0) return 200.f;
return 50.f;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_sectionstitle objectAtIndex:section];
}
-(void)sliderValueChanged:(UISlider *)sender
{
int nsection = (int)(sender.tag/1000), nrow = sender.tag%1000;
NSString *title = [[_sections objectAtIndex:nsection] objectAtIndex:nrow];
std::vector<onelab::number> number;
onelab::server::instance()->get(number,[title UTF8String]);
number[0].setValue(sender.value);
onelab::server::instance()->set(number[0]);
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:nrow inSection:nsection]];
UILabel *lbl = nil;
for(UIView *v in cell.subviews)
if ([v isKindOfClass:UILabel.class]){
lbl = (UILabel*)v;
break;
}
if(lbl != nil)
[lbl setText:[NSString stringWithFormat:@"%s %f" ,number[0].getLabel().c_str(), sender.value]];
if(onelab_cb("check") == 1)
[[NSNotificationCenter defaultCenter] postNotificationName:@"requestRender" object:nil];
}
#pragma mark - pickerView
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int nsection = (int)(pickerView.tag/1000), nrow = pickerView.tag%1000;
NSString *title = [[_sections objectAtIndex:nsection] objectAtIndex:nrow];
std::vector<onelab::string> string;
std::vector<onelab::number> number;
onelab::server::instance()->get(string,[title UTF8String]);
onelab::server::instance()->get(number,[title UTF8String]);
if(string.size() > 0) return string[0].getChoices().size();
else if(number.size() > 0) return number[0].getChoices().size();
else return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
int nsection = (int)(pickerView.tag/1000), nrow = pickerView.tag%1000;
NSString *title = [[_sections objectAtIndex:nsection] objectAtIndex:nrow];
std::vector<onelab::string> string;
std::vector<onelab::number> number;
onelab::server::instance()->get(string,[title UTF8String]);
onelab::server::instance()->get(number,[title UTF8String]);
if(string.size() > 0)
{
return [NSString stringWithCString:string[0].getChoices()[row].c_str() encoding:[NSString defaultCStringEncoding]];
}
else if(number.size() > 0) return [NSString stringWithCString:number[0].getValueLabel(number[0].getChoices()[row]).c_str() encoding:[NSString defaultCStringEncoding]];
else return @"?";
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
int nsection = (int)(pickerView.tag/1000), nrow = pickerView.tag%1000;
NSString *title = [[_sections objectAtIndex:nsection] objectAtIndex:nrow];
std::vector<onelab::string> string;
std::vector<onelab::number> number;
onelab::server::instance()->get(string,[title UTF8String]);
onelab::server::instance()->get(number,[title UTF8String]);
int index = [pickerView selectedRowInComponent:0];
if(string.size() > 0)
{
std::string selected = string[0].getChoices()[index];
string[0].setValue(selected);
onelab::server::instance()->set(string[0]);
}
else if(number.size() > 0)
{
double selected = number[0].getChoices()[index];
number[0].setValue(selected);
onelab::server::instance()->set(number[0]);
}
if(onelab_cb("check") == 1)
[[NSNotificationCenter defaultCenter] postNotificationName:@"requestRender" object:nil];
}
#pragma mark - textfield
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
int nsection = (int)(textField.tag/1000), nrow = textField.tag%1000;
NSString *title = [[_sections objectAtIndex:nsection] objectAtIndex:nrow];
std::vector<onelab::string> string;
std::vector<onelab::number> number;
onelab::server::instance()->get(string,[title UTF8String]);
onelab::server::instance()->get(number,[title UTF8String]);
if(number.size() > 0)
{
number[0].setValue([textField.text doubleValue]);
onelab::server::instance()->set(number[0]);
}
else if(string.size() > 0)
{
string[0].setValue([textField.text UTF8String]);
onelab::server::instance()->set(string[0]);
}
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField endEditing:YES];
return YES;
}
@end