Commit f8a7e183 authored by Nicolas Vuaille's avatar Nicolas Vuaille Committed by Kitware Robot

Merge topic 'SMTranserFunctionPresetSingleton'

0c33ccb5 Reformatting vtkSMTransferFunctionPresets: NULL to nullptr
0000c0c3 vtkSMTransferFunctionPresets become a singleton
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: Mathieu Westphal's avatarMathieu Westphal <mathieu.westphal@kitware.com>
Merge-request: !3727
parents e9aad47a 0c33ccb5
Pipeline #152982 running with stage
......@@ -41,8 +41,7 @@ int TestTransferFunctionPresets(int argc, char* argv[])
vtkInitializationHelper::Initialize(argv[0], vtkProcessModule::PROCESS_CLIENT);
vtkSmartPointer<vtkSMTransferFunctionPresets> presets =
vtkSmartPointer<vtkSMTransferFunctionPresets>::New();
auto presets = vtkSMTransferFunctionPresets::GetInstance();
myassert(presets->GetNumberOfPresets() > 0, "Load presets");
cout << "Number of presets: " << presets->GetNumberOfPresets() << endl;
......
......@@ -31,39 +31,43 @@
#include <set>
#include <sstream>
vtkSmartPointer<vtkSMTransferFunctionPresets> vtkSMTransferFunctionPresets::Instance;
class vtkSMTransferFunctionPresets::vtkInternals
{
public:
vtkInternals()
: Modified(false)
, CustomPresetsLoaded(false)
: CustomPresetsLoaded(false)
{
}
~vtkInternals()
~vtkInternals() {}
void SaveToSettings()
{
if (this->Modified)
assert(this->CustomPresetsLoaded);
vtkSMSettings* settings = vtkSMSettings::GetInstance();
std::ostringstream stream;
stream << "[\n";
bool not_first = false;
for (std::vector<Json::Value>::const_iterator iter = this->CustomPresets.begin();
iter != this->CustomPresets.end(); ++iter)
{
assert(this->CustomPresetsLoaded);
if (vtkSMSettings* settings = vtkSMSettings::GetInstance())
if (not_first)
{
std::ostringstream stream;
stream << "[\n";
bool not_first = false;
for (std::vector<Json::Value>::const_iterator iter = this->CustomPresets.begin();
iter != this->CustomPresets.end(); ++iter)
{
if (not_first)
{
stream << ",\n";
}
not_first = true;
stream << iter->toStyledString().c_str();
}
stream << "]";
settings->SetSetting("TransferFunctionPresets.CustomPresets", stream.str());
stream << ",\n";
}
not_first = true;
stream << iter->toStyledString().c_str();
}
stream << "]";
settings->SetSetting("TransferFunctionPresets.CustomPresets", stream.str());
}
void Reload()
{
this->Presets.clear();
this->GetPresets();
}
const std::vector<Json::Value>& GetPresets()
......@@ -91,9 +95,9 @@ public:
this->Presets.erase(this->Presets.begin() + index);
index = (index - static_cast<unsigned int>(this->BuiltinPresets.size()));
this->Modified = true;
assert(this->CustomPresets.size() > index);
this->CustomPresets.erase(this->CustomPresets.begin() + index);
this->SaveToSettings();
return true;
}
......@@ -102,8 +106,8 @@ public:
this->LoadCustomPresets();
this->CustomPresets.push_back(value);
this->CustomPresets.back()["Name"] = name;
this->Presets.clear();
this->Modified = true;
this->SaveToSettings();
this->Reload();
}
bool IsPresetBuiltin(unsigned int index)
......@@ -120,12 +124,13 @@ public:
if (index < static_cast<unsigned int>(presets.size()) &&
index >= static_cast<unsigned int>(this->BuiltinPresets.size()))
{
this->Presets[index]["Name"] = newname;
index = (index - static_cast<unsigned int>(this->BuiltinPresets.size()));
assert(this->CustomPresets.size() > index);
this->CustomPresets[index]["Name"] = newname;
this->Presets.clear();
this->Modified = true;
this->SaveToSettings();
return true;
}
}
......@@ -161,8 +166,8 @@ public:
{
this->LoadCustomPresets();
this->CustomPresets.insert(this->CustomPresets.end(), root.begin(), root.end());
this->Modified = true;
this->Presets.clear();
this->SaveToSettings();
this->Reload();
return true;
}
......@@ -170,7 +175,6 @@ private:
std::vector<Json::Value> BuiltinPresets;
std::vector<Json::Value> CustomPresets;
std::vector<Json::Value> Presets;
bool Modified;
bool CustomPresetsLoaded;
void LoadBuiltinPresets()
......@@ -204,7 +208,7 @@ private:
const char* const settingsKey = "TransferFunctionPresets.CustomPresets";
vtkSMSettings* settings = vtkSMSettings::GetInstance();
if (settings == NULL || !settings->HasSetting(settingsKey))
if (settings == nullptr || !settings->HasSetting(settingsKey))
{
return;
}
......@@ -226,7 +230,13 @@ private:
}
};
vtkStandardNewMacro(vtkSMTransferFunctionPresets);
vtkSMTransferFunctionPresets* vtkSMTransferFunctionPresets::New()
{
auto instance = vtkSMTransferFunctionPresets::GetInstance();
instance->Register(nullptr);
return instance;
}
//----------------------------------------------------------------------------
vtkSMTransferFunctionPresets::vtkSMTransferFunctionPresets()
: Internals(new vtkSMTransferFunctionPresets::vtkInternals())
......@@ -237,7 +247,20 @@ vtkSMTransferFunctionPresets::vtkSMTransferFunctionPresets()
vtkSMTransferFunctionPresets::~vtkSMTransferFunctionPresets()
{
delete this->Internals;
this->Internals = NULL;
this->Internals = nullptr;
}
//----------------------------------------------------------------------------
vtkSMTransferFunctionPresets* vtkSMTransferFunctionPresets::GetInstance()
{
if (vtkSMTransferFunctionPresets::Instance.GetPointer() == nullptr)
{
auto presets = new vtkSMTransferFunctionPresets();
presets->InitializeObjectBase();
vtkSMTransferFunctionPresets::Instance.TakeReference(presets);
}
return Instance;
}
//----------------------------------------------------------------------------
......@@ -260,7 +283,7 @@ const Json::Value& vtkSMTransferFunctionPresets::GetPreset(unsigned int index)
const Json::Value& vtkSMTransferFunctionPresets::GetFirstPresetWithName(const char* name)
{
static Json::Value nullValue;
if (name == NULL)
if (name == nullptr)
{
return nullValue;
}
......@@ -331,7 +354,7 @@ bool vtkSMTransferFunctionPresets::AddPreset(const char* name, const Json::Value
//----------------------------------------------------------------------------
vtkStdString vtkSMTransferFunctionPresets::AddUniquePreset(
const Json::Value& preset, const char* prefix /*=NULL*/)
const Json::Value& preset, const char* prefix /*=nullptr*/)
{
prefix = prefix ? prefix : "Preset";
......@@ -459,3 +482,9 @@ void vtkSMTransferFunctionPresets::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
void vtkSMTransferFunctionPresets::ReloadPresets()
{
this->Internals->Reload();
}
......@@ -21,11 +21,11 @@
* annotation presets. It also uses vtkSMSettings to support persistent
* customizations besides hard-coded/builtin presets.
*
* vtkSMTransferFunctionPresets designed to be instantiated, used and then
* destroyed. While there is no explicit synchronization between multiple
* instances of vtkSMTransferFunctionPresets, there should not be any need to
* have multiple instances alive at the same time.
*/
* vtkSMTransferFunctionPresets is designed as a singleton, accessible through
* the `GetInstance()` static method.
* Public API ensure that presets are loaded, but a reload can be explictly asked (see
* `ReloadPresets()`).
*/
#ifndef vtkSMTransferFunctionPresets_h
#define vtkSMTransferFunctionPresets_h
......@@ -33,8 +33,9 @@
#include "vtkSMObject.h"
#include "vtkPVServerManagerRenderingModule.h" // needed for exports
#include "vtkStdString.h" // needed for vtkStdString.
#include <vtk_jsoncpp_fwd.h> // for forward declarations
#include "vtkSmartPointer.h"
#include "vtkStdString.h" // needed for vtkStdString.
#include <vtk_jsoncpp_fwd.h> // for forward declarations
class vtkPVXMLElement;
class VTKPVSERVERMANAGERRENDERING_EXPORT vtkSMTransferFunctionPresets : public vtkSMObject
......@@ -44,6 +45,11 @@ public:
vtkTypeMacro(vtkSMTransferFunctionPresets, vtkSMObject);
void PrintSelf(ostream& os, vtkIndent indent) override;
/**
* Get singleton instance.
*/
static vtkSMTransferFunctionPresets* GetInstance();
/**
* Returns the number of presets current available (including builtin and
* custom).
......@@ -157,6 +163,11 @@ public:
bool ImportPresets(const Json::Value& presets);
//@}
/**
* Reload the presets from the configuration file.
*/
void ReloadPresets();
protected:
vtkSMTransferFunctionPresets();
~vtkSMTransferFunctionPresets() override;
......@@ -167,6 +178,8 @@ private:
class vtkInternals;
vtkInternals* Internals;
static vtkSmartPointer<vtkSMTransferFunctionPresets> Instance;
};
#endif
......@@ -838,7 +838,7 @@ bool vtkSMTransferFunctionProxy::ApplyPreset(const Json::Value& arg, bool rescal
//----------------------------------------------------------------------------
bool vtkSMTransferFunctionProxy::ApplyPreset(const char* presetname, bool rescale)
{
vtkNew<vtkSMTransferFunctionPresets> presets;
auto presets = vtkSMTransferFunctionPresets::GetInstance();
return this->ApplyPreset(presets->GetFirstPresetWithName(presetname), rescale);
}
......
......@@ -855,7 +855,7 @@ pqColorAnnotationsPropertyWidget::~pqColorAnnotationsPropertyWidget()
//-----------------------------------------------------------------------------
void pqColorAnnotationsPropertyWidget::applyPreset(const char* presetName)
{
vtkNew<vtkSMTransferFunctionPresets> presets;
auto presets = vtkSMTransferFunctionPresets::GetInstance();
const Json::Value& preset = presets->GetFirstPresetWithName(presetName);
const Json::Value& indexedColors = preset["IndexedColors"];
if (indexedColors.isNull() || !indexedColors.isArray() || (indexedColors.size() % 4) != 0 ||
......@@ -1386,10 +1386,7 @@ void pqColorAnnotationsPropertyWidget::saveAsPreset()
vtkStdString presetName;
if (!cpreset.isNull())
{
// This scoping is necessary to ensure that the vtkSMTransferFunctionPresets
// saves the new preset to the "settings" before the choosePreset dialog is
// shown.
vtkNew<vtkSMTransferFunctionPresets> presets;
auto presets = vtkSMTransferFunctionPresets::GetInstance();
presetName = presets->AddUniquePreset(cpreset);
}
this->choosePreset(presetName);
......
......@@ -935,13 +935,8 @@ void pqColorOpacityEditorWidget::saveAsPreset()
}
vtkStdString presetName;
{
// This scoping is necessary to ensure that the vtkSMTransferFunctionPresets
// saves the new preset to the "settings" before the choosePreset dialog is
// shown.
vtkNew<vtkSMTransferFunctionPresets> presets;
presetName = presets->AddUniquePreset(preset);
}
auto presets = vtkSMTransferFunctionPresets::GetInstance();
presetName = presets->AddUniquePreset(preset);
this->choosePreset(presetName);
}
......
......@@ -89,7 +89,7 @@ public:
pqPresetDialogTableModel(QObject* parentObject)
: Superclass(parentObject)
{
this->Presets = vtkSmartPointer<vtkSMTransferFunctionPresets>::New();
this->Presets = vtkSMTransferFunctionPresets::GetInstance();
this->Pixmaps.reserve(this->Presets->GetNumberOfPresets());
this->GroupManager = qobject_cast<pqPresetGroupsManager*>(
pqApplicationCore::instance()->manager("PRESET_GROUP_MANAGER"));
......@@ -111,7 +111,7 @@ public:
void reset()
{
this->beginResetModel();
this->Presets = vtkSmartPointer<vtkSMTransferFunctionPresets>::New();
this->Presets->ReloadPresets();
this->Pixmaps.clear();
this->Pixmaps.reserve(this->Presets->GetNumberOfPresets());
this->endResetModel();
......
......@@ -113,8 +113,6 @@ QPixmap pqPresetToPixmap::render(const Json::Value& preset, const QSize& resolut
return QPixmap();
}
vtkNew<vtkSMTransferFunctionPresets> presets;
pqInternals& internals = (*this->Internals);
vtkSMProxy* lutProxy = internals.lookupTable();
vtkSMTransferFunctionProxy::ApplyPreset(lutProxy, preset);
......@@ -127,6 +125,7 @@ QPixmap pqPresetToPixmap::render(const Json::Value& preset, const QSize& resolut
else
{
vtkPiecewiseFunction* pf = NULL;
auto presets = vtkSMTransferFunctionPresets::GetInstance();
if (presets->GetPresetHasOpacities(preset))
{
vtkSMProxy* piecewiseFunctionProxy = internals.piecewiseFunction();
......
......@@ -1114,7 +1114,7 @@ class ParaViewWebColorManager(ParaViewWebProtocol):
super(ParaViewWebColorManager, self).__init__()
if pathToColorMaps:
simple.ImportPresets(filename=pathToColorMaps)
self.presets = servermanager.vtkSMTransferFunctionPresets()
self.presets = servermanager.vtkSMTransferFunctionPresets.GetInstance()
self.colorMapNames = []
for i in range(self.presets.GetNumberOfPresets()):
if showBuiltin or not self.presets.IsPresetBuiltin(i):
......
......@@ -1543,7 +1543,7 @@ def GetOpacityTransferFunction(arrayname, representation=None, separate=False, *
def ImportPresets(filename):
"""Import presets from a file. The file can be in the legacy color map xml
format or in the new JSON format. Returns True on success."""
presets = servermanager.vtkSMTransferFunctionPresets()
presets = servermanager.vtkSMTransferFunctionPresets.GetInstance()
return presets.ImportPresets(filename)
# -----------------------------------------------------------------------------
......@@ -1598,7 +1598,7 @@ def AssignLookupTable(arrayInfo, lutName, rangeOveride=[]):
AssignLookupTable(arrayInfo, "Cool to Warm")
"""
presets = servermanager.vtkSMTransferFunctionPresets()
presets = servermanager.vtkSMTransferFunctionPresets.GetInstance()
if not presets.HasPreset(lutName):
raise RuntimeError("no preset with name `%s` present", lutName)
......@@ -1614,7 +1614,7 @@ def AssignLookupTable(arrayInfo, lutName, rangeOveride=[]):
def GetLookupTableNames():
"""Returns a list containing the currently available transfer function
presets."""
presets = servermanager.vtkSMTransferFunctionPresets()
presets = servermanager.vtkSMTransferFunctionPresets.GetInstance()
return [presets.GetPresetName(index) for index in range(presets.GetNumberOfPresets())]
# -----------------------------------------------------------------------------
......@@ -1625,7 +1625,7 @@ def LoadLookupTable(fileName):
If the filename ends with a .xml, it's assumed to be a legacy color map XML
and will be converted to the new format before processing.
"""
presets = servermanager.vtkSMTransferFunctionPresets()
presets = servermanager.vtkSMTransferFunctionPresets.GetInstance()
return presets.ImportPresets(fileName)
# -----------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment