~ubuntu-branches/ubuntu/raring/voxbo/raring

« back to all changes in this revision

Viewing changes to vbwidgets/covariates.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-06-06 11:33:11 UTC
  • Revision ID: james.westby@ubuntu.com-20100606113311-v3c13imdkkd5n7ae
Tags: upstream-1.8.5~svn1172
ImportĀ upstreamĀ versionĀ 1.8.5~svn1172

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// covariates.cpp
 
3
// Copyright (c) 2010 by The VoxBo Development Team
 
4
 
 
5
// This file is part of VoxBo
 
6
// 
 
7
// VoxBo is free software: you can redistribute it and/or modify it
 
8
// under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation, either version 3 of the License, or
 
10
// (at your option) any later version.
 
11
// 
 
12
// VoxBo is distributed in the hope that it will be useful, but
 
13
// WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
// General Public License for more details.
 
16
// 
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with VoxBo.  If not, see <http://www.gnu.org/licenses/>.
 
19
// 
 
20
// For general information on VoxBo, including the latest complete
 
21
// source code and binary distributions, manual, and associated files,
 
22
// see the VoxBo home page at: http://www.voxbo.org/
 
23
// 
 
24
// original version written by Tom King?
 
25
 
 
26
#include "covariates.h"
 
27
#include <qmessagebox.h>
 
28
#include <q3header.h>
 
29
 
 
30
using namespace VB;
 
31
using namespace std;
 
32
 
 
33
/*---------------------------------------------------------------------------*/
 
34
 
 
35
// Covariate
 
36
 
 
37
Covariate::Covariate(QObject * parent, const char * name)
 
38
  : QObject(parent, name)
 
39
{
 
40
}
 
41
 
 
42
Covariate::Covariate(const Covariate& aCov)
 
43
  : mName(aCov.mName), mInterest(aCov.mInterest)
 
44
{
 
45
}
 
46
 
 
47
Covariate::InterestType Covariate::str2type(const std::string& str)
 
48
{
 
49
  // Looks at the first character in str and returns the appropriate type.
 
50
  
 
51
  char interest = str[0];
 
52
  switch (interest)
 
53
  {
 
54
    case 'I': return Covariate::INTEREST_I;
 
55
    case 'N': return Covariate::INTEREST_N;
 
56
    case 'K': return Covariate::INTEREST_K;
 
57
    case 'U': return Covariate::INTEREST_U;
 
58
    case 'D': return Covariate::INTEREST_D;
 
59
    default:  return Covariate::INTEREST_ERR;
 
60
  }
 
61
}
 
62
 
 
63
std::string Covariate::type2str(Covariate::InterestType inter)
 
64
{
 
65
  // Looks at the first character in str and returns the appropriate type.
 
66
  
 
67
  switch (inter)
 
68
  {
 
69
    case Covariate::INTEREST_I: return "I";
 
70
    case Covariate::INTEREST_N: return "N";
 
71
    case Covariate::INTEREST_K: return "K";
 
72
    case Covariate::INTEREST_U: return "U";
 
73
    case Covariate::INTEREST_D: return "D";
 
74
    default:                    return "error";
 
75
  }
 
76
}
 
77
 
 
78
void Covariate::setName(const std::string& aName)
 
79
{
 
80
  mName = aName;
 
81
  emit nameChanged(aName);
 
82
}
 
83
 
 
84
void Covariate::setType(Covariate::InterestType aType)
 
85
{
 
86
  mInterest = aType;
 
87
  emit typeChanged(aType);
 
88
}
 
89
 
 
90
const std::string& Covariate::getName() const
 
91
{
 
92
  return mName;
 
93
}
 
94
 
 
95
const Covariate::InterestType& Covariate::getType() const
 
96
{
 
97
  return mInterest;
 
98
}
 
99
 
 
100
Covariate& Covariate::operator=(const Covariate& aCov)
 
