1
// vim:expandtab:shiftwidth=2:tabstop=2:
2
// Copyright (C) 2015-2016 Canonical Ltd.
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
// Lesser General Public License for more details.
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this library; if not, write to the Free Software
16
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
#include "oxide_qquick_touch_handle_drawable.h"
22
#include <QQmlComponent>
23
#include <QQmlContext>
27
#include "qt/quick/api/oxideqquicktouchselectioncontroller_p.h"
28
#include "qt/quick/api/oxideqquickwebview_p.h"
29
#include "qt/quick/api/oxideqquickwebview_p_p.h"
34
class TouchHandleDrawableContext : public QObject {
36
Q_PROPERTY(OxideQQuickTouchSelectionController::HandleOrientation orientation READ orientation NOTIFY orientationChanged FINAL)
37
Q_PROPERTY(bool mirrorVertical READ mirrorVertical NOTIFY mirrorVerticalChanged FINAL)
38
Q_PROPERTY(bool mirrorHorizontal READ mirrorHorizontal NOTIFY mirrorHorizontalChanged FINAL)
39
Q_PROPERTY(qreal horizontalPaddingRatio READ horizontalPaddingRatio WRITE setHorizontalPaddingRatio NOTIFY horizontalPaddingRatioChanged)
42
virtual ~TouchHandleDrawableContext() {}
43
TouchHandleDrawableContext();
45
OxideQQuickTouchSelectionController::HandleOrientation orientation() const;
47
OxideQQuickTouchSelectionController::HandleOrientation orientation);
49
bool mirrorVertical() const;
50
void setMirrorVertical(bool mirror);
52
bool mirrorHorizontal() const;
53
void setMirrorHorizontal(bool mirror);
55
qreal horizontalPaddingRatio() const;
56
void setHorizontalPaddingRatio(qreal ratio);
59
void orientationChanged() const;
60
void mirrorVerticalChanged() const;
61
void mirrorHorizontalChanged() const;
62
void horizontalPaddingRatioChanged() const;
65
OxideQQuickTouchSelectionController::HandleOrientation orientation_;
66
bool mirror_vertical_;
67
bool mirror_horizontal_;
68
qreal horizontal_padding_ratio_;
71
TouchHandleDrawableContext::TouchHandleDrawableContext()
73
OxideQQuickTouchSelectionController::HandleOrientationUndefined)
74
, mirror_vertical_(false)
75
, mirror_horizontal_(false)
76
, horizontal_padding_ratio_(0.0) {}
78
OxideQQuickTouchSelectionController::HandleOrientation
79
TouchHandleDrawableContext::orientation() const {
83
void TouchHandleDrawableContext::setOrientation(
84
OxideQQuickTouchSelectionController::HandleOrientation orientation) {
85
if (orientation_ != orientation) {
86
orientation_ = orientation;
87
Q_EMIT orientationChanged();
91
bool TouchHandleDrawableContext::mirrorVertical() const {
92
return mirror_vertical_;
95
void TouchHandleDrawableContext::setMirrorVertical(bool mirror) {
96
if (mirror_vertical_ != mirror) {
97
mirror_vertical_ = mirror;
98
Q_EMIT mirrorVerticalChanged();
102
bool TouchHandleDrawableContext::mirrorHorizontal() const {
103
return mirror_horizontal_;
106
void TouchHandleDrawableContext::setMirrorHorizontal(bool mirror) {
107
if (mirror_horizontal_ != mirror) {
108
mirror_horizontal_ = mirror;
109
Q_EMIT mirrorHorizontalChanged();
113
qreal TouchHandleDrawableContext::horizontalPaddingRatio() const {
114
return horizontal_padding_ratio_;
117
void TouchHandleDrawableContext::setHorizontalPaddingRatio(qreal ratio) {
118
qreal bound = qBound(0.0, ratio, 1.0);
119
if (horizontal_padding_ratio_ != bound) {
120
horizontal_padding_ratio_ = bound;
121
Q_EMIT horizontalPaddingRatioChanged();
125
TouchHandleDrawable::~TouchHandleDrawable() {}
127
void TouchHandleDrawable::SetEnabled(bool enabled) {
128
if (!item_.isNull()) {
129
item_->setVisible(enabled);
133
void TouchHandleDrawable::SetOrientation(Orientation orientation,
134
bool mirror_vertical,
135
bool mirror_horizontal) {
136
if (!context_.isNull()) {
137
TouchHandleDrawableContext* context =
138
qobject_cast<TouchHandleDrawableContext*>(context_->contextObject());
140
OxideQQuickTouchSelectionController::HandleOrientation o;
141
switch (orientation) {
142
case Orientation::Left:
143
o = OxideQQuickTouchSelectionController::HandleOrientationLeft;
145
case Orientation::Center:
146
o = OxideQQuickTouchSelectionController::HandleOrientationCenter;
148
case Orientation::Right:
149
o = OxideQQuickTouchSelectionController::HandleOrientationRight;
151
case Orientation::Undefined:
152
o = OxideQQuickTouchSelectionController::HandleOrientationUndefined;
157
context->setOrientation(o);
158
context->setMirrorVertical(mirror_vertical);
159
context->setMirrorHorizontal(mirror_horizontal);
164
void TouchHandleDrawable::SetOrigin(const QPointF& origin) {
165
if (!item_.isNull()) {
166
item_->setX(origin.x());
167
item_->setY(origin.y());
171
void TouchHandleDrawable::SetAlpha(float alpha) {
172
if (!item_.isNull()) {
173
item_->setOpacity(alpha);
177
QRectF TouchHandleDrawable::GetVisibleBounds() const {
178
if (item_.isNull()) {
182
return QRectF(item_->x(), item_->y(), item_->width(), item_->height());
185
float TouchHandleDrawable::GetDrawableHorizontalPaddingRatio() const {
186
if (context_.isNull()) {
190
TouchHandleDrawableContext* context =
191
qobject_cast<TouchHandleDrawableContext*>(context_->contextObject());
192
return context->horizontalPaddingRatio();
195
void TouchHandleDrawable::instantiateComponent() {
196
if (view_.isNull()) {
198
"TouchHandleDrawable: "
199
"Can't instantiate after the view has gone";
203
TouchHandleDrawableContext* contextObject =
204
new TouchHandleDrawableContext();
205
QQmlComponent* component = view_->touchSelectionController()->handle();
208
"TouchHandleDrawable: Content requested a touch handle "
209
"drawable, but the application hasn't provided one";
210
delete contextObject;
214
QQmlContext* baseContext = component->creationContext();
216
baseContext = QQmlEngine::contextForObject(view_.data());
218
context_.reset(new QQmlContext(baseContext));
220
context_->setContextObject(contextObject);
221
contextObject->setParent(context_.data());
224
qobject_cast<QQuickItem*>(component->beginCreate(context_.data())));
227
"TouchHandleDrawable: Failed to create instance of "
228
"Qml touch selection handle component";
233
OxideQQuickWebViewPrivate::get(
234
view_.data())->addAttachedPropertyTo(item_.data());
235
item_->setParentItem(view_.data());
236
component->completeCreate();
239
TouchHandleDrawable::TouchHandleDrawable(OxideQQuickWebView* view)
241
instantiateComponent();
244
connect(view->touchSelectionController(), SIGNAL(handleChanged()),
245
SLOT(handleComponentChanged()));
249
void TouchHandleDrawable::handleComponentChanged() {
250
bool visible = item_.isNull() ? false : item_->isVisible();
251
OxideQQuickTouchSelectionController::HandleOrientation orientation =
252
OxideQQuickTouchSelectionController::HandleOrientationUndefined;
253
bool mirror_vertical = false;
254
bool mirror_horizontal = false;
255
if (!context_.isNull()) {
256
TouchHandleDrawableContext* context =
257
qobject_cast<TouchHandleDrawableContext*>(context_->contextObject());
258
orientation = context->orientation();
259
mirror_vertical = context->mirrorVertical();
260
mirror_horizontal = context->mirrorHorizontal();
262
QPointF position(item_.isNull() ? 0.0 : item_->x(),
263
item_.isNull() ? 0.0 : item_->y());
264
qreal opacity = item_.isNull() ? 0.0 : item_->opacity();
268
instantiateComponent();
270
if (!item_.isNull()) {
272
TouchHandleDrawableContext* context =
273
qobject_cast<TouchHandleDrawableContext*>(
274
context_->contextObject());
275
context->setOrientation(orientation);
276
context->setMirrorVertical(mirror_vertical);
277
context->setMirrorHorizontal(mirror_horizontal);
283
} // namespace qquick
286
#include "oxide_qquick_touch_handle_drawable.moc"