COPASI API  4.16.103
CQMergingData.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/UI/CQMergingData.cpp,v $
3 // $Revision: 1.9 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/03/15 17:07:54 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 #include <map>
20 
22 #include "model/CMetab.h"
23 #include "model/CModelValue.h"
24 #include "model/CModelExpansion.h"
25 #include "model/CChemEqInterface.h"
26 
28 
29 #include "UI/qtUtilities.h"
31 
32 
33 #include "CQMergingData.h"
34 
35 class CMetab;
36 class CModelEntity;
37 
38 
39 /*
40  * Constructs a CQMergingData which is a child of 'parent', with the
41  * name 'name'.'
42  */
43 
44 CQMergingData::CQMergingData(QWidget* parent, Qt::WindowFlags fl, bool simple)
45  : QDialog(parent, fl)
46 {
47  setupUi(this);
48  connect(mpTree1, SIGNAL(currentItemChanged(QTreeWidgetItem * , QTreeWidgetItem *)), this, SLOT(treeSelectionChanged()));
49  connect(mpTree2, SIGNAL(currentItemChanged(QTreeWidgetItem * , QTreeWidgetItem *)), this, SLOT(treeSelectionChanged()));
50 
51  load();
52 }
53 
54 /*
55  * Destroys the object and frees any allocated resources
56  */
58 {
59  // no need to delete child widgets, Qt does it all for us
60 }
61 
62 void CQMergingData::fillTree(QTreeWidget* pW, const CModel* pModel, std::map<QTreeWidgetItem*, CCopasiObject*> & itemMap,
63  bool flagGlobalQuantities, bool flagReactions,
64  const std::set<CCopasiObject*> & added,
65  bool highlightInvolved)
66 {
67  itemMap.clear();
68  pW->clear();
69  CModelExpansion mex(const_cast<CModel*>(pModel));
70 
71  //create an italic font for highlighting new model elements
72  QFont tmpFont = pW->font();
73  tmpFont.setItalic(true);
74 
75  //add the compartments
76  size_t i, imax = pModel->getCompartments().size();
77  for(i=0; i<imax; ++i)
78  {
79  CCopasiObject * pObj = pModel->getCompartments()[i];
80  QTreeWidgetItem * pItem = new QTreeWidgetItem((QTreeWidget*)NULL, 1000);
81  pItem->setText(0, FROM_UTF8(pObj->getObjectName()));
82 
83  //highlight new objects
84  std::set<CCopasiObject*>::const_iterator it = added.find(pObj);
85  if (it != added.end())
86  {
87  pItem->setFont(0, tmpFont);
88  //pItem->setBackgroundColor(0, QColor(200,200,250));
89  }
90 
91  //highlight objects that are referred to by others
92  if (highlightInvolved)
93  {
94  if (!mex.existDependentEntities(pObj))
95  pItem->setTextColor(0, QColor(130,130,130));
96  }
97 
98  itemMap[pItem] = pObj;
99  pW->addTopLevelItem(pItem);
100  pW->setFirstItemColumnSpanned(pItem, true);
101 
102  //add species
103  //QTreeWidgetItem * pChild;
104  size_t j, jmax = pModel->getCompartments()[i]->getMetabolites().size();
105  for(j=0; j<jmax; ++j)
106  {
107  pObj = pModel->getCompartments()[i]->getMetabolites()[j];
108  QTreeWidgetItem * pChild = new QTreeWidgetItem(pItem, 1001);
109  pChild->setText(0, FROM_UTF8(pObj->getObjectName()));
110  pW->setFirstItemColumnSpanned(pChild, true);
111 
112  //highlight new objects
113  std::set<CCopasiObject*>::const_iterator it = added.find(pObj);
114  if (it != added.end())
115  {
116  pChild->setFont(0, tmpFont);
117  //pChild->setBackgroundColor(0, QColor(200,200,250));
118  }
119  //highlight objects that are referred to by others
120  if (highlightInvolved)
121  {
122  if (!mex.existDependentEntities(pObj))
123  pChild->setTextColor(0, QColor(130,130,130));
124  }
125 
126  itemMap[pChild]=pObj;
127  }
128  pItem->setExpanded(true);
129 
130  }
131 
132  if (flagGlobalQuantities && pModel->getModelValues().size())
133  {
134  QTreeWidgetItem * pItem = new QTreeWidgetItem((QTreeWidget*)NULL, 1000);
135  pItem->setText(0, "Global Quantities");
136  pW->addTopLevelItem(pItem);
137  pW->setFirstItemColumnSpanned(pItem, true);
138 
139 
140  //QTreeWidgetItem * pChild;
141  size_t j, jmax = pModel->getModelValues().size();
142  for(j=0; j<jmax; ++j)
143  {
144  CCopasiObject * pObj = pModel->getModelValues()[j];
145  QTreeWidgetItem * pChild = new QTreeWidgetItem(pItem, 1001);
146  pChild->setText(0, FROM_UTF8(pObj->getObjectName()));
147  pW->setFirstItemColumnSpanned(pChild, true);
148 
149  //highlight new objects
150  std::set<CCopasiObject*>::const_iterator it = added.find(pObj);
151  if (it != added.end())
152  {
153  pChild->setFont(0, tmpFont);
154  //pChild->setBackgroundColor(0, QColor(200,200,250));
155  }
156  //highlight objects that are referred to by others
157  if (highlightInvolved)
158  {
159  if (!mex.existDependentEntities(pObj))
160  pChild->setTextColor(0, QColor(130,130,130));
161  }
162 
163  itemMap[pChild]=pObj;
164  }
165  }
166  if (flagReactions && pModel->getReactions().size())
167  {
168  pW->setColumnCount(2);
169 
170  QTreeWidgetItem * pItem = new QTreeWidgetItem((QTreeWidget*)NULL, 1000);
171  pItem->setText(0, "Reactions");
172  pW->addTopLevelItem(pItem);
173  pW->setFirstItemColumnSpanned(pItem, true);
174 
175  QFont tmpFontSmall = pItem->font(0);
176  tmpFontSmall.setPointSize(tmpFontSmall.pointSize()-2);
177 
178  //QTreeWidgetItem * pChild;
179  size_t j, jmax = pModel->getReactions().size();
180  for(j=0; j<jmax; ++j)
181  {
182  CCopasiObject * pObj = pModel->getReactions()[j];
183  QTreeWidgetItem * pChild = new QTreeWidgetItem(pItem, 1001);
184  pChild->setText(0, FROM_UTF8(pObj->getObjectName()));
185  pW->setFirstItemColumnSpanned(pChild, false);
186 
187  //add the chemical equation (with a smaller font)
188  const CReaction * pReaction = dynamic_cast<const CReaction*>(pObj);
189  if (pReaction)
190  pChild->setText(1, FROM_UTF8(CChemEqInterface::getChemEqString(const_cast<CModel*>(pModel), *pReaction, false) ) );
191  pChild->setFont(1, tmpFontSmall);
192 
193  //highlight new objects
194  std::set<CCopasiObject*>::const_iterator it = added.find(pObj);
195  if (it != added.end())
196  {
197  pChild->setFont(0, tmpFont);
198  //pChild->setBackgroundColor(0, QColor(200,200,250));
199  }
200  //highlight objects that are referred to by others
201  if (highlightInvolved)
202  {
203  if (!mex.existDependentEntities(pObj))
204  pChild->setTextColor(0, QColor(130,130,130));
205  pChild->setTextColor(1, QColor(130,130,130));
206  }
207 
208  itemMap[pChild]=pObj;
209  }
210  //pW->setCo
211  }
212 
213 }
214 
216 {
217  // only enable the merging button if the selected items match and are not identical
218  CCopasiObject* p1=NULL;
219  CCopasiObject* p2=NULL;
220  std::map<QTreeWidgetItem*, CCopasiObject*>::const_iterator it;
221  it = mItemMap1.find(mpTree1->currentItem());
222  if (it != mItemMap1.end())
223  p1 = it->second;
224  it = mItemMap2.find(mpTree2->currentItem());
225  if (it != mItemMap2.end())
226  p2 = it->second;
227  if (!p1 || !p2 || p1->getObjectType()!=p2->getObjectType() || p1==p2)
228  mpBtnMerge->setEnabled(false);
229  else
230  mpBtnMerge->setEnabled(true);
231 }
232 
234 {
235 
236  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
237 
238 
239  mpModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel();
240 
241  fillTree(mpTree1, mpModel, mItemMap1, true, true, (*CCopasiRootContainer::getDatamodelList())[0]->mLastAddedObjects, true);
242  fillTree(mpTree2, mpModel, mItemMap2, true, true, (*CCopasiRootContainer::getDatamodelList())[0]->mLastAddedObjects, false);
243 
245  }
246 
248 {
249  //simple, preliminary
250  CCopasiObject* p1=NULL;
251  CCopasiObject* p2=NULL;
252 
253  std::map<QTreeWidgetItem*, CCopasiObject*>::const_iterator it;
254  it = mItemMap1.find(mpTree1->currentItem());
255  if (it != mItemMap1.end())
256  p1 = it->second;
257  it = mItemMap2.find(mpTree2->currentItem());
258  if (it != mItemMap2.end())
259  p2 = it->second;
260 
261 /* for (it=mItemMap1.begin(); it != mItemMap1.end(); ++it)
262  {
263  if (it->first->checkState(0)==Qt::Checked)
264  {
265  p1=it->second;
266  break;
267  }
268  }
269  for (it=mItemMap2.begin(); it != mItemMap2.end(); ++it)
270  {
271  if (it->first->checkState(0)==Qt::Checked)
272  {
273  p2=it->second;
274  break;
275  }
276  }*/
277 
278  //check if the replacement matches in type
279  if (!p1 || !p2 || p1->getObjectType()!=p2->getObjectType())
280  return;
281  //TODO it would be better to check this constantly and disable the merge button accordingly
282 
283  //pModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel();
284  CModelExpansion expa(mpModel);
286  emap.add(p1, p2);
287  expa.replaceInModel(emap, true); //true means remove replaced items
288 
289  //CModelMerging merging(pModel);
290  //merging.simpleCall(mColumnKey, mObjectKey);
291 
292  load();
293 
294  //accept();
295 }
296 
297 
299 {
300  //this is actually not "Cancel", but "Done"
301  accept();
302 }
303 
Header file of class CModelEntity and CModelValue.
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
const std::string & getObjectName() const
const CCopasiVectorN< CModelValue > & getModelValues() const
Definition: CModel.cpp:1060
virtual size_t size() const
void treeSelectionChanged()
const std::string & getObjectType() const
std::map< QTreeWidgetItem *, CCopasiObject * > mItemMap2
Definition: CQMergingData.h:33
Definition: CMetab.h:178
bool existDependentEntities(const CCopasiObject *pObj)
void add(const CCopasiObject *source, CCopasiObject *copy)
add a source->duplicate mapping
CModel * mpModel
Definition: CQMergingData.h:36
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
std::string getChemEqString(bool expanded) const
CQMergingData(QWidget *parent=0, Qt::WindowFlags fl=0, bool simple=false)
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
void fillTree(QTreeWidget *pW, const CModel *pModel, std::map< QTreeWidgetItem *, CCopasiObject * > &itemMap, bool flagGlobalQuantities, bool flagReactions, const std::set< CCopasiObject * > &added, bool highlightInvolved)
bool fl(const C_FLOAT64 &d1, const C_FLOAT64 &d2)
std::map< QTreeWidgetItem *, CCopasiObject * > mItemMap1
Definition: CQMergingData.h:32
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
void replaceInModel(const ElementsMap &emap, bool remove)
Definition: CModel.h:50
if(!yymsg) yymsg