Skip to content

Commit

Permalink
Refs #10372 Ask user before discarding changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Jeffery committed Oct 20, 2014
1 parent 6e0145f commit 935e8b5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
Expand Up @@ -89,6 +89,7 @@ namespace MantidQt
void actionDeleteRow();
void actionProcess();
void actionGroupRows();
void tableUpdated(const QModelIndex& topLeft, const QModelIndex& bottomRight);
};


Expand Down
Expand Up @@ -71,6 +71,7 @@ namespace MantidQt
static const int GroupRowsFlag = 6;
static const int OpenTableFlag = 7;
static const int NewTableFlag = 8;
static const int TableUpdatedFlag = 9;
};
}
}
Expand Down
Expand Up @@ -41,9 +41,14 @@ namespace MantidQt
virtual ~ReflMainViewPresenter();
virtual void notify(int flag);
protected:
//the model the table is currently representing
Mantid::API::ITableWorkspace_sptr m_model;
//the name of the workspace/table/model in the ADS, blank if unsaved
std::string m_wsName;
//the view we're managing
ReflMainView* m_view;
//stores whether or not the table has changed since it was last saved
bool m_tableDirty;

//process selected rows
virtual void process();
Expand Down
15 changes: 14 additions & 1 deletion Code/Mantid/MantidQt/CustomInterfaces/src/QtReflMainView.cpp
Expand Up @@ -76,7 +76,10 @@ namespace MantidQt
*/
void QtReflMainView::showTable(ITableWorkspace_sptr model)
{
ui.viewTable->setModel(new QReflTableModel(model));
QAbstractItemModel* qModel = new QReflTableModel(model);
//So we can notify the presenter when the user updates the table
connect(qModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(tableUpdated(const QModelIndex&, const QModelIndex&)));
ui.viewTable->setModel(qModel);
ui.viewTable->resizeColumnsToContents();
}

Expand Down Expand Up @@ -136,6 +139,16 @@ namespace MantidQt
m_presenter->notify(NewTableFlag);
}

/**
This slot notifies the presenter that the table has been updated/changed by the user
*/
void QtReflMainView::tableUpdated(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
Q_UNUSED(topLeft);
Q_UNUSED(bottomRight);
m_presenter->notify(TableUpdatedFlag);
}

/**
Show an information dialog
@param prompt : The prompt to appear on the dialog
Expand Down
Expand Up @@ -70,7 +70,7 @@ namespace MantidQt
{
namespace CustomInterfaces
{
ReflMainViewPresenter::ReflMainViewPresenter(ReflMainView* view): m_view(view)
ReflMainViewPresenter::ReflMainViewPresenter(ReflMainView* view): m_view(view), m_tableDirty(false)
{
//Set up the instrument selectors
std::vector<std::string> instruments;
Expand Down Expand Up @@ -263,6 +263,7 @@ namespace MantidQt

//Update the model
m_model->String(rowNo, COL_ANGLE) = Strings::toString<double>(Utils::roundToDP(thetaVal, 3));
m_tableDirty = true;
}

//If we need to calculate the resolution, do.
Expand All @@ -276,6 +277,7 @@ namespace MantidQt
//Update the model
double dqqVal = calcResAlg->getProperty("Resolution");
m_model->String(rowNo, COL_DQQ) = Strings::toString<double>(dqqVal);
m_tableDirty = true;
}

//Make sure the view updates
Expand Down Expand Up @@ -452,6 +454,7 @@ namespace MantidQt
if(m_model->String(rowNo, COL_QMAX).empty())
m_model->String(rowNo, COL_QMAX) = Strings::toString<double>(qrange[1]);

m_tableDirty = true;
m_view->showTable(m_model);
}
}
Expand Down Expand Up @@ -641,6 +644,7 @@ namespace MantidQt
insertRow(m_model->rowCount());
else
insertRow(*rows.rbegin() + 1);
m_tableDirty = true;
}

/**
Expand All @@ -654,6 +658,7 @@ namespace MantidQt
m_model->removeRow(rows.at(0));

m_view->showTable(m_model);
m_tableDirty = true;
}

/**
Expand All @@ -671,6 +676,7 @@ namespace MantidQt

//Make sure the view updates
m_view->showTable(m_model);
m_tableDirty = true;
}

/**
Expand All @@ -688,6 +694,7 @@ namespace MantidQt
case ReflMainView::GroupRowsFlag: groupRows(); break;
case ReflMainView::OpenTableFlag: openTable(); break;
case ReflMainView::NewTableFlag: newTable(); break;
case ReflMainView::TableUpdatedFlag: m_tableDirty = true; break;

case ReflMainView::NoFlags: return;
}
Expand All @@ -700,9 +707,14 @@ namespace MantidQt
void ReflMainViewPresenter::saveTable()
{
if(!m_wsName.empty())
{
AnalysisDataService::Instance().addOrReplace(m_wsName,boost::shared_ptr<ITableWorkspace>(m_model->clone()));
m_tableDirty = false;
}
else
{
saveTableAs();
}
}

/**
Expand All @@ -723,19 +735,29 @@ namespace MantidQt
*/
void ReflMainViewPresenter::newTable()
{
if(m_tableDirty)
if(!m_view->askUserYesNo("Your current table has unsaved changes. Are you sure you want to discard them?","Start New Table?"))
return;

m_model = createWorkspace();
m_wsName.clear();
m_view->showTable(m_model);

//Start with one blank row
insertRow(0);

m_tableDirty = false;
}

/**
Open a table from the ADS
*/
void ReflMainViewPresenter::openTable()
{
if(m_tableDirty)
if(!m_view->askUserYesNo("Your current table has unsaved changes. Are you sure you want to discard them?","Open Table?"))
return;

auto& ads = AnalysisDataService::Instance();
const std::string toOpen = m_view->getWorkspaceToOpen();

Expand All @@ -762,6 +784,7 @@ namespace MantidQt
m_model = newModel;
m_wsName = toOpen;
m_view->showTable(m_model);
m_tableDirty = false;
}
}
}

0 comments on commit 935e8b5

Please sign in to comment.