Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2008 Emweb bv, Herent, Belgium. |
3 | | * |
4 | | * See the LICENSE file for terms of use. |
5 | | */ |
6 | | |
7 | | #include "Wt/WPointF.h" |
8 | | |
9 | | #include "Wt/WLogger.h" |
10 | | #include "Wt/WStringStream.h" |
11 | | |
12 | | #include "Wt/Json/Array.h" |
13 | | #include "Wt/Json/Value.h" |
14 | | |
15 | | #include "web/WebUtils.h" |
16 | | |
17 | | namespace Wt { |
18 | | |
19 | | LOGGER("WPointF"); |
20 | | |
21 | | WPointF::WPointF() |
22 | 0 | : x_(0), y_(0) |
23 | 0 | { } |
24 | | |
25 | | WPointF& WPointF::operator= (const WPointF& rhs) |
26 | 0 | { |
27 | 0 | WJavaScriptExposableObject::operator=(rhs); |
28 | |
|
29 | 0 | x_ = rhs.x(); |
30 | 0 | y_ = rhs.y(); |
31 | |
|
32 | 0 | return *this; |
33 | 0 | } |
34 | | |
35 | | #ifdef WT_TARGET_JAVA |
36 | | WPointF WPointF::clone() const |
37 | | { |
38 | | return WPointF(*this); |
39 | | } |
40 | | #endif |
41 | | |
42 | | void WPointF::setX(double x) |
43 | 0 | { |
44 | 0 | checkModifiable(); |
45 | 0 | x_ = x; |
46 | 0 | } |
47 | | |
48 | | void WPointF::setY(double y) |
49 | 0 | { |
50 | 0 | checkModifiable(); |
51 | 0 | y_ = y; |
52 | 0 | } |
53 | | |
54 | | bool WPointF::operator== (const WPointF& other) const |
55 | 0 | { |
56 | 0 | if (!sameBindingAs(other)) return false; |
57 | | |
58 | 0 | return (x_ == other.x_) && (y_ == other.y_); |
59 | 0 | } |
60 | | |
61 | | bool WPointF::operator!= (const WPointF& other) const |
62 | 0 | { |
63 | 0 | return !(*this == other); |
64 | 0 | } |
65 | | |
66 | | WPointF& WPointF::operator+= (const WPointF& other) |
67 | 0 | { |
68 | 0 | checkModifiable(); |
69 | |
|
70 | 0 | x_ += other.x_; |
71 | 0 | y_ += other.y_; |
72 | |
|
73 | 0 | return *this; |
74 | 0 | } |
75 | | |
76 | | std::string WPointF::jsValue() const |
77 | 0 | { |
78 | 0 | char buf[30]; |
79 | 0 | WStringStream ss; |
80 | 0 | ss << '['; |
81 | 0 | ss << Utils::round_js_str(x_, 3, buf) << ','; |
82 | 0 | ss << Utils::round_js_str(y_, 3, buf) << ']'; |
83 | 0 | return ss.str(); |
84 | 0 | } |
85 | | |
86 | | WPointF WPointF::swapHV(double width) const |
87 | 0 | { |
88 | 0 | WPointF result(width - y(), x()); |
89 | |
|
90 | 0 | if (isJavaScriptBound()) { |
91 | 0 | WStringStream ss; |
92 | 0 | char buf[30]; |
93 | 0 | ss << "((function(p){return ["; |
94 | 0 | ss << Utils::round_js_str(width, 3, buf) << "-p[1],p[0]];})(" << jsRef() + "))"; |
95 | 0 | result.assignBinding(*this, ss.str()); |
96 | 0 | } |
97 | |
|
98 | 0 | return result; |
99 | 0 | } |
100 | | |
101 | | WPointF WPointF::inverseSwapHV(double width) const |
102 | 0 | { |
103 | 0 | WPointF result(y(), width - x()); |
104 | |
|
105 | 0 | if (isJavaScriptBound()) { |
106 | 0 | WStringStream ss; |
107 | 0 | char buf[30]; |
108 | 0 | ss << "((function(p){return ["; |
109 | 0 | ss << "p[1]," << Utils::round_js_str(width, 3, buf) << "-p[0]];})(" << jsRef() + "))"; |
110 | 0 | result.assignBinding(*this, ss.str()); |
111 | 0 | } |
112 | |
|
113 | 0 | return result; |
114 | 0 | } |
115 | | |
116 | | void WPointF::assignFromJSON(const Json::Value &value) |
117 | 0 | { |
118 | 0 | try { |
119 | 0 | #ifndef WT_TARGET_JAVA |
120 | 0 | const Json::Array &ar = value; |
121 | | #else |
122 | | const Json::Array &ar = static_cast<Json::Array&>(value); |
123 | | #endif |
124 | 0 | if (ar.size() == 2 && |
125 | 0 | !ar[0].toNumber().isNull() && |
126 | 0 | !ar[1].toNumber().isNull()) { |
127 | 0 | x_ = ar[0].toNumber().orIfNull(x_); |
128 | 0 | y_ = ar[1].toNumber().orIfNull(y_); |
129 | 0 | } else { |
130 | 0 | LOG_ERROR("Couldn't convert JSON to WPointF"); |
131 | 0 | } |
132 | 0 | } catch (std::exception &e) { |
133 | 0 | LOG_ERROR("Couldn't convert JSON to WPointF: " + std::string(e.what())); |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | | } |