Skip to content

Commit

Permalink
Refs #4039, #3882: Load a color map file in SliceViewer. Double-click…
Browse files Browse the repository at this point in the history
… the color bar, or use menu.
  • Loading branch information
Janik Zikovsky committed Nov 2, 2011
1 parent 273766b commit 5feb8e4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ui_ColorBarWidget.h"
#include <qwt_color_map.h>
#include <qwt_scale_widget.h>
#include "MantidQtAPI/MantidColorMap.h"

class ColorBarWidget : public QWidget
{
Expand All @@ -14,7 +15,8 @@ class ColorBarWidget : public QWidget
ColorBarWidget(QWidget *parent = 0);
~ColorBarWidget();

void setColorMap(QwtColorMap * colorMap);
void update();

void setDataRange(double min, double max);
void setDataRange(QwtDoubleInterval range);
void setViewRange(double min, double max);
Expand All @@ -25,18 +27,22 @@ class ColorBarWidget : public QWidget
double getMaximum() const;
bool getLog() const;
QwtDoubleInterval getViewRange() const;
MantidColorMap & getColorMap();

public slots:
void changedLogState(int);
void changedMinimum();
void changedMaximum();

signals:
/// Signal sent when the range or log mode of the color scale changes.
void changedColorRange(double min, double max, bool log);
/// When the user double-clicks the color bar (e.g. load a new color map)
void colorBarDoubleClicked();

private:
void setSpinBoxesSteps();
void update();
void mouseDoubleClickEvent(QMouseEvent * event);

/// Auto-gen UI classes
Ui::ColorBarWidgetClass ui;
Expand All @@ -45,7 +51,7 @@ public slots:
QwtScaleWidget * m_colorBar;

/// Color map being displayed
QwtColorMap * m_colorMap;
MantidColorMap m_colorMap;

/// Logarithmic scale?
bool m_log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class QWT_EXPORT QwtRasterDataMD : public QwtRasterData
QwtDoubleInterval range() const;
void setRange(const QwtDoubleInterval & range);

void setLogMode(bool log);
void setSliceParams(size_t dimX, size_t dimY, std::vector<Mantid::coord_t> & slicePoint);

double value(double x, double y) const;
Expand All @@ -39,9 +38,6 @@ class QWT_EXPORT QwtRasterDataMD : public QwtRasterData
/// Workspace being shown
Mantid::API::IMDWorkspace_sptr m_ws;

/// Is the log10 of the data being displayed?
bool m_logMode;

/// Number of dimensions
size_t m_nd;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ class EXPORT_OPT_MANTIDQT_SLICEVIEWER SliceViewer : public QWidget

public slots:
void changedShownDim(int index, int dim, int oldDim);
void updateDisplaySlot(int index, double value);
void resetZoom();
void showInfoAt(double, double);
void colorRangeFullSlot();
void colorRangeSliceSlot();
void colorRangeChanged();
void zoomInSlot();
void zoomOutSlot();
void updateDisplaySlot(int index, double value);
void loadColorMap();


private:
Expand Down Expand Up @@ -74,9 +75,6 @@ public slots:
/// Layout containing the spectrogram
QHBoxLayout * m_spectLayout;

/// Color map in use
MantidColorMap * m_colorMap;

/// File of the last loaded color map.
QString m_currentColorMapFile;

Expand Down
32 changes: 20 additions & 12 deletions Code/Mantid/MantidQt/SliceViewer/src/ColorBarWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <iostream>
#include <qwt_scale_map.h>
#include <qwt_scale_widget.h>
#include <QKeyEvent>

//-------------------------------------------------------------------------------------------------
/** Constructor */
Expand All @@ -15,10 +16,10 @@ ColorBarWidget::ColorBarWidget(QWidget *parent)
ui.setupUi(this);

// Default values.
m_colorMap = new MantidColorMap();
m_min = 0;
m_max = 1000;
m_log = false;
m_colorMap.changeScaleType( GraphOptions::Linear );
this->setDataRange(0, 1000);

// Create and add the color bar
Expand Down Expand Up @@ -54,17 +55,18 @@ bool ColorBarWidget::getLog() const
QwtDoubleInterval ColorBarWidget::getViewRange() const
{ return QwtDoubleInterval(m_min, m_max); }

/// @return the color map in use (ref)
MantidColorMap & ColorBarWidget::getColorMap()
{ return m_colorMap;
}


//-------------------------------------------------------------------------------------------------
/** Change the color map shown
*
* @param colorMap
*/
void ColorBarWidget::setColorMap(QwtColorMap * colorMap)
/** Send a double-clicked event but only when clicking the color bar */
void ColorBarWidget::mouseDoubleClickEvent(QMouseEvent * event)
{
if (m_colorMap) delete m_colorMap;
m_colorMap = colorMap;
update();
if (m_colorBar->rect().contains(event->x(), event->y()))
emit colorBarDoubleClicked();
}

//-------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -156,6 +158,7 @@ void ColorBarWidget::changedLogState(int log)
void ColorBarWidget::setLog(bool log)
{
m_log = log;
m_colorMap.changeScaleType( m_log ? GraphOptions::Log10 : GraphOptions::Linear );
ui.checkLog->setChecked( m_log );
ui.valMin->setLogSteps( m_log );
ui.valMax->setLogSteps( m_log );
Expand Down Expand Up @@ -195,12 +198,17 @@ void ColorBarWidget::changedMaximum()
void ColorBarWidget::update()
{
m_colorBar->setColorBarEnabled(true);
m_colorBar->setColorMap( QwtDoubleInterval(m_min, m_max), *m_colorMap);
QwtDoubleInterval range(m_min, m_max);

m_colorBar->setColorMap( range, m_colorMap);
m_colorBar->setColorBarWidth(15);

QwtScaleDiv scaleDiv;
scaleDiv.setInterval(m_min, m_max);
m_colorBar->setScaleDiv(new QwtScaleTransformation(QwtScaleTransformation::Linear), scaleDiv);
scaleDiv.setInterval(range);
if (m_log)
m_colorBar->setScaleDiv(new QwtScaleTransformation(QwtScaleTransformation::Log10), scaleDiv);
else
m_colorBar->setScaleDiv(new QwtScaleTransformation(QwtScaleTransformation::Linear), scaleDiv);

ui.valMin->setValue( m_min );
ui.valMax->setValue( m_max );
Expand Down
34 changes: 4 additions & 30 deletions Code/Mantid/MantidQt/SliceViewer/src/QwtRasterDataMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ QwtRasterDataMD::QwtRasterDataMD()
m_minVal = DBL_MAX;
m_maxVal = -DBL_MAX;
m_range = QwtDoubleInterval(0.0, 1.0);
m_logMode = true;
nan = std::numeric_limits<double>::quiet_NaN();
}

Expand All @@ -34,7 +33,6 @@ QwtRasterData* QwtRasterDataMD::copy() const
{
QwtRasterDataMD* out = new QwtRasterDataMD();
out->m_ws = this->m_ws;
out->m_logMode = this->m_logMode;
out->m_dimX = this->m_dimX;
out->m_dimY = this->m_dimY;
out->m_nd = this->m_nd;
Expand All @@ -53,13 +51,6 @@ QwtRasterData* QwtRasterDataMD::copy() const
void QwtRasterDataMD::setRange(const QwtDoubleInterval & range)
{ m_range = range; }

//-------------------------------------------------------------------------
/** Sets whether to show the log10 of the data
* @param log :: true to use log color scaling.
*/
void QwtRasterDataMD::setLogMode(bool log)
{ m_logMode = log;
}


//-------------------------------------------------------------------------
Expand Down Expand Up @@ -90,36 +81,19 @@ double QwtRasterDataMD::value(double x, double y) const
if (value > m_maxVal) m_maxVal = value;
delete [] lookPoint;

if (m_logMode)
{
if (value <= 0.)
return nan;
else
return log10(value);
}
if (value == 0.)
return nan;
else
{
return value;
}
}


//------------------------------------------------------------------------------------------------------
/** Return the data range to show */
QwtDoubleInterval QwtRasterDataMD::range() const
{
if (m_logMode)
{
double min = log10(m_range.minValue());
double max = log10(m_range.maxValue());
if (m_range.minValue() <= 0)
min = max-6;
return QwtDoubleInterval(min,max);
}
else
// Linear color plot
return m_range;

// Linear color plot
return m_range;
}


Expand Down
53 changes: 42 additions & 11 deletions Code/Mantid/MantidQt/SliceViewer/src/SliceViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <qwt_scale_map.h>
#include <sstream>
#include <vector>
#include <qfiledialog.h>

using namespace Mantid;
using namespace Mantid::Kernel;
Expand All @@ -52,24 +53,21 @@ SliceViewer::SliceViewer(QWidget *parent)
m_spect = new QwtPlotSpectrogram();
m_spect->attach(m_plot);

// ---- Default Color Map ------
m_colorMap = new MantidColorMap();
QwtDoubleInterval range(0.0, 10.0);

m_data = new QwtRasterDataMD();
m_spect->setColorMap(*m_colorMap);
m_plot->autoRefresh();


// --- Create a color bar on the right axis ---------------
m_colorBar = new ColorBarWidget(this);
m_colorBar->setColorMap(m_colorMap);
m_colorBar->setDataRange( range.minValue(), range.maxValue() );
m_colorBar->setViewRange( range.minValue(), range.maxValue() );
m_colorBar->setLog(true);
m_spectLayout->addWidget(m_colorBar, 0, 0);
QObject::connect(m_colorBar, SIGNAL(changedColorRange(double,double,bool)), this, SLOT(colorRangeChanged()));

// ---- Set the color map on the data ------
m_data = new QwtRasterDataMD();
m_spect->setColorMap( m_colorBar->getColorMap() );
m_plot->autoRefresh();

// m_colorBar = m_plot->axisWidget(QwtPlot::yRight);
// m_colorBar->setColorBarEnabled(true);
// m_colorBar->setColorMap(range, m_colorMap);
Expand All @@ -80,16 +78,21 @@ SliceViewer::SliceViewer(QWidget *parent)
ui.splitter->setStretchFactor(0, 0);
ui.splitter->setStretchFactor(1, 1);
initZoomer();
ui.btnZoom->hide();

// ----------- Toolbar button signals ----------------
QObject::connect(ui.btnResetZoom, SIGNAL(clicked()), this, SLOT(resetZoom()));
QObject::connect(ui.btnRangeFull, SIGNAL(clicked()), this, SLOT(colorRangeFullSlot()));
QObject::connect(ui.btnRangeSlice, SIGNAL(clicked()), this, SLOT(colorRangeSliceSlot()));
ui.btnZoom->hide();

// ----------- Other signals ----------------
QObject::connect(m_colorBar, SIGNAL(colorBarDoubleClicked()), this, SLOT(loadColorMap()));

initMenus();

loadSettings();

updateDisplay();
}

//------------------------------------------------------------------------------------
Expand All @@ -113,7 +116,10 @@ void SliceViewer::loadSettings()
m_currentColorMapFile = settings.value("ColormapFile", "").toString();
// Set values from settings
if (!m_currentColorMapFile.isEmpty())
m_colorMap->loadMap(m_currentColorMapFile);
{
m_colorBar->getColorMap().loadMap(m_currentColorMapFile);
m_spect->setColorMap( m_colorBar->getColorMap() );
}
m_colorBar->setLog(scaleType);
settings.endGroup();
}
Expand All @@ -138,6 +144,11 @@ void SliceViewer::initMenus()

