OpenShot Library | libopenshot  0.2.4
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 "../include/Coordinate.h"
32 
33 using namespace std;
34 using namespace openshot;
35 
36 // Default constructor for a coordinate, which defaults the X and Y to zero (0,0)
37 Coordinate::Coordinate() :
38  X(0), Y(0) {
39 }
40 
41 // Constructor which also allows the user to set the X and Y
42 Coordinate::Coordinate(double x, double y) :
43  X(x), Y(y) {
44 }
45 
46 
47 // Generate JSON string of this object
48 std::string Coordinate::Json() {
49 
50  // Return formatted string
51  return JsonValue().toStyledString();
52 }
53 
54 // Generate Json::JsonValue for this object
55 Json::Value Coordinate::JsonValue() {
56 
57  // Create root json object
58  Json::Value root;
59  root["X"] = X;
60  root["Y"] = Y;
61  //root["increasing"] = increasing;
62  //root["repeated"] = Json::Value(Json::objectValue);
63  //root["repeated"]["num"] = repeated.num;
64  //root["repeated"]["den"] = repeated.den;
65  //root["delta"] = delta;
66 
67  // return JsonValue
68  return root;
69 }
70 
71 // Load JSON string into this object
72 void Coordinate::SetJson(std::string value) {
73 
74  // Parse JSON string into JSON objects
75  Json::Value root;
76  Json::CharReaderBuilder rbuilder;
77  Json::CharReader* reader(rbuilder.newCharReader());
78 
79  std::string errors;
80  bool success = reader->parse( value.c_str(),
81  value.c_str() + value.size(), &root, &errors );
82  delete reader;
83 
84  if (!success)
85  // Raise exception
86  throw InvalidJSON("JSON could not be parsed (or is invalid)");
87 
88  try
89  {
90  // Set all values that match
91  SetJsonValue(root);
92  }
93  catch (const std::exception& e)
94  {
95  // Error parsing JSON (or missing keys)
96  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
97  }
98 }
99 
100 // Load Json::JsonValue into this object
101 void Coordinate::SetJsonValue(Json::Value root) {
102 
103  // Set data from Json (if key is found)
104  if (!root["X"].isNull())
105  X = root["X"].asDouble();
106  if (!root["Y"].isNull())
107  Y = root["Y"].asDouble();
108 }
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Coordinate.cpp:101
STL namespace.
void SetJson(std::string value)
Load JSON string into this object.
Definition: Coordinate.cpp:72
double Y
The Y value of the coordinate (usually representing the value of the property being animated) ...
Definition: Coordinate.h:58
double X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:57
This namespace is the default namespace for all code in the openshot library.
Coordinate()
The default constructor, which defaults to (0,0)
Definition: Coordinate.cpp:37
Exception for invalid JSON.
Definition: Exceptions.h:205
std::string Json()
Get and Set JSON methods.
Definition: Coordinate.cpp:48
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Coordinate.cpp:55