Skip to content

Commit

Permalink
Re #3982. Added option to view TableWorkspace transposed
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Nov 4, 2011
1 parent 7671abb commit d5ead78
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Code/Mantid/MantidPlot/src/Mantid/MantidDock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ void MantidDockWidget::createWorkspaceMenuActions()

m_delete = new QAction(tr("Delete"),this);
connect(m_delete,SIGNAL(triggered()),this,SLOT(deleteWorkspaces()));

m_showTransposed = new QAction(tr("Show Transposed"),this);
connect(m_showTransposed,SIGNAL(triggered()),m_mantidUI,SLOT(importTransposed()));

}

/**
Expand Down Expand Up @@ -906,6 +910,7 @@ void MantidDockWidget::addWorkspaceGroupMenuItems(QMenu *menu) const
void MantidDockWidget::addTableWorkspaceMenuItems(QMenu * menu) const
{
menu->addAction(m_showData);
menu->addAction(m_showTransposed);
menu->addAction(m_showHist);
}

Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/MantidPlot/src/Mantid/MantidDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private slots:
*m_colorFill, *m_showLogs, *m_showHist,
*m_saveNexus, *m_rename, *m_delete,
*m_program, * m_ascendingSortAction,
*m_descendingSortAction, *m_byNameChoice, *m_byLastModifiedChoice;
*m_descendingSortAction, *m_byNameChoice, *m_byLastModifiedChoice, *m_showTransposed;

static Mantid::Kernel::Logger& logObject;
};
Expand Down
97 changes: 94 additions & 3 deletions Code/Mantid/MantidPlot/src/Mantid/MantidTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ using namespace MantidQt::API;
* @return the MantidTable created
*/
MantidTable::MantidTable(ScriptingEnv *env, Mantid::API::ITableWorkspace_sptr ws, const QString &label,
ApplicationWindow* parent, const QString& /*name*/, Qt::WFlags f):
Table(env,ws->rowCount(),ws->columnCount(),label,parent,"",f),
ApplicationWindow* parent, bool transpose):
Table(env,
transpose? ws->columnCount() : ws->rowCount(),
transpose? ws->rowCount() + 1 : ws->columnCount(),
label,parent,"",0),
m_ws(ws),
m_wsName(ws->getName())
m_wsName(ws->getName()),
m_transposed(transpose)
{
// Set name and stuff
parent->initTable(this, parent->generateUniqueName("Table-"));
Expand All @@ -43,6 +47,12 @@ m_wsName(ws->getName())
/** Refresh the table by filling it */
void MantidTable::fillTable()
{
if (m_transposed)
{
fillTableTransposed();
return;
}

setNumCols(m_ws->columnCount());
setNumRows(m_ws->rowCount());

Expand Down Expand Up @@ -98,6 +108,76 @@ void MantidTable::fillTable()

}

/**
* Make the trasposed table.
*/
void MantidTable::fillTableTransposed()
{

int ncols = m_ws->rowCount() + 1;
int nrows = m_ws->columnCount();

setNumCols(ncols);
setNumRows(nrows);

// Track the column width. All text should fit in.
std::vector<int> maxWidth(numCols(), 6);
QFontMetrics fm( this->getTextFont() );
// Add all columns
for(int i = 0; i < m_ws->columnCount(); ++i)
{
Mantid::API::Column_sptr c = m_ws->getColumn(i);

QString colName = QString::fromStdString(c->name());

int row = i;
setText(row, 0, colName);

int thisWidth = fm.width(colName) + 20;
if (thisWidth > maxWidth[0]) maxWidth[0] = thisWidth;

// Print out the data in each row of this column
for(int j = 0; j < m_ws->rowCount(); ++j)
{
std::ostringstream ostr;
// This is the method on the Column object to convert to a string.
c->print(ostr,j);
QString qstr = QString::fromStdString(ostr.str());

int col = j + 1;
setText(row, col, qstr);

// Measure the width
thisWidth = fm.width(qstr) + 20;
if (thisWidth > maxWidth[col]) maxWidth[col] = thisWidth;
// Avoid crazy widths
if (maxWidth[col] > 300)
{
maxWidth[col] = 300;
}
}

}
// Make columns of ITableWorkspaces read only, and of the right width
for(int j = 0; j < numCols(); ++j)
{
setReadOnlyColumn(j);
setColumnWidth(j,maxWidth[j]);
// make columns unplottable
setColPlotDesignation(j,Table::None);
if (j == 0)
{
setColName(j,"Name");
}
else
{
// give names to columns as corresponding row numbers in the TableWorkspace
setColName(j,QString::number(j-1));
}
}

}

//------------------------------------------------------------------------------------------------
void MantidTable::closeTable()
{
Expand Down Expand Up @@ -133,6 +213,11 @@ void MantidTable::afterReplaceHandle(const std::string& wsName,const boost::shar
/** Called when a cell is edited */
void MantidTable::cellEdited(int row,int col)
{
if (m_transposed)
{
return;
}

std::string text = d_table->text(row,col).remove(QRegExp("\\s")).toStdString();
Mantid::API::Column_sptr c = m_ws->getColumn(col);

Expand All @@ -156,6 +241,12 @@ void MantidTable::cellEdited(int row,int col)
*/
void MantidTable::deleteRows(int startRow, int endRow)
{
if (m_transposed)
{
QMessageBox::warning(this,"MantidPlot - Warning","Cannot delete rows in a transposed table");
return;
}

Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("DeleteTableRows");
alg->setPropertyValue("TableWorkspace",m_ws->getName());
QStringList rows;
Expand Down
4 changes: 3 additions & 1 deletion Code/Mantid/MantidPlot/src/Mantid/MantidTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MantidTable: public Table, public MantidQt::API::WorkspaceObserver
Q_OBJECT
public:
MantidTable(ScriptingEnv *env, Mantid::API::ITableWorkspace_sptr ws, const QString &label,
ApplicationWindow* parent, const QString& name = QString(), Qt::WFlags f=0);
ApplicationWindow* parent, bool transpose = false);

/// returns the workspace name
const std::string & getWorkspaceName() {return m_wsName; }
Expand All @@ -28,12 +28,14 @@ public slots:
protected slots:
void closeTable();
void fillTable();
void fillTableTransposed();
protected:
void deleteHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws);
void afterReplaceHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws);
private:
Mantid::API::ITableWorkspace_sptr m_ws;
const std::string m_wsName;
bool m_transposed; ///< Show the table workspace transposed

};

Expand Down
22 changes: 20 additions & 2 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "MantidKernel/FacilityInfo.h"

#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/ITableWorkspace.h"

#include <QMessageBox>
#include <QTextEdit>
Expand Down Expand Up @@ -455,6 +456,22 @@ void MantidUI::importWorkspace()
importWorkspace(wsName,true,true);
}

