SpikeStream Application Library
0.2
|
00001 //SpikeStream includes 00002 #include "Globals.h" 00003 #include "NetworkDisplay.h" 00004 #include "SynapseParametersModel.h" 00005 #include "SpikeStreamException.h" 00006 using namespace spikestream; 00007 00008 //Qt includes 00009 #include <QDebug> 00010 #include <QIcon> 00011 00012 00014 SynapseParametersModel::SynapseParametersModel(unsigned int synapseTypeID){ 00015 //Listen for changes in the network 00016 connect(Globals::getEventRouter(), SIGNAL(networkChangedSignal()), this, SLOT(networkChanged())); 00017 00018 //Store the synapse type - this class can only handle connections of a single type 00019 this->synapseTypeID = synapseTypeID; 00020 00021 //Load up the parameters for the current network 00022 loadParameters(); 00023 } 00024 00025 00027 SynapseParametersModel::~SynapseParametersModel(){ 00028 } 00029 00030 00031 /*--------------------------------------------------------*/ 00032 /*------- PUBLIC METHODS -------*/ 00033 /*--------------------------------------------------------*/ 00034 00036 int SynapseParametersModel::columnCount(const QModelIndex&) const{ 00037 return parameterInfoList.size() + 2; 00038 } 00039 00040 00043 QVariant SynapseParametersModel::data(const QModelIndex & index, int role) const{ 00044 //Return invalid index if index is invalid or no network loaded 00045 if (!index.isValid()) 00046 return QVariant(); 00047 if(!Globals::networkLoaded()) 00048 return QVariant(); 00049 00050 //Check rows and columns are in range 00051 if (index.row() < 0 || index.row() >= rowCount() || index.column() < 0 || index.column() >= columnCount()) 00052 return QVariant(); 00053 00054 //Return appropriate data 00055 if (role == Qt::DisplayRole){ 00056 if(index.column() == 0) 00057 return conGrpInfoList[index.row()].getDescription().left(30) + "(" + QString::number(conGrpInfoList[index.row()].getID()) + ")"; 00058 if(index.column() > 0 && index.column() <= parameterInfoList.size()){ 00059 unsigned int conGrpID = conGrpInfoList[index.row()].getID(); 00060 00061 //Check that we have the parameter data 00062 if(parameterMap.contains(conGrpID) && parameterMap[conGrpID].contains(parameterInfoList.at(index.column()-1).getName())){ 00063 ParameterInfo info = parameterInfoList.at(index.column()-1); 00064 00065 //Only return double value if parameter is a double 00066 if(info.getType() == ParameterInfo::DOUBLE){ 00067 return parameterMap[conGrpID][info.getName()]; 00068 } 00069 else if(info.getType() == ParameterInfo::BOOLEAN){ 00070 if(parameterMap[conGrpID][info.getName()] == 0) 00071 return "off"; 00072 return "on"; 00073 } 00074 else 00075 throw SpikeStreamException("Parameter type not recognized: " + QString::number(info.getType())); 00076 00077 } 00078 //Cannot find parameter data - error has occurred somewhere 00079 else{ 00080 throw SpikeStreamException("Maps missing entry somewhere."); 00081 } 00082 } 00083 } 00084 00085 //Icons 00086 if (role == Qt::DecorationRole){ 00087 //Edit button 00088 if(index.column() == parameterInfoList.size() + 1 ){ 00089 QIcon tmpIcon(Globals::getSpikeStreamRoot() + "/images/edit.png"); 00090 return tmpIcon; 00091 } 00092 } 00093 00094 //Size hint 00095 if(role == Qt::SizeHintRole){ 00096 //Edit button 00097 if(index.column() == parameterInfoList.size() + 1 ){ 00098 return QSize(40, 40); 00099 } 00100 } 00101 00102 //If we have reached this point ignore request 00103 return QVariant(); 00104 } 00105 00106 00108 ConnectionGroupInfo SynapseParametersModel::getConnectionGroupInfo(int row){ 00109 if(row >= conGrpInfoList.size()) 00110 throw SpikeStreamException("Request for connection group info at row " + QString::number(row) + " is out of range."); 00111 return conGrpInfoList.at(row); 00112 } 00113 00114 00116 QList<ParameterInfo> SynapseParametersModel::getParameterInfoList(){ 00117 return parameterInfoList; 00118 } 00119 00120 00122 QHash<QString, double> SynapseParametersModel::getParameterValues(int row){ 00123 ConnectionGroupInfo info = getConnectionGroupInfo(row); 00124 if(!parameterMap.contains(info.getID())) 00125 throw SpikeStreamException("Mismatch between parameter map and connection group info list."); 00126 00127 return parameterMap[info.getID()]; 00128 } 00129 00130 00132 bool SynapseParametersModel::setData(const QModelIndex& index, const QVariant&, int) { 00133 if (!index.isValid() || !Globals::networkLoaded()) 00134 return false; 00135 if (index.row() < 0 || index.row() >= rowCount()) 00136 return false; 00137 00138 //If we have reached this point no data has been set 00139 return false; 00140 } 00141 00142 00144 QVariant SynapseParametersModel::headerData(int section, Qt::Orientation orientation, int role) const{ 00145 if (role != Qt::DisplayRole) 00146 return QVariant(); 00147 00148 if (orientation == Qt::Horizontal){ 00149 if(section == 0) 00150 return "Connection Group"; 00151 if(section == parameterInfoList.size() + 1)//Edit column 00152 return ""; 00153 if(section > 0 && section <= parameterInfoList.size())//Parameter name column 00154 return parameterInfoList.at(section - 1).getName(); 00155 } 00156 00157 return QVariant(); 00158 } 00159 00161 void SynapseParametersModel::reload(){ 00162 loadParameters(); 00163 } 00164 00165 00167 int SynapseParametersModel::rowCount(const QModelIndex&) const{ 00168 return conGrpInfoList.size(); 00169 } 00170 00171 00172 /*--------------------------------------------------------*/ 00173 /*------- PRIVATE SLOTS -------*/ 00174 /*--------------------------------------------------------*/ 00175 00177 void SynapseParametersModel::networkChanged(){ 00178 loadParameters(); 00179 } 00180 00181 00182 /*--------------------------------------------------------*/ 00183 /*------- PRIVATE METHODS -------*/ 00184 /*--------------------------------------------------------*/ 00185 00187 void SynapseParametersModel::checkParameters(){ 00188 foreach(ConnectionGroupInfo conGrpInfo, conGrpInfoList){ 00189 if(!parameterMap.contains(conGrpInfo.getID())) 00190 throw SpikeStreamException("Parameter map does not contain a loaded connection group"); 00191 00192 //Check that keys in parameter map match keys for this synapse type 00193 QHash<QString, double> conGrpParamMap = parameterMap[conGrpInfo.getID()]; 00194 QList<QString> paramKeys =conGrpParamMap.keys(); 00195 foreach(QString key, paramKeys){ 00196 bool paramFound = false; 00197 foreach(ParameterInfo paramInfo, parameterInfoList){ 00198 if(paramInfo.getName() == key) 00199 paramFound = true; 00200 } 00201 if(!paramFound){ 00202 throw SpikeStreamException("Key " + key + " from connection group not found in parameter info list"); 00203 } 00204 } 00205 } 00206 } 00207 00208 00210 void SynapseParametersModel::loadParameters(){ 00211 //Clear lists of information 00212 conGrpInfoList.clear(); 00213 parameterMap.clear(); 00214 parameterInfoList.clear(); 00215 00216 //Do nothing if network is not loaded 00217 if(!Globals::networkLoaded()){ 00218 this->reset(); 00219 return; 00220 } 00221 00222 try{ 00223 //Get list of current connection group info filtered by the synapse type 00224 conGrpInfoList = Globals::getNetwork()->getConnectionGroupsInfo(synapseTypeID); 00225 00226 //Check that they are all the same type 00227 bool firstTime = true; 00228 foreach(ConnectionGroupInfo info, conGrpInfoList){ 00229 if(firstTime){ 00230 synapseTypeID = info.getSynapseTypeID(); 00231 firstTime = false; 00232 } 00233 else if(synapseTypeID != info.getSynapseTypeID()){ 00234 throw SpikeStreamException("SynapseParametersModel only supports synapses of a single type."); 00235 } 00236 } 00237 00238 //Get list of available parameters 00239 parameterInfoList = Globals::getNetworkDao()->getSynapseType(synapseTypeID).getParameterInfoList(); 00240 00241 //Get the parameters for each connection group 00242 foreach(ConnectionGroupInfo conGrpInfo, conGrpInfoList){ 00243 parameterMap[conGrpInfo.getID()] = Globals::getNetwork()->getConnectionGroup(conGrpInfo.getID())->getParameters(); 00244 } 00245 00246 //Check that it all matches up 00247 checkParameters(); 00248 } 00249 catch(SpikeStreamException& ex){ 00250 qCritical()<<ex.getMessage(); 00251 } 00252 00253 //Instruct listening classes to reload data 00254 this->reset(); 00255 } 00256 00257