// --------------- Color options Menu ----------------------------------------
m_menuColorOptions = new QMenu("&ColorMap", this);

action = new QAction(QPixmap(), "&Load Colormap", this);
connect(action, SIGNAL(triggered()), this, SLOT(loadColorMap()));
m_menuColorOptions->addAction(action);

action = new QAction(QPixmap(), "&Full range", this);
connect(action, SIGNAL(triggered()), this, SLOT(colorRangeFullSlot()));
m_menuColorOptions->addAction(action);
Expand Down Expand Up @@ -343,6 +354,7 @@ void SliceViewer::colorRangeSliceSlot()
/// Slot called when the ColorBarWidget changes the range of colors
void SliceViewer::colorRangeChanged()
{
m_spect->setColorMap( m_colorBar->getColorMap() );
this->updateDisplay();
}

Expand Down Expand Up @@ -379,6 +391,26 @@ void SliceViewer::updateDisplaySlot(int index, double value)
this->updateDisplay();
}


//------------------------------------------------------------------------------------
/** SLOT to open a dialog to choose a file, load a color map from that file */
void SliceViewer::loadColorMap()
{
QString fileselection;
fileselection = QFileDialog::getOpenFileName(this, tr("Pick a Colormap"),
QFileInfo(m_currentColorMapFile).absoluteFilePath(),
tr("Colormaps (*.map *.MAP)"));
// User cancelled if filename is still empty
if( fileselection.isEmpty() ) return;
m_currentColorMapFile = fileselection;

// Load from file
m_colorBar->getColorMap().loadMap( fileselection );
m_colorBar->update();
m_spect->setColorMap( m_colorBar->getColorMap() );
this->updateDisplay();
}

//=================================================================================================
//=================================================================================================
//=================================================================================================
Expand Down Expand Up @@ -555,7 +587,6 @@ void SliceViewer::updateDisplay(bool resetAxes)

// Set the color range
m_data->setRange(m_colorBar->getViewRange());
m_data->setLogMode( m_colorBar->getLog() );

// m_colorBar->setColorMap(m_colorRange, m_colorMap);
// m_plot->setAxisScale(QwtPlot::yRight, m_colorRange.minValue(), m_colorRange.maxValue() );
Expand Down

0 comments on commit 5feb8e4

Please sign in to comment.