101
{
 
102
  mName = aCov.mName;
 
103
  mInterest = aCov.mInterest;
 
104
  return *this;
 
105
}
 
106
 
 
107
/*---------------------------------------------------------------------------*/
 
108
 
 
109
// Contrast
 
110
 
 
111
Contrast::Contrast(std::vector<Covariate>& aCovList,
 
112
                   QObject * parent, const char * name)
 
113
  : QObject(parent, name), Covariates(aCovList)
 
114
{
 
115
}
 
116
 
 
117
Contrast::Parameter& Contrast::operator[](int aIndex)
 
118
{
 
119
  return Weights[aIndex];
 
120
}
 
121
 
 
122
Contrast::Parameter& Contrast::operator[](std::string aCovName)
 
123
{
 
124
  int index = 0;
 
125
  for (vector<Covariate>::iterator iter = Covariates.begin();
 
126
       iter != Covariates.end();
 
127
       ++iter)
 
128
  {
 
129
    if (iter->getName() == aCovName) break;
 
130
    ++index;
 
131
  }
 
132
  
 
133
  // i'm not doing bounds checking.  maybe VoxBo should start throwing around
 
134
  // exceptions.  i've always liked the idea of exceptions.
 
135
  return Weights[index];
 
136
}
 
137
 
 
138
/********************************************************************************
 
139
 * VB::CovatiatesView class
 
140
 ********************************************************************************/
 
141
 
 
142
const char* CovariatesView::NAME_COL = "Name";
 
143
const char* CovariatesView::ID_COL = "ID";
 
144
const char* CovariatesView::TYPE_COL = "Type";
 
145
 
 
146
CovariatesView::CovariatesView(QWidget *parent, const char *name)
 
147
  : Q3ListView(parent,name)
 
148
{
 
149
  setSelectionMode(Q3ListView::Extended);
 
150
  setRootIsDecorated(true);
 
151
  setupColumns();
 
152
 
 
153
  connect( this, SIGNAL( selectionChanged( ) ),
 
154
           this, SLOT( onSelectionChanged( ) ) );
 
155
}
 
156
 
 
157
void CovariatesView::setupColumns()
 
158
{
 
159
  addColumn(NAME_COL);
 
160
  addColumn(TYPE_COL);
 
161
  setColumnAlignment(1, Qt::AlignHCenter);
 
162
  addColumn(ID_COL);
 
163
  setColumnAlignment(2, Qt::AlignHCenter);
 
164
  setSortColumn(-1);
 
165
}
 
166
 
 
167
void CovariatesView::buildTree(GLMInfo* aGLMInfo, bool aShowAll)
 
168
{
 
169
  std::vector<std::string> names;
 
170
  std::vector<std::string> types;
 
171
  
 
172
  // Prepare and build the parameter tree
 
173
  std::vector<std::string>::iterator name_iter;
 
174
  for (name_iter = aGLMInfo->cnames.begin();
 
175
       name_iter != aGLMInfo->cnames.end();
 
176
       ++name_iter)
 
177
  {
 
178
    names.push_back(name_iter->substr(1));
 
179
    types.push_back(name_iter->substr(0,1));
 
180
  }
 
181
  buildTree(names, types, aShowAll);
 
182
}
 
183
 
 
184
void CovariatesView::buildTree(std::vector<VB::Covariate>& aCovList, bool aShowAll)
 
185
{
 
186
  vector<string> name_list;
 
187
  vector<string> type_list;
 
188
  
 
189
  string name;
 
190
  string interest;
 
191
  
 
192
  // Copy the covariate information into the name and interest type lists.
 
193
  vector<Covariate>::iterator cov_iter;
 
194
  for (cov_iter = aCovList.begin();
 
195
       cov_iter != aCovList.end();
 
196
       ++cov_iter)
 
197
  {
 
198
    name = cov_iter->getName();
 
199
    interest = Covariate::type2str(cov_iter->getType());
 
200
    
 
201
    name_list.push_back(name);
 
202
    type_list.push_back(interest);
 
203
  }
 
204
  buildTree(name_list, type_list, aShowAll);
 
205
}
 
