Adds functionality to the matrix and clip widget.
authorchudy@google.com <chudy@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 3 Jul 2012 16:05:59 +0000 (16:05 +0000)
committerchudy@google.com <chudy@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 3 Jul 2012 16:05:59 +0000 (16:05 +0000)
Review URL: https://codereview.appspot.com/6348058

git-svn-id: http://skia.googlecode.com/svn/trunk@4453 2bbb7eff-a529-9590-31e7-b0007b416f81

debugger/QT/SkCanvasWidget.cpp
debugger/QT/SkCanvasWidget.h
debugger/QT/SkDebuggerGUI.cpp
debugger/QT/SkInspectorWidget.cpp
debugger/QT/SkInspectorWidget.h

index 6e82656..c38fdbb 100644 (file)
 #include "SkPicture.h"
 #include "SkStream.h"
 #include "SkCanvasWidget.h"
+#include "SkColor.h"
 #include <iostream>
 
 SkCanvasWidget::SkCanvasWidget(QWidget *parent) :
     QWidget(parent) {
 
-    fBitmap = new SkBitmap();
-    fBitmap->setConfig(SkBitmap::kARGB_8888_Config, 800, 800);
-    fBitmap->allocPixels();
-    fBitmap->eraseColor(0);
-    fDevice = new SkDevice(*fBitmap);
+    /* TODO(chudy): The 800x800 is a default number. Change it to be
+     * set dynamically. Also need to pass size into debugCanvas for current
+     * command filter. */
+    fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 800, 800);
+    fBitmap.allocPixels();
+    fBitmap.eraseColor(0);
+
+    /* TODO(chudy): Add fCanvas, fDevice to the stack. The bitmap being
+     * cleared does get rid of fDevices link to it. See if there's someway around
+     * it so that we don't have to delete both canvas and device to clear out
+     * the bitmap. */
+    fDevice = new SkDevice(fBitmap);
     fCanvas = new SkCanvas(fDevice);
     fDebugCanvas = new SkDebugCanvas();
+
+    fScaleFactor = 1;
+    fIndex = 0;
+    fPreviousPoint.set(0,0);
+    fTransform.set(0,0);
+
     this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}");
 }
 
-SkCanvasWidget::~SkCanvasWidget() {}
+SkCanvasWidget::~SkCanvasWidget() {
+    delete fCanvas;
+    delete fDevice;
+    delete fDebugCanvas;
+}
+
+void SkCanvasWidget::resizeEvent(QResizeEvent* event) {
+    fBitmap.setConfig(SkBitmap::kARGB_8888_Config, event->size().width(), event->size().height());
+    fBitmap.allocPixels();
+    fBitmap.eraseColor(0);
+
+    delete fCanvas;
+    delete fDevice;
+
+    fDevice = new SkDevice(fBitmap);
+    fCanvas = new SkCanvas(fDevice);
+    fDebugCanvas->drawTo(fCanvas, fIndex);
+    this->update();
+}
 
-void SkCanvasWidget::drawTo(int index) {
+void SkCanvasWidget::drawTo(int fIndex) {
     delete fCanvas;
     fCanvas = new SkCanvas(fDevice);
-    fDebugCanvas->drawTo(fCanvas, index+1);
+
+    fCanvas->translate(fTransform.fX, fTransform.fY);
+    if(fScaleFactor < 0) {
+        fCanvas->scale((1.0 / -fScaleFactor),(1.0 / -fScaleFactor));
+    } else if (fScaleFactor > 0) {
+        fCanvas->scale(fScaleFactor, fScaleFactor);
+    }
+
+    fDebugCanvas->drawTo(fCanvas, fIndex+1);
     this->update();
+    this->fIndex = fIndex;
 }
 
 void SkCanvasWidget::loadPicture(QString filename) {
@@ -44,6 +85,14 @@ void SkCanvasWidget::loadPicture(QString filename) {
     picture->draw(fDebugCanvas);
     fDebugCanvas->draw(fCanvas);
 
+    fIndex = fDebugCanvas->getSize();
+
+    SkColor color = fBitmap.getColor(fBitmap.width()-1,fBitmap.height()-1);
+
+    int r = SkColorGetR(color);
+    int g = SkColorGetG(color);
+    int b = SkColorGetB(color);
+
     /* NOTE(chudy): This was a test to determine if the canvas size is accurately
      * saved in the bounds of the recorded picture. It is not. Everyone of the
      * sample GM images is 1000x1000. Even the one that claims it is
@@ -51,9 +100,37 @@ void SkCanvasWidget::loadPicture(QString filename) {
     std::cout << "Width: " << picture->width();
     std::cout << " Height: " <<  picture->height() << std::endl; */
 
-    /* NOTE(chudy): Updated style sheet without a background specified to
-     * draw over our SkCanvas. */
-    this->setStyleSheet("QWidget {border: 1px solid #cccccc;}");
+    /* Updated style sheet without a background specified. If not removed
+     * QPainter paints the specified background color on top of our canvas. */
+
+    QString style("QWidget {border: 1px solid #cccccc; background-color: #");
+    style.append(QString::number(r, 16));
+    style.append(QString::number(g, 16));
+    style.append(QString::number(b, 16));
+    style.append(";}");
+    this->setStyleSheet(style);
+    this->update();
+}
+
+void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
+
+    SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
+    fTransform += eventPoint - fPreviousPoint;
+    fPreviousPoint = eventPoint;
+
+    // TODO(chudy): Fix and remove +1 from drawTo calls.
+    drawTo(fIndex+1);
+    this->update();
+}
+
+void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
+    fPreviousPoint.set(event->globalX(), event->globalY());
+}
+
+void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
+    fTransform.set(0,0);
+    fScaleFactor = 0;
+    drawTo(fIndex+1);
     this->update();
 }
 
