OpenShot Library | libopenshot  0.1.9
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../include/Coordinate.h"
29 
30 using namespace std;
31 using namespace openshot;
32 
33 // Default constructor for a coordinate, which defaults the X and Y to zero (0,0)
34 Coordinate::Coordinate() :
35  X(0), Y(0), increasing(true), repeated(1,1), delta(0.0) {
36 }
37 
38 // Constructor which also allows the user to set the X and Y
39 Coordinate::Coordinate(double x, double y) :
40  X(x), Y(y), increasing(true), repeated(1,1), delta(0.0) {
41 }
42 
43 
44 // Generate JSON string of this object
45 string Coordinate::Json() {
46 
47  // Return formatted string
48  return JsonValue().toStyledString();
49 }
50 
51 // Generate Json::JsonValue for this object
52 Json::Value Coordinate::JsonValue() {
53 
54  // Create root json object
55  Json::Value root;
56  root["X"] = X;
57  root["Y"] = Y;
58  //root["increasing"] = increasing;
59  //root["repeated"] = Json::Value(Json::objectValue);
60  //root["repeated"]["num"] = repeated.num;
61  //root["repeated"]["den"] = repeated.den;
62  //root["delta"] = delta;
63 
64  // return JsonValue
65  return root;
66 }
67 
68 // Load JSON string into this object
69 void Coordinate::SetJson(string value) {
70 
71  // Parse JSON string into JSON objects
72  Json::Value root;
73  Json::Reader reader;
74  bool success = reader.parse( value, root );
75  if (!success)
76  // Raise exception
77  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
78 
79  try
80  {
81  // Set all values that match
82  SetJsonValue(root);
83  }
84  catch (exception e)
85  {
86  // Error parsing JSON (or missing keys)
87  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
88  }
89 }
90 
91 // Load Json::JsonValue into this object
92 void Coordinate::SetJsonValue(Json::Value root) {
93 
94  // Set data from Json (if key is found)
95  if (!root["X"].isNull())
96  X = root["X"].asDouble();
97  if (!root["Y"].isNull())
98  Y = root["Y"].asDouble();
99  if (!root["increasing"].isNull())
100  increasing = root["increasing"].asBool();
101  if (!root["repeated"].isNull() && root["repeated"].isObject())
102  {
103  if (!root["repeated"]["num"].isNull())
104  repeated.num = root["repeated"]["num"].asInt();
105  if (!root["repeated"]["den"].isNull())
106  repeated.den = root["repeated"]["den"].asInt();
107  }
108  if (!root["delta"].isNull())
109  delta = root["delta"].asDouble();
110 }
int num
Numerator for the fraction.
Definition: Fraction.h:44
string Json()
Get and Set JSON methods.
Definition: Coordinate.cpp:45
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Coordinate.cpp:92
double Y
The Y value of the coordinate (usually representing the value of the property being animated) ...
Definition: Coordinate.h:62
double X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:61
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:34
Exception for invalid JSON.
Definition: Exceptions.h:152
void SetJson(string value)
Load JSON string into this object.
Definition: Coordinate.cpp:69
int den
Denominator for the fraction.
Definition: Fraction.h:45
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Coordinate.cpp:52