SpikeStream Application Library  0.2
AbstractExportAnalysisDialog.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "AbstractExportAnalysisDialog.h"
00003 #include "Globals.h"
00004 #include "SpikeStreamException.h"
00005 using namespace spikestream;
00006 
00007 //Qt includes
00008 #include <QDebug>
00009 #include <QFile>
00010 #include <QFileDialog>
00011 #include <QLayout>
00012 #include <QLabel>
00013 #include <QMessageBox>
00014 #include <QPushButton>
00015 #include <QTextStream>
00016 
00017 
00019 AbstractExportAnalysisDialog::AbstractExportAnalysisDialog(const QString& analysisName, QWidget* parent) : QDialog(parent){
00020         //Store name
00021         this->analysisName = analysisName;
00022 
00023         //Create layouts
00024         QVBoxLayout* mainVerticalBox = new QVBoxLayout(this);
00025         QGridLayout* gridLayout = new QGridLayout();
00026         gridLayout->setMargin(10);
00027 
00028         //Add the file selection components
00029         gridLayout->addWidget(new QLabel("File: "), 0, 0);
00030         fileLineEdit = new QLineEdit();
00031         fileLineEdit->setMinimumSize(250, 30);
00032         gridLayout->addWidget(fileLineEdit, 0, 1);
00033         QPushButton* fileButt = new QPushButton("Browse");
00034         connect (fileButt, SIGNAL(clicked()), this, SLOT(getFile()));
00035         gridLayout->addWidget(fileButt, 0, 2);
00036 
00037         //Add the export type selection combo
00038         gridLayout->addWidget(new QLabel("Export type: "), 1, 0);
00039         exportTypeCombo = new QComboBox();
00040         exportTypeCombo->addItem("Tab separated fields");
00041         gridLayout->addWidget(exportTypeCombo, 1, 1);
00042 
00043         //Add the grid layout to the dialog
00044         mainVerticalBox->addLayout(gridLayout);
00045 
00046         //Add Ok and Cancel buttons
00047         QHBoxLayout *okCanButtonBox = new QHBoxLayout();
00048         QPushButton *okPushButton = new QPushButton("Ok");
00049         connect(okPushButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
00050         QPushButton *cancelPushButton = new QPushButton("Cancel");
00051         connect(cancelPushButton, SIGNAL(clicked()), this, SLOT(reject()));
00052         okCanButtonBox->addWidget(okPushButton);
00053         okCanButtonBox->addWidget(cancelPushButton);
00054         mainVerticalBox->addLayout(okCanButtonBox);
00055 }
00056 
00057 
00059 AbstractExportAnalysisDialog::~AbstractExportAnalysisDialog(){
00060 }
00061 
00062 
00063 /*----------------------------------------------------------*/
00064 /*-----                  PRIVATE SLOTS                 -----*/
00065 /*----------------------------------------------------------*/
00066 
00068 void AbstractExportAnalysisDialog::okButtonClicked(){
00069         //Check file has been entered
00070         if(fileLineEdit->text().isEmpty()){
00071                 qCritical()<<"No file selected for saving.";
00072                 return;
00073         }
00074 
00075         if(!Globals::isAnalysisLoaded(analysisName)){
00076                 qCritical()<<"No analysis loaded; nothing to export.";
00077                 return;
00078         }
00079 
00080         //If file exists, check to see if user wants to overwrite file
00081         if(QFile::exists(fileLineEdit->text())){
00082                 int response = QMessageBox::warning(this, "Overwrite File?", fileLineEdit->text() + " already exists.\nAre you sure that you want to overwrite it?", QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel);
00083                 if(response != QMessageBox::Ok)
00084                         return;
00085         }
00086 
00087         try{
00088                 switch(exportTypeCombo->currentIndex()){
00089                         case 0: exportCommaSeparated(fileLineEdit->text());
00090                         break;
00091                         default: throw SpikeStreamException("Export type not recognized.");
00092                 }
00093         }
00094         catch(SpikeStreamException& ex){
00095                 qCritical()<<ex.getMessage();
00096         }
00097 
00098         //If we have reached this point, info should be ok
00099         accept();
00100 }
00101 
00102 
00104 void AbstractExportAnalysisDialog::getFile(){
00105         QString filePath = getFilePath("*.dat");
00106 
00107         //Fix extension
00108         if(filePath.indexOf(".") == -1)
00109                 fileLineEdit->setText(filePath + ".dat");
00110         else
00111                 fileLineEdit->setText(filePath);
00112 }
00113 
00114 
00115 /*----------------------------------------------------------*/
00116 /*-----                PRIVATE METHODS                 -----*/
00117 /*----------------------------------------------------------*/
00118 
00120 QString AbstractExportAnalysisDialog::getFilePath(QString fileFilter){
00121         QFileDialog dialog(this);
00122         dialog.setDirectory(Globals::getWorkingDirectory());
00123         dialog.setFileMode(QFileDialog::AnyFile);
00124         dialog.setNameFilter( QString("Data files (" + fileFilter + ")") );
00125         dialog.setViewMode(QFileDialog::Detail);
00126         QStringList fileNames;
00127         if (dialog.exec())
00128                 fileNames = dialog.selectedFiles();
00129         if(fileNames.size() > 0)
00130                 return fileNames[0];
00131         else
00132                 return QString("");
00133 }
00134 
 All Classes Files Functions Variables Typedefs Friends Defines