Bug 586722 - Incubator qt embedding should switch to QGraphicsWidgets. r=romaxa
authorTatiana Meshkova <tanya.meshkova@gmail.com>
Wed, 18 Aug 2010 21:24:07 -0700
changeset 127 13ee3bfc6a101b4adb4b166bce524d8935231776
parent 126 af6926dd204a95edab67916764dacbab0ff714f6
child 128 fd2905d0088cd27ed8cfd4c33fad8a65a914b6f0
push id40
push userromaxa@gmail.com
push dateThu, 19 Aug 2010 04:25:25 +0000
reviewersromaxa
bugs586722
Bug 586722 - Incubator qt embedding should switch to QGraphicsWidgets. r=romaxa
qt/QMozView.cpp
qt/QMozView.h
qt/test/test.cpp
qt/test/test.h
--- a/qt/QMozView.cpp
+++ b/qt/QMozView.cpp
@@ -118,34 +118,34 @@ void QMozViewListener::ExitModal(nsresul
 }
 
 void QMozViewListener::OnConsoleMessage(const char * message)
 {
     QString qt_message(QString::fromUtf8(message));
     emit pQMozView->consoleMessage(qt_message);
 }
 
-QMozView::QMozView(QWidget *parent, unsigned int flags) :
-    QWidget(parent),
+QMozView::QMozView(QGraphicsWidget *parent, unsigned int flags) :
+    QGraphicsWidget(parent),
     mPrivate(new Private(this))
 {
 #if defined Q_OS_WIN
     mPrivate->mozView.CreateBrowser((void*)winId(), 0, 0, 100, 100, flags);
 #else
     // TODO: Hmmm what if we are not using a mozilla with Qt backend
     mPrivate->mozView.CreateBrowser(this, 0, 0, 0, 0, flags);
 #endif
 }
 
 QMozView::~QMozView()
 {
     delete mPrivate;
 }
 
