SpikeStream Nemo Plugin  0.2
PatternManager.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "PatternManager.h"
00003 #include "SpikeStreamIOException.h"
00004 #include "Util.h"
00005 using namespace spikestream;
00006 
00007 //Qt includes
00008 #include <QDebug>
00009 #include <QFile>
00010 #include <QTextStream>
00011 
00012 
00014 PatternManager::PatternManager(){
00015 }
00016 
00017 
00019 PatternManager::~PatternManager(){
00020 }
00021 
00022 
00023 /*----------------------------------------------------------*/
00024 /*-----                 PUBLIC METHODS                 -----*/
00025 /*----------------------------------------------------------*/
00026 
00030 void PatternManager::load(const QString &filePath, Pattern& pattern){
00031         //Reset pattern
00032         pattern.reset();
00033 
00034         //Open Pattern file
00035         QFile configFile(filePath);
00036         if(!configFile.exists())
00037                 throw SpikeStreamIOException("Cannot find config file.");
00038         if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text))
00039                 throw SpikeStreamIOException("Cannot open file for reading: " + configFile.fileName());
00040 
00041         //Load contents of file into pattern
00042         QTextStream in(&configFile);
00043         QString line;
00044         while (!in.atEnd()) {
00045                 line = in.readLine();
00046 
00047                 //Skip empty lines and comment lines
00048                 if(line.isEmpty() || line.at(0) == '#'){
00049                         ;
00050                 }
00051 
00052                 //Set the name of the pattern
00053                 else if(line.startsWith("NAME", Qt::CaseInsensitive) ){
00054                         pattern.setName(line.section(':', 1, 1).trimmed());
00055                 }
00056 
00057                 //Add a box to the pattern
00058                 else if(line.contains("x", Qt::CaseInsensitive) ) {
00059                         pattern.addBox(getBox(line));
00060                 }
00061                 //Add a point to the pattern
00062                 else if (line.contains(",")){
00063                         pattern.addPoint(getPoint(line));
00064                 }
00065                 else{
00066                         throw SpikeStreamException("Pattern data not recognized: " + line);
00067                 }
00068         }
00069         configFile.close();
00070 }
00071 
00072 
00073 /*----------------------------------------------------------*/
00074 /*-----                 PRIVATE METHODS                -----*/
00075 /*----------------------------------------------------------*/
00076 
00078 Box PatternManager::getBox(const QString &str){
00079         //X coordinates
00080         QString xCoords = str.section('x', 0, 0, QString::SectionSkipEmpty).trimmed();
00081         float x1 = Util::getFloat(xCoords.section(':', 0, 0, QString::SectionSkipEmpty).trimmed());
00082         float x2 = Util::getFloat(xCoords.section(':', 1, 1, QString::SectionSkipEmpty).trimmed());
00083 
00084         //Y coordinates
00085         QString yCoords = str.section('x', 1, 1, QString::SectionSkipEmpty).trimmed();
00086         float y1 = Util::getFloat(yCoords.section(':', 0, 0, QString::SectionSkipEmpty).trimmed());
00087         float y2 = Util::getFloat(yCoords.section(':', 1, 1, QString::SectionSkipEmpty).trimmed());
00088 
00089         //Z coordinates
00090         QString zCoords = str.section('x', 2, 2, QString::SectionSkipEmpty).trimmed();
00091         float z1 = Util::getFloat(zCoords.section(':', 0, 0, QString::SectionSkipEmpty).trimmed());
00092         float z2 = Util::getFloat(zCoords.section(':', 1, 1, QString::SectionSkipEmpty).trimmed());
00093 
00094         return Box(x1, y1, z1, x2, y2, z2);
00095 }
00096 
00097 
00099 Point3D PatternManager::getPoint(const QString &str){
00100         float xPos = Util::getFloat(str.section(',', 0, 0, QString::SectionSkipEmpty).trimmed());
00101         float yPos = Util::getFloat(str.section(',', 1, 1, QString::SectionSkipEmpty).trimmed());
00102         float zPos = Util::getFloat(str.section(',', 2, 3, QString::SectionSkipEmpty).trimmed());
00103 
00104         return Point3D(xPos, yPos, zPos);
00105 }
 All Classes Files Functions Variables Typedefs Defines