@@ -62,12 +139,28 @@ void SkCanvasWidget::paintEvent(QPaintEvent *event) {
     QStyleOption opt;
     opt.init(this);
 
-    if (fBitmap) {
-        const QPoint origin(0,0);
-        QImage image((uchar *)fBitmap->getPixels(), fBitmap->width(), fBitmap->height(), QImage::Format_ARGB32_Premultiplied);
-        painter.drawImage(origin,image);
-    }
-
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
+
+    QPoint origin(0,0);
+    QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(),
+            fBitmap.height(), QImage::Format_ARGB32_Premultiplied);
+
+    painter.drawImage(origin, image);
     painter.end();
 }
+
+void SkCanvasWidget::wheelEvent(QWheelEvent* event) {
+    fScaleFactor += event->delta()/120;
+
+    /* The range of the fScaleFactor crosses over the range -1,0,1 frequently.
+     * Based on the code below, -1 and 1 both scale the image to it's original
+     * size we do the following to never have a registered wheel scroll
+     * not effect the fScaleFactor. */
+    if (fScaleFactor == 0) {
+        fScaleFactor += (event->delta()/120) * 2;
+    }
+
+    // TODO(chudy): Fix and remove +1 from drawTo calls.
+    drawTo(fIndex+1);
+    this->update();
+}
index a7e1c8a..bfe517e 100644 (file)
@@ -18,6 +18,7 @@
 #include <QApplication>
 #include <QtGui>
 #include <QWidget>
+#include <QWheelEvent>
 
 /** \class SkCanvasWidget
 
@@ -47,13 +48,27 @@ public:
     /**
         Returns the height of the bitmap.
      */
-    int getBitmapHeight() { return fBitmap->height(); }
+    int getBitmapHeight() { return fBitmap.height(); }
 
 
     /*
         Returns the width of the bitmap.
      */
