| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 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> |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 21 | #include <QWheelEvent> |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 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 | */ |
| 30 | class SkCanvasWidget : public QWidget { |
| 31 | Q_OBJECT |
| 32 | |
| 33 | public: |
| 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 | */ |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 51 | int getBitmapHeight() { return fBitmap.height(); } |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | /* |
| 55 | Returns the width of the bitmap. |
| 56 | */ |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 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 | } |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 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 | Toggles the visibility / execution of the draw command at index i. |
| 98 | */ |
| 99 | void toggleCommand(int index) { |
| 100 | fDebugCanvas->toggleCommand(index); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | Toggles the visibility / execution of the draw command at index i with |
| 105 | the value of toggle. |
| 106 | */ |
| 107 | void toggleCommand(int index, bool toggle) { |
| 108 | fDebugCanvas->toggleCommand(index, toggle); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | Toggles drawing filter on all drawing commands previous to current. |
| 113 | */ |
| 114 | void toggleCurrentCommandFilter(bool toggle) { |
| 115 | fDebugCanvas->toggleFilter(toggle); |
| 116 | } |
| 117 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 118 | /** |
| 119 | Captures mouse clicks |
| 120 | @param event The event intercepted by Qt |
| 121 | */ |
| 122 | void mouseMoveEvent(QMouseEvent* event); |
| 123 | |
| 124 | void mousePressEvent(QMouseEvent* event); |
| 125 | |
| 126 | void mouseDoubleClickEvent(QMouseEvent* event); |
| 127 | |
| 128 | void resizeEvent(QResizeEvent* event); |
| 129 | |
| 130 | void wheelEvent(QWheelEvent* event); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 131 | |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame^] | 132 | signals: |
| 133 | void scaleFactorChanged(float newScaleFactor); |
| 134 | void commandChanged(int newCommand); |
| 135 | |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 136 | protected: |
| 137 | /** |
| 138 | Draws the current state of the widget. |
| 139 | @param event The event intercepted by Qt |
| 140 | */ |
| 141 | void paintEvent(QPaintEvent *event); |
| 142 | |
| 143 | private: |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 144 | SkBitmap fBitmap; |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 145 | SkCanvas* fCanvas; |
| 146 | SkDebugCanvas* fDebugCanvas; |
| 147 | SkDevice* fDevice; |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 148 | |
| 149 | SkIPoint fPreviousPoint; |
| 150 | SkIPoint fTransform; |
| 151 | |
| 152 | int fIndex; |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame^] | 153 | float fScaleFactor; |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | #endif |