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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* LICENSE
|
2013-09-12 17:52:10 -05:00
|
|
|
*
|
2019-06-11 06:48:32 -04:00
|
|
|
* Copyright (c) 2008-2019 OpenShot Studios, LLC
|
2014-03-29 18:49:22 -05:00
|
|
|
* <http://www.openshotstudios.com/>. This file is part of
|
|
|
|
|
* OpenShot Library (libopenshot), an open-source project dedicated to
|
|
|
|
|
* delivering high quality video editing and animation solutions to the
|
|
|
|
|
* world. For more information visit <http://www.openshot.org/>.
|
2013-09-12 17:52:10 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2013-09-12 17:52:10 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is distributed in the hope that it will be
|
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-12 17:52:10 -05:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
2011-10-11 08:44:27 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "../include/Coordinate.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
|
|
|
|
// Default constructor for a coordinate, which defaults the X and Y to zero (0,0)
|
|
|
|
|
Coordinate::Coordinate() :
|
2019-03-06 15:35:03 -06:00
|
|
|
X(0), Y(0) {
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Constructor which also allows the user to set the X and Y
|
2017-01-24 18:39:17 -06:00
|
|
|
Coordinate::Coordinate(double x, double y) :
|
2019-03-06 15:35:03 -06:00
|
|
|
X(x), Y(y) {
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate JSON string of this object
|
2019-08-04 22:48:57 -04:00
|
|
|
std::string Coordinate::Json() {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Return formatted string
|
|
|
|
|
return JsonValue().toStyledString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate Json::JsonValue for this object
|
|
|
|
|
Json::Value Coordinate::JsonValue() {
|
|
|
|
|
|
|
|
|
|
// 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-08-04 22:48:57 -04:00
|
|
|
void Coordinate::SetJson(std::string value) {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Parse JSON string into JSON objects
|
|
|
|
|
Json::Value root;
|
2019-06-19 21:20:04 -04:00
|
|
|
Json::CharReaderBuilder rbuilder;
|
|
|
|
|
Json::CharReader* reader(rbuilder.newCharReader());
|
|
|
|
|
|
2019-08-04 22:48:57 -04:00
|
|
|
std::string errors;
|
2019-07-11 05:00:47 -04:00
|
|
|
bool success = reader->parse( value.c_str(),
|
2019-06-19 21:20:04 -04:00
|
|
|
value.c_str() + value.size(), &root, &errors );
|
2019-07-11 05:00:47 -04:00
|
|
|
delete reader;
|
|
|
|
|
|
2013-12-06 00:40:26 -06:00
|
|
|
if (!success)
|
|
|
|
|
// Raise exception
|
2019-08-27 15:47:39 -04:00
|
|
|
throw InvalidJSON("JSON could not be parsed (or is invalid)");
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load Json::JsonValue into this object
|
2013-12-07 21:09:55 -06:00
|
|
|
void Coordinate::SetJsonValue(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();
|
|
|
|
|
}
|