WebKitTools/MiniBrowser/qt/BrowserWindow.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
       
     3  * Copyright (C) 2010 University of Szeged
       
     4  *
       
     5  * All rights reserved.
       
     6  *
       
     7  * Redistribution and use in source and binary forms, with or without
       
     8  * modification, are permitted provided that the following conditions
       
     9  * are met:
       
    10  * 1. Redistributions of source code must retain the above copyright
       
    11  *    notice, this list of conditions and the following disclaimer.
       
    12  * 2. Redistributions in binary form must reproduce the above copyright
       
    13  *    notice, this list of conditions and the following disclaimer in the
       
    14  *    documentation and/or other materials provided with the distribution.
       
    15  *
       
    16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
       
    17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
       
    20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    27  */
       
    28 
       
    29 #include "BrowserWindow.h"
       
    30 #include <WebKit2/WKPageNamespace.h>
       
    31 #include <WebKit2/qwkpage.h>
       
    32 
       
    33 static QWKPage* createNewPage(QWKPage* page)
       
    34 {
       
    35     return page;
       
    36 }
       
    37 
       
    38 BrowserView::BrowserView(QWidget* parent)
       
    39     : QGraphicsView(parent)
       
    40     , m_item(0)
       
    41 {
       
    42     m_context.adopt(WKContextGetSharedProcessContext());
       
    43 
       
    44     WKRetainPtr<WKPageNamespaceRef> pageNamespace(AdoptWK, WKPageNamespaceCreate(m_context.get()));
       
    45 
       
    46     m_item = new QGraphicsWKView(pageNamespace.get(), QGraphicsWKView::Simple, 0);
       
    47     setScene(new QGraphicsScene(this));
       
    48     scene()->addItem(m_item);
       
    49 
       
    50     setFrameShape(QFrame::NoFrame);
       
    51     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       
    52     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       
    53 
       
    54     connect(m_item, SIGNAL(titleChanged(QString)), this, SLOT(setWindowTitle(QString)));
       
    55     m_item->page()->setCreateNewPageFunction(createNewPage);
       
    56 }
       
    57 
       
    58 void BrowserView::resizeEvent(QResizeEvent* event)
       
    59 {
       
    60     QGraphicsView::resizeEvent(event);
       
    61     QRectF rect(QPoint(0, 0), event->size());
       
    62     m_item->setGeometry(rect);
       
    63     scene()->setSceneRect(rect);
       
    64 }
       
    65 
       
    66 void BrowserView::load(const QUrl& url)
       
    67 {
       
    68 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
       
    69     return m_item->load(QUrl::fromUserInput(url.toString()));
       
    70 #else
       
    71     return m_item->load(url);
       
    72 #endif
       
    73 }
       
    74 
       
    75 QGraphicsWKView* BrowserView::view() const
       
    76 {
       
    77     return m_item;
       
    78 }
       
    79 
       
    80 BrowserWindow::BrowserWindow()
       
    81 {
       
    82     m_menu = new QMenuBar();
       
    83     m_browser = new BrowserView();
       
    84     m_addressBar = new QLineEdit();
       
    85 
       
    86     m_menu->addAction("Quit", this, SLOT(close()));
       
    87 
       
    88     m_browser->setFocus(Qt::OtherFocusReason);
       
    89 
       
    90     connect(m_addressBar, SIGNAL(returnPressed()), SLOT(changeLocation()));
       
    91     connect(m_browser->view(), SIGNAL(loadProgress(int)), SLOT(loadProgress(int)));
       
    92     connect(m_browser->view(), SIGNAL(titleChanged(const QString&)), SLOT(titleChanged(const QString&)));
       
    93     connect(m_browser->view(), SIGNAL(urlChanged(const QUrl&)), SLOT(urlChanged(const QUrl&)));
       
    94 
       
    95     QToolBar* bar = addToolBar("Navigation");
       
    96     bar->addAction(m_browser->view()->page()->action(QWKPage::Back));
       
    97     bar->addAction(m_browser->view()->page()->action(QWKPage::Forward));
       
    98     bar->addAction(m_browser->view()->page()->action(QWKPage::Reload));
       
    99     bar->addAction(m_browser->view()->page()->action(QWKPage::Stop));
       
   100     bar->addWidget(m_addressBar);
       
   101 
       
   102     this->setMenuBar(m_menu);
       
   103     this->setCentralWidget(m_browser);
       
   104 
       
   105     m_browser->setFocus(Qt::OtherFocusReason);
       
   106 }
       
   107 
       
   108 void BrowserWindow::load(const QString& url)
       
   109 {
       
   110     m_addressBar->setText(url);
       
   111     m_browser->load(QUrl(url));
       
   112 }
       
   113 
       
   114 void BrowserWindow::changeLocation()
       
   115 {
       
   116     QString string = m_addressBar->text();
       
   117     m_browser->load(string);
       
   118 }
       
   119 
       
   120 void BrowserWindow::loadProgress(int progress)
       
   121 {
       
   122     QColor backgroundColor = QApplication::palette().color(QPalette::Base);
       
   123     QColor progressColor = QColor(120, 180, 240);
       
   124     QPalette pallete = m_addressBar->palette();
       
   125 
       
   126     if (progress <= 0 || progress >= 100)
       
   127         pallete.setBrush(QPalette::Base, backgroundColor);
       
   128     else {
       
   129         QLinearGradient gradient(0, 0, width(), 0);
       
   130         gradient.setColorAt(0, progressColor);
       
   131         gradient.setColorAt(((double) progress) / 100, progressColor);
       
   132         if (progress != 100)
       
   133             gradient.setColorAt((double) progress / 100 + 0.001, backgroundColor);
       
   134         pallete.setBrush(QPalette::Base, gradient);
       
   135     }
       
   136     m_addressBar->setPalette(pallete);
       
   137 }
       
   138 
       
   139 void BrowserWindow::titleChanged(const QString& title)
       
   140 {
       
   141     setWindowTitle(title);
       
   142 }
       
   143 
       
   144 void BrowserWindow::urlChanged(const QUrl& url)
       
   145 {
       
   146     m_addressBar->setText(url.toString());
       
   147 }
       
   148 
       
   149 BrowserWindow::~BrowserWindow()
       
   150 {
       
   151     delete m_addressBar;
       
   152     delete m_browser;
       
   153     delete m_menu;
       
   154 }