2011-10-11 08:44:27 -05:00
|
|
|
/**
|
2013-09-09 23:32:16 -05:00
|
|
|
* @file
|
2013-09-12 17:52:10 -05:00
|
|
|
* @brief Source file for Coordinate class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "Coordinate.h"
|
2020-11-21 15:19:08 -05:00
|
|
|
#include "Exceptions.h"
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2020-12-04 10:04:56 -05:00
|
|
|
// Default constructor for a coordinate, delegating to the full signature
|
2021-08-09 09:54:59 -04:00
|
|
|
Coordinate::Coordinate() : Coordinate::Coordinate(0, 0) {}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Constructor which also allows the user to set the X and Y
|
2021-08-09 09:54:59 -04:00
|
|
|
Coordinate::Coordinate(double x, double y) : X(x), Y(y) {}
|
2013-12-06 00:40:26 -06:00
|
|
|
|
2020-12-04 10:04:56 -05:00
|
|
|
// Constructor which accepts a std::pair for (X, Y)
|
2020-12-04 11:27:18 -05:00
|
|
|
Coordinate::Coordinate(const std::pair<double, double>& co)
|
2021-08-09 09:54:59 -04:00
|
|
|
: X(co.first), Y(co.second) {}
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Generate JSON string of this object
|
2019-12-27 08:51:51 -05:00
|
|
|
std::string Coordinate::Json() const {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Return formatted string
|
|
|
|
|
return JsonValue().toStyledString();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Generate Json::Value for this object
|
|
|
|
|
Json::Value Coordinate::JsonValue() const {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Create root json object
|
|
|
|
|
Json::Value root;
|
|
|
|
|
root["X"] = X;
|
|
|
|
|
root["Y"] = Y;
|
2014-01-05 23:28:21 -06:00
|
|
|
//root["increasing"] = increasing;
|
|
|
|
|
//root["repeated"] = Json::Value(Json::objectValue);
|
|
|
|
|
//root["repeated"]["num"] = repeated.num;
|
|
|
|
|
//root["repeated"]["den"] = repeated.den;
|
|
|
|
|
//root["delta"] = delta;
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// return JsonValue
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load JSON string into this object
|
2019-12-27 08:51:51 -05:00
|
|
|
void Coordinate::SetJson(const std::string value) {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Parse JSON string into JSON objects
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-27 08:51:51 -05:00
|
|
|
const Json::Value root = openshot::stringToJson(value);
|
2013-12-06 00:40:26 -06:00
|
|
|
// Set all values that match
|
2013-12-07 21:09:55 -06:00
|
|
|
SetJsonValue(root);
|
2013-12-06 00:40:26 -06:00
|
|
|
}
|
2019-07-03 12:58:02 -04:00
|
|
|
catch (const std::exception& e)
|
2013-12-06 00:40:26 -06:00
|
|
|
{
|
|
|
|
|
// Error parsing JSON (or missing keys)
|
2019-08-27 15:47:39 -04:00
|
|
|
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
|
2013-12-06 00:40:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Load Json::Value into this object
|
|
|
|
|
void Coordinate::SetJsonValue(const Json::Value root) {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Set data from Json (if key is found)
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["X"].isNull())
|
2013-12-06 00:40:26 -06:00
|
|
|
X = root["X"].asDouble();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["Y"].isNull())
|
2013-12-06 00:40:26 -06:00
|
|
|
Y = root["Y"].asDouble();
|
|
|
|
|
}
|