-    int getBitmapWidth() { return fBitmap->width(); }
+    int getBitmapWidth() { return fBitmap.width(); }
+
+    /**
+        Returns an array of values of the current matrix.
+     */
+    const SkMatrix& getCurrentMatrix() {
+        return fCanvas->getTotalMatrix();
+    }
+
+    /**
+        Returns an array of values of the current bounding clip.
+     */
+    const SkIRect& getCurrentClip() {
+        return fCanvas->getTotalClip().getBounds();
+    }
 
     /**
         TODO(chudy): Refactor into a struct of char**
@@ -100,6 +115,19 @@ public:
         fDebugCanvas->toggleFilter(toggle);
     }
 
+    /**
+        Captures mouse clicks
+        @param event  The event intercepted by Qt
+     */
+    void mouseMoveEvent(QMouseEvent* event);
+
+    void mousePressEvent(QMouseEvent* event);
+
+    void mouseDoubleClickEvent(QMouseEvent* event);
+
+    void resizeEvent(QResizeEvent* event);
+
+    void wheelEvent(QWheelEvent* event);
 
 protected:
     /**
@@ -109,10 +137,16 @@ protected:
     void paintEvent(QPaintEvent *event);
 
 private:
-    SkBitmap* fBitmap;
+    SkBitmap fBitmap;
     SkCanvas* fCanvas;
     SkDebugCanvas* fDebugCanvas;
     SkDevice* fDevice;
+
+    SkIPoint fPreviousPoint;
+    SkIPoint fTransform;
+
+    int fIndex;
+    int fScaleFactor;
 };
 
 #endif
index 9c98f48..4c00f0e 100644 (file)
@@ -190,7 +190,6 @@ void SkDebuggerGUI::openFile() {
                                                      tr("Files (*.*)"));
     fDirectoryWidgetActive = false;
     if (!fileName.isNull()) {
-
         QFileInfo pathInfo(fileName);
         fPath = pathInfo.path();
         loadPicture(fileName);
@@ -225,6 +224,9 @@ void SkDebuggerGUI::registerListClick(QListWidgetItem *item) {
                 info.append("<br/>");
             }
             fInspectorWidget->setDetailText(info);
+            fInspectorWidget->setDisabled(false);
+            fInspectorWidget->setMatrix(fCanvasWidget->getCurrentMatrix());
+            fInspectorWidget->setClip(fCanvasWidget->getCurrentClip());
         }
     }
 }
index 76dce91..3f64e6f 100644 (file)
 #include "SkInspectorWidget.h"
 #include <iostream>
 
-SkInspectorWidget::SkInspectorWidget(QWidget *parent) : QWidget(parent) {
-    // NOTE(chudy): Keeps the inspector widget fully expanded.
-    fHorizontalLayout = new QHBoxLayout(this);
-    fHorizontalLayout->setSpacing(6);
-    fHorizontalLayout->setContentsMargins(11, 11, 11, 11);
-
-    fTabWidget = new QTabWidget();
-
-    fOverviewTab = new QWidget();
-    fOverviewLayout = new QHBoxLayout(fOverviewTab);
-    fOverviewLayout->setSpacing(6);
-    fOverviewLayout->setContentsMargins(11, 11, 11, 11);
-    fOverviewText = new QTextEdit();
-    fOverviewText->setReadOnly(true);
-    fOverviewLayout->addWidget(fOverviewText);
-
-    fDetailTab = new QWidget();
-    fDetailLayout = new QHBoxLayout(fDetailTab);
-    fDetailLayout->setSpacing(6);
-    fDetailLayout->setContentsMargins(11,11,11,11);
-    fDetailText = new QTextEdit();
-    fDetailText->setReadOnly(true);
-    fDetailLayout->addWidget(fDetailText);
-
-    fTabWidget->addTab(fOverviewTab, QString("Overview"));
-    fTabWidget->addTab(fDetailTab, QString("Details"));
-
-    fHorizontalLayout->setAlignment(Qt::AlignTop);
-    fHorizontalLayout->addWidget(fTabWidget);
+SkInspectorWidget::SkInspectorWidget(QWidget *parent)
+    : QWidget(parent)
+    , fHorizontalLayout(this)
+    , fOverviewTab()
+    , fOverviewLayout(&fOverviewTab)
+    , fDetailTab()
+    , fDetailLayout(&fDetailTab)
+    , fMatrixAndClipWidget(this)
+    , fVerticalLayout(&fMatrixAndClipWidget)
+    , fMatrixLabel(this)
+    , fClipLabel(this) {
+
+    fHorizontalLayout.setSpacing(6);
+    fHorizontalLayout.setContentsMargins(11, 11, 11, 11);
+
+    fOverviewLayout.setSpacing(6);
+    fOverviewLayout.setContentsMargins(11, 11, 11, 11);
+
+    fOverviewText.setReadOnly(true);
+    fOverviewLayout.addWidget(&fOverviewText);
+
+    fDetailLayout.setSpacing(6);
+    fDetailLayout.setContentsMargins(11,11,11,11);
+
+    fDetailText.setReadOnly(true);
+    fDetailLayout.addWidget(&fDetailText);
+
+    fTabWidget.addTab(&fOverviewTab, QString("Overview"));
+    fTabWidget.addTab(&fDetailTab, QString("Details"));
+
+    fHorizontalLayout.setAlignment(Qt::AlignTop);
+    fHorizontalLayout.addWidget(&fTabWidget);
 
     /* NOTE(chudy): We add all the line edits to (this). Then we lay them out
      * by adding them to horizontal layouts.
@@ -47,81 +50,80 @@ SkInspectorWidget::SkInspectorWidget(QWidget *parent) : QWidget(parent) {
      * line edits in each horizontal layout.
      */
 
