COPASI API  4.16.103
ObjectBrowserItem.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2003 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 /********************************************************
16 Author: Liang Xu
17 Version : 1.xx <first>
18 Description:
19 Date: 04/03
20 Comment : Copasi Object Browser including:
21 
22 CBrowserObject: A complex structure uiniquely map to a CopasiObject
23 ObjectBrowserItem: A wraper to a broserObject,
24 there may exist multiply wrappers to one CBrowserObject
25 ObjectListItem
26 ObjectList: A queue for all element:
27 The reason I dont use std:vector is
28 for efficiency requirement for all
29 object browser item update
30 Contact: Please contact lixu1@vt.edu.
31  *********************************************************/
32 
33 #include <QtGui/QTreeWidget>
34 #include "ObjectBrowserItem.h"
35 
36 #include "report/CCopasiObject.h"
40 
42 
44  pCopasiObject(NULL),
45  mChecked(false),
46  referenceList(NULL) //keep pointer to all its referenced items for later update
47 {
48  referenceList = new ObjectList();
49 }
50 
52 {
53  ObjectListItem* currentItem = referenceList->getRoot();
54 
55  for (; currentItem != NULL; currentItem = currentItem->pNext)
56  currentItem->pItem->setBrowserObject(NULL);
57 
59 }
60 
61 /**
62  * Constructs a new ObjectBrowserItem
63  */
64 ObjectBrowserItem::ObjectBrowserItem(QTreeWidget * parent, ObjectBrowserItem * after, const CCopasiObject* mObject, ObjectList* pList)
65  : QTreeWidgetItem(parent, after)
66 {
67  if (mObject != NULL)
68  {
69  /* To change
70  ObjectListItem* pTmp = pList->getRoot();
71  while (pTmp != NULL)
72  {
73  if (pTmp->pItem->getObject()->pCopasiObject == mObject) // already be pointed in the list
74  break;
75  pTmp = pTmp->pNext;
76  }
77  if (pTmp && pTmp->pItem->getObject()->pCopasiObject == mObject) //exist already in list
78  {
79  pBrowserObject = pTmp->pItem->getObject();
80  pBrowserObject->referenceList->insert(this);
81  }
82  else
83  To change */
84  {
85  CBrowserObject* newBrowserObject = new CBrowserObject();
86  newBrowserObject->pCopasiObject = mObject;
87  newBrowserObject->mChecked = false;
88  pBrowserObject = newBrowserObject;
90  }
91  }
92  else //this is not an ending node
93  {
94  CBrowserObject* newBrowserObject = new CBrowserObject();
95  newBrowserObject->pCopasiObject = mObject;
96  newBrowserObject->mChecked = false;
97  pBrowserObject = newBrowserObject;
99  }
100 
101  if (pList)
102  pList->insert(this);
103 
104  mKey = " ";
105 }
106 
108  : QTreeWidgetItem(parent, after)
109 {
110  if (mObject != NULL)
111  {
112  /* To change
113  ObjectListItem* pTmp = pList->getRoot();
114  while (pTmp != NULL)
115  {
116  if (pTmp->pItem->getObject()->pCopasiObject == mObject)
117  break;
118  pTmp = pTmp->pNext;
119  }
120  if (pTmp && pTmp->pItem->getObject()->pCopasiObject == mObject) //exist already in list
121  {
122  pBrowserObject = pTmp->pItem->getObject();
123  pBrowserObject->referenceList->insert(this);
124  }
125  else
126  To change */
127  {
128  CBrowserObject* newBrowserObject = new CBrowserObject();
129  newBrowserObject->pCopasiObject = mObject;
130  newBrowserObject->mChecked = false;
131  pBrowserObject = newBrowserObject;
133  }
134  }
135  else //this is not an ending node
136  {
137  CBrowserObject* newBrowserObject = new CBrowserObject();
138  newBrowserObject->pCopasiObject = mObject;
139  newBrowserObject->mChecked = false;
140  pBrowserObject = newBrowserObject;
142  }
143 
144  if (pList)
145  pList->insert(this);
146 
147  mKey = " ";
148 }
149 
151 {
152  mKey = QString::number(KeySpace++);
153 }
154 
156 {
157  int condition;
158 
159  if (child(0))
160  {
161  ObjectBrowserItem* pChild = (ObjectBrowserItem *)child(0);
162  condition = pChild->nUserChecked();
163 
164  for (; pChild != NULL; pChild = (ObjectBrowserItem *)pChild->nextSibling())
165  {
166  switch (pChild->nUserChecked())
167  {
168  case ALLCHECKED:
169 
170  if (condition == NOCHECKED)
171  condition = PARTCHECKED;
172 
173  break;
174  case PARTCHECKED:
175 
176  if (condition == NOCHECKED || condition == ALLCHECKED)
177  condition = PARTCHECKED;
178 
179  break;
180  case NOCHECKED:
181 
182  if (condition == ALLCHECKED)
183  condition = PARTCHECKED;
184 
185  break;
186  }
187  }
188  }
189  else //it has no child
190  {
191  if (isChecked())
192  condition = ALLCHECKED;
193  else
194  condition = NOCHECKED;
195  }
196 
197  return condition;
198 }
199 
201 {
202  return pBrowserObject->mChecked;
203 }
204 
206 {
208 }
209 
211 {
212  if (this->parent() == NULL)
213  return NULL;
214  else
215  {
216  int indexOfThisChild = this->parent()->indexOfChild(this);
217 
218  if (this->parent()->child(indexOfThisChild + 1))
219  return this->parent()->child(indexOfThisChild + 1);
220  }
221 
222  return NULL;
223 }
224 
226  quickIndex(NULL),
227  pointerList(NULL),
228  index_length(0),
229  root(NULL),
230  length(0)
231 {}
232 
234 {
235  int i = 0;
236  ObjectListItem* pNewItem = new ObjectListItem(pItem, NULL, NULL);
237 
238  if (length == 0)
239  {
240  root = pNewItem;
241  length++;
242  return;
243  }
244 
245  ObjectListItem* pCurrent = root;
246 
247  for (; i < length - 1; i++)
248  pCurrent = pCurrent->pNext;
249 
250  pCurrent->pNext = pNewItem;
251  pNewItem->pLast = pCurrent;
252  length++;
253 }
254 
256 {
257  return root;
258 }
259 
261 {
262  if (length == 0) return NULL;
263 
264  ObjectBrowserItem* returnValue = root->pItem;
265  ObjectListItem* delNode = root;
266  root = root->pNext;
267 
268  if (root)
269  root->pLast = NULL;
270 
271  length--;
272  pdelete(delNode);
273  return returnValue;
274 }
275 
276 /* Caution: ObjectList must be sorted already for this function to work */
278 {
279  ObjectListItem* objectLast = getRoot();
280  ObjectListItem* objectNext = (objectLast != NULL) ? objectLast->pNext : NULL;
281 
282  for (; objectNext != NULL; objectNext = objectNext->pNext)
283  {
284  if (objectLast->pItem->key(0, 0) == objectNext->pItem->key(0, 0)) //delete the current item
285  {
286  objectLast->pNext = objectNext->pNext;
287 
288  if (objectNext->pNext) objectNext->pNext->pLast = objectLast;
289 
290  pdelete(objectNext);
291  length--;
292  objectNext = objectLast;
293  }
294  else // objectLast.key<objectNext.Key
295  objectLast = objectNext; //next step
296  }
297 }
298 
300 {
301  if (len() <= 1) //sorted
302  return;
303 
304  ObjectListItem* pHead = getRoot();
305  ObjectListItem* pTail = pHead->pNext;
306 
307  for (; pHead->pNext != NULL; pHead = pHead->pNext)
308  {
309  for (pTail = pHead->pNext; pTail != NULL; pTail = pTail->pNext)
310  if (pHead->pItem->key(0, 0) > pTail->pItem->key(0, 0))
311  {
312  ObjectBrowserItem * pTmp = pHead->pItem;
313  pHead->pItem = pTail->pItem;
314  pTail->pItem = pTmp;
315  }
316  }
317 }
318 
319 //please do call createQuickIndex() first
320 bool ObjectList::sortListInsert(ObjectBrowserItem* pItem) //insert and keep the sort order
321 {
322  if (getRoot() == NULL) //list is empty
323  {
324  ObjectListItem* pNewItem = new ObjectListItem(pItem, NULL, NULL);
325  root = pNewItem;
326  length++;
327  return true;
328  }
329 
330  if (pItem->key(0, 0) < getRoot()->pItem->key(0, 0)) //insert at the front
331  {
332  ObjectListItem* pNewItem = new ObjectListItem(pItem, root, NULL);
333  root->pLast = pNewItem;
334  root = pNewItem;
335  length++;
336  return true;
337  }
338 
339  ObjectListItem* pHead = getRoot();
340 
341  for (; (pHead != NULL) && (pItem->key(0, 0) > pHead->pItem->key(0, 0)); pHead = pHead->pNext)
342  ;
343 
344  if (pHead && (pHead->pItem->key(0, 0) == pItem->key(0, 0))) //duplicate key
345  return false;
346 
347  //else insert
348 
349  if (pHead == NULL) //insert at the end of the list
350  {
351  insert(pItem);
352  return true;
353  }
354 
355  length++;
356  ObjectListItem* pNewItem = new ObjectListItem(pItem, pHead, pHead->pLast);
357  pHead->pLast = pNewItem;
358  pNewItem->pLast->pNext = pNewItem;
359  return true;
360 }
361 
363 {
364  index_length = max;
365  quickIndex = new bool[max];
367  int i = 0;
368 
369  for (; i < max; i++)
370  quickIndex[i] = false;
371 
372  int tmpIndex;
373  ObjectListItem* pDel;
374 
375  for (ObjectListItem* pHead = getRoot(); pHead != NULL;)
376  {
377  tmpIndex = pHead->pItem->key(0, 0).toInt() - KEYBASE;
378 
379  if (quickIndex[tmpIndex]) //delete
380  {
381  pDel = pHead;
382  if (pHead->pLast) pHead->pLast->pNext = pHead->pNext; else root = pHead->pNext;
383 
384  if (pHead->pNext)
385  pHead->pNext->pLast = pHead->pLast;
386 
387  pHead = pHead->pNext;
388  pdelete(pDel);
389  length--;
390  }
391  else //
392  {
393  quickIndex[tmpIndex] = true;
394  pointerList[tmpIndex] = pHead->pItem;
395  pHead = pHead->pNext;
396  }
397  }
398 }
399 
401 {
402  int tmpIndex = pItem->key(0, 0).toInt() - KEYBASE;
403  quickIndex[tmpIndex] = true;
404  pointerList[tmpIndex] = pItem;
405 }
406 
408 {
409  for (; cursor < index_length; cursor++)
410  if (quickIndex[cursor])
411  {
412  cursor++;
413  return pointerList[cursor - 1];
414  }
415 
416  //nothing avaiable in list
417  return NULL;
418 }
419 
421 {
422  delete[] quickIndex;
423  delete[] pointerList;
424 
425  for (ObjectBrowserItem* pHead = pop(); pHead != NULL; pHead = pop());
426 
427  index_length = 0;
428 }
#define pdelete(p)
Definition: copasi.h:215
void insert(ObjectBrowserItem *pItem)
void createBucketIndex(int max)
ObjectBrowserItem * bucketPop(int &cursor)
bool sortListInsert(ObjectBrowserItem *pItem)
QTreeWidgetItem * nextSibling()
#define NOCHECKED
const CCopasiObject * pCopasiObject
ObjectListItem * pNext
ObjectListItem * pLast
ObjectList * referenceList
void insertBucket(ObjectBrowserItem *pItem)
void setBrowserObject(CBrowserObject *updateObject)
ObjectBrowserItem ** pointerList
ObjectListItem * root
ObjectBrowserItem * pItem
Header file of class CCopasiContainer.
ObjectBrowserItem(QTreeWidget *parent=NULL, ObjectBrowserItem *after=NULL, const CCopasiObject *mObject=NULL, ObjectList *pList=NULL)
#define PARTCHECKED
virtual QString key(int C_UNUSED(column), bool C_UNUSED(ascending)) const
#define ALLCHECKED
#define KEYBASE
CBrowserObject * pBrowserObject
ObjectListItem * getRoot()
ObjectBrowserItem * pop()
#define max(a, b)
Definition: f2c.h:176