SkCanvasWidget.h revision 902ebe5eb41a350b766238b3b103c22fe9fc0fb5
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
22/** \class SkCanvasWidget
23
24      The QtWidget encompasses all skia screen drawing elements. It initializes
25      an SkBitmap in memory that our SkCanvas draws to directly in memory.
26      Then using QImage and QPainter we draw those pixels on the screen in
27      this widget.
28 */
29class SkCanvasWidget : public QWidget {
30    Q_OBJECT
31
32public:
33    /**
34         Constructs a widget with the specified parent for layout purposes.
35        @param parent  The parent container of this widget
36     */
37    SkCanvasWidget(QWidget *parent);
38
39    ~SkCanvasWidget();
40
41    /**
42        Executes all saved draw commands up to the specified index.
43         @param index  The position of the command we draw up to.
44     */
45    void drawTo(int index);
46
47    /**
48        Returns the height of the bitmap.
49     */
50    int getBitmapHeight() { return fBitmap->height(); }
51
52
53    /*
54        Returns the width of the bitmap.
55     */
56    int getBitmapWidth() { return fBitmap->width(); }
57
58    /**
59        TODO(chudy): Refactor into a struct of char**
60        Returns parameter information about the ith draw command.
61        @param: i  The index of the draw command we are accessing
62     */
63    std::vector<std::string>* getCurrentCommandInfo(int i) {
64        return fDebugCanvas->getCommandInfoAt(i);
65    }
66
67    /**
68          Returns a vector of strings with all the current canvas draw
69          commands.
70     */
71    std::vector<std::string>* getDrawCommands() {
72        return fDebugCanvas->getDrawCommandsAsStrings();
73    }
74
75    /**
76        Loads a skia picture located at filename.
77        @param filename  The name of the file we are loading.
78     */
79    void loadPicture(QString filename);
80
81    /**
82        Toggles the visibility / execution of the draw command at index i.
83     */
84    void toggleCommand(int index) {
85        fDebugCanvas->toggleCommand(index);
86    }
87
88    /**
89        Toggles the visibility / execution of the draw command at index i with
90        the value of toggle.
91     */
92    void toggleCommand(int index, bool toggle) {
93        fDebugCanvas->toggleCommand(index, toggle);
94    }
95
96    /**
97        Toggles drawing filter on all drawing commands previous to current.
98     */
99    void toggleCurrentCommandFilter(bool toggle) {
100        fDebugCanvas->toggleFilter(toggle);
101    }
102
103
104protected:
105    /**
106        Draws the current state of the widget.
107        @param event  The event intercepted by Qt
108     */
109    void paintEvent(QPaintEvent *event);
110
111private:
112    SkBitmap* fBitmap;
113    SkCanvas* fCanvas;
114    SkDebugCanvas* fDebugCanvas;
115    SkDevice* fDevice;
116};
117
118#endif
119