Skip to content

Commit

Permalink
Refs #3837. Enhance loading to work with GroupWorkspaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Whitley committed Nov 15, 2011
1 parent d68f41e commit 71457c3
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 56 deletions.
180 changes: 139 additions & 41 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8860,20 +8860,23 @@ void ApplicationWindow::newProject()
}

void ApplicationWindow::savedProject()
{ QCoreApplication::processEvents();
if(actionSaveFile) actionSaveFile->setEnabled(false);
if(actionSaveProject)actionSaveProject->setEnabled(false);
saved = true;

Folder *f = projectFolder();
while (f){
QList<MdiSubWindow *> folderWindows = f->windowsList();
foreach(MdiSubWindow *w, folderWindows){
if (w->isA("Matrix"))
((Matrix *)w)->undoStack()->setClean();
{
QCoreApplication::processEvents();
if(actionSaveFile) actionSaveFile->setEnabled(false);
if(actionSaveProject)actionSaveProject->setEnabled(false);
saved = true;

Folder *f = projectFolder();
while (f)
{
QList<MdiSubWindow *> folderWindows = f->windowsList();
foreach(MdiSubWindow *w, folderWindows)
{
if (w->isA("Matrix"))
((Matrix *)w)->undoStack()->setClean();
}
f = f->folderBelow();
}
f = f->folderBelow();
}
}

void ApplicationWindow::modifiedProject()
Expand Down Expand Up @@ -10549,16 +10552,19 @@ void ApplicationWindow::openMantidMatrix(const QStringList &list)
QString wsName=qlist[1];
MantidMatrix *m=newMantidMatrix(wsName,-1,-1);//mantidUI->importMatrixWorkspace(wsName,-1,-1,false,false);
//if(!m)throw std::runtime_error("Error on opening matrixworkspace ");
if(!m) return;
if(!m)
return;
//adding the mantid matrix windows opened to a list.
//this list is used for find the MantidMatrix window pointer to open a 3D/2DGraph
m_mantidmatrixWindows<<m;
QStringList::const_iterator line = list.begin();
for (line++; line!=list.end(); line++)
{ QStringList fields = (*line).split("\t");
if (fields[0] == "geometry" || fields[0] == "tgeometry") {
restoreWindowGeometry(this, m, *line);
}
{
QStringList fields = (*line).split("\t");
if (fields[0] == "geometry" || fields[0] == "tgeometry")
{
restoreWindowGeometry(this, m, *line);
}
}
}
void ApplicationWindow::openInstrumentWindow(const QStringList &list)
Expand All @@ -10567,42 +10573,134 @@ void ApplicationWindow::openInstrumentWindow(const QStringList &list)
QStringList qlist=s.split("\t");
QString wsName=qlist[1];
InstrumentWindow *insWin = mantidUI->getInstrumentView(wsName);
if(!insWin) return;
if(!insWin)
return;
insWin->show();
QStringList::const_iterator line = list.begin();
for (line++; line!=list.end(); line++)
{ QStringList fields = (*line).split("\t");
if (fields[0] == "geometry" || fields[0] == "tgeometry") {
restoreWindowGeometry(this, insWin, *line);
}
{
QStringList fields = (*line).split("\t");
if (fields[0] == "geometry" || fields[0] == "tgeometry")
{
restoreWindowGeometry(this, insWin, *line);
}
}
}

/** This method opens script window when project file is loaded
*/
void ApplicationWindow::openScriptWindow(const QStringList &list)
{ showScriptWindow();
if(!scriptingWindow) return;
scriptingWindow->setWindowTitle("MantidPlot: " + scriptingEnv()->scriptingLanguage() + " Window");
QString s=list[0];
QStringList scriptnames=s.split("\t");
int count=scriptnames.size();
if(count==0) return;
// don't create a new tab when the first script file from theproject file opened
if(!scriptnames[1].isEmpty()) scriptingWindow->open(scriptnames[1],false);
// create a new tab and open the script for all otehr filenames
for(int i=2;i<count;++i)
{ if(!scriptnames[i].isEmpty())scriptingWindow->open(scriptnames[i],true);
}
{
showScriptWindow();
if(!scriptingWindow)
return;
scriptingWindow->setWindowTitle("MantidPlot: " + scriptingEnv()->scriptingLanguage() + " Window");
QString s=list[0];
QStringList scriptnames=s.split("\t");
int count=scriptnames.size();
if(count==0)
return;
// don't create a new tab when the first script file from theproject file opened
if(!scriptnames[1].isEmpty())
scriptingWindow->open(scriptnames[1],false);
// create a new tab and open the script for all otehr filenames
for(int i=2;i<count;++i)
{
if(!scriptnames[i].isEmpty())
scriptingWindow->open(scriptnames[i],true);
}
}
/** This method populates the mantid workspace tree when project file is loaded
*/

/** This method populates the mantid workspace tree when project file is loaded and
* then groups all the workspaces that belonged to a group when the project was saved.
*
* @params &s :: A QString that contains all the names of workspaces and group workspaces
* that the user is trying to load from a project.
*/
void ApplicationWindow::populateMantidTreeWdiget(const QString &s)
{
QStringList list = s.split("\t");
QStringList::const_iterator line = list.begin();
for (++line; line!=list.end(); ++line)
{ std::string wsName=(*line).toStdString();
if(wsName.empty())throw std::runtime_error("Workspace Name not found in project file ");
{
if ((*line).contains(',')) // ...it is a group and more work needs to be done
{
// Format of string is "GroupName, Workspace, Workspace, Workspace, .... and so on "
QStringList groupWorkspaces = (*line).split(',');
std::string groupName = groupWorkspaces[0].toStdString();
std::vector<std::string> inputWsVec;
// Work through workspaces, load into Mantid and then push into vectorgroup (ignore group name, start at 1)
for (int i=1; i<groupWorkspaces.size(); i++)
{
std::string wsName = groupWorkspaces[i].toStdString();
loadWsToMantidTree(wsName);
inputWsVec.push_back(wsName);
}

try
{
bool smallGroup(inputWsVec.size() < 2);
if (smallGroup) // if the group contains less than two items...
{
// ...create a new workspace and then delete it later on (group workspace requires two workspaces in order to run the alg)
Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("CreateWorkspace",1);
alg->setProperty("OutputWorkspace", "boevsMoreBoevs");
alg->setProperty< std::vector<double> >("DataX", std::vector<double>(2,0.0) );
alg->setProperty< std::vector<double> >("DataY", std::vector<double>(2,0.0) );
// execute the algorithm
alg->execute();
// name picked because random and won't ever be used.
inputWsVec.push_back("boevsMoreBoevs");
}

// Group the workspaces as they were when the project was saved
std::string algName("GroupWorkspaces");
Mantid::API::IAlgorithm_sptr groupingAlg = Mantid::API::AlgorithmManager::Instance().create(algName,1);
groupingAlg->initialize();
groupingAlg->setProperty("InputWorkspaces",inputWsVec);
groupingAlg->setPropertyValue("OutputWorkspace",groupName);
//execute the algorithm
groupingAlg->execute();

if (smallGroup)
{
// Delete the temporary workspace used to create a group of 1 or less (currently can't have group of 0)
Mantid::API::AnalysisDataService::Instance().remove("boevsMoreBoevs");
}
}
// Error catching for algorithms
catch(std::invalid_argument &)
{
QMessageBox::critical(this,"MantidPlot - Algorithm error"," Error in Grouping Workspaces");
}
catch(Mantid::Kernel::Exception::NotFoundError&)
{
QMessageBox::critical(this,"MantidPlot - Algorithm error"," Error in Grouping Workspaces");
}
catch(std::runtime_error& )
{
QMessageBox::critical(this,"MantidPlot - Algorithm error"," Error in Grouping Workspaces");
}
catch(std::exception& )
{
QMessageBox::critical(this,"MantidPlot - Algorithm error"," Error in Grouping Workspaces");
}
}
else // ...not a group so just load the workspace
{
loadWsToMantidTree((*line).toStdString());
}
}
}

/** This method populates the mantid workspace tree when project file is loaded
*/
void ApplicationWindow::loadWsToMantidTree(const std::string & wsName)
{
if(wsName.empty())
{
throw std::runtime_error("Workspace Name not found in project file ");
}
std::string fileName(workingDir.toStdString()+"/"+wsName);
fileName.append(".nxs");
try
Expand All @@ -10612,8 +10710,8 @@ void ApplicationWindow::populateMantidTreeWdiget(const QString &s)
catch(...)
{
}
}
}

/** This method opens mantid matrix window when project file is loaded
*/
MantidMatrix* ApplicationWindow::newMantidMatrix(const QString& wsName,int lower,int upper)
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,7 @@ public slots:
MantidMatrix* newMantidMatrix(const QString& wsName,int lower,int upper);
void openScriptWindow(const QStringList &list);
void populateMantidTreeWdiget(const QString &s);
void loadWsToMantidTree(const std::string& wsName);
void openInstrumentWindow(const QStringList &list);
/// this method saves the data on project save
void savedatainNexusFormat(const std::string& wsName,const std::string & fileName);
Expand Down
59 changes: 44 additions & 15 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1750,9 +1750,11 @@ void MantidUI::showMantidInstrument(const QString& wsName)
void MantidUI::showMantidInstrument()
{
MantidMatrix* m = (MantidMatrix*)appWindow()->activeWindow();
if (!m || !m->isA("MantidMatrix")) return;
if (!m || !m->isA("MantidMatrix"))
return;
if(!m->workspaceName().isEmpty())
{showMantidInstrument(m->workspaceName());
{
showMantidInstrument(m->workspaceName());
}
}

Expand Down Expand Up @@ -1836,20 +1838,47 @@ QString MantidUI::saveToString(const std::string& workingDir)
QTreeWidget *tree=m_exploreMantid->m_tree;
int count=tree->topLevelItemCount();
for(int i=0;i<count;++i)
{ QTreeWidgetItem* item=tree->topLevelItem(i);
QString wsName=item->text(0);
wsNames+="\t";
wsNames+=wsName;
{
QTreeWidgetItem* item=tree->topLevelItem(i);
QString wsName=item->text(0);
if (Mantid::API::FrameworkManager::Instance().getWorkspace(wsName.toStdString())->id() == "WorkspaceGroup")
{
Mantid::API::WorkspaceGroup_sptr group = boost::dynamic_pointer_cast<Mantid::API::WorkspaceGroup>(Mantid::API::AnalysisDataService::Instance().retrieve(wsName.toStdString()));
wsNames+="\t";
//wsName is a group, add it to list and indicate what the group contains by a "[" and end the group with a "]"
wsNames+=wsName;
int secondLevelCount = group->getNumberOfEntries();
std::vector<std::string> secondLevelItems = group->getNames();
for(int j=0; j<secondLevelItems.size(); j++) //ignore string "WorkspaceGroup at position 0" (start at child '1')
{
wsNames+=",";
wsNames+=QString::fromStdString(secondLevelItems[j]);
std::string fileName(workingDir + "//" + secondLevelItems[j] + ".nxs");
//saving to nexus file
try
{
savedatainNexusFormat(fileName,secondLevelItems[j]);
}
catch(...)
{
}
}
}
else
{
wsNames+="\t";
wsNames+=wsName;

std::string fileName(workingDir+"//"+wsName.toStdString()+".nxs");
//saving to nexus file
try
{
savedatainNexusFormat(fileName,wsName.toStdString());
}
catch(...)
{
}
std::string fileName(workingDir + "//" + wsName.toStdString() + ".nxs");
//saving to nexus file
try
{
savedatainNexusFormat(fileName,wsName.toStdString());
}
catch(...)
{
}
}
}
wsNames+="\n</mantidworkspaces>\n";
return wsNames;
Expand Down

0 comments on commit 71457c3

Please sign in to comment.