Commit 0000c0c3 authored by Nicolas Vuaille's avatar Nicolas Vuaille

vtkSMTransferFunctionPresets become a singleton

Rationale : it was possible to have multiple instance at some point.
(typically, the pqChoosePresetReaction, through a pqSavePresetDialog, has a long-live
vtkSMTransferFunctionPresets instance)
But as they used to ignore each other and write the preset file in the destructor,
modifying both instance was not well supported (in fact, file will represent the last destroyed one).

Now it is a singleton, so no more sync problem. Two new methods were added to force the writing
(`SaveToFile()`) and the reading (`ReloadPresets()`) of the preset file, if needed.
parent efedc372
......@@ -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;
......
......@@ -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