-    fMatrixAndClipWidget = new QWidget(this);
-    fMatrixAndClipWidget->setFixedSize(200,300);
-    fMatrixAndClipWidget->setDisabled(true);
-
-    fVerticalLayout = new QVBoxLayout(fMatrixAndClipWidget);
-    fVerticalLayout->setAlignment(Qt::AlignVCenter);
-    fVerticalLayout->addLayout(currentMatrix());
-    fVerticalLayout->addLayout(currentClip());
-    fHorizontalLayout->addWidget(fMatrixAndClipWidget);
-
+    fMatrixAndClipWidget.setFixedSize(260,300);
+    fMatrixAndClipWidget.setDisabled(true);
 
+    fVerticalLayout.setAlignment(Qt::AlignVCenter);
+    fVerticalLayout.addLayout(setupMatrix());
+    fVerticalLayout.addLayout(setupClip());
+    fHorizontalLayout.addWidget(&fMatrixAndClipWidget);
 }
 
 SkInspectorWidget::~SkInspectorWidget() {}
 
-QString SkInspectorWidget::getDetailText() {
-    return fDetailText->toHtml();
+void SkInspectorWidget::setDetailText(QString text) {
+    fDetailText.setHtml(text);
 }
 
-QString SkInspectorWidget::getOverviewText() {
-    return fOverviewText->toHtml();
+void SkInspectorWidget::setOverviewText(QString text) {
+    fOverviewText.setHtml(text);
 }
 
-void SkInspectorWidget::setDetailText(QString text) {
-    fDetailText->setHtml(text);
+void SkInspectorWidget::setMatrix(const SkMatrix& matrix) {
+    for(int i=0; i<9; i++) {
+        fMatrixEntry[i].setText(QString::number(matrix.get(i)));
+    }
 }
 
-void SkInspectorWidget::setOverviewText(QString text) {
-    fOverviewText->setHtml(text);
+void SkInspectorWidget::setClip(const SkIRect& clip) {
+    fClipEntry[0].setText(QString::number(clip.left()));
+    fClipEntry[1].setText(QString::number(clip.top()));
+    fClipEntry[2].setText(QString::number(clip.right()));
+    fClipEntry[3].setText(QString::number(clip.bottom()));
 }
 
-QVBoxLayout* SkInspectorWidget::currentMatrix() {
-    fMatrixLabel = new QLabel(this);
-    fMatrixLabel->setText("Current Matrix");
-    fMatrixLabel->setAlignment(Qt::AlignHCenter);
-    fMatrixLayout = new QVBoxLayout();
-    fMatrixLayout->setSpacing(6);
-    fMatrixLayout->setContentsMargins(11,11,11,0);
-    fMatrixLayout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
-    fMatrixLayout->addWidget(fMatrixLabel);
+QVBoxLayout* SkInspectorWidget::setupMatrix() {
+    fMatrixLabel.setText("Current Matrix");
+    fMatrixLabel.setAlignment(Qt::AlignHCenter);
+
+    fMatrixLayout.setSpacing(6);
+    fMatrixLayout.setContentsMargins(11,11,11,0);
+    fMatrixLayout.setAlignment(Qt::AlignTop | Qt::AlignHCenter);
+    fMatrixLayout.addWidget(&fMatrixLabel);
 
     for(int i =0; i<9; i++) {
-        fMatrixEntry[i] = new QLineEdit();
-        fMatrixEntry[i]->setMinimumSize(QSize(50,25));
-        fMatrixEntry[i]->setMaximumSize(QSize(50,16777215));
-        fMatrixEntry[i]->setReadOnly(true);
-
-        if(!(i%3)) fMatrixRow[i/3] = new QHBoxLayout();
-        fMatrixRow[i/3]->addWidget(fMatrixEntry[i]);
-        if(i%3 == 2) fMatrixLayout->addLayout(fMatrixRow[i/3]);
+        fMatrixEntry[i].setMinimumSize(QSize(70,25));
+        fMatrixEntry[i].setMaximumSize(QSize(70,16777215));
+        fMatrixEntry[i].setReadOnly(true);
+
+        fMatrixRow[i/3].addWidget(&fMatrixEntry[i]);
+        if(i%3 == 2) {
+            fMatrixLayout.addLayout(&fMatrixRow[i/3]);
+        }
     }
 
-    return fMatrixLayout;
+    return &fMatrixLayout;
 }
 
-QVBoxLayout* SkInspectorWidget::currentClip() {
-    fClipLabel = new QLabel(this);
-    fClipLabel->setText("Current Clip");
-    fClipLabel->setAlignment(Qt::AlignHCenter);
-    fClipLayout = new QVBoxLayout();
-    fClipLayout->setSpacing(6);
-    fClipLayout->setContentsMargins(11,11,11,0);
-    fClipLayout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
-    fClipLayout->addWidget(fClipLabel);
+QVBoxLayout* SkInspectorWidget::setupClip() {
+    fClipLabel.setText("Current Clip");
+    fClipLabel.setAlignment(Qt::AlignHCenter);
+
+    fClipLayout.setSpacing(6);
+    fClipLayout.setContentsMargins(11,11,11,0);
+    fClipLayout.setAlignment(Qt::AlignTop | Qt::AlignHCenter);
+    fClipLayout.addWidget(&fClipLabel);
 
     for(int i =0; i<4; i++) {
-        fClipEntry[i] = new QLineEdit();
-        fClipEntry[i]->setMinimumSize(QSize(50,25));
-        fClipEntry[i]->setMaximumSize(QSize(50,16777215));
-        fClipEntry[i]->setReadOnly(true);
-
-        if(!(i%2)) fClipRow[i/2] = new QHBoxLayout();
-        fClipRow[i/2]->addWidget(fClipEntry[i]);
-        if(i%2 == 1) fClipLayout->addLayout(fClipRow[i/2]);
+        fClipEntry[i].setMinimumSize(QSize(70,25));
+        fClipEntry[i].setMaximumSize(QSize(70,16777215));
+        fClipEntry[i].setReadOnly(true);
+
+        fClipRow[i/2].addWidget(&fClipEntry[i]);
+        if(i%2 == 1) {
+            fClipLayout.addLayout(&fClipRow[i/2]);
+        }
     }
 
-    return fClipLayout;
+    return &fClipLayout;
 }
index 1025f4a..3e76104 100644 (file)
@@ -10,6 +10,8 @@
 #ifndef SKINSPECTORWIDGET_H_
 #define SKINSPECTORWIDGET_H_
 
+#include "SkMatrix.h"
+
 #include <QWidget>
 #include <QTabWidget>
 #include <QTextEdit>
@@ -34,16 +36,9 @@ public:
 
     ~SkInspectorWidget();
 
-    /**
-        Returns a QString representation of the text currently in the detail tab.
-     */
-    QString getDetailText();
-
-    /**
-        Returns a QString representation of the text currently in the overview tab.
-     */
-    QString getOverviewText();
-
+    void setDisabled(bool isDisabled) {
+        fMatrixAndClipWidget.setDisabled(isDisabled);
+    }
     /**
         Sets the text in the detail tab.
         @param text
@@ -56,31 +51,45 @@ public:
      */
     void setOverviewText(QString text);
 
+    /**
+        Sets the text in the current matrix.
+        @param matrixValues
+     */
+    void setMatrix(const SkMatrix& matrix);
+
+    /**
+        Sets the text in the current clip.
+        @param clipValues
+     */
+    void setClip(const SkIRect& clip);
+
 private:
-    QWidget* fDetailTab;
-    QTextEdit* fDetailText;
-    QHBoxLayout* fDetailLayout;
-    QHBoxLayout* fHorizontalLayout;
-    QWidget* fOverviewTab;
-    QTextEdit* fOverviewText;
-    QHBoxLayout* fOverviewLayout;
-    QTabWidget* fTabWidget;
-
-    QWidget* fMatrixAndClipWidget;
-    QVBoxLayout* fVerticalLayout;
-
-    QVBoxLayout* fMatrixLayout;
-    QLabel* fMatrixLabel;
-    QHBoxLayout* fMatrixRow[3];
-    QLineEdit* fMatrixEntry[9];
-
-    QVBoxLayout* fClipLayout;
-    QLabel* fClipLabel;
-    QHBoxLayout* fClipRow[2];
-    QLineEdit* fClipEntry[4];
-
-    QVBoxLayout* currentMatrix();
-    QVBoxLayout* currentClip();
+    QHBoxLayout fHorizontalLayout;
+    QTabWidget fTabWidget;
+
+    QWidget fOverviewTab;
+    QHBoxLayout fOverviewLayout;
+    QTextEdit fOverviewText;
+
+    QWidget fDetailTab;
+    QHBoxLayout fDetailLayout;
+    QTextEdit fDetailText;
+
+    QWidget fMatrixAndClipWidget;
+    QVBoxLayout fVerticalLayout;
+
+    QLabel fMatrixLabel;
+    QVBoxLayout fMatrixLayout;
+    QHBoxLayout fMatrixRow[3];
+    QLineEdit fMatrixEntry[9];
+
+    QLabel fClipLabel;
+    QVBoxLayout fClipLayout;
+    QHBoxLayout fClipRow[2];
+    QLineEdit fClipEntry[4];
+
+    QVBoxLayout* setupMatrix();
+    QVBoxLayout* setupClip();
 };
 
 #endif