~osomon/oxide/override-touchUngrabEvent

« back to all changes in this revision

Viewing changes to qt/quick/oxide_qquick_touch_handle_drawable.cc

  • Committer: Olivier Tilloy
  • Date: 2016-01-12 11:46:34 UTC
  • mfrom: (1272.2.38 touch-selection-api)
  • Revision ID: olivier.tilloy@canonical.com-20160112114634-u1t78baa0u4hfoha
Add a touch selection API, to allow embedders to display handles for resizing the current selection, and contextual actions for it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim:expandtab:shiftwidth=2:tabstop=2:
 
2
// Copyright (C) 2015-2016 Canonical Ltd.
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
 
 
18
#include "oxide_qquick_touch_handle_drawable.h"
 
19
 
 
20
#include <QDebug>
 
21
#include <QObject>
 
22
#include <QQmlComponent>
 
23
#include <QQmlContext>
 
24
#include <QQmlEngine>
 
25
#include <QQuickItem>
 
26
 
 
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"
 
30
 
 
31
namespace oxide {
 
32
namespace qquick {
 
33
 
 
34
class TouchHandleDrawableContext : public QObject {
 
35
  Q_OBJECT
 
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)
 
40
 
 
41
 public:
 
42
  virtual ~TouchHandleDrawableContext() {}
 
43
  TouchHandleDrawableContext();
 
44
 
 
45
  OxideQQuickTouchSelectionController::HandleOrientation orientation() const;
 
46
  void setOrientation(
 
47
      OxideQQuickTouchSelectionController::HandleOrientation orientation);
 
48
 
 
49
  bool mirrorVertical() const;
 
50
  void setMirrorVertical(bool mirror);
 
51
 
 
52
  bool mirrorHorizontal() const;
 
53
  void setMirrorHorizontal(bool mirror);
 
54
 
 
55
  qreal horizontalPaddingRatio() const;
 
56
  void setHorizontalPaddingRatio(qreal ratio);
 
57
 
 
58
 Q_SIGNALS:
 
59
  void orientationChanged() const;
 
60
  void mirrorVerticalChanged() const;
 
61
  void mirrorHorizontalChanged() const;
 
62
  void horizontalPaddingRatioChanged() const;
 
63
 
 
64
 private:
 
65
  OxideQQuickTouchSelectionController::HandleOrientation orientation_;
 
66
  bool mirror_vertical_;
 
67
  bool mirror_horizontal_;
 
68
  qreal horizontal_padding_ratio_;
 
69
};
 
70
 
 
71
TouchHandleDrawableContext::TouchHandleDrawableContext()
 
72
    : orientation_(
 
73
        OxideQQuickTouchSelectionController::HandleOrientationUndefined)
 
74
    , mirror_vertical_(false)
 
75
    , mirror_horizontal_(false)
 
76
    , horizontal_padding_ratio_(0.0) {}
 
77
 
 
78
OxideQQuickTouchSelectionController::HandleOrientation
 
79
TouchHandleDrawableContext::orientation() const {
 
80
  return orientation_;
 
81
}
 
82
 
 
83
void TouchHandleDrawableContext::setOrientation(
 
84
    OxideQQuickTouchSelectionController::HandleOrientation orientation) {
 
85
  if (orientation_ != orientation) {
 
86
    orientation_ = orientation;
 
87
    Q_EMIT orientationChanged();
 
88
  }
 
89
}
 
90
 
 
91
bool TouchHandleDrawableContext::mirrorVertical() const {
 
92
  return mirror_vertical_;
 
93
}
 
94
 
 
95
void TouchHandleDrawableContext::setMirrorVertical(bool mirror) {
 
96
  if (mirror_vertical_ != mirror) {
 
97
    mirror_vertical_ = mirror;
 
98
    Q_EMIT mirrorVerticalChanged();
 
99
  }
 
100
}
 
101
 
 
102
bool TouchHandleDrawableContext::mirrorHorizontal() const {
 
103
  return mirror_horizontal_;
 
104
}
 
105
 
 
106
void TouchHandleDrawableContext::setMirrorHorizontal(bool mirror) {
 
107
  if (mirror_horizontal_ != mirror) {
 
108
    mirror_horizontal_ = mirror;
 
109
    Q_EMIT mirrorHorizontalChanged();
 
110
  }
 
111
}
 
112
 
 
113
qreal TouchHandleDrawableContext::horizontalPaddingRatio() const {
 
114
  return horizontal_padding_ratio_;
 
115
}
 
116
 
 
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();
 
122
  }
 
123
}
 
124
 
 
125
TouchHandleDrawable::~TouchHandleDrawable() {}
 
126
 
 
127
void TouchHandleDrawable::SetEnabled(bool enabled) {
 
128
  if (!item_.isNull()) {
 
129
    item_->setVisible(enabled);
 
130
  }
 
131
}
 
132
 
 
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());
 
