Commit 7233d309 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

configure plot axis properties

parent ef2ef4ee
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#pragma once
#include <unordered_map>
#include "utils/return_code.h"
#include "element.h"
#include "element_spec.h"

namespace signaltk {

template <typename T>
using PropertyDefinitions = std::unordered_map<
    std::string,
    std::function<ReturnCode (const std::string&, T*)>>;

template<typename T>
ReturnCode configureProperties(
    const PropertyList& plist,
    const PropertyDefinitions<T>& pdefs,
    T* config) {
  for (const auto& prop : plist.properties) {
    const auto& pdef = pdefs.find(prop.first);
    if (pdef != pdefs.end()) {
      if (auto rc = pdef->second(prop.second, config); !rc.isSuccess()) {
        return rc;
      }
    }
  }

  return ReturnCode::success();
}

} // namespace signaltk
+32 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
 * <http://www.gnu.org/licenses/>.
 */
#include "elements/plot/plot_element.h"
#include "elements/element_config_helpers.h"

namespace signaltk {

@@ -48,7 +49,38 @@ ReturnCode renderPlot(const PlotConfig& config, Layer* frame) {
  return ReturnCode::success();
}

ReturnCode configurePlotAxisTop(const std::string& value, PlotConfig* config) {
  config->axis_top_enabled = (value == "on");
  return ReturnCode::success();
}

ReturnCode configurePlotAxisRight(const std::string& value, PlotConfig* config) {
  config->axis_right_enabled = (value == "on");
  return ReturnCode::success();
}

ReturnCode configurePlotAxisBottom(const std::string& value, PlotConfig* config) {
  config->axis_bottom_enabled = (value == "on");
  return ReturnCode::success();
}

ReturnCode configurePlotAxisLeft(const std::string& value, PlotConfig* config) {
  config->axis_left_enabled = (value == "on");
  return ReturnCode::success();
}

ReturnCode configurePlot(const PropertyList& plist, PlotConfig* config) {
  static const PropertyDefinitions<PlotConfig> pdefs = {
    {"axis-top", &configurePlotAxisTop},
    {"axis-right", &configurePlotAxisRight},
    {"axis-bottom", &configurePlotAxisBottom},
    {"axis-left", &configurePlotAxisLeft},
  };

  if (auto rc = configureProperties(plist, pdefs, config); !rc.isSuccess()) {
    return rc;
  }

  return ReturnCode::success();
}