Skip to content
Snippets Groups Projects
Commit 0af140a2 authored by Anton Piccardo-Selg's avatar Anton Piccardo-Selg
Browse files

Refs #10656 Adding user-settable default colormap and background color

parent 1324972e
No related branches found
No related tags found
No related merge requests found
Showing
with 8490 additions and 9 deletions
This diff is collapsed.
......@@ -2,6 +2,8 @@ project( MantidVatesSimpleGuiViewWidgets )
# These are the C++ files to be compiled.
set( INCLUDE_FILES
inc/MantidVatesSimpleGuiViewWidgets/BackgroundRgbProvider.h
inc/MantidVatesSimpleGuiViewWidgets/ColorMapManager.h
inc/MantidVatesSimpleGuiViewWidgets/ColorSelectionWidget.h
inc/MantidVatesSimpleGuiViewWidgets/ColorUpdater.h
inc/MantidVatesSimpleGuiViewWidgets/LibHelper.h
......@@ -17,6 +19,8 @@ set( INCLUDE_FILES
)
set( SOURCE_FILES
src/BackgroundRgbProvider.cpp
src/ColorMapManager.cpp
src/ColorSelectionWidget.cpp
src/ColorUpdater.cpp
src/MdViewerWidget.cpp
......
#ifndef BACKGROUNDRGB_PROVIDER_H_
#define BACKGROUNDRGB_PROVIDER_H_
#include "MantidVatesSimpleGuiViewWidgets/WidgetDllOption.h"
#include <vector>
#include <map>
#include <string>
namespace Mantid
{
namespace Vates
{
namespace SimpleGui
{
/**
*
This class gets the default color values for the background of the view.
@date 10/12/2014
Copyright &copy; 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class EXPORT_OPT_MANTIDVATES_SIMPLEGUI_VIEWWIDGETS BackgroundRgbProvider
{
public:
BackgroundRgbProvider();
virtual ~BackgroundRgbProvider();
/**
* Get the Rgb values for the color of the view's background
* @returns A vector with the RGB values
*/
std::vector<double> getRgb();
private:
/**
* Get the Rgb values for the color of the view's background from the user setting
* @returns A vector with the RGB values
*/
std::vector<double> getRbgFromPropertiesFile();
/**
* Extract the rgb vector from the numeric user setting
* @param background A vector with three color settings
* @returns A vector with the RGB values or a default RGB vector
*/
std::vector<double> getFromNumericSetting(std::vector<std::string> background);
/**
* Extract the rgb vector from the name setting
* @param background A string with a color setting
* @returns A vector with the RGB values or a default RGB vector
*/
std::vector<double> getFromNameSetting(std::string background);
/**
* Extract all comma separated elements from the background setting string.
* @param background A string with a color setting.
* @returns A vector with the color entries.
*/
std::vector<std::string> getCommaSeparatedEntries(std::string background);
/**
* Check if the numeric entry is acceptable, i.e. if it is between 0 and 255
* @param entry The color value.
* @returns True if the value is in the acceptable range.
*/
bool BackgroundRgbProvider::isValidNumericEntry(double entry);
/**
* Check if the entry is a numeric entry at all
* @param entry entry The color value.
* @returns True if the value is a numeric value, otherwise false.
*/
bool isNumeric(std::string entry);
/// The separator string
std::string separator;
/// The default background color
std::vector<double> defaultBackground;
/// Background map which associates a name with an RGB vector
std::map<std::string, std::vector<double>> backgroundMap;
};
}
}
}
#endif
\ No newline at end of file
#ifndef COLORMAP_MANAGER_H_
#define COLORMAP_MANAGER_H_
#include "MantidVatesSimpleGuiViewWidgets/WidgetDllOption.h"
#include <string>
#include <map>
namespace Mantid
{
namespace Vates
{
namespace SimpleGui
{
/**
*
This class handles the colormaps which are loaded into the
@date 10/12/2014
Copyright &copy; 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class EXPORT_OPT_MANTIDVATES_SIMPLEGUI_VIEWWIDGETS ColorMapManager
{
public:
ColorMapManager();
virtual ~ColorMapManager();
/**
* Read in and store the available color maps
* @param xml The path to the colormap.
*/
void readInColorMap(std::string xml);
/**
* Get index for colormap
* @param colorMap The name of the color map.
* @returns The index of the colormap in the Paraview store.
*/
int getColorMapIndex(std::string colorMap);
/**
* Check if a color map already has been recorded.
* @param colorMap The name of the color map.
* @returns True if the name already exists
*/
bool isRecordedColorMap(std::string colorMap);
private:
int indexCounter;
std::map<std::string, int> colorMapInfo;
};
}
}
}
#endif
\ No newline at end of file
......@@ -3,7 +3,8 @@
#include "ui_ColorSelectionWidget.h"
#include "MantidVatesSimpleGuiViewWidgets/WidgetDllOption.h"
#include "boost/scoped_ptr.hpp"
#include "MantidVatesSimpleGuiViewWidgets/ColorMapManager.h"
#include <QWidget>
class pqColorMapModel;
......@@ -62,6 +63,8 @@ public:
double getMinRange();
/// Get the maximum color range value
double getMaxRange();
/// Load the default color map
void loadDefaultColorMap();
public slots:
/// Set state for all control widgets.
......@@ -117,6 +120,8 @@ private:
pqColorPresetManager *presets; ///< Dialog for choosing color presets
Ui::ColorSelectionWidgetClass ui; ///< The mode control widget's UI form
boost::scoped_ptr<ColorMapManager> colorMapManager; ///< Keeps track of the available color maps.
};
} // SimpleGui
......
......@@ -175,6 +175,8 @@ private:
bool checkIfTechniqueContainsKeyword(const std::set<std::string>& techniques, const std::string& keyword) const;
/// Reset the current view to the appropriate initial view.
void resetCurrentView(int workspaceType, const std::string& instrumentName);
/// Set up the default color for the background of the view.
void setDefaultColorForBackground();
};
} // SimpleGui
......
#include "MantidVatesSimpleGuiViewWidgets/BackgroundRgbProvider.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Logger.h"
#include <algorithm>
#include <vector>
#include <cctype>
#include <stdlib.h>
namespace Mantid
{
namespace Vates
{
namespace SimpleGui
{
namespace
{
/// static logger
Mantid::Kernel::Logger g_log("BackgroundRgbProvider");
}
BackgroundRgbProvider::BackgroundRgbProvider():separator(",")
{
// Set the default color
defaultBackground.push_back(84);
defaultBackground.push_back(89);
defaultBackground.push_back(109);
// Set the map for the background colors
// Black
std::string black = "BLACK";
std::vector<double> blackValues;
blackValues.push_back(0.0);
blackValues.push_back(0.0);
blackValues.push_back(0.0);
backgroundMap.insert(std::pair<std::string, std::vector<double>>(black, blackValues));
// White
std::string white = "WHITE";
std::vector<double> whiteValues;
whiteValues.push_back(255.0);
whiteValues.push_back(255.0);
whiteValues.push_back(255.0);
backgroundMap.insert(std::pair<std::string, std::vector<double>>(white, whiteValues));
// Grey
std::string grey = "GREY";
std::vector<double> greyValues;
greyValues.push_back(160.0);
greyValues.push_back(160.0);
greyValues.push_back(160.0);
backgroundMap.insert(std::pair<std::string, std::vector<double>>(grey, greyValues));
}
BackgroundRgbProvider::~BackgroundRgbProvider()
{
}
std::vector<double> BackgroundRgbProvider::getRgb()
{
// Get the rgb setting from the config file
std::vector<double> userSettingRgb = getRbgFromPropertiesFile();
// Normalize the entries to 256
userSettingRgb[0] = userSettingRgb[0]/255.0;
userSettingRgb[1] = userSettingRgb[1]/255.0;
userSettingRgb[2] = userSettingRgb[2]/255.0;
return userSettingRgb;
}
std::vector<double> BackgroundRgbProvider::getRbgFromPropertiesFile()
{
// Set the mantid default here
std::vector<double> background;
// Check in the Mantid.users.properties file if a default color map was specified
std::string userBackground= Kernel::ConfigService::Instance().getVsiDefaultBackgroundColor();
if (!userBackground.empty())
{
// Try to get comma separated values from the
std::vector<std::string> colorsNumeric = this->getCommaSeparatedEntries(userBackground);
if (colorsNumeric.size() == 3)
{
background = this->getFromNumericSetting(colorsNumeric);
}
else
{
background = this->getFromNameSetting(userBackground);
}
}
else
{
background = this-> defaultBackground;
}
return background;
}
std::vector<std::string> BackgroundRgbProvider::getCommaSeparatedEntries(std::string background)
{
std::vector<std::string> colors;
size_t pos = 0;
std::string token;
while ((pos = background.find(this->separator)) != std::string::npos)
{
token = background.substr(0, pos);
colors.push_back(token);
background.erase(0, pos + this->separator.length());
}
if (!background.empty())
{
colors.push_back(background);
}
return colors;
}
std::vector<double> BackgroundRgbProvider::getFromNumericSetting(std::vector<std::string> background)
{
std::vector<double> colors;
if (background.size() == 3)
{
// Convert the string values to double values
double entry;
for (std::vector<std::string>::iterator it = background.begin(); it != background.end(); ++it)
{
entry = atof(it->c_str());
// Make sure that the entry is valid -> 0 <= entry <= 255
if (isNumeric(*it) && isValidNumericEntry(entry))
{
colors.push_back(entry);
}
else
{
// If an entry is invalid, then exit with the default setting
g_log.warning() << "Warning: The VSI background color specified in the Mantid.user.profiles file is not valid. \n";
return this->defaultBackground;
}
}
}
else
{
// If the wrong number of columns is specifed
g_log.warning() << "Warning: The VSI background color specified in the Mantid.user.profiles file is not valid. \n";
colors = this->defaultBackground;
}
return colors;
}
bool BackgroundRgbProvider::isNumeric(std::string entry)
{
std::string::const_iterator it = entry.begin();
// Currently we only allow integer entries
while (it != entry.end() && std::isdigit(*it))
{
++it;
}
if (!entry.empty() && it == entry.end())
{
return true;
}
else
{
return false;
}
}
bool BackgroundRgbProvider::isValidNumericEntry(double entry)
{
double lowerBound = 0.0;
double upperBound = 255.0;
if (entry >= lowerBound && entry <= upperBound)
{
return true;
}
else
{
return false;
}
}
std::vector<double> BackgroundRgbProvider::getFromNameSetting(std::string background)
{
// Convert to upper case
std::transform(background.begin(), background.end(), background.begin(), toupper);
// Check if exists
if (backgroundMap.count(background) > 0)
{
return backgroundMap[background];
}
else
{
g_log.warning() << "Warning: The VSI background color specified in the Mantid.user.profiles file is not valid. \n";
return this->defaultBackground;
}
}
}
}
}
\ No newline at end of file
#include "MantidVatesSimpleGuiViewWidgets/ColorMapManager.h"
#include "MantidKernel/Logger.h"
#include <map>
#include <string>
namespace Mantid
{
namespace Vates
{
namespace SimpleGui
{
namespace
{
/// static logger
Mantid::Kernel::Logger g_log("ColorMapManager");
}
ColorMapManager::ColorMapManager() :indexCounter(0)
{
}
ColorMapManager::~ColorMapManager()
{
}
void ColorMapManager::readInColorMap(std::string name)
{
// Add the name to the colormap map and increment the index counter
if (!name.empty())
{
this->colorMapInfo.insert(std::pair<std::string, int>(name, this->indexCounter));
this->indexCounter = this->indexCounter + 1;
}
}
int ColorMapManager::getColorMapIndex(std::string colorMap)
{
if (this->colorMapInfo.count(colorMap) == 1 )
{
return colorMapInfo[colorMap];
}
else
{
// requested color map was not found
g_log.warning() <<"Warning: Requested color map could not be found. Setting default color map. \n";
return 0;
}
}
bool ColorMapManager::isRecordedColorMap(std::string colorMap)
{
if (this->colorMapInfo.count(colorMap) > 0)
{
return true;
}
else
{
return false;
}
}
}
}
}
\ No newline at end of file
#include "MantidVatesSimpleGuiViewWidgets/ColorSelectionWidget.h"
#include "MantidKernel/ConfigService.h"
#include "MantidVatesSimpleGuiViewWidgets/ColorMapManager.h"
#include <pqBuiltinColorMaps.h>
#include <pqChartValue.h>
#include <pqColorMapModel.h>
#include <pqColorPresetManager.h>
#include <pqColorPresetModel.h>
#include <vtkPVXMLElement.h>
#include <vtkPVXMLParser.h>
......@@ -29,7 +31,7 @@ namespace SimpleGui
* sub-components and connections.
* @param parent the parent widget of the mode control widget
*/
ColorSelectionWidget::ColorSelectionWidget(QWidget *parent) : QWidget(parent)
ColorSelectionWidget::ColorSelectionWidget(QWidget *parent) : QWidget(parent), colorMapManager(new ColorMapManager())
{
this->ui.setupUi(this);
this->ui.autoColorScaleCheckBox->setChecked(true);
......@@ -75,18 +77,21 @@ void ColorSelectionWidget::loadBuiltinColorPresets()
{
pqColorPresetModel *presetModel = this->presets->getModel();
// get builtin color maps xml
const char *xml = pqComponentsGetColorMapsXML();
// Associate the colormap value with the index a continuous index
// create xml parser
vtkPVXMLParser *xmlParser = vtkPVXMLParser::New();
// 1. Get builtinw color maps (Reading fragment requires: InitializeParser, ParseChunk, CleanupParser)
const char *xml = pqComponentsGetColorMapsXML();
xmlParser->InitializeParser();
xmlParser->ParseChunk(xml, static_cast<unsigned>(strlen(xml)));
xmlParser->CleanupParser();
this->addColorMapsFromXML(xmlParser, presetModel);
// Add color maps from IDL and Matplotlib
// 2. Add color maps from Slice Viewer, IDL and Matplotlib
this->addColorMapsFromFile("All_slice_viewer_cmaps_for_vsi.xml", xmlParser, presetModel);
this->addColorMapsFromFile("All_idl_cmaps.xml", xmlParser, presetModel);
this->addColorMapsFromFile("All_mpl_cmaps.xml", xmlParser, presetModel);
......@@ -94,6 +99,29 @@ void ColorSelectionWidget::loadBuiltinColorPresets()
xmlParser->Delete();
}
/**
* Load the default color map
*/
void ColorSelectionWidget::loadDefaultColorMap()
{
// Check in the Mantid.users.properties file if a default color map was specified
std::string defaultColorMap = Kernel::ConfigService::Instance().getString("vsi.colormap");
int defaultColorMapIndex = 0;
if (!defaultColorMap.empty())
{
defaultColorMapIndex = this->colorMapManager->getColorMapIndex(defaultColorMap);
}
const pqColorMapModel *colorMap = this->presets->getModel()->getColorMap(defaultColorMapIndex);
if (colorMap)
{
emit this->colorMapChanged(colorMap);
}
}
/**
* This function takes color maps from a XML file, parses them and loads and
* adds them to the color preset model.
......@@ -143,8 +171,15 @@ void ColorSelectionWidget::addColorMapsFromXML(vtkPVXMLParser *parser,
pqColorPresetManager::createColorMapFromXML(colorMapElement);
QString name = colorMapElement->GetAttribute("name");
// add color map to the model
model->addBuiltinColorMap(colorMap, name);
// Only add the color map if the name does not exist yet
if (!this->colorMapManager->isRecordedColorMap(name.toStdString()))
{
// add color map to the model
model->addBuiltinColorMap(colorMap, name);
// add color map to the color map manager
this->colorMapManager->readInColorMap(name.toStdString());
}
}
}
......
......@@ -2,6 +2,7 @@
#include "MantidVatesSimpleGuiQtWidgets/ModeControlWidget.h"
#include "MantidVatesSimpleGuiQtWidgets/RotationPointDialog.h"
#include "MantidVatesSimpleGuiViewWidgets/BackgroundRgbProvider.h"
#include "MantidVatesSimpleGuiViewWidgets/ColorSelectionWidget.h"
#include "MantidVatesSimpleGuiViewWidgets/MultisliceView.h"
#include "MantidVatesSimpleGuiViewWidgets/SaveScreenshotReaction.h"
......@@ -39,6 +40,7 @@
#include <vtkSMPropertyHelper.h>
#include <vtkSMProxyManager.h>
#include <vtkSMProxy.h>
#include <vtkSMViewProxy.h>
#include <vtkSMSourceProxy.h>
#include <vtkSMReaderFactory.h>
#include <vtksys/SystemTools.hxx>
......@@ -164,6 +166,7 @@ void MdViewerWidget::internalSetup(bool pMode)
this->rotPointDialog = NULL;
this->lodThreshold = 5.0;
this->viewSwitched = false;
this->startingUp = true;
}
/**
......@@ -712,10 +715,34 @@ void MdViewerWidget::setupPluginMode()
*/
void MdViewerWidget::renderAndFinalSetup()
{
this->setDefaultColorForBackground();
this->currentView->render();
this->currentView->setColorsForView();
this->currentView->checkView(this->initialView);
this->currentView->updateAnimationControls();
// Only load the default color map for the first time
// that a representation is created.
if (this->startingUp)
{
this->ui.colorSelectionWidget->loadDefaultColorMap();
this->startingUp = false;
}
}
/**
* Set the background color for this view.
*/
void MdViewerWidget::setDefaultColorForBackground()
{
// Get background setting
BackgroundRgbProvider backgroundRgbProvider;
std::vector<double> backgroundRgb = backgroundRgbProvider.getRgb();
vtkSMDoubleVectorProperty* background = vtkSMDoubleVectorProperty::SafeDownCast(this->currentView->getView()->getViewProxy()->GetProperty("Background"));
background->SetElements3(backgroundRgb[0],backgroundRgb[1],backgroundRgb[2]);
this->currentView->getView()->resetCamera();
}
/**
......@@ -779,8 +806,10 @@ void MdViewerWidget::switchViews(ModeControlWidget::Views v)
this->hiddenView->close();
this->hiddenView->destroyView();
delete this->hiddenView;
this->setDefaultColorForBackground(); // Keeps background color to default value
this->currentView->render();
this->currentView->setColorsForView();
this->ui.colorSelectionWidget->loadDefaultColorMap(); // Load the default color map
this->currentView->checkViewOnSwitch();
this->updateAppState();
this->initialView = v;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment