SpikeStream Application Library  0.2
ConnectionGroupModel.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "Globals.h"
00003 #include "NetworkDisplay.h"
00004 #include "ConnectionGroupModel.h"
00005 #include "SpikeStreamException.h"
00006 using namespace spikestream;
00007 
00008 //Qt includes
00009 #include <QDebug>
00010 #include <QIcon>
00011 
00013 ConnectionGroupModel::ConnectionGroupModel() : QAbstractTableModel(){
00014         connect(Globals::getEventRouter(), SIGNAL(networkChangedSignal()), this, SLOT(loadConnectionGroups()));
00015         connect(Globals::getEventRouter(), SIGNAL(showAllOrNoneConnectionsSignal()), this, SLOT(showAllOrNone()));
00016 }
00017 
00018 
00020 ConnectionGroupModel::~ConnectionGroupModel(){
00021 }
00022 
00023 
00024 /*--------------------------------------------------------*/
00025 /*-------             PUBLIC METHODS               -------*/
00026 /*--------------------------------------------------------*/
00027 
00029 void ConnectionGroupModel::clearSelection(){
00030         selectionMap.clear();
00031 }
00032 
00033 
00035 int ConnectionGroupModel::columnCount(const QModelIndex&) const{
00036         return NUM_COLS;
00037 }
00038 
00039 
00042 QVariant ConnectionGroupModel::data(const QModelIndex & index, int role) const{
00043     //Return invalid index if index is invalid or no network loaded
00044     if (!index.isValid())
00045                 return QVariant();
00046     if(!Globals::networkLoaded())
00047                 return QVariant();
00048 
00049     //Check rows and columns are in range
00050     if (index.row() < 0 || index.row() >= rowCount() || index.column() < 0 || index.column() >= columnCount())
00051                 return QVariant();
00052 
00053     //Return appropriate data
00054     if (role == Qt::DisplayRole){
00055                 if(index.column() == ID_COL)
00056                         return conGrpInfoList[index.row()].getID();
00057                 if(index.column() == DESC_COL)
00058                         return conGrpInfoList[index.row()].getDescription();
00059                 if(index.column() == SIZE_COL)
00060                         return conGrpSizeList[index.row()];
00061                 if(index.column() == FROM_NEUR_ID_COL)
00062                         return conGrpInfoList[index.row()].getFromNeuronGroupID();
00063                 if(index.column() == T0_NEUR_ID_COL)
00064                         return conGrpInfoList[index.row()].getToNeuronGroupID();
00065                 if(index.column() == SYNAPSE_TYPE_COL)
00066                         return conGrpInfoList[index.row()].getSynapseTypeID();
00067     }
00068 
00069         //Icons
00070     if (role == Qt::DecorationRole){
00071                 if(index.column() == VIS_COL ){
00072                         if(Globals::getNetworkDisplay()->connectionGroupVisible(conGrpInfoList[index.row()].getID()))
00073                                 return QIcon(Globals::getSpikeStreamRoot() + "/images/visible.xpm");
00074                         return QIcon(Globals::getSpikeStreamRoot() + "/images/hidden.xpm");
00075                 }
00076                 if(index.column() == PARAM_COL){
00077                         return QIcon(Globals::getSpikeStreamRoot() + "/images/parameters.xpm");
00078                 }
00079     }
00080 
00081         if(role == Qt::SizeHintRole){
00082                 if(index.column() == PARAM_COL){
00083                         return QSize(50, 18);
00084                 }
00085         }
00086 
00087         //Check boxes
00088         if(role == Qt::CheckStateRole){
00089                 if(index.column() == SELECT_COL){
00090                         if(selectionMap.contains(index.row())){
00091                                 return true;
00092                         }
00093                         else {
00094                                 return false;
00095                         }
00096                 }
00097         }
00098 
00099     //If we have reached this point ignore request
00100     return QVariant();
00101 }
00102 
00103 
00106 ConnectionGroupInfo ConnectionGroupModel::getInfo(const QModelIndex & index){
00107         if(index.row() >= conGrpInfoList.size())
00108                 throw SpikeStreamException("Index out of range: " + QString::number(index.row()));
00109         return conGrpInfoList[index.row()];
00110 }
00111 
00112 
00114 QHash<QString, double> ConnectionGroupModel::getParameters(int row){
00115         if(row >= rowCount())
00116                 throw SpikeStreamException("Failed to get parameters: row out of range: " + QString::number(row));
00117         return conGrpInfoList[row].getParameterMap();
00118 }
00119 
00120 
00122 QList<unsigned int> ConnectionGroupModel::getSelectedConnectionGroupIDs(){
00123         //Double check lengths
00124         if(conGrpInfoList.size() < selectionMap.size())
00125                 throw SpikeStreamException("There are more selected indexes than indexes");
00126 
00127         QList<unsigned int> conGrpIDList;
00128         foreach(unsigned int index, selectionMap.keys()){
00129                 conGrpIDList.append(conGrpInfoList.at(index).getID());
00130         }
00131 
00132         //Return list
00133         return conGrpIDList;
00134 }
00135 
00136 
00138 void ConnectionGroupModel::reload(){
00139         loadConnectionGroups();
00140 }
00141 
00142 
00144 void ConnectionGroupModel::selectAllOrNone(){
00145         //Deselect all groups
00146         if(selectionMap.size() == conGrpInfoList.size()){
00147                 selectionMap.clear();
00148         }
00149 
00150         //Select all groups
00151         else{
00152                 for(int i=0; i<conGrpInfoList.size(); ++i)
00153                         selectionMap[i] = true;
00154         }
00155         reset();
00156 }
00157 
00158 
00160 void ConnectionGroupModel::showAllOrNone(){
00161         //Make all groups visible if there are none showing
00162         QList<unsigned> visConGrpIds = Globals::getNetworkDisplay()->getVisibleConnectionGroupIDs();
00163         if(visConGrpIds.isEmpty()){
00164                 for(int i=0; i<conGrpInfoList.size(); ++i)
00165                         visConGrpIds.append(conGrpInfoList.at(i).getID());
00166         }
00167 
00168         //One or more is hidden: make all groups invisible
00169         else{
00170                 visConGrpIds.clear();
00171         }
00172         Globals::getNetworkDisplay()->setVisibleConnectionGroupIDs(visConGrpIds);
00173         reset();
00174 }
00175 
00176 
00178 bool ConnectionGroupModel::setData(const QModelIndex& index, const QVariant&, int) {
00179     if (!index.isValid() || !Globals::networkLoaded())
00180                 return false;
00181     if (index.row() < 0 || index.row() >= rowCount())
00182                 return false;
00183 
00184     //Change visibility of neuron group
00185         if(index.column() == VIS_COL){
00186                 unsigned int tmpConGrpID = conGrpInfoList[index.row()].getID();
00187                 if(Globals::getNetworkDisplay()->connectionGroupVisible(tmpConGrpID))
00188                         Globals::getNetworkDisplay()->setConnectionGroupVisibility(tmpConGrpID, false);
00189                 else
00190                         Globals::getNetworkDisplay()->setConnectionGroupVisibility(tmpConGrpID, true);
00191 
00192                 //Emit signal that data has changed and return true to indicate data set succesfully.
00193                 emit dataChanged(index, index);
00194                 return true;
00195     }
00196 
00197         //Change selection status of connection group
00198         if(index.column() == SELECT_COL){
00199                 if(selectionMap.contains(index.row()))
00200                         selectionMap.remove(index.row());
00201                 else
00202                         selectionMap[index.row()] = true;
00203                 reset();
00204                 return true;
00205         }
00206 
00207     //If we have reached this point no data has been set
00208     return false;
00209 }
00210 
00211 
00213 QVariant ConnectionGroupModel::headerData(int section, Qt::Orientation orientation, int role) const{
00214     if (role != Qt::DisplayRole)
00215                 return QVariant();
00216 
00217     if (orientation == Qt::Horizontal){
00218                 if(section == ID_COL)
00219                         return "ID";
00220                 if(section == DESC_COL)
00221                         return "Description";
00222                 if(section == SIZE_COL)
00223                         return "Size";
00224                 if(section == FROM_NEUR_ID_COL)
00225                         return "From";
00226                 if(section == T0_NEUR_ID_COL)
00227                         return "To";
00228                 if(section == SYNAPSE_TYPE_COL)
00229                         return "Synapse Type";
00230                 if(section == PARAM_COL)
00231                         return "Parameters";
00232     }
00233 
00234     return QVariant();//QString("Roow %1").arg(section);
00235 }
00236 
00237 
00239 int ConnectionGroupModel::rowCount(const QModelIndex&) const{
00240     return conGrpInfoList.size();
00241 }
00242 
00243 
00245 void ConnectionGroupModel::loadConnectionGroups(){
00246     //Clear current list of neuron group information
00247     conGrpInfoList.clear();
00248         conGrpSizeList.clear();
00249 
00250         //Get list of current connection group info
00251     if(Globals::networkLoaded())
00252                 conGrpInfoList = Globals::getNetwork()->getConnectionGroupsInfo();
00253 
00254         //Load up sizes of connection groups
00255         for(int i=0; i<conGrpInfoList.size(); ++i)
00256                 conGrpSizeList.append( Globals::getNetwork()->getConnectionCount( conGrpInfoList.at(i).getID() ) );
00257 
00258         //Sanity check
00259         if(conGrpInfoList.size() != conGrpSizeList.size())
00260                 qCritical()<<"Mismatch between list of connection group info and list of connection group size.";
00261 
00262     //Instruct listening classes to reload data
00263     this->reset();
00264 }
00265 
00266 
00267 
 All Classes Files Functions Variables Typedefs Friends Defines