SpikeStream Application Library  0.2
ConnectionWidget.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "ConnectionWidget.h"
00003 #include "ConnectionGroupTableView.h"
00004 #include "Globals.h"
00005 #include "PluginsDialog.h"
00006 #include "SpikeStreamException.h"
00007 using namespace spikestream;
00008 
00009 //Qt includes
00010 #include <QLayout>
00011 #include <QMessageBox>
00012 #include <QMutexLocker>
00013 
00014 
00016 ConnectionWidget::ConnectionWidget(QWidget* parent)  : QWidget(parent) {
00017         //Vertical box to organize layout
00018         QVBoxLayout* verticalBox = new QVBoxLayout(this);
00019 
00020         //Add button to launch add neurons dialog
00021         QHBoxLayout* buttonLayout = new QHBoxLayout();
00022         addConnectionsButton = new QPushButton(" Add Connections ");
00023         addConnectionsButton->setEnabled(false);
00024         connect (addConnectionsButton, SIGNAL(clicked()), this, SLOT(addConnections()));
00025         buttonLayout->addWidget(addConnectionsButton);
00026         deleteButton = new QPushButton(QIcon(Globals::getSpikeStreamRoot() + "/images/trash_can.png"), "");
00027         deleteButton->setEnabled(false);
00028         connect (deleteButton, SIGNAL(clicked()), this, SLOT(deleteSelectedConnections()));
00029         buttonLayout->addWidget(deleteButton);
00030         buttonLayout->addStretch(10);
00031         verticalBox->addLayout(buttonLayout);
00032 
00033         //Construct table view and model
00034         connectionGroupModel = new ConnectionGroupModel();
00035         QTableView* connGroupView = new ConnectionGroupTableView(this, connectionGroupModel);
00036         verticalBox->addWidget(connGroupView);
00037 
00038         //Connection manager to delete connections in a separate thread
00039         connectionManager = new ConnectionManager();
00040         connect(connectionManager, SIGNAL(progress(int,int,QString)), this, SLOT(updateProgress(int,int,QString)), Qt::QueuedConnection);
00041         connect(connectionManager, SIGNAL(finished()), this, SLOT(connectionManagerFinished()));
00042 
00043         //Enable or disable buttons depending on whether a network is loaded or not
00044         connect(Globals::getEventRouter(), SIGNAL(networkChangedSignal()), this, SLOT(networkChanged()));
00045 }
00046 
00047 
00049 ConnectionWidget::~ConnectionWidget(){
00050 }
00051 
00052 
00053 /*--------------------------------------------------------*/
00054 /*-------             PRIVATE SLOTS                -------*/
00055 /*--------------------------------------------------------*/
00056 
00058 void ConnectionWidget::addConnections(){
00059         //Run some checks - cannot change network if simulation or analysis is going on.
00060         if(!Globals::networkChangeOk())
00061                 return;
00062         PluginsDialog* pluginsDialog = new PluginsDialog(this, "/plugins/connections", "Add Connections");
00063         pluginsDialog->exec();
00064         delete pluginsDialog;
00065 }
00066 
00067 
00069 void ConnectionWidget::deleteSelectedConnections(){
00070         //Runs some checks - cannot change network if simulation or analysis is going on.
00071         if(!Globals::networkLoaded()){
00072                 qCritical()<<"Attempting to delete connections when network is not loaded";
00073                 return;
00074         }
00075         //Check if network is editable or not
00076         if(Globals::getNetwork()->hasArchives()){
00077                 qCritical()<<"You cannot alter a network that has archives.\nDelete the archives and try again.";
00078                 return;
00079         }
00080 
00081         if(!Globals::networkChangeOk())
00082                 return;
00083 
00084         //Do nothing if no connection groups are selected
00085         QList<unsigned> deleteConnectionIDList = connectionGroupModel->getSelectedConnectionGroupIDs();
00086         if(deleteConnectionIDList.isEmpty())
00087                 return;
00088 
00089         //Show warning
00090         QMessageBox msgBox(QMessageBox::Warning, "Deleting Connections", "Are you sure that you want to delete these connection groups?\nThis step cannot be undone.", QMessageBox::Ok | QMessageBox::Cancel, this);
00091         if(msgBox.exec() != QMessageBox::Ok)
00092                 return;
00093 
00094         //Delete connection groups
00095         try{
00096                 progressDialog = new QProgressDialog("Deleting connection group(s)", "Cancel", 0, 0, this, Qt::CustomizeWindowHint);
00097                 progressDialog->setWindowModality(Qt::WindowModal);
00098                 progressDialog->setCancelButton(0);//Too complicated to implement cancel sensibly
00099                 progressDialog->show();
00100                 updatingProgress = false;
00101                 connectionManager->deleteConnectionGroups(deleteConnectionIDList);
00102         }
00103         catch(SpikeStreamException& ex){
00104                 qCritical()<<ex.getMessage();
00105         }
00106 }
00107 
00108 
00110 void ConnectionWidget::updateProgress(int stepsCompleted, int totalSteps, QString message){
00111         //Set flag to avoid multiple calls to progress dialog while it is redrawing
00112         if(updatingProgress)
00113                 return;
00114         updatingProgress = true;
00115 
00116         //Check for cancellation
00117         if(progressDialog->wasCanceled()){
00118                 qCritical()<<"SpikeStream does not currently support cancellation of deleting connections.";
00119         }
00120 
00121         //Update progress
00122         else if(stepsCompleted < totalSteps){
00123                 progressDialog->setValue(stepsCompleted);
00124                 progressDialog->setMaximum(totalSteps);
00125                 progressDialog->setLabelText(message);
00126         }
00127 
00128         //Progress has finished
00129         else{
00130                 progressDialog->close();
00131         }
00132 
00133         //Clear flag to indicate that update of progress is complete
00134         updatingProgress = false;
00135 }
00136 
00137 
00139 void ConnectionWidget::networkChanged(){
00140         if(Globals::networkLoaded() && (Globals::getNetwork()->getNeuronGroupCount() > 0) ){
00141                 addConnectionsButton->setEnabled(true);
00142                 deleteButton->setEnabled(true);
00143         }
00144         else{
00145                 addConnectionsButton->setEnabled(false);
00146                 deleteButton->setEnabled(false);
00147         }
00148 }
00149 
00150 
00153 void ConnectionWidget::connectionManagerFinished(){
00154         //Check for errors
00155         if(connectionManager->isError())
00156                 qCritical()<<connectionManager->getErrorMessage();
00157 
00158         //Clean up progress dialog
00159         if(progressDialog != NULL){
00160                 progressDialog->close();
00161                 delete progressDialog;
00162                 progressDialog = NULL;
00163         }
00164 
00165         //Clear selection on connection group model
00166         connectionGroupModel->clearSelection();
00167 
00168         //Inform other classes that network has changed
00169         Globals::getEventRouter()->networkChangedSlot();
00170 }
00171 
 All Classes Files Functions Variables Typedefs Friends Defines