OpenShot Library | libopenshot  0.2.6
Coordinate.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Coordinate class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "Coordinate.h"
32 #include "Exceptions.h"
33 
34 using namespace openshot;
35 
36 // Default constructor for a coordinate, delegating to the full signature
38 
39 // Constructor which also allows the user to set the X and Y
40 Coordinate::Coordinate(double x, double y) : X(x), Y(y) {};
41 
42 // Constructor which accepts a std::pair for (X, Y)
43 Coordinate::Coordinate(const std::pair<double, double>& co)
44  : X(co.first), Y(co.second) {};
45 
46 // Generate JSON string of this object
47 std::string Coordinate::Json() const {
48 
49  // Return formatted string
50  return JsonValue().toStyledString();
51 }
52 
53 // Generate Json::Value for this object
54 Json::Value Coordinate::JsonValue() const {
55 
56  // Create root json object
57  Json::Value root;
58  root["X"] = X;
59  root["Y"] = Y;
60  //root["increasing"] = increasing;
61  //root["repeated"] = Json::Value(Json::objectValue);
62  //root["repeated"]["num"] = repeated.num;
63  //root["repeated"]["den"] = repeated.den;
64  //root["delta"] = delta;
65 
66  // return JsonValue
67  return root;
68 }
69 
70 // Load JSON string into this object
71 void Coordinate::SetJson(const std::string value) {
72 
73  // Parse JSON string into JSON objects
74  try
75  {
76  const Json::Value root = openshot::stringToJson(value);
77  // Set all values that match
78  SetJsonValue(root);
79  }
80  catch (const std::exception& e)
81  {
82  // Error parsing JSON (or missing keys)
83  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
84  }
85 }
86 
87 // Load Json::Value into this object
88 void Coordinate::SetJsonValue(const Json::Value root) {
89 
90  // Set data from Json (if key is found)
91  if (!root["X"].isNull())
92  X = root["X"].asDouble();
93  if (!root["Y"].isNull())
94  Y = root["Y"].asDouble();
95 }
This class represents a Cartesian coordinate (X, Y) used in the Keyframe animation system...
Definition: Coordinate.h:54
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Coordinate.cpp:54
Header file for all Exception classes.
double Y
The Y value of the coordinate (usually representing the value of the property being animated) ...
Definition: Coordinate.h:57
double X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:56
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Coordinate.cpp:88
Header file for Coordinate class.
Coordinate()
The default constructor, which defaults to (0,0)
Definition: Coordinate.cpp:37
Exception for invalid JSON.
Definition: Exceptions.h:205
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Coordinate.cpp:71
std::string Json() const
Generate JSON string of this object.
Definition: Coordinate.cpp:47