Skip to content

Commit

Permalink
refs #6271. PeakPalette made into class.
Browse files Browse the repository at this point in the history
We need to be able to modify the palette for both the foreground and
background peaks. We will also need to pass the palettes around so that they
can be used by various widgets.
  • Loading branch information
OwenArnold committed Jan 7, 2013
1 parent e1c145b commit ea6abfe
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Code/Mantid/MantidQt/SliceViewer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set ( SRC_FILES
src/PeakOverlaySphereFactory.cpp
src/PeakOverlayCrossFactory.cpp
src/PeakOverlayViewFactoryBase.cpp
src/PeakPalette.cpp
src/PeakTransform.cpp
src/PeakTransformHKL.cpp
src/PeakTransformQSample.cpp
Expand Down Expand Up @@ -49,6 +50,7 @@ set ( INC_FILES
inc/MantidQtSliceViewer/PeakOverlayViewFactoryBase.h
inc/MantidQtSliceViewer/PeakOverlaySphereFactory.h
inc/MantidQtSliceViewer/PeakOverlayCrossFactory.h
inc/MantidQtSliceViewer/PeakPalette.h
inc/MantidQtSliceViewer/PeaksPresenter.h
inc/MantidQtSliceViewer/PeakTransformHKL.h
inc/MantidQtSliceViewer/PeakTransformQSample.h
Expand Down Expand Up @@ -94,13 +96,13 @@ set ( UI_FILES
inc/MantidQtSliceViewer/ColorBarWidget.ui
inc/MantidQtSliceViewer/SnapToGridDialog.ui
inc/MantidQtSliceViewer/PeaksWorkspaceWidget.ui
inc/MantidQtSliceViewer/PeaksViewer.ui
inc/MantidQtSliceViewer/XYLimitsDialog.ui
)

set ( TEST_FILES
test/CompositePeaksPresenterTest.h
test/ConcretePeaksPresenterTest.h
test/PeakPaletteTest.h
test/PeakTransformHKLTest.h
test/PeakTransformQSampleTest.h
test/PeakTransformQLabTest.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef MANTID_SLICEVIEWER_PEAKPALETTE_H
#define MANTID_SLICEVIEWER_PEAKPALETTE_H

#include "MantidKernel/System.h"
#include <map>

namespace MantidQt
{
namespace SliceViewer
{

enum Colour
{
White=3,
Black=2,
Red=7,
DarkRed=13,
Green=8,
DarkGreen=14,
Blue=9,
DarkBlue=15,
Cyan=10,
DarkCyan=16,
Magenta=11,
DarkMagenta=17,
Yellow=12,
DarkYellow=18,
Gray=5,
DarkGray=4,
LightGray=6,
Transparent=19,
Color0=0,
Color1=1
};

class DLLExport PeakPalette
{
private:

typedef std::map<int, Colour> ColourMapType;
ColourMapType m_backgroundMap;
ColourMapType m_foregroundMap;
ColourMapType::iterator safeFetchPair(ColourMapType& map, const int index);
ColourMapType::const_iterator safeFetchPair(const ColourMapType& map, const int index) const;
public:

PeakPalette();
PeakPalette(const PeakPalette& other);
PeakPalette& operator=(const PeakPalette& other);
Colour foregroundIndexToColour(const int index) const;
Colour backgroundIndexToColour(const int index) const;
void setForegroundColour(const int index, const Colour);
void setBackgroundColour(const int index, const Colour);
int paletteSize() const;
~PeakPalette();
};

} //namespace
}
#endif // MANTID_SLICEVIEWER_PEAKSPALETTE_H
109 changes: 109 additions & 0 deletions Code/Mantid/MantidQt/SliceViewer/src/PeakPalette.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "MantidQtSliceViewer/PeakPalette.h"
#include <algorithm>
#include <sstream>

namespace MantidQt
{
namespace SliceViewer
{
PeakPalette::PeakPalette()
{
int index = 0;
m_foregroundMap.insert(std::make_pair(index++, Colour::Green));
m_foregroundMap.insert(std::make_pair(index++, Colour::DarkMagenta));
m_foregroundMap.insert(std::make_pair(index++, Colour::Cyan));
m_foregroundMap.insert(std::make_pair(index++, Colour::DarkGreen));
m_foregroundMap.insert(std::make_pair(index++, Colour::DarkCyan));
m_foregroundMap.insert(std::make_pair(index++, Colour::DarkYellow));
m_foregroundMap.insert(std::make_pair(index++, Colour::DarkRed));
m_foregroundMap.insert(std::make_pair(index++, Colour::Black));
m_foregroundMap.insert(std::make_pair(index++, Colour::White));
m_foregroundMap.insert(std::make_pair(index++, Colour::DarkGray));
m_backgroundMap = m_foregroundMap;
}

PeakPalette::PeakPalette(const PeakPalette& other) : m_foregroundMap(other.m_foregroundMap), m_backgroundMap(other.m_backgroundMap)
{
}

PeakPalette& PeakPalette::operator=(const PeakPalette& other)
{
m_foregroundMap.clear();
m_backgroundMap.clear();
if(this != &other)
{
m_foregroundMap.insert(other.m_foregroundMap.begin(), other.m_foregroundMap.end());
m_backgroundMap.insert(other.m_backgroundMap.begin(), other.m_backgroundMap.end());
}
return *this;
}

PeakPalette::~PeakPalette()
{
}

void throwIndex(const int index)
{
std::stringstream stream;
stream << "Index " << index << " is out of range";
throw std::out_of_range(stream.str());
}

PeakPalette::ColourMapType::const_iterator PeakPalette::safeFetchPair(const PeakPalette::ColourMapType& map, const int index) const
{
auto it = map.find(index);
if(it == map.end())
{
throwIndex(index);
}
return it;
}


PeakPalette::ColourMapType::iterator PeakPalette::safeFetchPair(PeakPalette::ColourMapType& map, const int index)
{
ColourMapType::iterator it = map.find(index);
if(it == map.end())
{
throwIndex(index);
}
return it;
}

Colour PeakPalette::foregroundIndexToColour(const int index) const
{
auto it = safeFetchPair(m_foregroundMap, index);
return it->second;
}

Colour PeakPalette::backgroundIndexToColour(const int index) const
{
auto it = safeFetchPair(m_backgroundMap, index);
return it->second;
}

void PeakPalette::setForegroundColour(const int index, const Colour colour)
{
auto it = safeFetchPair(m_foregroundMap, index);
// overwrite
it->second = colour;
}

void PeakPalette::setBackgroundColour(const int index, const Colour colour)
{
auto it = safeFetchPair(m_backgroundMap, index);
// owverwirte
it->second = colour;
}

int PeakPalette::paletteSize() const
{
if(m_foregroundMap.size() != m_backgroundMap.size())
{
throw std::runtime_error("The PeakPalette size is not consistent");
}
return m_foregroundMap.size();
}

}
}
180 changes: 180 additions & 0 deletions Code/Mantid/MantidQt/SliceViewer/test/PeakPaletteTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#ifndef SLICE_VIEWER_PEAKPALETTE_TEST_H_
#define SLICE_VIEWER_PEAKPALETTE_TEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidQtSliceViewer/PeakPalette.h"
#include <sstream>

using namespace MantidQt::SliceViewer;

class PeakPaletteTest : public CxxTest::TestSuite
{

public:

void test_paletteSize()
{
PeakPalette palette;
const int expectedNumberOfEntries = 10;
TSM_ASSERT_EQUALS("\n\nPalette should have a default and fixed size\n", expectedNumberOfEntries, palette.paletteSize());
}

void test_default_foregroundIndexToColour()
{
PeakPalette palette;
TS_ASSERT_EQUALS(Colour::Green, palette.foregroundIndexToColour(0));
TS_ASSERT_EQUALS(Colour::DarkMagenta, palette.foregroundIndexToColour(1));
TS_ASSERT_EQUALS(Colour::Cyan, palette.foregroundIndexToColour(2));
TS_ASSERT_EQUALS(Colour::DarkGreen, palette.foregroundIndexToColour(3));
TS_ASSERT_EQUALS(Colour::DarkCyan, palette.foregroundIndexToColour(4));
TS_ASSERT_EQUALS(Colour::DarkYellow, palette.foregroundIndexToColour(5));
TS_ASSERT_EQUALS(Colour::DarkRed, palette.foregroundIndexToColour(6));
TS_ASSERT_EQUALS(Colour::Black, palette.foregroundIndexToColour(7));
TS_ASSERT_EQUALS(Colour::White, palette.foregroundIndexToColour(8));
TS_ASSERT_EQUALS(Colour::DarkGray, palette.foregroundIndexToColour(9));
}

void test_default_backgroundIndexToColour()
{
PeakPalette palette;
TS_ASSERT_EQUALS(Colour::Green, palette.backgroundIndexToColour(0));
TS_ASSERT_EQUALS(Colour::DarkMagenta, palette.backgroundIndexToColour(1));
TS_ASSERT_EQUALS(Colour::Cyan, palette.backgroundIndexToColour(2));
TS_ASSERT_EQUALS(Colour::DarkGreen, palette.backgroundIndexToColour(3));
TS_ASSERT_EQUALS(Colour::DarkCyan, palette.backgroundIndexToColour(4));
TS_ASSERT_EQUALS(Colour::DarkYellow, palette.backgroundIndexToColour(5));
TS_ASSERT_EQUALS(Colour::DarkRed, palette.backgroundIndexToColour(6));
TS_ASSERT_EQUALS(Colour::Black, palette.backgroundIndexToColour(7));
TS_ASSERT_EQUALS(Colour::White, palette.backgroundIndexToColour(8));
TS_ASSERT_EQUALS(Colour::DarkGray, palette.backgroundIndexToColour(9));
}

void test_foregroundIndexToColour_throws_if_out_of_range()
{
const int indexTooHigh = 10;
const int indexTooLow = -1;

PeakPalette palette;
TSM_ASSERT_THROWS("\n\nIndex > Max Index, should throw.\n", palette.foregroundIndexToColour(indexTooHigh), std::out_of_range);
TSM_ASSERT_THROWS("\n\nIndex < Max Index, should throw.\n", palette.foregroundIndexToColour(indexTooLow), std::out_of_range);
}

void test_backgroundIndexToColour_throws_if_out_of_range()
{
const int indexTooHigh = 10;
const int indexTooLow = -1;

PeakPalette palette;
TSM_ASSERT_THROWS("\n\nIndex > Max Index, should throw.\n", palette.backgroundIndexToColour(indexTooHigh), std::out_of_range);
TSM_ASSERT_THROWS("\n\nIndex < Max Index, should throw.\n", palette.backgroundIndexToColour(indexTooLow), std::out_of_range);
}

void test_setForgroundColour()
{
PeakPalette palette;
const int indexToChange = 0;
const Colour originalColour = palette.foregroundIndexToColour(indexToChange);
const Colour requestColour = Colour::Black;

palette.setForegroundColour(indexToChange, requestColour);

const Colour finalColour = palette.foregroundIndexToColour(indexToChange);

TSM_ASSERT_DIFFERS("\n\nForeground palette colour has not changed at requested index.\n", originalColour, finalColour);
TSM_ASSERT_EQUALS("\n\nForeground palette colour has not changed to the requested colour.\n", requestColour, finalColour);

const int expectedNumberOfEntries = 10;
TSM_ASSERT_EQUALS("\n\nPalette should have a default and fixed size\n", expectedNumberOfEntries, palette.paletteSize());
}

void test_setBackgroundColour()
{
PeakPalette palette;
const int indexToChange = 0;
const Colour originalColour = palette.backgroundIndexToColour(indexToChange);
const Colour requestColour = Colour::Black;

palette.setForegroundColour(indexToChange, requestColour);

const Colour finalColour = palette.foregroundIndexToColour(indexToChange);

TSM_ASSERT_DIFFERS("\n\nBackground palette colour has not changed at requested index.\n", originalColour, finalColour);
TSM_ASSERT_EQUALS("\n\nBackground palette colour has not changed to the requested colour.\n", requestColour, finalColour);

const int expectedNumberOfEntries = 10;
TSM_ASSERT_EQUALS("\n\nPalette should have a default and fixed size\n", expectedNumberOfEntries, palette.paletteSize());
}

void test_setForegroundColour_throws_if_out_of_range()
{
const int indexTooHigh = 10;
const int indexTooLow = -1;

PeakPalette palette;
TSM_ASSERT_THROWS("\n\nIndex is > Max Index. Should throw\n.", palette.setForegroundColour(indexTooHigh, Colour::Red), std::out_of_range);
TSM_ASSERT_THROWS("\n\nIndex is < Min Index. Should throw\n", palette.setForegroundColour(indexTooLow, Colour::Red), std::out_of_range);
}

void test_setBackgroundColour_throws_if_out_of_range()
{
const int indexTooHigh = 10;
const int indexTooLow = -1;

PeakPalette palette;
TSM_ASSERT_THROWS("\n\nIndex is > Max Index. Should throw\n.", palette.setBackgroundColour(indexTooHigh, Colour::Red), std::out_of_range);
TSM_ASSERT_THROWS("\n\nIndex is < Min Index. Should throw\n", palette.setBackgroundColour(indexTooLow, Colour::Red), std::out_of_range);
}

void testCopy()
{
// Create an original, and modify the palette a little, so we can be sure that the copy is a genuine copy of the current state.
PeakPalette original;
original.setForegroundColour(0, Colour::Red);
original.setBackgroundColour(0, Colour::Blue);

// Make a copy.
PeakPalette copy(original);

// Check the size.
TSM_ASSERT_EQUALS("\n\nSize of the copy is not the same as the size of the original.\n", original.paletteSize(), copy.paletteSize());

// Check all entries.
for(int i = 0; i < original.paletteSize(); ++i)
{
TSM_ASSERT_EQUALS("\n\n\Foreground colour different between orignial and copy", original.foregroundIndexToColour(i), copy.foregroundIndexToColour(i));
TSM_ASSERT_EQUALS("\n\n\Background colour different between orignial and copy", original.backgroundIndexToColour(i), copy.backgroundIndexToColour(i));
}

}

void testAssignment()
{
// Create an original, and modify the palette a little, so we can be sure that the copy is a genuine copy of the current state.
PeakPalette A;
A.setForegroundColour(0, Colour::Red);
A.setBackgroundColour(0, Colour::Blue);

// Make another.
PeakPalette B;

// Make A == B
B = A;

// Check the size.
TSM_ASSERT_EQUALS("\n\nSize of the copy is not the same as the size of the original.\n", A.paletteSize(), B.paletteSize());

// Check all entries.
for(int i = 0; i < A.paletteSize(); ++i)
{
TSM_ASSERT_EQUALS("\n\n\Foreground colour different between orignial and copy.\n", B.foregroundIndexToColour(i), A.foregroundIndexToColour(i));
TSM_ASSERT_EQUALS("\n\n\Background colour different between orignial and copy.\n", B.backgroundIndexToColour(i), A.backgroundIndexToColour(i));
}

// Specifically check that B has taken A's values using a couple of test cases.
TSM_ASSERT_EQUALS("\n\n\Assignment of foreground colours has not worked.\n", B.foregroundIndexToColour(0), Colour::Red);
TSM_ASSERT_EQUALS("\n\n\Assignment of background colours has not worked.\n", B.backgroundIndexToColour(0), Colour::Blue);
}

};

#endif

0 comments on commit ea6abfe

Please sign in to comment.