diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/DataComparison.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/DataComparison.h index 0eec52c2ab47f3c144fa21a46e56cf07491b38b2..4f139d634949823379caac96fe9ba412b8fbe8e8 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/DataComparison.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/DataComparison.h @@ -8,54 +8,59 @@ #include "MantidQtAPI/UserSubWindow.h" #include "MantidAPI/MatrixWorkspace.h" +#include <QPointer> + #include <qwt_plot.h> #include <qwt_plot_curve.h> + namespace MantidQt { namespace CustomInterfaces { -class DataComparison : public MantidQt::API::UserSubWindow -{ - Q_OBJECT - -public: - /// The name of the interface as registered into the factory - static std::string name() { return "Data Comparison"; } - // This interface's categories. - static QString categoryInfo() { return "General"; } - -public: - /// Default Constructor - DataComparison(QWidget *parent = 0); - -private slots: - /// Add selected data to plot - void addData(); - /// Remove selected data from plot - void removeSelectedData(); - /// Remove all data from plot - void removeAllData(); - /// Create a diff of the two selected workspaces - void diffSelected(); - /// Remove the diff from the plot - void clearDiff(); - -private: - /// Initialize the layout - virtual void initLayout(); - -private: - // The form generated by Qt Designer - Ui::DataComparison m_uiForm; - // The plot object - QwtPlot *m_plot; - // Vector of curves shown on plot - std::vector<QwtPlotCurve *> m_curves; - // Pointer to the current diff workspace - Mantid::API::MatrixWorkspace_sptr m_diffWorkspace; - -}; + class DataComparison : public MantidQt::API::UserSubWindow + { + Q_OBJECT + + public: + /// The name of the interface as registered into the factory + static std::string name() { return "Data Comparison"; } + // This interface's categories. + static QString categoryInfo() { return "General"; } + + public: + /// Default Constructor + DataComparison(QWidget *parent = 0); + + private slots: + /// Add selected data to plot + void addData(); + /// Remove selected data from plot + void removeSelectedData(); + /// Remove all data from plot + void removeAllData(); + /// Create a diff of the two selected workspaces + void diffSelected(); + /// Remove the diff from the plot + void clearDiff(); + + private: + /// Initialize the layout + virtual void initLayout(); + + void plotWorkspaces(); + + private: + // The form generated by Qt Designer + Ui::DataComparison m_uiForm; + // The plot object + QwtPlot *m_plot; + // Curves shown on plot, indexed by workspace name + QMap<QString, boost::shared_ptr<QwtPlotCurve>> m_curves; + // Pointer to the current diff workspace + Mantid::API::MatrixWorkspace_sptr m_diffWorkspace; + + }; } } diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/DataComparison.ui b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/DataComparison.ui index 48f4424aba74d15fbd103e720152fe12ff0f83db..847ba07a60210b3965322fb12764f44bb1871188 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/DataComparison.ui +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/DataComparison.ui @@ -43,14 +43,6 @@ <property name="autoLoad" stdset="0"> <bool>true</bool> </property> - <property name="loadLabelText" stdset="0"> - <string/> - </property> - <property name="fileBrowserSuffixes" stdset="0"> - <stringlist> - <string>.nxs</string> - </stringlist> - </property> <property name="showLoad" stdset="0"> <bool>false</bool> </property> @@ -79,13 +71,16 @@ </property> <layout class="QGridLayout" name="gridLayout_2"> <item row="0" column="0" colspan="2"> - <widget class="QTableWidget" name="tableWidget"> + <widget class="QTableWidget" name="twCurrentData"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> + <property name="columnCount"> + <number>3</number> + </property> </widget> </item> <item row="1" column="0"> @@ -117,23 +112,30 @@ <string>Tools</string> </property> <layout class="QGridLayout" name="gridLayout"> - <item row="2" column="0"> + <item row="0" column="0"> + <widget class="QLabel" name="lbSpectrum"> + <property name="text"> + <string>Spectrum:</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QSpinBox" name="sbSpectrum"/> + </item> + <item row="1" column="0"> <widget class="QPushButton" name="pbDiffSelected"> <property name="text"> <string>Diff Selected</string> </property> </widget> </item> - <item row="2" column="1"> + <item row="1" column="1"> <widget class="QPushButton" name="pbClearDiff"> <property name="text"> <string>Clear Diff</string> </property> </widget> </item> - <item row="0" column="1"> - <widget class="QSpinBox" name="sbSpectrum"/> - </item> </layout> </widget> </item> diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/DataComparison.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/DataComparison.cpp index 7510aaeca87a356adee148d470264ac620df9558..977e430995f17ce5bd524c5a920533093000ffa9 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/DataComparison.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/DataComparison.cpp @@ -3,6 +3,14 @@ //---------------------- #include "MantidQtCustomInterfaces/DataComparison.h" +#include "MantidQtAPI/QwtWorkspaceSpectrumData.h" + + +namespace +{ + Mantid::Kernel::Logger g_log("DataComparison"); +} + //Add this class to the list of specialised dialogs in this namespace namespace MantidQt { @@ -13,6 +21,8 @@ namespace CustomInterfaces } using namespace MantidQt::CustomInterfaces; +using namespace Mantid::API; + //---------------------- // Public member functions @@ -24,6 +34,7 @@ DataComparison::DataComparison(QWidget *parent) : { } + /// Set up the dialog layout void DataComparison::initLayout() { @@ -41,29 +52,125 @@ void DataComparison::initLayout() connect(m_uiForm.pbDiffSelected, SIGNAL(clicked()), this, SLOT(diffSelected())); connect(m_uiForm.pbClearDiff, SIGNAL(clicked()), this, SLOT(clearDiff())); + + // Add headers to data table + QStringList headerLabels; + headerLabels << "Workspace" << "Offset" << "Colour"; + m_uiForm.twCurrentData->setHorizontalHeaderLabels(headerLabels); } + /** * Adds the data currently selected by the data selector to the plot. */ void DataComparison::addData() { + QString dataName = m_uiForm.dsData->getCurrentDataName(); + + // Append a new row to the data table + int currentRows = m_uiForm.twCurrentData->rowCount(); + m_uiForm.twCurrentData->insertRow(currentRows); + + // Insert the workspace name + QTableWidgetItem *wsNameItem = new QTableWidgetItem(tr(dataName)); + wsNameItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); + m_uiForm.twCurrentData->setItem(currentRows, 0, wsNameItem); + + // Insert the spectra offset + QTableWidgetItem *offsetItem = new QTableWidgetItem(tr("0")); + offsetItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable); + m_uiForm.twCurrentData->setItem(currentRows, 1, offsetItem); + + plotWorkspaces(); } + /** * Removes the data currently selected in the table from the plot. */ void DataComparison::removeSelectedData() { + QList<QTableWidgetItem *> items = m_uiForm.twCurrentData->selectedItems(); + for(auto it = items.begin(); it != items.end(); ++it) + { + // Get workspace name + int row = m_uiForm.twCurrentData->row(*it); + QString workspaceName = m_uiForm.twCurrentData->item(row, 0)->text(); + + // Remove from data tabel + m_uiForm.twCurrentData->removeRow(row); + + // Detach the old curve from the plot if it exists + if(m_curves.contains(workspaceName)) + m_curves[workspaceName]->attach(NULL); + + plotWorkspaces(); + } } + /** * Removed all loaded data from the plot. */ void DataComparison::removeAllData() { + int numRows = m_uiForm.twCurrentData->rowCount(); + for(int row = 0; row < numRows; row++) + { + // Get workspace name + QString workspaceName = m_uiForm.twCurrentData->item(0, 0)->text(); + + // Remove from data tabel + m_uiForm.twCurrentData->removeRow(0); + + // Detach the old curve from the plot if it exists + if(m_curves.contains(workspaceName)) + m_curves[workspaceName]->attach(NULL); + + plotWorkspaces(); + } +} + + +void DataComparison::plotWorkspaces() +{ + int numRows = m_uiForm.twCurrentData->rowCount(); + for(int row = 0; row < numRows; row++) + { + int specIndex = 0; //TODO + + // Get workspace + QString workspaceName = m_uiForm.twCurrentData->item(row, 0)->text(); + MatrixWorkspace_const_sptr workspace = + AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(workspaceName.toStdString()); + + // Create the curve data + const bool logScale(false), distribution(false); + QwtWorkspaceSpectrumData wsData(*workspace, static_cast<int>(specIndex), logScale, distribution); + + // Check the spectrum index is in range + int numSpec = static_cast<int>(workspace->getNumberHistograms()); + if(specIndex >= numSpec) + { + g_log.debug() << "Workspace " << workspaceName.toStdString() << ", spectrum index out of range."; + continue; + } + + // Detach the old curve from the plot if it exists + if(m_curves.contains(workspaceName)) + m_curves[workspaceName]->attach(NULL); + + // Create a new curve and attach it to the plot + boost::shared_ptr<QwtPlotCurve> curve(new QwtPlotCurve); + curve->setData(wsData); + curve->attach(m_plot); + m_curves[workspaceName] = curve; + } + + m_plot->replot(); } + /** * Creates a diff workspace of the two currently selected workspaces in the table * and plots it on the plot. @@ -72,8 +179,10 @@ void DataComparison::removeAllData() */ void DataComparison::diffSelected() { + //TODO } + /** * Removes the diff workspace form the plot. * @@ -81,4 +190,5 @@ void DataComparison::diffSelected() */ void DataComparison::clearDiff() { + //TODO }