homescreenapp/examples/minibrowserwidgetplugin/src/minibrowserwidget.cpp
changeset 63 52b0f64eeb51
equal deleted inserted replaced
62:341166945d65 63:52b0f64eeb51
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Example of home screen widget
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QDebug>
       
    19 #include <QGraphicsLinearLayout>
       
    20 #include <QGraphicsWebView>
       
    21 #include <HbPushButton>
       
    22 #include <HbLineEdit>
       
    23 #include <HbFrameDrawer>
       
    24 #include <HbFrameItem>
       
    25 #include <HbDeviceProfile>
       
    26 #include "minibrowserwidget.h"
       
    27 
       
    28 /*!
       
    29     \ingroup group_minibrowser_widget
       
    30     \class MiniBrowserWidget
       
    31     \brief Example implementation for home screen widget.
       
    32 
       
    33     MiniBrowserWidget derived from the HbWidget and implements 
       
    34     needed functions for the home screen widget. 
       
    35     As name suggests MiniBrowserWidget is a small web browser widget in
       
    36     the home screen. 
       
    37     
       
    38     Mini browser widget demonstrates the following home screen widget features:
       
    39     <ol>
       
    40     <li> home screen online/offline state, see \ref sssection_isonline </li>
       
    41     <li> preferences, see \ref sssection_setpreferences </li>
       
    42     </ol>
       
    43     It also shows use of HbPushButton and HbLineEdit widgets. 
       
    44     @image html hs_minibrowser.png
       
    45     
       
    46     
       
    47 */
       
    48 
       
    49 /*!
       
    50     Constructs a widget which is a child of \a parent, with widget flags set to \a flags.
       
    51     Widget creates two buttons ('go' and 'stop'), text field to write url and browser view.
       
    52     Note that this is just an simple example so this creates child widgets directly to the layout.
       
    53     For more complicated widget layouting, see Orbit documentation and especially WidgetML section of it.
       
    54 */
       
    55 MiniBrowserWidget::MiniBrowserWidget(QGraphicsItem* parent, Qt::WindowFlags flags)
       
    56     : HbWidget(parent, flags)
       
    57 {
       
    58     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
       
    59     setLayout(layout);
       
    60     
       
    61     QGraphicsLinearLayout *buttonLayout = new QGraphicsLinearLayout(Qt::Horizontal);
       
    62     mGoButton = new HbPushButton("Go");
       
    63     connect(mGoButton, SIGNAL(pressed()), SLOT(pressGoButton()));
       
    64     buttonLayout->addItem(mGoButton);
       
    65     mStopButton = new HbPushButton("Stop");
       
    66     connect(mStopButton, SIGNAL(pressed()), SLOT(pressStopButton()));
       
    67     buttonLayout->addItem(mStopButton);
       
    68     mStopButton->setEnabled(false);
       
    69     mEditor = new HbLineEdit();
       
    70     buttonLayout->addItem(mEditor);
       
    71     
       
    72     layout->addItem(buttonLayout);
       
    73 
       
    74     mWeb = new QGraphicsWebView();
       
    75     layout->addItem(mWeb);
       
    76     
       
    77     connect(mWeb, SIGNAL(loadStarted()), SLOT(loadStarted()));
       
    78     connect(mWeb, SIGNAL(loadProgress(int)), SLOT(loadProgress(int)));
       
    79     connect(mWeb, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
       
    80 
       
    81     // maximum size of the home screen widget is (48,39) units. Convert units to pixels and 
       
    82     // resize minibrowser as maximum size
       
    83     HbFrameDrawer *drawer = new HbFrameDrawer(
       
    84         QLatin1String("qtg_fr_hsshortcut_normal"), HbFrameDrawer::NinePieces);
       
    85     setBackgroundItem(new HbFrameItem(drawer));
       
    86     
       
    87     HbDeviceProfile profile;
       
    88     qreal factor = profile.unitValue();
       
    89     setPreferredSize(48*factor, 39*factor);
       
    90     resize(preferredSize());
       
    91     
       
    92     mUrl = QString("http://www.nokia.com");
       
    93 }
       
    94 
       
    95 /*!
       
    96     Destructor
       
    97 */
       
    98 MiniBrowserWidget::~MiniBrowserWidget()
       
    99 {
       
   100 }
       
   101 
       
   102 /*!
       
   103     Returns widget's online state
       
   104 */
       
   105 // Start of snippet 1
       
   106 bool MiniBrowserWidget::isOnline()
       
   107 { 
       
   108     return mOnline;
       
   109 }
       
   110 
       
   111 /*!
       
   112     Sets online state of the widget as \a online
       
   113 */
       
   114 void MiniBrowserWidget::setOnline(bool online)
       
   115 { 
       
   116     mOnline = online;
       
   117 }
       
   118 // End of snippet 1
       
   119 
       
   120 /*!
       
   121     Returns url of the widget currently showing.
       
   122 */
       
   123 // Start of snippet 2
       
   124 QString MiniBrowserWidget::url() const
       
   125 {
       
   126     return mUrl;
       
   127 }
       
   128 
       
   129 /*!
       
   130     Sets url of the widget as \a url
       
   131 */
       
   132 void MiniBrowserWidget::setUrl(const QString& url)
       
   133 {
       
   134     mUrl = url;
       
   135 }
       
   136 // End of snippet 2
       
   137 
       
   138 /*!
       
   139     Called when widget is initialized and all to properties are set
       
   140 */
       
   141 void MiniBrowserWidget::onInitialize()
       
   142 {
       
   143     mGoButton->setEnabled(mOnline);
       
   144     mEditor->setText(mUrl);
       
   145 }
       
   146 
       
   147 /*!
       
   148     Called when widget is shown in the home screen
       
   149 */
       
   150 // Start of snippet 3
       
   151 void MiniBrowserWidget::onShow()
       
   152 {
       
   153     if (mOnline) {
       
   154         mWeb->load(QUrl(mUrl));
       
   155         mStopButton->setEnabled(true);
       
   156         }
       
   157 }
       
   158 // End of snippet 3
       
   159 
       
   160 /*!
       
   161     Called when widget is hidden from the home screen.
       
   162     Widget can decide what is appropriate action when 
       
   163     widget is not visible on the screen. At least it
       
   164     should not update itself as often as when on the 
       
   165     screen to save battery etc.
       
   166 */
       
   167 // Start of snippet 4
       
   168 void MiniBrowserWidget::onHide()
       
   169 {
       
   170     mWeb->stop();
       
   171 }
       
   172 // End of snippet 4
       
   173 
       
   174 /*!
       
   175     Slot connected to 'go' button
       
   176 */
       
   177 // Start of snippet 5
       
   178 void MiniBrowserWidget::pressGoButton()
       
   179 {
       
   180     setUrl(mEditor->text());
       
   181     if (mOnline) {
       
   182         mWeb->load(QUrl(mUrl));
       
   183         mStopButton->setEnabled(true);
       
   184         }
       
   185     QStringList list;
       
   186     list.append("url");
       
   187     emit setPreferences(list);
       
   188 }
       
   189 // End of snippet 5
       
   190 
       
   191 /*!
       
   192     Slot connected to 'stop' button
       
   193 */
       
   194 void MiniBrowserWidget::pressStopButton()
       
   195 {
       
   196     mWeb->stop();
       
   197     //mStopButton->setEnabled(false);
       
   198 }
       
   199 
       
   200 /*!
       
   201     Slot connected to webview's loadProgress signal
       
   202     TODO: check if this is needed.
       
   203 */
       
   204 void MiniBrowserWidget::loadProgress(int progress)
       
   205 {
       
   206     Q_UNUSED(progress)
       
   207     qDebug() << progress;
       
   208 }
       
   209 
       
   210 /*!
       
   211     Slot connected to webview's loadFinished signal
       
   212 */
       
   213 void MiniBrowserWidget::loadFinished(bool ok)
       
   214 {
       
   215     Q_UNUSED(ok)
       
   216     mStopButton->setEnabled(false);
       
   217 }
       
   218 void MiniBrowserWidget::loadStarted()
       
   219 {
       
   220 }
       
   221