00001 #include "model.h" 00002 00003 #include "model.moc" 00004 00005 #include "viewer.h" 00006 00007 #include <QFile> 00008 #include <QTextStream> 00009 #include <util/forall.h> 00010 #include <QVariant> 00011 #include <QAction> 00012 #include <cassert> 00013 #include <util/assert.h> 00014 #include <QMetaObject> 00015 00016 #include <graph/vertex.h> 00017 00018 static QTextStream out(stdout); 00019 00020 Model::Model(QObject* parent) 00021 : FileObject(parent) 00022 { 00023 #ifndef NO_NUMBER_VERTICES 00024 graph::vertex_counter = 0; 00025 #endif 00026 connect(this, SIGNAL(fileRegister(std::string)), parent, SLOT(registerFile(std::string))); 00027 connect(this, SIGNAL(fileUnregister(std::string)), parent, SLOT(unregisterFile(std::string))); 00028 connect(this, SIGNAL(stopModel()), parent, SLOT(stopModel())); 00029 connect(this, SIGNAL(runModel()), parent, SLOT(runModel())); 00030 connect(this, SIGNAL(restartModel()), parent, SLOT(rewind())); 00031 connect(this, SIGNAL(menuActionInserted(QAction*,QAction*)), parent, SLOT(insertMenuAction(QAction*,QAction*))); 00032 connect(this, SIGNAL(menuActionRemoved(QAction*)), parent, SLOT(removeMenuAction(QAction*))); 00033 connect(this, SIGNAL(savingSnapshot(const QString&, Model*)), parent, SLOT(saveSnapshot(const QString&, Model*))); 00034 connect(this, SIGNAL(savingNextSnapshot(bool, Model*)), parent, SLOT(saveSnapshot(bool, Model*))); 00035 connect(this, SIGNAL(loadingSnapshot(const QString&, Model*)), parent, SLOT(loadSnapshot(const QString&, Model*))); 00036 connect(this, SIGNAL(loadingSnapshot(bool, Model*)), parent, SLOT(loadSnapshot(bool, Model*))); 00037 connect(this, SIGNAL(savingScreenshot(const QString&, bool)), parent, SLOT(screenshot(const QString&, bool))); 00038 00039 QVariant args = parent->property("modelArguments"); 00040 if(args.isValid()) 00041 { 00042 if(args.type() == QVariant::StringList) 00043 { 00044 _arguments = args.toStringList(); 00045 } 00046 } 00047 } 00048 00049 void Model::registerFile( std::string filename ) 00050 { 00051 emit fileRegister(filename); 00052 } 00053 00054 void Model::unregisterFile( std::string filename ) 00055 { 00056 emit fileUnregister(filename); 00057 } 00058 00059 QString Model::helpString() const 00060 { 00061 if(QFile::exists("description.html")) 00062 { 00063 QFile f("description.html"); 00064 if(f.open(QIODevice::ReadOnly | QIODevice::Text)) 00065 { 00066 QTextStream fss(&f); 00067 QString content = f.readAll(); 00068 return content; 00069 } 00070 } 00071 else if(QFile::exists("description.txt")) 00072 { 00073 QFile f("description.txt"); 00074 if(f.open(QIODevice::ReadOnly | QIODevice::Text)) 00075 { 00076 QTextStream fss(&f); 00077 QString content = f.readAll(); 00078 content.prepend("<pre>"); 00079 content.append("</pre>"); 00080 return content; 00081 } 00082 } 00083 return QString("Undocumented VV model"); 00084 } 00085 00086 void Model::run() 00087 { 00088 emit runModel(); 00089 } 00090 00091 void Model::stop() 00092 { 00093 emit stopModel(); 00094 } 00095 00096 void Model::restart() 00097 { 00098 emit restartModel(); 00099 } 00100 00101 void Model::postSelection(const QPoint&, Viewer* ) 00102 { 00103 } 00104 00105 void Model::setExitCode(int code) 00106 { 00107 emit changedExitCode(code); 00108 } 00109 00110 void Model::setAnimationPeriod(int ms) 00111 { 00112 if(parent()) 00113 parent()->setProperty("animationPeriod", ms); 00114 } 00115 00116 int Model::animationPeriod() 00117 { 00118 bool ok; 00119 int period = parent()->property("animationPeriod").toInt(&ok); 00120 if(!ok) 00121 return -1; 00122 return period; 00123 } 00124 00125 QList<QAction*> Model::actions() const 00126 { 00127 return _actions; 00128 } 00129 00130 QAction* Model::menuItem(const QString& text) const 00131 { 00132 foreach(QAction* act, actions()) 00133 { 00134 if(act->text() == text) 00135 return act; 00136 } 00137 return 0; 00138 } 00139 00140 QAction* Model::addMenuItem(const QString& text) 00141 { 00142 QAction *act = menuItem(text); 00143 if(not act) 00144 { 00145 act = new QAction(text, this); 00146 addAction(act); 00147 } 00148 return act; 00149 } 00150 00151 QAction* Model::addMenuItem(const QString& text, QObject* receiver, const char* slot) 00152 { 00153 QAction *act = menuItem(text); 00154 if(act) 00155 { 00156 connect(act, SIGNAL(triggered()), receiver, slot); 00157 } 00158 else 00159 { 00160 act = new QAction(text, this); 00161 connect(act, SIGNAL(triggered()), receiver, slot); 00162 addAction(act); 00163 } 00164 return act; 00165 } 00166 00167 QAction* Model::addMenuItem(const QString& text, const char* slot) 00168 { 00169 return addMenuItem(text, this, slot); 00170 } 00171 00172 bool Model::removeMenuItem(const QString& text) 00173 { 00174 QAction* toremove = menuItem(text); 00175 if(toremove) 00176 { 00177 removeAction(toremove); 00178 return true; 00179 } 00180 return false; 00181 } 00182 00183 bool Model::removeMenuItem(const QString& text, const char* slot) 00184 { 00185 return removeMenuItem(text, this, slot); 00186 } 00187 00188 bool Model::removeMenuItem(const QString& text, QObject* receiver, const char* slot) 00189 { 00190 QAction* toremove = menuItem(text); 00191 if(toremove and disconnect(toremove, SIGNAL(triggerred()), receiver, slot)) 00192 return true; 00193 return false; 00194 } 00195 00196 void Model::addAction(QAction *action) 00197 { 00198 insertAction(0, action); 00199 } 00200 00201 void Model::addActions(QList<QAction*> actions) 00202 { 00203 insertActions(0, actions); 00204 } 00205 00206 void Model::insertAction(QAction* before, QAction* action) 00207 { 00208 int pos = _actions.indexOf(before); 00209 if(pos < 0) 00210 { 00211 pos = _actions.size(); 00212 } 00213 _actions.insert(pos, action); 00214 emit menuActionInserted(before, action); 00215 } 00216 00217 void Model::insertActions(QAction* before, QList<QAction*> actions) 00218 { 00219 for(int i = 0 ; i < actions.size() ; ++i) 00220 { 00221 insertAction(before, actions[i]); 00222 } 00223 } 00224 00225 void Model::removeAction(QAction* action) 00226 { 00227 if(_actions.removeAll(action)) 00228 emit menuActionRemoved(action); 00229 } 00230 00231 bool Model::saveSnapshot(const QString& filename) 00232 { 00233 _error = false; 00234 emit savingSnapshot(filename, this); 00235 return !_error; 00236 } 00237 00238 bool Model::saveNextSnapshot() 00239 { 00240 _error = false; 00241 emit savingNextSnapshot(true, this); 00242 return !_error; 00243 } 00244 00245 bool Model::loadSnapshot() 00246 { 00247 _error = false; 00248 emit loadingSnapshot(false, this); 00249 return !_error; 00250 } 00251 00252 bool Model::loadSnapshot(const QString& filename) 00253 { 00254 _error = false; 00255 emit loadingSnapshot(filename, this); 00256 return !_error; 00257 } 00258 00259 void Model::screenshot(const QString &filename, bool overwrite) 00260 { 00261 emit savingScreenshot(filename, overwrite); 00262 } 00263 00264 void Model::setStatusMessage(const QString& msg) 00265 { 00266 bool success = parent()->setProperty("statusMessage", msg); 00267 assert(success); 00268 } 00269 00270 QString Model::statusMessage() const 00271 { 00272 QString result = parent()->property("statusMessage").toString(); 00273 return result; 00274 } 00275 00276