SpikeStream Application Library  0.2
NeuronGroupSelectionModel.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "NeuronGroupSelectionModel.h"
00003 #include "SpikeStreamException.h"
00004 using namespace spikestream;
00005 
00006 
00008 NeuronGroupSelectionModel::NeuronGroupSelectionModel(QList<NeuronGroup*> neuronGroupList) : QAbstractTableModel(){
00009         this->neurGrpList = neuronGroupList;
00010 
00011         //Select all by default
00012         for(int i=0; i<neurGrpList.size(); ++i)
00013                 selectionMap[i] = true;
00014 }
00015 
00016 
00018 NeuronGroupSelectionModel::~NeuronGroupSelectionModel(){
00019 }
00020 
00021 
00022 /*--------------------------------------------------------*/
00023 /*-------             PUBLIC METHODS               -------*/
00024 /*--------------------------------------------------------*/
00025 
00027 int NeuronGroupSelectionModel::columnCount(const QModelIndex&) const{
00028         return NUM_COLS;
00029 }
00030 
00031 
00034 QVariant NeuronGroupSelectionModel::data(const QModelIndex & index, int role) const{
00035         //Return invalid index if index is invalid or no network loaded
00036         if (!index.isValid())
00037                 return QVariant();
00038 
00039         //Check rows and columns are in range
00040         if (index.row() < 0 || index.row() >= rowCount() || index.column() < 0 || index.column() >= columnCount())
00041                 return QVariant();
00042 
00043         //Return appropriate data
00044         if (role == Qt::DisplayRole){
00045                 if(index.column() == ID_COL)
00046                         return neurGrpList[index.row()]->getID();
00047                 if(index.column() == NAME_COL)
00048                         return neurGrpList[index.row()]->getInfo().getName();
00049         }
00050 
00051         //Check boxes
00052         if(role == Qt::CheckStateRole){
00053                 if(index.column() == SELECT_COL){
00054                         if(selectionMap.contains(index.row())){
00055                                 return true;
00056                         }
00057                         else {
00058                                 return false;
00059                         }
00060                 }
00061         }
00062 
00063         //If we have reached this point ignore request
00064         return QVariant();
00065 }
00066 
00067 
00069 QList<NeuronGroup*> NeuronGroupSelectionModel::getSelectedNeuronGroups(){
00070         //Double check lengths
00071         if(neurGrpList.size() < selectionMap.size())
00072                 throw SpikeStreamException("There are more selected indexes than indexes");
00073 
00074         QList<NeuronGroup*> tmpNeurGrpList;
00075         foreach(unsigned int index, selectionMap.keys()){
00076                 tmpNeurGrpList.append(neurGrpList.at(index));
00077         }
00078 
00079         //Return list
00080         return tmpNeurGrpList;
00081 }
00082 
00083 
00085 void NeuronGroupSelectionModel::selectAllOrNone(){
00086         //Deselect all groups
00087         if(selectionMap.size() == neurGrpList.size()){
00088                 selectionMap.clear();
00089         }
00090 
00091         //Select all groups
00092         else{
00093                 for(int i=0; i<neurGrpList.size(); ++i)
00094                         selectionMap[i] = true;
00095         }
00096         reset();
00097 }
00098 
00099 
00100 
00102 bool NeuronGroupSelectionModel::setData(const QModelIndex& index, const QVariant&, int) {
00103         if (!index.isValid())
00104                 return false;
00105         if (index.row() < 0 || index.row() >= rowCount())
00106                 return false;
00107 
00108         //Change selection status of neuron group
00109         if(index.column() == SELECT_COL){
00110                 if(selectionMap.contains(index.row()))
00111                         selectionMap.remove(index.row());
00112                 else
00113                         selectionMap[index.row()] = true;
00114                 reset();
00115                 return true;
00116         }
00117 
00118         //If we have reached this point no data has been set
00119         return false;
00120 }
00121 
00122 
00124 QVariant NeuronGroupSelectionModel::headerData(int section, Qt::Orientation orientation, int role) const{
00125         if (role != Qt::DisplayRole)
00126                 return QVariant();
00127 
00128         if (orientation == Qt::Horizontal){
00129                 if(section == ID_COL)
00130                         return "ID";
00131                 if(section == NAME_COL)
00132                         return "Name";
00133         }
00134 
00135         return QVariant();
00136 }
00137 
00138 
00140 int NeuronGroupSelectionModel::rowCount(const QModelIndex&) const{
00141         return neurGrpList.size();
00142 }
00143 
00144 
00145 
 All Classes Files Functions Variables Typedefs Friends Defines