206
 
 
207
void CovariatesView::buildTree(vector<string>& aNameList,
 
208
                               vector<string>& aTypeList, bool aShowAll)
 
209
{
 
210
  clearSelection();
 
211
  
 
212
  QString nameStr, sectionStr, typeStr;
 
213
  QStringList qList;
 
214
  for (unsigned i = 0; i < aNameList.size(); i++) {
 
215
  
 
216
    // Get the root of this ListView.
 
217
//    cerr << "finding the root of the listview...\n";
 
218
    Q3ListViewItem *parent = firstChild();
 
219
//    cerr << "first child is at "<< parent << ".\n";
 
220
 
 
221
    // Get the name for the item...
 
222
    nameStr = QString(aNameList[i].c_str());
 
223
//    cerr << "name for this item: " << nameStr << endl;
 
224
    // And get it's type.
 
225
    typeStr = QString(aTypeList[i].c_str());
 
226
//    cerr << "type for this item: " << typeStr << endl;
 
227
    // Parse the hierarchy for this item from the name.
 
228
    QStringList qList = QStringList::split("->", nameStr);
 
229
//    cerr << "...parsed the hierarchy for this file.\n";
 
230
    
 
231
    // Iterate through the hierarchy list...
 
232
//    cerr << "iterating through the hierarchy...\n";
 
233
    for (int32 j = 0; j < qList.size(); j++) {
 
234
      sectionStr = *qList.at(j);
 
235
      // Covariate is a direct child of the root
 
236
      if (qList.size() == 1) {
 
237
        (void) new Q3ListViewItem(this, lastChild(), sectionStr, typeStr, QString::number(i));
 
238
        break;
 
239
      }
 
240
      // Covariate belongs to a certain group
 
241
      else if (j == qList.size() - 1) {
 
242
        (void) new Q3ListViewItem(parent, lastChild(parent), sectionStr, typeStr, QString::number(i));
 
243
        break;
 
244
      }
 
245
      // Create the covariate's first layer group item (if not available)
 
246
      else if (j == 0) {
 
247
        parent = findGroup(sectionStr);
 
248
        if (!parent) {
 
249
          parent = new Q3ListViewItem(this, lastChild(), sectionStr);
 
250
          parent->setOpen(true);
 
251
        }
 
252
      }
 
253
      // Create group items after the first layer
 
254
      else {
 
255
        // ::FIXED:: have to make a copy of the parent in this case so that,
 
256
        //           in case the subgroup does not yet exist, we still know
 
257
        //           the address of the parent and we can create it.
 
258
        //                                          Mjumbe 2007.04.26
 
259
        Q3ListViewItem* sub_parent = findGroup(parent, sectionStr);
 
260
        if (!sub_parent) {
 
261
                sub_parent = new Q3ListViewItem(parent, lastChild(parent), sectionStr);
 
262
          sub_parent->setOpen(true);
 
263
        }
 
264
        parent = sub_parent;
 
265
      }
 
266
    }
 
267
  }
 
268
  
 
269
  if (!aShowAll)
 
270
    showInterestOnly();
 
271
    
 
272
//  cerr << "done!\n";
 
273
}
 
274
 
 
275
/* cpView() copies input QListView and add it on the main interface */
 
276
void CovariatesView::copyTree(const CovariatesView* aCovView, bool aShowAll)
 