-void QMozView::resizeEvent(QResizeEvent* event)
+void QMozView::resizeEvent(QGraphicsSceneResizeEvent* event)
 {
     Q_UNUSED(event);
     mPrivate->mozView.SetPositionAndSize(0, 0, size().width(), size().height());
 }
 
 void QMozView::loadUri(const QString &uri)
 {
     mPrivate->mozView.LoadURI(uri.toUtf8());
--- a/qt/QMozView.h
+++ b/qt/QMozView.h
@@ -39,26 +39,27 @@
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef QMOZEMBED_QMOZVIEW_H
 #define QMOZEMBED_QMOZVIEW_H
 
 #include "QMozEmbedExport.h"
 
 #include <QtGui/QWidget>
+#include <QtGui/QGraphicsWidget>
 
 class QMozViewListener;
 class nsIInterfaceRequestor;
 
-class Q_MOZEMBED_EXPORT QMozView : public QWidget
+class Q_MOZEMBED_EXPORT QMozView : public QGraphicsWidget
 {
     Q_OBJECT
 
 public:
-    explicit QMozView(QWidget *parent = 0, unsigned int flags = 0);
+    explicit QMozView(QGraphicsWidget *parent = 0, unsigned int flags = 0);
     virtual ~QMozView();
 
     void loadUri(const QString& uri);
     void getInterfaceRequestor(nsIInterfaceRequestor** aRequestor);
     QString evaluateJavaScript(const QString& script);
 
     virtual QSize sizeHint() const;
 
@@ -70,17 +71,17 @@ Q_SIGNALS:
     void locationChanged(const QString& newUri);
     void titleChanged(const QString& newTitle);
     void statusChanged(const QString& newStatus);
     void consoleMessage(const QString & message);
     void startModal();
     void exitModal();
 
 protected:
-    virtual void resizeEvent(QResizeEvent*);
+    virtual void resizeEvent(QGraphicsSceneResizeEvent* event);
 
     virtual QMozView* openWindow(unsigned int flags);
 
 private:
     class Private;
     Private * const mPrivate;
 
     friend class QMozViewListener;
--- a/qt/test/test.cpp
+++ b/qt/test/test.cpp
@@ -15,128 +15,113 @@
  *
  * The Initial Developer of the Original Code is
  * Mozilla Corporation.
  * Portions created by the Initial Developer are Copyright (C) 2007
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Anton Rogaynis <wildriding@gmail.com>
+ *   Tatiana Meshkova <tanya.meshkova@gmail.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include <QApplication>
-#include <QWidget>
-#include <QVBoxLayout>
-#include <QLineEdit>
-#include <QLabel>
-#include <QUrl>
 #include <QDebug>
+#include <QPushButton>
+#include <QGraphicsProxyWidget>
 
 #include "test.h"
-#include "QMozApp.h"
+
+MyQGraphicsView::MyQGraphicsView(QGraphicsScene* scene, QWidget* parent)
+ : QGraphicsView(scene, parent)
+{
+    setAlignment(Qt::AlignLeft | Qt::AlignTop);
+    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+
+    mLayout = new QGraphicsGridLayout;
 
-MyQMozView::MyQMozView(QWidget *parent, unsigned int flags)
-: QMozView(parent, flags)
-{}
+    mTitle = new MyTextWidget("title");
+    mLayout->addItem(mTitle, 0, 0, 1, 2);
+    mLayout->setRowMaximumHeight(0, 20);
+
+    mLocation = new MyTextWidget("location");
+    mLayout->addItem(mLocation, 1, 0, 1, 2);
+    mLayout->setRowMaximumHeight(1, 20);
+
+    mForm = new QGraphicsWidget;
+    mForm->setLayout(mLayout);
+    scene->addItem(mForm);
 
-QMozView* MyQMozView::openWindow(unsigned int flags)
-{
-    MyBrowser* newBrowser = new MyBrowser(0, flags);
-    newBrowser->resize(400, 400);
-    newBrowser->show();
-    newBrowser->setAttribute(Qt::WA_DeleteOnClose);
-    return newBrowser->getQMozView();
+    mozView = new QMozView(mForm);
+    mLayout->addItem(mozView, 2, 0, 1, 2);
+
+    mStatus = new MyTextWidget("status");
+    mLayout->addItem(mStatus, 3, 0);
+    mLayout->setRowMaximumHeight(3, 20);
+
+    QWidget* exitButton = new QPushButton("Exit");
+    mLayout->addItem(scene->addWidget(exitButton), 3, 1);
+    mLayout->setColumnMaximumWidth(1, 50);
+
+    connect(mozView, SIGNAL(locationChanged(const QString&)),
+            mLocation, SLOT(setText(const QString&)));
+
+    connect(mozView, SIGNAL(titleChanged(const QString&)),
+            mTitle, SLOT(setText(const QString&)));
+
+    connect(mozView, SIGNAL(statusChanged(const QString&)),
+            mStatus, SLOT(setText(const QString&)));
+
+    connect(mozView, SIGNAL(consoleMessage(const QString&)),
+            this, SLOT(consoleMessage(const QString&)));
+
+    connect(exitButton, SIGNAL(clicked()), this, SLOT(close()));
 }
 
-MyBrowser::MyBrowser(QWidget *parent, unsigned int flags)
-: QDialog(parent)
+MyQGraphicsView::~MyQGraphicsView()
 {
-    QVBoxLayout* layout = new QVBoxLayout(this);
-
-    location = new QLineEdit(this);
-    layout->addWidget(location);
-
-    mozView = new MyQMozView(this, flags);
-    layout->addWidget(mozView, 1);
-
-    status = new QLabel(this);
-    layout->addWidget(status);
-
-    connect(mozView, SIGNAL(locationChanged(const QString&)),
-            location, SLOT(setText(const QString&)));
-
-    connect(mozView, SIGNAL(titleChanged(const QString&)),
-            this, SLOT(setWindowTitle(const QString&)));
-
-    connect(mozView, SIGNAL(statusChanged(const QString&)),
-            status, SLOT(setText(const QString&)));
-
-    connect(mozView, SIGNAL(startModal()),
-            this, SLOT(startModal()));
-
-    connect(mozView, SIGNAL(exitModal()),
-            this, SLOT(exitModal()));
-
-    connect(location, SIGNAL(returnPressed()),
-            this, SLOT(go()));
-
-    connect(mozView, SIGNAL(consoleMessage(const QString &)),
-            this, SLOT(consoleMessage(const QString &)));
 }
 
-void MyBrowser::loadUri(const QString& uri)
+void MyQGraphicsView::resizeEvent(QResizeEvent* event)
 {
-    location->setText(uri);
+    mForm->resize(event->size());
+    QGraphicsView::resizeEvent(event);
+}
+
+void MyQGraphicsView::loadUri(const QString& uri)
+{
     mozView->loadUri(uri);
 }
 
-void MyBrowser::go()
-{
-    mozView->loadUri(location->text());
-}
-
-void MyBrowser::startModal()
-{
-    hide();
-    exec();
-}
-
-void MyBrowser::exitModal()
-{
-    done(0);
-    // have to delete mozView now to avoid JS context assertions
-    delete mozView;
-}
-
-void MyBrowser::consoleMessage(const QString& message)
+void MyQGraphicsView::consoleMessage(const QString& message)
 {
     qDebug() << "CONSOLE:" << message;
 }
 
 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
 
-    MyBrowser window;
-
-    window.resize(400, 400);
-    window.show();
+    QGraphicsScene scene;
+    MyQGraphicsView view(&scene);
+    if(argc > 1)
+        view.loadUri(argv[argc - 1]);
+    else
+        view.loadUri("http://mozilla.org");
 
-    if(argc > 1)
-        window.loadUri(argv[argc - 1]);
-    else
-        window.loadUri("http://mozilla.org");
+    view.showFullScreen();
 
     return app.exec();
 }
--- a/qt/test/test.h
+++ b/qt/test/test.h
@@ -15,16 +15,17 @@
  *
  * The Initial Developer of the Original Code is
  * Mozilla Corporation.
  * Portions created by the Initial Developer are Copyright (C) 2007
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Anton Rogaynis <wildriding@gmail.com>
+ *   Tatiana Meshkova <tanya.meshkova@gmail.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -33,43 +34,74 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef __test_h_
 #define __test_h_
 
-#include <QDialog>
+#include <QGraphicsView>
+#include <QGraphicsWidget>
+#include <QGraphicsGridLayout>
+
 #include "QMozView.h"
+#include "QMozApp.h"
 
-class QUrl;
-class QLineEdit;
-class QLabel;
+class MyTextWidget : public QGraphicsWidget
+{
+    Q_OBJECT
+
+public:
+    MyTextWidget(const QString& aText, QGraphicsItem* parent = 0)
+     : QGraphicsWidget(parent)
+     , text(aText)
+    {
+    }
 
-class MyQMozView : public QMozView
-{
-public:
-    MyQMozView(QWidget *parent = 0, unsigned int flags = 0);
 protected:
-    QMozView* openWindow(unsigned int flags);
+    void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
+    {
+        if (text.isEmpty())
+            return;
+
+        painter->drawText(boundingRect(), text);
+    }
+
+private slots:
+    void setText(const QString& aText)
+    {
+        text = aText;
+        update();
+    }
+
+private:
+    QString text;
+
 };
 
-class MyBrowser : public QDialog
+class MyQGraphicsView : public QGraphicsView
 {
-Q_OBJECT
+    Q_OBJECT
+
 public:
-    MyBrowser(QWidget *parent = 0, unsigned int flags = 0);
-    QMozView* getQMozView() {return mozView;}
+    MyQGraphicsView(QGraphicsScene* scene, QWidget* parent = 0);
+    ~MyQGraphicsView();
+
     void loadUri(const QString& uri);
-public slots:
-    void go();
-    void startModal();
-    void exitModal();
-    void consoleMessage(const QString &);
+
+protected:
+    void resizeEvent(QResizeEvent* event);
+
+private slots:
+    void consoleMessage(const QString& message);
 
 private:
-    QLineEdit* location;
-    MyQMozView* mozView;
-    QLabel* status;
+    QGraphicsWidget* mForm;
+    QGraphicsGridLayout* mLayout;
+    QMozView* mozView;
+
+    MyTextWidget* mTitle;
+    MyTextWidget* mLocation;
+    MyTextWidget* mStatus;
 };
 
 #endif /* __test_h_ */