139
    if (context) {
 
140
      OxideQQuickTouchSelectionController::HandleOrientation o;
 
141
      switch (orientation) {
 
142
        case Orientation::Left:
 
143
          o = OxideQQuickTouchSelectionController::HandleOrientationLeft;
 
144
          break;
 
145
        case Orientation::Center:
 
146
          o = OxideQQuickTouchSelectionController::HandleOrientationCenter;
 
147
          break;
 
148
        case Orientation::Right:
 
149
          o = OxideQQuickTouchSelectionController::HandleOrientationRight;
 
150
          break;
 
151
        case Orientation::Undefined:
 
152
          o = OxideQQuickTouchSelectionController::HandleOrientationUndefined;
 
153
          break;
 
154
        default:
 
155
          Q_UNREACHABLE();
 
156
      }
 
157
      context->setOrientation(o);
 
158
      context->setMirrorVertical(mirror_vertical);
 
159
      context->setMirrorHorizontal(mirror_horizontal);
 
160
    }
 
161
  }
 
162
}
 
163
 
 
164
void TouchHandleDrawable::SetOrigin(const QPointF& origin) {
 
165
  if (!item_.isNull()) {
 
166
    item_->setX(origin.x());
 
167
    item_->setY(origin.y());
 
168
  }
 
169
}
 
170
 
 
171
void TouchHandleDrawable::SetAlpha(float alpha) {
 
172
  if (!item_.isNull()) {
 
173
    item_->setOpacity(alpha);
 
174
  }
 
175
}
 
176
 
 
177
QRectF TouchHandleDrawable::GetVisibleBounds() const {
 
178
  if (item_.isNull()) {
 
179
    return QRectF();
 
180
  }
 
181
 
 
182
  return QRectF(item_->x(), item_->y(), item_->width(), item_->height());
 
183
}
 
184
 
 
185
float TouchHandleDrawable::GetDrawableHorizontalPaddingRatio() const {
 
186
  if (context_.isNull()) {
 
187
    return 0.0f;
 
188
  }
 
189
 
 
190
  TouchHandleDrawableContext* context =
 
191
      qobject_cast<TouchHandleDrawableContext*>(context_->contextObject());
 
192
  return context->horizontalPaddingRatio();
 
193
}
 
194
 
 
195
void TouchHandleDrawable::instantiateComponent() {
 
196
  if (view_.isNull()) {
 
197
    qWarning() <<
 
198
        "TouchHandleDrawable: "
 
199
        "Can't instantiate after the view has gone";
 
200
    return;
 
201
  }
 
202
 
 
203
  TouchHandleDrawableContext* contextObject =
 
204
      new TouchHandleDrawableContext();
 
205
  QQmlComponent* component = view_->touchSelectionController()->handle();
 
206
  if (!component) {
 
207
    qWarning() <<
 
208
        "TouchHandleDrawable: Content requested a touch handle "
 
209
        "drawable, but the application hasn't provided one";
 
210
    delete contextObject;
 
211
    return;
 
212
  }
 
213
 
 
214
  QQmlContext* baseContext = component->creationContext();
 
215
  if (!baseContext) {
 
216
    baseContext = QQmlEngine::contextForObject(view_.data());
 
217
  }
 
218
  context_.reset(new QQmlContext(baseContext));
 
219
 
 
220
  context_->setContextObject(contextObject);
 
221
  contextObject->setParent(context_.data());
 
222
 
 
223
  item_.reset(
 
224
      qobject_cast<QQuickItem*>(component->beginCreate(context_.data())));
 
225
  if (!item_) {
 
226
    qWarning() <<
 
227
        "TouchHandleDrawable: Failed to create instance of "
 
228
        "Qml touch selection handle component";
 
229
    context_.reset();
 
230
    return;
 
231
  }
 
232
 
 
233
  OxideQQuickWebViewPrivate::get(
 
234
      view_.data())->addAttachedPropertyTo(item_.data());
 
235
  item_->setParentItem(view_.data());
 
236
  component->completeCreate();
 
237
}
 
238
 
 
239
TouchHandleDrawable::TouchHandleDrawable(OxideQQuickWebView* view)
 
240
    : view_(view) {
 
241
  instantiateComponent();
 
242
 
 
243
  if (view) {
 
244
    connect(view->touchSelectionController(), SIGNAL(handleChanged()),
 
245
            SLOT(handleComponentChanged()));
 
246
  }
 
247
}
 
248
 
 
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();
 
261
  }
 
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();
 
265
 
 
266
  item_.reset();
 
267
  context_.reset();
 
268
  instantiateComponent();
 
269
 
 
270
  if (!item_.isNull()) {
 
271
    SetEnabled(visible);
 
272
    TouchHandleDrawableContext* context =
 
273
        qobject_cast<TouchHandleDrawableContext*>(
 
274
          context_->contextObject());
 
275
    context->setOrientation(orientation);
 
276
    context->setMirrorVertical(mirror_vertical);
 
277
    context->setMirrorHorizontal(mirror_horizontal);
 
278
    SetOrigin(position);
 
279
    SetAlpha(opacity);
 
280
  }
 
281
}
 
282
 
 
283
} // namespace qquick
 
284
} // namespace oxide
 
285
 
 
286
#include "oxide_qquick_touch_handle_drawable.moc"