Skip to content

Commit

Permalink
Move <Matrix> parsing into interfaced stub.
Browse files Browse the repository at this point in the history
This is the first half of the work required to move <Matrix> parsing
into an IProjectSerialisable interface. It does everything up to the
point of actually parsing the contents of <Matrix>. That ought to come
in the next commit.

Refs #9970
  • Loading branch information
Harry Jeffery committed Aug 6, 2014
1 parent 160e172 commit 8c4e2f7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 73 deletions.
114 changes: 43 additions & 71 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Expand Up @@ -4702,9 +4702,7 @@ void ApplicationWindow::openProjectFolder(Folder* curFolder, std::string lines,
std::vector<std::string> matrixSections = tsv.sections("matrix");
for(auto it = matrixSections.begin(); it != matrixSections.end(); ++it)
{
std::string matrixLines = *it;
QStringList sl = QString(matrixLines.c_str()).split("\n");
openMatrix(this, sl);
openMatrix(*it, d_file_version);
}
}

Expand Down Expand Up @@ -11105,84 +11103,58 @@ Note* ApplicationWindow::openNote(ApplicationWindow* app, const QStringList &fli
return w;
}

Matrix* ApplicationWindow::openMatrix(ApplicationWindow* app, const QStringList &flist)
void ApplicationWindow::openMatrix(const std::string& lines, const int fileVersion)
{
QStringList::const_iterator line = flist.begin();
//The first line specifies the name, dimensions and date.
std::vector<std::string> lineVec;
boost::split(lineVec, lines, boost::is_any_of("\n"));
std::string firstLine = lineVec.front();
lineVec.erase(lineVec.begin());
std::string newLines = boost::algorithm::join(lineVec, "\n");

QStringList list=(*line).split("\t");
QString caption=list[0];
int rows = list[1].toInt();
int cols = list[2].toInt();
//Parse the first line
std::vector<std::string> values;
boost::split(values, firstLine, boost::is_any_of("\t"));

Matrix* w = app->newMatrix(caption, rows, cols);
app->setListViewDate(caption,list[3]);
w->setBirthDate(list[3]);
if(values.size() < 4)
{
throw std::runtime_error("Error parsing <matrix>. Too few values on first line.");
return;
}

for (line++; line!=flist.end(); ++line)
const std::string caption = values[0];
const std::string date = values[3];

int rows, cols;
//Parse rows
{
QStringList fields = (*line).split("\t");
if (fields[0] == "geometry") {
restoreWindowGeometry(app, w, *line);
} else if (fields[0] == "ColWidth") {
w->setColumnsWidth(fields[1].toInt());
} else if (fields[0] == "Formula") {
w->setFormula(fields[1]);
} else if (fields[0] == "<formula>") {
QString formula;
for (line++; line!=flist.end() && *line != "</formula>"; ++line)
formula += *line + "\n";
formula.truncate(formula.length()-1);
w->setFormula(formula);
} else if (fields[0] == "TextFormat") {
if (fields[1] == "f")
w->setTextFormat('f', fields[2].toInt());
else
w->setTextFormat('e', fields[2].toInt());
} else if (fields[0] == "WindowLabel") { // d_file_version > 71
w->setWindowLabel(fields[1]);
w->setCaptionPolicy((MdiSubWindow::CaptionPolicy)fields[2].toInt());
} else if (fields[0] == "Coordinates") { // d_file_version > 81
w->setCoordinates(fields[1].toDouble(), fields[2].toDouble(), fields[3].toDouble(), fields[4].toDouble());
} else if (fields[0] == "ViewType") { // d_file_version > 90
w->setViewType((Matrix::ViewType)fields[1].toInt());
} else if (fields[0] == "HeaderViewType") { // d_file_version > 90
w->setHeaderViewType((Matrix::HeaderViewType)fields[1].toInt());
} else if (fields[0] == "ColorPolicy"){// d_file_version > 90
w->setColorMapType((Matrix::ColorMapType)fields[1].toInt());
} else if (fields[0] == "<ColorMap>"){// d_file_version > 90
QStringList lst;
while ( *line != "</ColorMap>" ){
++line;
lst << *line;
}
lst.pop_back();
w->setColorMap(lst);
} else // <data> or values
break;
std::stringstream ss(values[1]);
ss >> rows;
}
if (*line == "<data>") ++line;

//read and set table values
for (; line!=flist.end() && *line != "</data>"; ++line){
QStringList fields = (*line).split("\t");
int row = fields[0].toInt();
for (int col=0; col<cols; col++){
QString cell = fields[col+1];
if (cell.isEmpty())
continue;
//Parse cols
{
std::stringstream ss(values[2]);
ss >> cols;
}

if (d_file_version < 90)
w->setCell(row, col, QLocale::c().toDouble(cell));
else if (d_file_version == 90)
w->setText(row, col, cell);
else
w->setCell(row, col, cell.toDouble());
}
qApp->processEvents(QEventLoop::ExcludeUserInput);
const std::string data = values[3];

Matrix* m = newMatrix(QString(caption.c_str()), rows, cols);
setListViewDate(QString(caption.c_str()), QString(date.c_str()));
m->setBirthDate(QString(date.c_str()));

TSVSerialiser tsv(newLines);

if(tsv.hasLine("geometry"))
{
std::string gStr = tsv.lineAsString("geometry");
restoreWindowGeometry(this, m, QString(gStr.c_str()));
}
w->resetView();
return w;

m->loadFromProject(newLines, this, fileVersion);
}

void ApplicationWindow::openMantidMatrix(const QStringList &list)
{
QString s=list[0];
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/MantidPlot/src/ApplicationWindow.h
Expand Up @@ -1088,7 +1088,7 @@ public slots:
void openProjectFolder(Folder* curFolder, std::string lines, bool isTopLevel = false);
///void open spectrogram plot from project
Spectrogram* openSpectrogram(Graph*ag,const std::string &wsName,const QStringList &lst);
Matrix* openMatrix(ApplicationWindow* app, const QStringList &flist);
void openMatrix(const std::string& lines, const int fileVersion);
void openMantidMatrix(const QStringList &lst);
MantidMatrix* newMantidMatrix(const QString& wsName,int lower,int upper);
void openScriptWindow(const QStringList &list);
Expand Down
9 changes: 9 additions & 0 deletions Code/Mantid/MantidPlot/src/Matrix.cpp
Expand Up @@ -33,6 +33,7 @@
#include "muParserScript.h"
#include "ScriptingEnv.h"
#include "pixmaps.h"
#include "TSVSerialiser.h"

#include <QtGlobal>
#include <QTextStream>
Expand Down Expand Up @@ -1523,3 +1524,11 @@ Matrix::~Matrix()
delete d_undo_stack;
delete d_matrix_model;
}

void Matrix::loadFromProject(const std::string& lines, ApplicationWindow* app, const int fileVersion)
{
Q_UNUSED(app);
Q_UNUSED(fileVersion);

TSVSerialiser tsv(lines);
}
5 changes: 4 additions & 1 deletion Code/Mantid/MantidPlot/src/Matrix.h
Expand Up @@ -44,6 +44,8 @@
#include "ScriptingEnv.h"
#include "Scripted.h"

#include "Mantid/IProjectSerialisable.h"

#include <qwt_double_rect.h>
#include <qwt_color_map.h>

Expand All @@ -59,7 +61,7 @@ class QShortcut;
class QUndoStack;

//! Matrix worksheet class
class Matrix: public MdiSubWindow, public Scripted
class Matrix: public MdiSubWindow, public Scripted, public Mantid::IProjectSerialisable
{
Q_OBJECT

Expand Down Expand Up @@ -235,6 +237,7 @@ public slots:

//! Return a string to save the matrix in a project file (\<matrix\> section)
QString saveToString(const QString &info, bool saveAsTemplate = false);
virtual void loadFromProject(const std::string& lines, ApplicationWindow* app, const int fileVersion);

// selection operations
//! Standard cut operation
Expand Down

0 comments on commit 8c4e2f7

Please sign in to comment.