Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

propositiongraph.cpp

Go to the documentation of this file.
00001 /*
00002  Modular Psi - A graph tracker of the Parapsychologcal scientific development
00003  Copyright (C) 2005 Leonardo Stern
00004 
00005  This program is free software; you can redistribute it and/or modify it under
00006  the terms of the Common Public License Version 1.0 or (at your option) any later version.
00007 
00008  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
00009  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010  See the Common Public License for more details.
00011 
00012  You should have received a copy of the Common Public License along with this program;
00013 */
00014 
00015 
00016 #include <qcombobox.h>
00017 #include <qlineedit.h>
00018 #include <qmessagebox.h>
00019 #include <qpushbutton.h>
00020 #include <qstatusbar.h>
00021 #include <qstring.h>
00022 #include <qtextedit.h>
00023 #include <qwidgetstack.h>
00024 
00025 #include "propositiongraph.hpp"
00026 #include "parsers/graphml.hpp"
00027 #include "parsers/dot.hpp"
00028 #include "parsers/cat.hpp"
00029 #include "edgelist.hpp"
00030 #include "gui/buildpropositions.hpp"
00031 #include "const.hpp"
00032 #include "utils.hpp"
00033 
00034 PropositionGraph::PropositionGraph()
00035     : Graph()
00036 {
00037         init();
00038 }
00039 
00040 PropositionGraph::~PropositionGraph()
00041 {
00042     //Noop
00043 }
00044 
00045 void PropositionGraph::deleteEdge(Edge* delEdge)
00046 {
00047         // Make sure graph still connected
00048         if (delEdge->target()->incoming->count() >= 2 
00049                 && delEdge->target()->outgoing->count() == 0)
00050         {
00051                 gui->keywordsEdit->setText(QString("%1").arg(delEdge->target()->incoming->count()));
00052                 delEdge->source()->outgoing->removeRef(delEdge);
00053                 delEdge->target()->incoming->removeRef(delEdge);
00054                 edges->removeRef(delEdge);
00055                 delEdge->lineColor = QColor (255,0,0);
00056                 useDot();
00057                 
00058         }
00059         else 
00060         {
00061                 QMessageBox::information( gui,"Modular Psi",
00062                 QObject::tr("Deletion was not possible.\n"
00063                 "Target node needs to be connected to the graph"));             
00064     }
00065         
00066 }
00067 
00068 void PropositionGraph::deleteProp(Proposition* delProp)
00069 {
00070         // Make sure graph still connected
00071         if (delProp->outgoing->count() == 0 )
00072         {
00073                 EdgeList::iterator ix = delProp->incoming->begin();
00074                 // Ensure that selected don't point to a non-existing proposition
00075                 if (propositionSelected() == operator[](delProp->name()))
00076                         setPropositionSelected(operator[]((*ix)->source()->name()));
00077         while ( ix != delProp->incoming->end()) {
00078                 edges->removeRef(*ix);
00079                 (*ix)->source()->outgoing->removeRef(*ix);
00080                 ++ix;
00081                 }
00082         delProp->incoming->clear();
00083                 remove(delProp->name());
00084                 //useDot();
00085                 setChanged(true);
00086         }
00087         else 
00088         {
00089                 QMessageBox::information( gui,"Modular Psi",
00090                 QObject::tr("Deletion was not possible.\n"
00091                 "Proposition have outgoing connections"));              
00092     }
00093 }
00094 
00095 void PropositionGraph::init()
00096 {
00097         categories = new CategoryList;
00098 }
00099 
00100 QString PropositionGraph::newProposition()
00101 {
00102     QString newName = QString("P%1").arg(lastName()+1);
00103     newProposition(newName);
00104     return newName;
00105 }
00106 
00107 void PropositionGraph::newProposition(QString newName)
00108 {
00109     replace(newName, new Proposition(gui->props, newName));
00110     this->operator[](newName)->setName(newName);
00111     this->operator[](newName)->graph = this;
00112     checkLastName(newName);
00113     this->operator[](newName)->setCategoryId("na");
00114 }
00115 
00116 void PropositionGraph::newProposition (Proposition* father)
00117 {
00118         QString newprop = newProposition();
00119         operator[](newprop)->setPublicName(Proposition::tr("New Proposition"));
00120         connect(father, operator[](newprop));
00121         useDot();
00122         setChanged(true);
00123 } 
00124 
00125 Edge* PropositionGraph::connect(QString source, QString target)
00126 {    
00127     Node *s, *t;
00128     s = operator[](source);
00129     t = operator[](target);
00130     
00131     QString ss = s->name();
00132     QString st = t->name();
00133    Edge* e = new Edge (gui->props, s,t);
00134    e->setName(ss + "-" + st);
00135    edges->append(e);
00136    return e;
00137 }
00138 
00139 Edge* PropositionGraph::connect(Proposition* source, Proposition* target)
00140 {
00141         return connect (source->name() , target->name());
00142 }
00143 
00144 Edge* PropositionGraph::connect(QString source, QString target, QString trust)
00145 {
00146         Edge* e = connect (source, target);
00147         e->setTrust(trust);
00148         return e;
00149 }
00150 
00151 void PropositionGraph::load()
00152 {
00153     
00154     // erase previous existing itens
00155     clear(); 
00156     edges->clear();
00157     clearAllEdges();
00158     
00159     loadCats();
00160 
00161     GraphML handler(this);
00162     QFile file("data/prop.graphml");
00163     QXmlSimpleReader reader;
00164     reader.setContentHandler(&handler);
00165     reader.setErrorHandler(&handler);
00166     reader.parse(&file);
00167     file.close();
00168     initComboCat();
00169     
00170     // Prevents previous invalid selections.
00171     if (count() > 0)
00172     setPropositionSelected(operator[]("P1"));
00173     
00174     useDot();
00175     setChanged(false);
00176 }
00177 
00178 void PropositionGraph::initComboCat()
00179 {
00180         int i=0;
00181         gui->categorycombo->clear();
00182         QDictIterator<Category> ix( *categories );
00183     for( ; ix.current(); ++ix )
00184     {
00185         gui->categorycombo->insertItem(ix.current()->name(),i);
00186         ix.current()->setPosId(i);
00187         i++;
00188     }
00189 }
00190 
00191 void PropositionGraph::save(QString dir)
00192 {
00193     GraphML handler(this);
00194     handler.save(dir);
00195     setChanged(false);
00196 }
00197 
00198 void PropositionGraph::useDot()
00199 {
00200     Dot* d = new Dot(this);
00201     d->callDot();
00202     if (count() > 0) 
00203         BuildPropositions bp (gui,  this);
00204     gui->props->repaint();
00205 }
00206 
00207 void PropositionGraph::setPropositionSelected(Proposition* sel)
00208 {
00209         gui->infoStack->raiseWidget(0);
00210         p_selected = sel;
00211         gui->nameEdit->setText(sel->publicName());
00212         gui->DescriptionEdit->setText(sel->description());
00213         gui->categorycombo->setCurrentItem(sel->cat->posId());
00214         gui->referencesEdit->setText(sel->references());
00215         gui->trustCombo->setCurrentItem( float2TrustItem(sel->trust()));
00216         gui->changeButton->setEnabled(false);
00217 }
00218 
00219 void PropositionGraph::setEdgeSelected(Edge* sel)
00220 {
00221         gui->infoStack->raiseWidget(1);
00222         e_selected = sel;
00223         gui->edgeTrust->setCurrentItem( float2TrustItem(sel->trust()));
00224         gui->edgeSource->setText(sel->source()->publicName());
00225         gui->edgeTarget->setText(sel->target()->publicName());
00226 }
00227 
00228 void PropositionGraph::setInfo (short int inf)
00229 {
00230         p_info = inf;
00231 }
00232 
00233 void PropositionGraph::setStatus(int stat)
00234 {
00235         if (stat == STATUS_NOOP)
00236         {
00237                 gui->setStatusText(Principal::tr("Ready"));
00238         } else if (stat == STATUS_NEWPROP)
00239         {
00240                 gui->setStatusText(Principal::tr("Select Parent Proposition"));
00241         } else if (stat == STATUS_CONN_SOURCE)
00242         {
00243                 gui->setStatusText(Principal::tr("Select Connection Source"));
00244         } else if (stat == STATUS_CONN_TARGET)
00245         {
00246                 gui->setStatusText(Principal::tr("Select Connection Target"));
00247         } else if (stat == STATUS_EDGE_DELETE)
00248         {
00249                 gui->setStatusText(Principal::tr("Select Connection to be Removed"));
00250         }
00251         
00252         p_status = stat;
00253 }
00254 
00255 void PropositionGraph::loadCats()
00256 {
00257         Cat chandler(this);
00258     QFile cfile("data/cats.mpsi");
00259     QXmlSimpleReader creader;
00260     creader.setContentHandler(&chandler);
00261     creader.setErrorHandler(&chandler);
00262     creader.parse(&cfile);
00263     cfile.close();
00264 }
00265 
00266 void PropositionGraph::update()
00267 {
00268         propositionSelected()->setPublicName(gui->nameEdit->text());
00269         propositionSelected()->setDescription(gui->DescriptionEdit->text());
00270         propositionSelected()->setReferences(gui->referencesEdit->text());
00271         propositionSelected()->setCategory(gui->categorycombo->currentText());
00272         setChanged(true);
00273 }
00274 
00275 void PropositionGraph::edgeUpdate()
00276 {
00277         edgeSelected()->setTrust( trustItem2float(gui->edgeTrust->currentItem()));
00278         setChanged(true);
00279         useDot();
00280 }
Untitled Document Pesquisa Psi SourceForge.net Logo