277
{
 
278
  clearSelection();
 
279
  
 
280
  // Copy the input tree, select 1st selectable covariate and set baseIndex
 
281
  Q3ListViewItemIterator it(const_cast<CovariatesView*> (aCovView));
 
282
  while (it.current()) {
 
283
  
 
284
    // Copy the current item
 
285
    Q3ListViewItem *newItem;
 
286
    Q3ListViewItem *inputItem = it.current();
 
287
    
 
288
    if (inputItem->text(2).isEmpty()) {
 
289
      if (!inputItem->childCount())
 
290
        return;
 
291
      if (inputItem->depth() == 0) 
 
292
        newItem = new Q3ListViewItem(this, lastChild(), inputItem->text(0));
 
293
      else
 
294
        newItem = new Q3ListViewItem(findParent(inputItem), lastChild(findParent(inputItem)), 
 
295
                                  inputItem->text(0));
 
296
      newItem->setOpen(true);
 
297
      newItem->setEnabled(false);
 
298
      return;
 
299
    }
 
300
 
 
301
    if (inputItem->depth() == 0)
 
302
      newItem = new Q3ListViewItem(this, lastChild(), inputItem->text(0),
 
303
                                inputItem->text(1), inputItem->text(2)); 
 
304
    else
 
305
      newItem = new Q3ListViewItem(findParent(inputItem), lastChild(findParent(inputItem)), 
 
306
                                inputItem->text(0), inputItem->text(1), inputItem->text(2)); 
 
307
    // By default, only enable type I covariates
 
308
    if (newItem->text(1) != "I")
 
309
      newItem->setEnabled(false);
 
310
      
 
311
    ++it;
 
312
  }
 
313
 
 
314
  Q3ListViewItemIterator all(this, Q3ListViewItemIterator::Selectable);
 
315
 
 
316
  if (!aShowAll)
 
317
    showInterestOnly();
 
318
}
 
319
 
 
320
/* lastChild(...) gets the last 1st-gen child of parent, or of the View if 
 
321
 * parent is NULL.                                                           */
 
322
Q3ListViewItem* CovariatesView::lastChild(const Q3ListViewItem* parent) const
 
323
{
 
324
  Q3ListViewItem *last_child, *temp_child;
 
325
 
 
326
  if (parent)
 
327
    temp_child = firstChild(parent);
 
328
  else
 
329
    temp_child = firstChild();
 
330
  
 
331
  for (last_child = 0; 
 
332
       temp_child; 
 
333
       temp_child = temp_child->nextSibling())
 
334
    last_child = temp_child;
 
335
  
 
336
//  cerr << "done looking at the last child" << endl;
 
337
  return last_child;
 
338
}
 
339
 
 
340
/* firstChild(...) gets the first 1st-gen child of parent, or of the View if 
 
341
 * parent is NULL.                                                           */
 
342
Q3ListViewItem* CovariatesView::firstChild(const Q3ListViewItem* parent) const
 
343
{
 
344
  Q3ListViewItem* first_child;
 
345
 
 
346
//  cerr << "  parent is at " << parent << ".\n";
 
347
  if (parent)
 
348
    first_child = parent->firstChild();
 
349
  else
 
350
    first_child = Q3ListView::firstChild();
 
351
  
 
352
//  cerr << "done looking at the first child" << endl;
 
353
  return first_child;
 
354
}
 
355
 
 
356
/* findChild(...) finds the first item in column that matches text and is a 
 
357
 * direct descendant of parent (or is at depth 0 if parent is NULL).         */
 
358
Q3ListViewItem* CovariatesView::findChild(const Q3ListViewItem* parent,
 
359
                                         const QString& text, int column) const
 
360
{
 
361
  Q3ListViewItem* found_item;
 
362
  
 
363
  for (found_item = firstChild(parent); 
 
364
       found_item; 
 
365
       found_item = found_item->nextSibling())
 
366
  {
 
367
    if (found_item->text(column) == text)
 
368
      break;
 
369
  }
 
370
  
 
371
  return found_item;
 
372
}
 
373
 
 
374
Q3ListViewItem* CovariatesView::findChild(const QString& text, int column) const
 
375
{
 
376
  return findChild(0, text, column);
 
377
}
 
