LibreOffice Module android (master) 1
DocumentOverlayView.java
Go to the documentation of this file.
1/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9package org.libreoffice.overlay;
10
11import android.content.Context;
12import android.graphics.Canvas;
13import android.graphics.Color;
14import android.graphics.Paint;
15import android.graphics.PointF;
16import android.graphics.RectF;
17import android.util.AttributeSet;
18import android.view.MotionEvent;
19import android.view.View;
20
22import org.libreoffice.R;
35
36import java.util.ArrayList;
37import java.util.List;
38
43public class DocumentOverlayView extends View implements View.OnTouchListener {
44 private static final String LOGTAG = DocumentOverlayView.class.getSimpleName();
45
46 private static final int CURSOR_BLINK_TIME = 500;
47
48 private boolean mInitialized = false;
49
50 private List<RectF> mSelections = new ArrayList<RectF>();
51 private List<RectF> mScaledSelections = new ArrayList<RectF>();
52 private Paint mSelectionPaint = new Paint();
53 private boolean mSelectionsVisible;
54
56
57 private boolean mGraphicSelectionMove = false;
58
60
64
65 private Cursor mCursor;
66
68
69 private List<RectF> mPartPageRectangles;
71 private boolean mPageNumberAvailable = false;
72 private int previousIndex = 0; // previous page number, used to compare with the current
74
79
80 public DocumentOverlayView(Context context) {
81 super(context);
82 }
83
84 public DocumentOverlayView(Context context, AttributeSet attrs) {
85 super(context, attrs);
86 }
87
88 public DocumentOverlayView(Context context, AttributeSet attrs, int defStyleAttr) {
89 super(context, attrs, defStyleAttr);
90 }
91
95 public void initialize(LayerView layerView) {
96 if (!mInitialized) {
97 setOnTouchListener(this);
98 mLayerView = layerView;
99
100 mCursor = new Cursor();
101 mCursor.setVisible(false);
102
103 mSelectionPaint.setColor(Color.BLUE);
104 mSelectionPaint.setAlpha(50);
105 mSelectionsVisible = false;
106
109
111
115
116 mInitialized = true;
117 }
118 }
119
124 public void changeCursorPosition(RectF position) {
126 return;
127 }
129
132 }
133
138 public void changeSelections(List<RectF> selectionRects) {
139 mSelections = selectionRects;
140
143 }
144
149 public void changeGraphicSelection(RectF rectangle) {
151 return;
152 }
153
155
158 }
159
160 public void repositionWithViewport(float x, float y, float zoom) {
161 RectF rect = convertToScreen(mCursor.mPosition, x, y, zoom);
162 mCursor.reposition(rect);
163
165 mHandleMiddle.reposition(rect.left, rect.bottom);
166
168 mHandleStart.reposition(rect.left, rect.bottom);
169
171 mHandleEnd.reposition(rect.left, rect.bottom);
172
173 mScaledSelections.clear();
174 for (RectF selection : mSelections) {
175 RectF scaledSelection = convertToScreen(selection, x, y, zoom);
176 mScaledSelections.add(scaledSelection);
177 }
178
179 if (mCalcSelectionBox != null) {
182 }
183
184 if (mGraphicSelection != null && mGraphicSelection.mRectangle != null) {
185 RectF scaledGraphicSelection = convertToScreen(mGraphicSelection.mRectangle, x, y, zoom);
186 mGraphicSelection.reposition(scaledGraphicSelection);
187 }
188
189 invalidate();
190 }
191
196 private static RectF convertToScreen(RectF inputRect, float x, float y, float zoom) {
197 RectF rect = RectUtils.scale(inputRect, zoom);
198 rect.offset(-x, -y);
199 return rect;
200 }
201
206 public void setPartPageRectangles (List<RectF> rectangles) {
207 mPartPageRectangles = rectangles;
210 }
211
215 @Override
216 protected void onDraw(Canvas canvas) {
217 super.onDraw(canvas);
218
220
223 }
224
228
229 if (mSelectionsVisible) {
230 for (RectF selection : mScaledSelections) {
231 canvas.drawRect(selection, mSelectionPaint);
232 }
233 }
234
235 if (mCalcSelectionBox != null) {
237 }
238
240
241 if (mCalcHeadersController != null) {
243 }
244
245 if (mAdjustLengthLine != null) {
247 }
248 }
249
253 private Runnable cursorAnimation = new Runnable() {
254 public void run() {
255 if (mCursor.isVisible()) {
257 invalidate();
258 }
260 }
261 };
262
266 public void showCursor() {
267 if (!mCursor.isVisible()) {
268 mCursor.setVisible(true);
269 invalidate();
270 }
271 }
272
276 public void hideCursor() {
277 if (mCursor.isVisible()) {
278 mCursor.setVisible(false);
279 invalidate();
280 }
281 }
282
290 public void showPageNumberRect() {
291 if (null == mPartPageRectangles) return;
292 PointF midPoint = mLayerView.getLayerClient().convertViewPointToLayerPoint(new PointF(getWidth()/2f, getHeight()/2f));
293 int index = previousIndex;
294 // search which page the user in currently on. can enhance the search algorithm to binary search if necessary
295 for (RectF page : mPartPageRectangles) {
296 if (page.top < midPoint.y && midPoint.y < page.bottom) {
297 index = mPartPageRectangles.indexOf(page) + 1;
298 break;
299 }
300 }
301 // index == 0 applies to non-text document, i.e. don't show page info on non-text docs
302 if (index == 0) {
303 return;
304 }
305 // if page rectangle canvas element is not visible or the page number is changed, show
308 String pageNumberString = getContext().getString(R.string.page) + " " + index + "/" + mPartPageRectangles.size();
309 mPageNumberRect.setPageNumberString(pageNumberString);
311 invalidate();
312 }
313 }
314
318 public void hidePageNumberRect() {
319 if (null == mPageNumberRect) return;
322 invalidate();
323 }
324 }
325
329 public void showSelections() {
330 if (!mSelectionsVisible) {
331 mSelectionsVisible = true;
332 invalidate();
333 }
334 }
335
339 public void hideSelections() {
340 if (mSelectionsVisible) {
341 mSelectionsVisible = false;
342 invalidate();
343 }
344 }
345
349 public void showGraphicSelection() {
351 mGraphicSelectionMove = false;
354 invalidate();
355 }
356 }
357
361 public void hideGraphicSelection() {
364 invalidate();
365 }
366 }
367
371 @Override
372 public boolean onTouch(View view, MotionEvent event) {
373 PointF point = new PointF(event.getX(), event.getY());
374 switch (event.getActionMasked()) {
375 case MotionEvent.ACTION_DOWN: {
378 invalidate();
379 }
381 // Check if inside graphic selection was hit
385 invalidate();
386 return true;
387 }
388 } else {
389 if (mHandleStart.contains(point.x, point.y)) {
392 return true;
393 } else if (mHandleEnd.contains(point.x, point.y)) {
396 return true;
397 } else if (mHandleMiddle.contains(point.x, point.y)) {
400 return true;
401 } else if (mCalcSelectionBox != null &&
406 return true;
407 } else if (mAdjustLengthLine != null &&
411 return true;
412 }
413 }
414 }
415 case MotionEvent.ACTION_UP: {
418 mGraphicSelectionMove = false;
419 invalidate();
420 return true;
421 } else if (mDragHandle != null) {
423 mDragHandle = null;
424 } else if (mCalcSelectionBoxDragging) {
427 } else if (mAdjustLengthLineDragging) {
430 invalidate();
431 }
432 }
433 case MotionEvent.ACTION_MOVE: {
436 invalidate();
437 return true;
438 } else if (mDragHandle != null) {
440 } else if (mCalcSelectionBoxDragging) {
442 } else if (mAdjustLengthLineDragging) {
444 invalidate();
445 }
446 }
447 }
448 return false;
449 }
450
456 public void positionHandle(SelectionHandle.HandleType type, RectF position) {
459 return;
460 }
461
463
466 }
467
474 if (handle.isVisible()) {
475 handle.setVisible(false);
476 invalidate();
477 }
478 }
479
486 if (!handle.isVisible()) {
487 handle.setVisible(true);
488 invalidate();
489 }
490 }
491
496 switch(type) {
497 case START:
498 return mHandleStart;
499 case END:
500 return mHandleEnd;
501 case MIDDLE:
502 return mHandleMiddle;
503 }
504 return null;
505 }
506
508 return mCursor.mPosition;
509 }
510
511 public void setCalcHeadersController(CalcHeadersController calcHeadersController) {
512 mCalcHeadersController = calcHeadersController;
514 }
515
516 public void showCellSelection(RectF cellCursorRect) {
517 if (mCalcHeadersController == null || mCalcSelectionBox == null) return;
519 return;
520 }
521
522 // show selection on main GL view (i.e. in the document)
525
528
529 // show selection on headers
531 showHeaderSelection(cellCursorRect);
532 } else {
534 }
535 }
536
537 public void showHeaderSelection(RectF rect) {
538 if (mCalcHeadersController == null) return;
540 }
541
542 public void showAdjustLengthLine(boolean isRow, final CalcHeadersView view) {
543 mAdjustLengthLine = new AdjustLengthLine((LibreOfficeMainActivity) getContext(), view, isRow, getWidth(), getHeight());
548 invalidate();
549 }
550}
551
552/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Main activity of the LibreOffice App.
void reposition(float x, float y)
Change the position of the handle.
CalcSelectionBox is the selection frame for the current highlighted area/cells in Calc.
void dragEnd(PointF point)
End of a touch and drag action on the box.
void dragging(PointF point)
Box has been dragged.
boolean contains(float x, float y)
Hit test.
void dragStart(PointF point)
Start of a touch and drag action on the box.
boolean contains(float x, float y)
Hit test.
void setVisible(boolean visible)
Set element visibility.
void draw(Canvas canvas)
Trigger drawing the element on the canvas.
Handles the cursor drawing on the canvas.
Definition: Cursor.java:11
void cycleAlpha()
Cycle the alpha color of the cursor, makes the.
Definition: Cursor.java:53
void reposition(RectF rect)
Reposition the cursor on screen.
Definition: Cursor.java:45
This class is responsible to draw and reposition the selection rectangle.
void dragging(PointF position)
Dragging is in process.
void dragStart(PointF position)
Dragging on the screen has started.
void setVisible(boolean visible)
Set the visibility of the graphic selection.
void dragEnd(PointF position)
Dragging has ended.
void reposition(RectF scaledRectangle)
Viewport has changed, reposition the selection to the new rectangle.
void setPageNumberString(String pageNumberString)
Selection handle for showing and manipulating the end of a selection.
Selection handle that is used to manipulate the cursor.
Selection handle for showing and manipulating the start of a selection.
Selection handle is a common class for "start", "middle" and "end" types of selection handles.
void dragEnd(PointF point)
End of a touch and drag action on the handle.
void dragging(PointF point)
Handle has been dragged.
void dragStart(PointF point)
Start of a touch and drag action on the handle.
Document overlay view is responsible for showing the client drawn overlay elements like cursor,...
void showGraphicSelection()
Show the graphic selection on the view.
void setCalcHeadersController(CalcHeadersController calcHeadersController)
static RectF convertToScreen(RectF inputRect, float x, float y, float zoom)
Convert the input rectangle from document to screen coordinates according to current viewport data (x...
void showPageNumberRect()
Calculate and show page number according to current viewport position.
void changeSelections(List< RectF > selectionRects)
Change the text selection rectangles.
void hideHandle(SelectionHandle.HandleType type)
Hide the handle.
void showHandle(SelectionHandle.HandleType type)
Show the handle.
void changeCursorPosition(RectF position)
Change the cursor position.
void hideGraphicSelection()
Hide the graphic selection.
void showAdjustLengthLine(boolean isRow, final CalcHeadersView view)
boolean onTouch(View view, MotionEvent event)
Handle the triggered touch event.
void initialize(LayerView layerView)
Initialize the selection and cursor view.
void setPartPageRectangles(List< RectF > rectangles)
Set part page rectangles and initialize a page number rectangle object (canvas element).
void hideSelections()
Hide text selection rectangles.
void showSelections()
Show text selection rectangles.
void changeGraphicSelection(RectF rectangle)
Change the graphic selection rectangle.
void onDraw(Canvas canvas)
Drawing on canvas.
void showCursor()
Show the cursor on the view.
Runnable cursorAnimation
Cursor animation function.
SelectionHandle getHandleForType(SelectionHandle.HandleType type)
Returns the handle instance for the input type.
void repositionWithViewport(float x, float y, float zoom)
DocumentOverlayView(Context context, AttributeSet attrs)
void positionHandle(SelectionHandle.HandleType type, RectF position)
Change the handle document position.
DocumentOverlayView(Context context, AttributeSet attrs, int defStyleAttr)
void hidePageNumberRect()
Hide page number rectangle canvas element.
PointF convertViewPointToLayerPoint(PointF viewPoint)
ImmutableViewportMetrics are used to store the viewport metrics in way that we can access a version o...
A view rendered by the layer compositor.
Definition: LayerView.java:41
GeckoLayerClient getLayerClient()
Definition: LayerView.java:155
ImmutableViewportMetrics getViewportMetrics()
Definition: LayerView.java:158
static RectF scale(RectF rect, float scale)
Definition: RectUtils.java:44
static void assign(final RectF target, final RectF source)
Assign rectangle values from source to target.
Definition: RectUtils.java:103
static boolean fuzzyEquals(RectF a, RectF b)
Definition: RectUtils.java:89
float y
float x
def position(n=-1)
def rectangle(l)
def run(arg=None, arg2=-1)
def point()
MIDDLE
index
END
START
ResultType type
const sal_uInt8 R