color_map.cpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #include "color_map.h"
8 #include <QSettings>
9 
11 {
12  _lua_function = {};
13  _lua_engine = {};
15  auto func = QString("function ColorMap(v)\n"
16  "%1\n"
17  "end\n")
18  .arg(text);
19  auto result = _lua_engine.safe_script(func.toStdString());
20  if (result.valid())
21  {
22  _script = text;
23  _lua_function = _lua_engine["ColorMap"];
24  }
25 
26  return result;
27 }
28 
29 QString ColorMap::getError(sol::error err) const
30 {
31  return QString(err.what());
32 }
33 
34 QColor ColorMap::mapColor(double value) const
35 {
36  auto res = _lua_function(value);
37  if (!res.valid())
38  {
39  return Qt::transparent;
40  }
41  if (res.return_count() == 1 && res.get_type(0) == sol::type::string)
42  {
43  return QColor(res.get<std::string>(0).c_str());
44  }
45  return Qt::transparent;
46 }
47 
48 std::map<QString, ColorMap::Ptr>& ColorMapLibrary()
49 {
50  static std::map<QString, ColorMap::Ptr> colormaps;
51  return colormaps;
52 }
53 
55 {
56  QSettings settings;
57  QMap<QString, QVariant> colormap_text;
58  for (const auto& it : ColorMapLibrary())
59  {
60  colormap_text.insert(it.first, it.second->script());
61  }
62  settings.setValue("ColorMapLibrary", colormap_text);
63 }
64 
66 {
67  QSettings settings;
68 
69  ColorMapLibrary().clear();
70 
71  QMap<QString, QVariant> colormap_text = settings.value("ColorMapLibrary").toMap();
72  for (const auto& key : colormap_text.keys())
73  {
74  QString script = colormap_text[key].toString();
75  auto colormap = std::make_shared<ColorMap>();
76  auto res = colormap->setScrip(script);
77  if (res.valid())
78  {
79  ColorMapLibrary().insert({ key, colormap });
80  }
81  }
82 }
sol::error::what
virtual const char * what() const noexcept override
Definition: sol.hpp:4593
color_map.h
LoadColorMapFromSettings
void LoadColorMapFromSettings()
Definition: color_map.cpp:65
sol::protected_function_result
Definition: sol.hpp:17228
sol::type::string
@ string
ColorMap::getError
QString getError(sol::error err) const
Definition: color_map.cpp:29
ColorMap::_lua_engine
sol::state _lua_engine
Definition: color_map.h:33
sol::state_view::safe_script
protected_function_result safe_script(lua_Reader reader, void *data, Fx &&on_error, const std::string &chunkname=detail::default_chunk_name(), load_mode mode=load_mode::any)
Definition: sol.hpp:27564
ColorMap::_lua_function
sol::protected_function _lua_function
Definition: color_map.h:34
QMap
Definition: qwt_compass.h:19
SaveColorMapToSettings
void SaveColorMapToSettings()
Definition: color_map.cpp:54
ColorMap::setScrip
sol::protected_function_result setScrip(QString script)
Definition: color_map.cpp:10
sol::error
Definition: sol.hpp:4573
ColorMapLibrary
std::map< QString, ColorMap::Ptr > & ColorMapLibrary()
Definition: color_map.cpp:48
sol::state_view::open_libraries
void open_libraries(Args &&... args)
Definition: sol.hpp:27320
ColorMap::mapColor
QColor mapColor(double value) const
Definition: color_map.cpp:34
ColorMap::_script
QString _script
Definition: color_map.h:35
sol::stack::script
void script(lua_State *L, lua_Reader reader, void *data, const std::string &chunkname=detail::default_chunk_name(), load_mode mode=load_mode::any)
Definition: sol.hpp:16651


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:22