378
 
 
379
/* findGroup(...) finds the first item in column that matches text and is a 
 
380
 * direct descendant of parent (or is at depth 0 if parent is NULL).         */
 
381
Q3ListViewItem* CovariatesView::findGroup(const Q3ListViewItem* parent,
 
382
                                         const QString& text) const
 
383
{
 
384
  Q3ListViewItem* found_item;
 
385
  
 
386
  for (found_item = firstChild(parent); 
 
387
       found_item; 
 
388
       found_item = found_item->nextSibling())
 
389
  {
 
390
    if (found_item->text(0) == text && found_item->text(2).isEmpty())
 
391
      break;
 
392
  }
 
393
  
 
394
  return found_item;
 
395
}
 
396
 
 
397
/* findGroup(...) finds the first item in column that matches text and is a 
 
398
 * direct descendant of parent (or is at depth 0 if parent is NULL).         */
 
399
Q3ListViewItem* CovariatesView::findGroup(const QString& text) const
 
400
{
 
401
  return findGroup(0, text);
 
402
}
 
403
 
 
404
/* Modified based on gdw's same function */
 
405
Q3ListViewItem * CovariatesView::findParent(Q3ListViewItem *inputItem) const
 
406
{
 
407
  int lastDepth = lastItem()->depth();
 
408
  int inputDepth = inputItem->depth();
 
409
  if (lastDepth < inputDepth)
 
410
    return lastItem();
 
411
 
 
412
  Q3ListViewItem *newParent = lastItem()->parent();
 
413
  while (newParent->depth() > inputDepth - 1)
 
414
    newParent = newParent->parent();
 
415
  return newParent;
 
416
}
 
417
 
 
418
void CovariatesView::showInterestOnly(bool aInterestOnly)
 
419
{
 
420
  Q3ListViewItemIterator it(this);
 
421
  while (it.current())
 
422
  {
 
423
    Q3ListViewItem* item = it.current();
 
424
    if (item->text(columnNumber(ID_COL)) != "")
 
425
    {
 
426
      if (aInterestOnly && item->text(columnNumber(TYPE_COL)) != "I") 
 
427
        item->setVisible(false);
 
428
      else
 
429
        item->setVisible(true);
 
430
    }
 
431
    ++it;
 
432
  }
 
433
}
 
434
 
 
435
int CovariatesView::columnNumber(const QString& text)
 
436
{
 
437
  for (int i = 0; i < columns(); ++i)
 
438
    if (text == columnText(i)) return i;
 
439
  
 
440
  return -1;
 
441
}
 
442
 
 
443
void CovariatesView::setColumnText(int column, const QStringList& textList)
 
444
{
 
445
  int id_col = columnNumber(ID_COL);
 
446
  
 
447
  Q3ListViewItemIterator it( this );
 
448
  QStringList::ConstIterator string_it = textList.begin();
 
449
  while ( it.current()  && string_it != textList.end() ) 
 
450
  {
 
451
    Q3ListViewItem *item = it.current();
 
452
    if (!item->text(id_col).isEmpty())
 
453
    {
 
454
      item->setText(column, *string_it);
 
455
      ++string_it;
 
456
    }
 
457
    ++it;
 
458
  }
 
459
}
 
460
 
 
461
void CovariatesView::setColumnText(int column, const QString& text)
 
462
{
 
463
  int id_col = columnNumber(ID_COL);
 
464
  
 
465
  Q3ListViewItemIterator it(this);
 
466
  while (it.current())
 
467
  {
 
468
    Q3ListViewItem *item = it.current();
 
469
    if (!item->text(id_col).isEmpty())
 
470
      item->setText(column, text);
 
471
    ++it;
 
472
  }
 
473
}
 
474
 
 
475
void CovariatesView::setColumnText(const QString& column, const QStringList& textList)
 
476
{
 
477
  setColumnText(columnNumber(column), textList);
 
478
}
 
479
 
 
480
void CovariatesView::setColumnText(const QString& column, const QString& text)
 