/** Import the selected table workspace transposed.
*/
void MantidUI::importTransposed()
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
QString wsName = getSelectedWorkspaceName();
ITableWorkspace_sptr ws;
if (AnalysisDataService::Instance().doesExist(wsName.toStdString()))
{
ws = boost::dynamic_pointer_cast<ITableWorkspace>(AnalysisDataService::Instance().retrieve(wsName.toStdString()));
Table *t = importTableWorkspace(wsName,true,true,true);
appWindow()->addListViewItem(t);
}
QApplication::restoreOverrideCursor();
}


/** Create a TableWorkspace of box data from the MDEventWorkspace
*/
Expand Down Expand Up @@ -595,9 +612,10 @@ void MantidUI::showAlgorithmHistory()
@param wsName :: Workspace name
@param showDlg :: If true show a dialog box to set some import parameters
@param makeVisible :: If true show the created Table, hide otherwise.
@param transpose :: Transpose the table
@return A pointer to the new Table.
*/
Table* MantidUI::importTableWorkspace(const QString& wsName, bool, bool makeVisible)
Table* MantidUI::importTableWorkspace(const QString& wsName, bool, bool makeVisible, bool transpose)
{
ITableWorkspace_sptr ws;
if (AnalysisDataService::Instance().doesExist(wsName.toStdString()))
Expand All @@ -613,7 +631,7 @@ Table* MantidUI::importTableWorkspace(const QString& wsName, bool, bool makeVisi
return 0;
}

Table* t = new MantidTable(appWindow()->scriptingEnv(), ws, wsName, appWindow(), 0);
Table* t = new MantidTable(appWindow()->scriptingEnv(), ws, wsName, appWindow(), transpose);
if (makeVisible) t->showNormal();
else t->showMinimized();
return t;
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/MantidPlot/src/Mantid/MantidUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ public slots:
// Import the workspace selected in the Workspace dock window
void importWorkspace();
void importBoxDataTable();
void importTransposed();

// Invoke the Vates Simple User Interface
void showVatesSimpleInterface();
Expand All @@ -349,7 +350,7 @@ public slots:
bool showDlg = true, bool makeVisible = true);

// Create a MantidMatrix from workspace wsName
Table *importTableWorkspace(const QString& wsName, bool showDlg = true, bool makeVisible = true);
Table *importTableWorkspace(const QString& wsName, bool showDlg = true, bool makeVisible = true, bool transpose = false);

void createLoadDAEMantidMatrix(const QString&);

Expand Down

0 comments on commit d5ead78

Please sign in to comment.