LeechCraft Monocle 0.6.70-17609-g3dde4097dd
Modular document viewer for LeechCraft
Loading...
Searching...
No Matches
coordsbase.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <QPointF>
12#include <QRectF>
13
14namespace LC::Monocle
15{
23
24 template<Relativity R>
25 struct Pos
26 {
27 static constexpr auto Relativity = R;
28
29 using Type = QPointF;
30
31 Type P_ {};
32
33 Pos () = default;
34
35 explicit Pos (Type p)
36 : P_ { p }
37 {
38 }
39
40 explicit Pos (qreal x, qreal y)
41 : P_ { x, y }
42 {
43 }
44
45 auto operator<=> (const Pos&) const = default;
46
47 template<typename Self>
48 [[nodiscard]]
49 auto ClearedX (this const Self& self)
50 {
51 return Self { 0, self.P_.y () };
52 }
53
54 template<typename Self>
55 [[nodiscard]]
56 auto ClearedY (this const Self& self)
57 {
58 return Self { self.P_.x (), 0 };
59 }
60
61 template<typename Self>
62 [[nodiscard]]
63 Self Shifted (this const Self& self, qreal dx, qreal dy)
64 {
65 return Self { self.P_ + Type { dx, dy } };
66 }
67
68 Type ToPointF () const
69 {
70 return P_;
71 }
72
73 template<typename Self>
74 Self operator+ (this Self p1, Self p2)
75 {
76 return Self { p1.P_ + p2.P_ };
77 }
78
79 template<typename Self>
80 Self operator- (this Self p1, Self p2)
81 {
82 return Self { p1.P_ - p2.P_ };
83 }
84
85 template<typename Self>
86 Self operator* (this Self p, qreal factor)
87 {
88 return Self { p.P_ * factor };
89 }
90
91 template<typename Self>
92 Self operator/ (this Self p, qreal factor)
93 {
94 return Self { p.P_ / factor };
95 }
96
97 bool BothGeqThan (Pos p) const
98 {
99 return P_.x () >= p.P_.x () && P_.y () >= p.P_.y ();
100 }
101
102 bool BothLeqThan (Pos p) const
103 {
104 return P_.x () <= p.P_.x () && P_.y () <= p.P_.y ();
105 }
106 };
107
108 template<Relativity R>
109 struct Rect
110 {
111 static constexpr auto Relativity = R;
112
113 using Type = QRectF;
114
116
117 Rect () = default;
118
119 explicit Rect (const Type& r)
120 : R_ { r }
121 {
122 }
123
124 explicit Rect (Pos<R> topLeft, Pos<R> bottomRight)
125 : R_ { topLeft.ToPointF (), bottomRight.ToPointF () }
126 {
127 }
128
129 auto operator<=> (const Rect&) const = default;
130
131 template<typename P>
132 requires (P::Relativity == R)
133 P TopLeft () const
134 {
135 return P { R_.topLeft () };
136 }
137
138 template<typename P>
139 requires (P::Relativity == R)
140 P BottomRight () const
141 {
142 return P { R_.bottomRight () };
143 }
144
145 void SetLeft (qreal x)
146 {
147 R_.setLeft (x);
148 }
149
150 void SetRight (qreal x)
151 {
152 R_.setRight (x);
153 }
154
155 Type ToRectF () const
156 {
157 return R_;
158 }
159
160 bool IsEmpty () const
161 {
162 return R_.isEmpty ();
163 }
164
165 template<typename Self>
166 Self operator| (this const Self& r1, const Self& r2)
167 {
168 return Self { r1.R_ | r2.R_ };
169 }
170
171 template<typename Self>
172 Self operator& (this const Self& r1, const Self& r2)
173 {
174 return Self { r1.R_ & r2.R_ };
175 }
176 };
177
178 struct PageRelativePosBase;
179 struct PageAbsolutePosBase;
180
181 struct PageRelativePosBase : Pos<Relativity::PageRelative>
182 {
183 using Pos::Pos;
184
185 PageAbsolutePosBase ToPageAbsolute (QSizeF) const;
186 };
187
188 struct PageAbsolutePosBase : Pos<Relativity::PageAbsolute>
189 {
190 using Pos::Pos;
191
192 PageRelativePosBase ToPageRelative (QSizeF) const;
193 };
194
196 {
197 return PageAbsolutePosBase { P_.x () * size.width (), P_.y () * size.height () };
198 }
199
201 {
202 return PageRelativePosBase { P_.x () / size.width (), P_.y () / size.height () };
203 }
204
207
208 struct PageRelativeRectBase : Rect<Relativity::PageRelative>
209 {
210 using Rect::Rect;
211
213 };
214
215 struct PageAbsoluteRectBase : Rect<Relativity::PageAbsolute>
216 {
217 using Rect::Rect;
218
220 };
221
222 namespace detail
223 {
224 template<typename Target, typename SrcPos, typename TargetPos, typename Source, typename Ctx>
225 Target Convert (TargetPos (SrcPos::*posConvert) (Ctx) const, const Source& src, const auto& context)
226 {
227 return Target
228 {
229 (src.template TopLeft<SrcPos> ().*posConvert) (context),
230 (src.template BottomRight<SrcPos> ().*posConvert) (context)
231 };
232 }
233 }
234
239
244}
245
246template<LC::Monocle::Relativity R>
247class QTypeInfo<LC::Monocle::Pos<R>> : public QTypeInfo<QPointF> {};
248
249template<LC::Monocle::Relativity R>
250class QTypeInfo<LC::Monocle::Rect<R>> : public QTypeInfo<QRectF> {};
251
252template<>
253class QTypeInfo<LC::Monocle::PageRelativePosBase> : public QTypeInfo<QPointF> {};
254template<>
255class QTypeInfo<LC::Monocle::PageAbsolutePosBase> : public QTypeInfo<QPointF> {};
256
257template<>
258class QTypeInfo<LC::Monocle::PageRelativeRectBase> : public QTypeInfo<QRectF> {};
259template<>
260class QTypeInfo<LC::Monocle::PageAbsoluteRectBase> : public QTypeInfo<QRectF> {};
Target Convert(TargetPos(SrcPos::*posConvert)(Ctx) const, const Source &src, const auto &context)
Definition coordsbase.h:225
PageRelativePosBase ToPageRelative(QSizeF) const
Definition coordsbase.h:200
PageRelativeRectBase ToPageRelative(QSizeF) const
Definition coordsbase.h:240
PageAbsolutePosBase ToPageAbsolute(QSizeF) const
Definition coordsbase.h:195
PageAbsoluteRectBase ToPageAbsolute(QSizeF) const
Definition coordsbase.h:235
bool BothGeqThan(Pos p) const
Definition coordsbase.h:97
bool BothLeqThan(Pos p) const
Definition coordsbase.h:102
auto ClearedY(this const Self &self)
Definition coordsbase.h:56
Type ToPointF() const
Definition coordsbase.h:68
auto ClearedX(this const Self &self)
Definition coordsbase.h:49
Pos(qreal x, qreal y)
Definition coordsbase.h:40
Self operator/(this Self p, qreal factor)
Definition coordsbase.h:92
Self operator-(this Self p1, Self p2)
Definition coordsbase.h:80
Self operator+(this Self p1, Self p2)
Definition coordsbase.h:74
Self operator*(this Self p, qreal factor)
Definition coordsbase.h:86
auto operator<=>(const Pos &) const =default
static constexpr auto Relativity
Definition coordsbase.h:27
Self Shifted(this const Self &self, qreal dx, qreal dy)
Definition coordsbase.h:63
P BottomRight() const
Definition coordsbase.h:140
static constexpr auto Relativity
Definition coordsbase.h:111
void SetRight(qreal x)
Definition coordsbase.h:150
P TopLeft() const
Definition coordsbase.h:133
bool IsEmpty() const
Definition coordsbase.h:160
Rect(const Type &r)
Definition coordsbase.h:119
Rect(Pos< R > topLeft, Pos< R > bottomRight)
Definition coordsbase.h:124
auto operator<=>(const Rect &) const =default
Type ToRectF() const
Definition coordsbase.h:155
Self operator|(this const Self &r1, const Self &r2)
Definition coordsbase.h:166
Self operator&(this const Self &r1, const Self &r2)
Definition coordsbase.h:172
void SetLeft(qreal x)
Definition coordsbase.h:145