481
{
 
482
  setColumnText(columnNumber(column), text);
 
483
}
 
484
 
 
485
void CovariatesView::setSelectedColumnText(int column, const QString& text)
 
486
{
 
487
  int id_col = columnNumber(ID_COL);
 
488
  list<Q3ListViewItem*>::iterator iter;
 
489
 
 
490
  for (iter = mSelection.begin();
 
491
       iter != mSelection.end();
 
492
       ++iter)
 
493
  {
 
494
    if (!(*iter)->text(id_col).isEmpty())
 
495
      (*iter)->setText(column, text);
 
496
  }
 
497
}
 
498
 
 
499
void CovariatesView::setSelectedColumnText(const QString& column, const QString& text)
 
500
{
 
501
  setSelectedColumnText(columnNumber(column), text);
 
502
}
 
503
 
 
504
int CovariatesView::itemIndex(const Q3ListViewItem* item)
 
505
{
 
506
  int index = 0;
 
507
 
 
508
  Q3ListViewItemIterator it(this);
 
509
  while (it.current())
 
510
  {
 
511
    if (it.current() == item)
 
512
      return index;
 
513
    ++it;
 
514
    ++index;
 
515
  }
 
516
  return -1;
 
517
}
 
518
 
 
519
list<Q3ListViewItem*>& CovariatesView::selectedItems()
 
520
{
 
521
  return mSelection;
 
522
}
 
523
 
 
524
list<int>& CovariatesView::selectedItemIDs()
 
525
{
 
526
  return mSelectionIDs;
 
527
}
 
528
 
 
529
void CovariatesView::onSelectionChanged()
 
530
{
 
531
  mSelection.clear();
 
532
  mSelectionIDs.clear();
 
533
  
 
534
  int id_col = columnNumber(ID_COL);
 
535
  
 
536
  Q3ListViewItemIterator it(this);
 
537
  while (it.current())
 
538
  {
 
539
    Q3ListViewItem* item = it.current();
 
540
    if (isSelected(item))
 
541
    {
 
542
      mSelection.push_back(item);
 
543
      if (!item->text(id_col).isEmpty())
 
544
        mSelectionIDs.push_back(item->text(id_col).toInt());
 
545
    }
 
546
    ++it;
 
547
  }
 
548
}
 
549
 
 
550
void CovariatesView::clear()
 
551
{
 
552
  mCovariates = 0;
 
553
  mSelection.clear();
 
554
  mSelectionIDs.clear();
 
555
  return Q3ListView::clear();
 
556
}
 
557
 
 
558
/********************************************************************************
 
559
 * VB::ContrastsView class
 
560
 ********************************************************************************/
 
561
 
 
562
ContrastsView::ContrastsView(QWidget *parent, const char *name)
 
563
  : Q3ListView(parent, name)
 
564
{
 
565
  setSelectionMode(Q3ListView::Single);
 
566
  setRootIsDecorated(false);
 
567
  addColumn("Name");
 
568
  addColumn("Scale Type");
 
569
  setSorting(-1);
 
570
  //header()->hide();
 
571
  
 
572
  connect( this, SIGNAL( selectionChanged() ),
 
573
           this, SLOT( onSelectionChanged() ) );
 
574
  
 
575
  connect( this, SIGNAL( itemRenamed(Q3ListViewItem *,int,const QString &) ),
 
576
           this, SLOT( onContrastRenamed(Q3ListViewItem *,int,const QString &) ) );
 
577
}
 
578
 
 
579
void ContrastsView::buildList(GLMInfo* aGLMInfo)
 
580
{
 
581
  vector<VBContrast*> contrast_list;
 
582
  VBContrast contrast;
 
583
  
 
584
  vector<VBContrast>::iterator cont_iter;
 
585
  for (cont_iter = aGLMInfo->contrasts.begin();
 
586
       cont_iter != aGLMInfo->contrasts.end();
 
587
       ++cont_iter)
 
588
  {
 
589
    contrast_list.push_back(new VBContrast(*cont_iter));
 
590
  }
 
591
  buildList(contrast_list);
 
592
}
 
593
 
 
594
void ContrastsView::buildList(std::vector<Contrast>& aContList)
 
595
{
 
596
  vector<VBContrast*> contrast_list;
 
597
  VBContrast contrast;
 
598
  
 
599
  // Copy the contrast information into the name, scale, and weight lists.
 
600
  vector<Contrast>::iterator cont_iter;
 
601
  for (cont_iter = aContList.begin();
 
602
       cont_iter != aContList.end();
 
603
       ++cont_iter)
 
604
  {
 
605
    contrast.name = cont_iter->Name;
 
606
    //contrast.scale = Contrast::scale2str(cont_iter->Scale);
 
607
    contrast.contrast = Vec(cont_iter->Weights);
 
608
    
 
609
    contrast_list.push_back(new VBContrast(contrast));
 
610
  }
 
611
  buildList(contrast_list);
 
612
}
 
613
 
 
614
void ContrastsView::buildList(vector<string> aNameList,
 
615
                              vector<string> aScaleList,
 
616
                              vector<VB_Vector> aWeightList)
 
617
{
 
618
  vector<VBContrast*> contrast_list;
 
619
  VBContrast contrast;
 
620
  
 
621
  int len = aNameList.size(); // should i check that they're all the same size?
 
622
  for (int i = 0; i < len; ++i)
 
623
  {
 
624
    contrast.name = aNameList[i];
 
625
    contrast.scale = aScaleList[i];
 
626
    contrast.contrast = Vec(aWeightList[i]);
 
627
    
 
628
    contrast_list.push_back(new VBContrast(contrast));
 
629
  }
 
630
  buildList(contrast_list);
 
631
}
 
632
 
 
633
void ContrastsView::buildList(vector<VBContrast*>& aContrastList)
 
634
{
 
635
  mContrasts = aContrastList;
 
636
  clear();
 
637
 
 
638
  clearSelection();
 
639
  
 
640
  vector<VBContrast*>::iterator cont_iter;
 
641
  for (cont_iter = aContrastList.begin();
 
642
       cont_iter != aContrastList.end();
 
643
       ++cont_iter)
 
644
  {
 
645
    Q3ListViewItem* temp_item = new Q3ListViewItem(this, lastItem(), QString((*cont_iter)->name.c_str()), QString((*cont_iter)->scale.c_str()));
 
646
    temp_item->setRenameEnabled(0,true);
 
647
//    cerr << "Contrast: " << (*cont_iter)->name.c_str() << endl;
 
648
  }
 
649
//  cerr << "Contrast list has been populated." << endl;
 
650
}
 
651
 
 
652
void ContrastsView::insertContrast(VBContrast* aContrast)
 
653
{
 
654
  mContrasts.push_back(aContrast);
 
655
  Q3ListViewItem* temp_item = new Q3ListViewItem(this, lastItem(), aContrast->name.c_str(), aContrast->scale.c_str());
 
656
  temp_item->setRenameEnabled(0,true);
 
657
}
 
658
 
 
659
void ContrastsView::takeContrast(VBContrast* aContrast)
 
660
{
 
661
  Q3ListViewItemIterator list_iter(this);
 
662
  vector<VBContrast*>::iterator iter;
 
663
  
 
664
  for (iter = mContrasts.begin(); iter != mContrasts.end(); ++iter)
 
665
  {
 
666
    if (*iter == aContrast) break;
 
667
    ++list_iter;
 
668
  }
 
669
  mContrasts.erase(iter);
 
670
  takeItem(*list_iter);
 
671
}
 
672
 
 
673
int ContrastsView::itemIndex(const Q3ListViewItem* item)
 
