SkCanvasWidget.h revision 7e4cfbf144af7d530d552946cee2a21d30b9b50f
1
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SKCANVASWIDGET_H
11#define SKCANVASWIDGET_H
12
13#include "SkBitmap.h"
14#include "SkCanvas.h"
15#include "SkDebugCanvas.h"
16#include "SkDevice.h"
17#include "SkPicture.h"
18#include <QApplication>
19#include <QtGui>
20#include <QWidget>
21#include <QWheelEvent>
22
23/** \class SkCanvasWidget
24
25      The QtWidget encompasses all skia screen drawing elements. It initializes
26      an SkBitmap in memory that our SkCanvas draws to directly in memory.
27      Then using QImage and QPainter we draw those pixels on the screen in
28      this widget.
29 */
30class SkCanvasWidget : public QWidget {
31    Q_OBJECT
32
33public:
34    /**
35         Constructs a widget with the specified parent for layout purposes.
36        @param parent  The parent container of this widget
37     */
38    SkCanvasWidget(QWidget *parent);
39
40    ~SkCanvasWidget();
41
42    /**
43        Executes all saved draw commands up to the specified index.
44         @param index  The position of the command we draw up to.
45     */
46    void drawTo(int index);
47
48    /**
49        Returns the height of the bitmap.
50     */
51    int getBitmapHeight() { return fBitmap.height(); }
52
53
54    /*
55        Returns the width of the bitmap.
56     */
57    int getBitmapWidth() { return fBitmap.width(); }
58
59    /**
60        Returns an array of values of the current matrix.
61     */
62    const SkMatrix& getCurrentMatrix() {
63        return fCanvas->getTotalMatrix();
64    }
65
66    /**
67        Returns an array of values of the current bounding clip.
68     */
69    const SkIRect& getCurrentClip() {
70        return fCanvas->getTotalClip().getBounds();
71    }
72
73    /**
74        TODO(chudy): Refactor into a struct of char**
75        Returns parameter information about the ith draw command.
76        @param: i  The index of the draw command we are accessing
77     */
78    std::vector<std::string>* getCurrentCommandInfo(int i) {
79        return fDebugCanvas->getCommandInfoAt(i);
80    }
81
82    /**
83          Returns a vector of strings with all the current canvas draw
84          commands.
85     */
86    std::vector<std::string>* getDrawCommands() {
87        return fDebugCanvas->getDrawCommandsAsStrings();
88    }
89
90    /**
91        Loads a skia picture located at filename.
92        @param filename  The name of the file we are loading.
93     */
94    void loadPicture(QString filename);
95
96    /**
97        Returns the visibility of the command at the specified index.
98        @param index  The index of the draw command
99     */
100    bool commandIsVisibleAtIndex(int index) {
101        return fDebugCanvas->getDrawCommandVisibilityAt(index);
102    }
103
104    /**
105        Toggles the visibility / execution of the draw command at index i with
106        the value of toggle.
107     */
108    void setCommandVisibliltyAtIndex(int index, bool toggle) {
109        fDebugCanvas->toggleCommand(index, toggle);
110    }
111
112    /**
113        Toggles drawing filter on all drawing commands previous to current.
114     */
115    void toggleCurrentCommandFilter(bool toggle) {
116        fDebugCanvas->toggleFilter(toggle);
117    }
118
119    /**
120        Captures mouse clicks
121        @param event  The event intercepted by Qt
122     */
123    void mouseMoveEvent(QMouseEvent* event);
124
125    void mousePressEvent(QMouseEvent* event);
126
127    void mouseDoubleClickEvent(QMouseEvent* event);
128
129    void resizeEvent(QResizeEvent* event);
130
131    void wheelEvent(QWheelEvent* event);
132
133signals:
134    void scaleFactorChanged(float newScaleFactor);
135    void commandChanged(int newCommand);
136    void hitChanged(int hit);
137
138protected:
139    /**
140        Draws the current state of the widget.
141        @param event  The event intercepted by Qt
142     */
143    void paintEvent(QPaintEvent *event);
144
145private:
146    SkBitmap fBitmap;
147    SkCanvas* fCanvas;
148    SkDebugCanvas* fDebugCanvas;
149    SkDevice* fDevice;
150
151    SkIPoint fPreviousPoint;
152    SkIPoint fTransform;
153
154    int fIndex;
155    float fScaleFactor;
156};
157
158#endif
159