674
{
 
675
  int index = 0;
 
676
 
 
677
  Q3ListViewItemIterator it(this);
 
678
  while (it.current())
 
679
  {
 
680
    if (it.current() == item)
 
681
      return index;
 
682
    ++it;
 
683
    ++index;
 
684
  }
 
685
  return -1;
 
686
}
 
687
 
 
688
VBContrast* ContrastsView::selectedContrast() const
 
689
{
 
690
  return mCurrentContrast;
 
691
}
 
692
 
 
693
void ContrastsView::onSelectionChanged()
 
694
{
 
695
  Q3ListViewItem* item = selectedItem();
 
696
  mCurrentContrast = contrastAt(item);
 
697
}
 
698
 
 
699
VBContrast* ContrastsView::contrastAt(const Q3ListViewItem* item, bool diag)
 
700
{
 
701
  VBContrast* contrast;
 
702
  if (!item)
 
703
  {
 
704
    contrast = 0;
 
705
    if (diag) cerr << "No contrast is selected." << endl;
 
706
  }
 
707
  else
 
708
  {
 
709
    int index = itemIndex(item);
 
710
    contrast = mContrasts[index];
 
711
    if (diag) cerr << "Contrast selected: " << contrast->name << endl <<
 
712
                      "  " << contrast->contrast << endl;
 
713
  }
 
714
  
 
715
  return contrast;
 
716
}
 
717
 
 
718
void ContrastsView::onContrastRenamed(Q3ListViewItem * item, int /* col */, const QString & text)
 
719
{
 
720
  VBContrast* contrast = contrastAt(item);
 
721
  contrast->name = text.ascii();
 
722
}
 
723
 
 
724
/********************************************************************************
 
725
 * VB::ContParamsView class
 
726
 ********************************************************************************/
 
727
 
 
728
const char* ContParamsView::WEIGHT_COL = "Weight";
 
729
 
 
730
ContParamsView::ContParamsView(QWidget *parent, const char *name)
 
731
  : CovariatesView(parent, name)
 
732
{
 
733
  addColumn(WEIGHT_COL);
 
734
  setColumnWidthMode(columnNumber(ID_COL), Q3ListView::Manual);
 
735
  header()->setResizeEnabled(FALSE, columnNumber(ID_COL));
 
736
  hideColumn(columnNumber(ID_COL));
 
737
}
 
738
 
 
739
void ContParamsView::buildTree(GLMInfo* aGLMInfo, bool aShowAll)
 
740
{
 
741
  clearSelection();
 
742
  clearContrast();
 
743
  CovariatesView::buildTree(aGLMInfo, aShowAll);
 
744
}
 
745
void ContParamsView::buildTree(std::vector<Covariate>& aCovList, bool aShowAll)
 
746
{
 
747
  clearSelection();
 
748
  clearContrast();
 
749
  CovariatesView::buildTree(aCovList, aShowAll);
 
750
}
 
751
 
 
752
void ContParamsView::buildTree(std::vector<std::string>& aNameList,
 
753
                               std::vector<std::string>& aTypeList, bool aShowAll)
 
754
{
 
755
  clearSelection();
 
756
  clearContrast();
 
757
  CovariatesView::buildTree(aNameList, aTypeList, aShowAll);
 
758
}
 
759
 
 
760
void ContParamsView::setContrast(const VBContrast& aContrast)
 
761
{
 
762
 
 
763
  setContrast(aContrast.contrast);
 
764
}
 
765
 
 
766
void ContParamsView::setContrast(const VB_Vector& aContrast)
 
767
{
 
768
  QStringList str_list;
 
769
  for (int i = 0; i < aContrast.size(); ++i)
 
770
    str_list += QString::number(double(aContrast[i]), 'f', 2);
 
771
    
 
772
  setColumnText(WEIGHT_COL, str_list);
 
773
}
 
774
 
 
775
void ContParamsView::clearContrast()
 
776
{
 
777
  setColumnText(WEIGHT_COL, QString(""));
 
778
}