ginebra2/UrlSearchSnippet.cpp
changeset 5 0f2326c2a325
parent 0 1450b09d0cfd
child 6 1c3b8676e58c
equal deleted inserted replaced
1:b0dd75e285d2 5:0f2326c2a325
     1 /*
     1 /*
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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 *
     4 *
     9 * Initial Contributors:
     5 * This program is free software: you can redistribute it and/or modify
    10 * Nokia Corporation - initial contribution.
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
    11 *
     8 *
    12 * Contributors:
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
    13 *
    13 *
    14 * Description: 
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
    15 *
    19 *
    16 */
    20 */
    17 
    21 
    18 
       
    19 #include "UrlSearchSnippet.h"
    22 #include "UrlSearchSnippet.h"
    20 #include "Utilities.h"
    23 #include "Utilities.h"
    21 
    24 
    22 #include "ChromeRenderer.h"
    25 #include "ChromeRenderer.h"
    23 #include "ChromeWidget.h"
    26 #include "ChromeWidget.h"
       
    27 #include "PageSnippet.h"
    24 #include "ViewController.h"
    28 #include "ViewController.h"
       
    29 #include "ViewStack.h"
    25 #include "WebChromeSnippet.h"
    30 #include "WebChromeSnippet.h"
    26 
    31 #include "LoadController.h"
    27 #include "webpagecontroller.h"
    32 #include "webpagecontroller.h"
       
    33 #include "GWebContentView.h"
       
    34 #include "WindowFlowView.h"
    28 
    35 
    29 namespace GVA {
    36 namespace GVA {
    30 
    37 
    31 // Methods for class UrlEditorWidget
    38 #define GO_BUTTON_ICON ":/chrome/bedrockchrome/urlsearch.snippet/icons/go_btn.png"
    32 
    39 #define STOP_BUTTON_ICON ":/chrome/bedrockchrome/urlsearch.snippet/icons/stop_btn.png"
    33 UrlEditorWidget::UrlEditorWidget(QGraphicsItem * parent)
    40 #define REFRESH_BUTTON_ICON ":/chrome/bedrockchrome/urlsearch.snippet/icons/refresh_btn.png"
    34 : QGraphicsTextItem(parent)
    41 #define BETWEEN_ENTRY_AND_BUTTON_SPACE 4
    35 {
    42 
    36     // Disable wrapping, force text to be stored and displayed
    43 GUrlSearchItem::GUrlSearchItem(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent)
    37     // as a single line.
       
    38 
       
    39     QTextOption textOption = document()->defaultTextOption();
       
    40     textOption.setWrapMode(QTextOption::NoWrap);
       
    41     document()->setDefaultTextOption(textOption);
       
    42 
       
    43     // Enable cursor keys.
       
    44 
       
    45     setTextInteractionFlags(Qt::TextEditorInteraction);
       
    46 
       
    47     // This is needed to initialize m_textLine.
       
    48 
       
    49     setText("");
       
    50 }
       
    51 
       
    52 UrlEditorWidget::~UrlEditorWidget()
       
    53 {
       
    54 }
       
    55 
       
    56 void UrlEditorWidget::setText(const QString & text)
       
    57 {
       
    58     setPlainText(text);
       
    59     m_textLine = document()->begin().layout()->lineForTextPosition(0);
       
    60 }
       
    61 
       
    62 qreal UrlEditorWidget::cursorX()
       
    63 {
       
    64     return m_textLine.cursorToX(textCursor().position());
       
    65 }
       
    66 
       
    67 void UrlEditorWidget::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
       
    68 {
       
    69     // Paint without ugly selection ants (the dashed line that surrounds
       
    70     // the selected text).
       
    71 
       
    72     QStyleOptionGraphicsItem newOption = *option;
       
    73     newOption.state &= (!QStyle::State_Selected | !QStyle::State_HasFocus);
       
    74 
       
    75     painter->save();
       
    76 
       
    77     QGraphicsTextItem::paint(painter, &newOption, widget);
       
    78 
       
    79     painter->restore();
       
    80 }
       
    81 
       
    82 void UrlEditorWidget::keyPressEvent(QKeyEvent * event)
       
    83 {
       
    84     // Signal horizontal cursor movement so UrlSearchSnippet can
       
    85     // implement horizontal scrolling.
       
    86 
       
    87     qreal oldX = cursorX();
       
    88 
       
    89     QGraphicsTextItem::keyPressEvent(event);
       
    90 
       
    91     qreal newX = cursorX();
       
    92 
       
    93     if (newX != oldX) {
       
    94         emit cursorXChanged(newX);
       
    95     }
       
    96 }
       
    97 
       
    98 // Methods for class UrlSearchSnippet
       
    99 
       
   100 UrlSearchSnippet::UrlSearchSnippet(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent)
       
   101 : NativeChromeItem(snippet, parent)
    44 : NativeChromeItem(snippet, parent)
   102 , m_chrome(chrome)
    45 , m_chrome(chrome)
   103 , m_percent(0)
       
   104 , m_pendingClearCalls(0)
       
   105 , m_viewPortWidth(0.0)
    46 , m_viewPortWidth(0.0)
   106 , m_viewPortHeight(0.0)
    47 , m_viewPortHeight(0.0)
   107 {
    48 , m_pendingClearCalls(0)
   108     setFlags(QGraphicsItem::ItemIsMovable);
    49 , m_backFromNewWinTrans(false)
   109 
    50 , m_justFocusIn(false)
       
    51 {
   110     // Extract style information from element CSS.
    52     // Extract style information from element CSS.
   111 
    53 
   112     // For border-related properties, we constrain all values (top, left, etc.)
    54     // For border-related properties, we constrain all values (top, left, etc.)
   113     // to be the same.  These can be set using the css shorthand (e.g. padding:10px),
    55     // to be the same.  These can be set using the css shorthand (e.g. padding:10px),
   114     // but the computed css style will be for the four primitive values (padding-top,
    56     // but the computed css style will be for the four primitive values (padding-top,
   115     // padding-left) etc, which will all be equal.  Hence we just use one of the
    57     // padding-left) etc, which will all be equal.  Hence we just use one of the
   116     // computed primitive values (top) to represent the common value.
    58     // computed primitive values (top) to represent the common value.
   117 
    59 
   118     QWebElement we = m_snippet->element();
    60     QWebElement we = m_snippet->element();
   119 
    61 
       
    62     QColor textColor;
   120     NativeChromeItem::CSSToQColor(
    63     NativeChromeItem::CSSToQColor(
   121             we.styleProperty("color", QWebElement::ComputedStyle),
    64             we.styleProperty("color", QWebElement::ComputedStyle),
   122             m_textColor);
    65             textColor);
   123 
    66 
       
    67     QColor backgroundColor;
   124     NativeChromeItem::CSSToQColor(
    68     NativeChromeItem::CSSToQColor(
   125             we.styleProperty("background-color", QWebElement::ComputedStyle),
    69             we.styleProperty("background-color", QWebElement::ComputedStyle),
   126             m_backgroundColor);
    70             backgroundColor); // FIXME text edit background color doesn't work
       
    71 
       
    72     QColor progressColor;
       
    73     NativeChromeItem::CSSToQColor(
       
    74             we.styleProperty("border-bottom-color", QWebElement::ComputedStyle),
       
    75             progressColor); //FIXME text-underline-color causes the crash
   127 
    76 
   128     NativeChromeItem::CSSToQColor(
    77     NativeChromeItem::CSSToQColor(
   129             we.styleProperty("border-top-color", QWebElement::ComputedStyle),
    78             we.styleProperty("border-top-color", QWebElement::ComputedStyle),
   130             m_borderColor);
    79             m_borderColor);
   131 
    80 
   133     m_padding = cssPadding.remove("px").toInt();
    82     m_padding = cssPadding.remove("px").toInt();
   134 
    83 
   135     QString cssBorder = we.styleProperty("border-top-width", QWebElement::ComputedStyle);
    84     QString cssBorder = we.styleProperty("border-top-width", QWebElement::ComputedStyle);
   136     m_border = cssBorder.remove("px").toInt();
    85     m_border = cssBorder.remove("px").toInt();
   137 
    86 
   138     // The viewport clips the editor when text overflows
    87     // Create the view port widget
   139 
       
   140     m_viewPort = new QGraphicsWidget(this);
    88     m_viewPort = new QGraphicsWidget(this);
   141     m_viewPort->setFlags(QGraphicsItem::ItemClipsChildrenToShape);
    89     m_viewPort->setFlags(QGraphicsItem::ItemClipsChildrenToShape);
   142 
    90 
   143     // The actual text editor item
    91     // Create the url search editor
   144 
    92     m_urlSearchEditor = new GProgressEditor(snippet, chrome, m_viewPort);
   145     m_editor = new UrlEditorWidget(m_viewPort);
    93     m_urlSearchEditor->setTextColor(textColor);
   146     m_editor->setDefaultTextColor(m_textColor);
    94     m_urlSearchEditor->setBackgroundColor(backgroundColor);
   147     m_editor->installEventFilter(this);
    95     m_urlSearchEditor->setProgressColor(progressColor);
   148 
    96     m_urlSearchEditor->setBorderColor(m_borderColor);
   149     // Monitor editor cursor position changes for horizontal scrolling.
    97     m_urlSearchEditor->setPadding(0.1); // draw the Rounded Rect
   150 
    98     safe_connect(m_urlSearchEditor, SIGNAL(textMayChanged()), this, SLOT(updateLoadStateAndSuggest()));
   151     safe_connect(m_editor, SIGNAL(cursorXChanged(qreal)),
    99     safe_connect(m_urlSearchEditor, SIGNAL(activated()),this, SLOT(urlSearchActivatedByEnterKey()));
   152             this, SLOT(makeVisible(qreal)));
   100     safe_connect(m_urlSearchEditor, SIGNAL(focusChanged(bool)),this, SLOT(focusChanged(bool)));
       
   101     safe_connect(m_urlSearchEditor, SIGNAL(tapped(QPointF&)),this, SLOT(tapped(QPointF&)));
       
   102 
       
   103     // Create the url search button
       
   104     m_urlSearchBtn = new ActionButton(snippet, m_viewPort);
       
   105     QAction* urlSearchBtnAction = new QAction(this);
       
   106     m_urlSearchBtn->setAction(urlSearchBtnAction); // FIXME: should use diff QActions
       
   107     safe_connect(urlSearchBtnAction, SIGNAL(triggered()), this, SLOT(urlSearchActivated()));
       
   108 
       
   109     // Get the icon size
       
   110     QIcon btnIcon(GO_BUTTON_ICON);
       
   111     QSize defaultSize(50, 50);
       
   112     QSize actualSize = btnIcon.actualSize(defaultSize);
       
   113     m_iconWidth = actualSize.width();
       
   114     m_iconHeight = actualSize.height();
       
   115     // Set the right text margin to accomodate the icon inside the editor
       
   116     m_urlSearchEditor->setRightTextMargin(m_iconWidth + BETWEEN_ENTRY_AND_BUTTON_SPACE);
       
   117 
       
   118     // Update state as soon as chrome completes loading.
       
   119     safe_connect(m_chrome, SIGNAL(chromeComplete()),
       
   120             this, SLOT(onChromeComplete()));
   153 
   121 
   154     // Monitor resize events.
   122     // Monitor resize events.
   155 
       
   156     safe_connect(m_chrome->renderer(), SIGNAL(chromeResized()),
   123     safe_connect(m_chrome->renderer(), SIGNAL(chromeResized()),
   157             this, SLOT(resize()));
   124             this, SLOT(resize()));
   158 
   125 
   159     // Update state as soon as chrome completes loading.
       
   160 
       
   161     safe_connect(m_chrome, SIGNAL(chromeComplete()),
       
   162             this, SLOT(setStarted()));
       
   163 
       
   164     // Monitor page loading.
       
   165 
       
   166     WebPageController * pageController = WebPageController::getSingleton();
   126     WebPageController * pageController = WebPageController::getSingleton();
   167 
   127 
   168     safe_connect(pageController, SIGNAL(pageUrlChanged(const QString)),
   128     safe_connect(pageController, SIGNAL(pageUrlChanged(const QString)),
   169             this, SLOT(setUrlText(const QString &)));
   129             m_urlSearchEditor, SLOT(setText(const QString &)))
   170 
   130 
   171     safe_connect(pageController, SIGNAL(pageLoadStarted()),
   131     safe_connect(pageController, SIGNAL(pageLoadStarted()),
   172             this, SLOT(setStarted()));
   132             this, SLOT(setStarted()));
   173 
   133 
   174     safe_connect(pageController, SIGNAL(pageLoadProgress(const int)),
   134     safe_connect(pageController, SIGNAL(pageLoadProgress(const int)),
   175             this, SLOT(setProgress(int)));
   135             this, SLOT(setProgress(int)));
   176 
   136 
   177     safe_connect(pageController, SIGNAL(pageLoadFinished(bool)),
   137     safe_connect(pageController, SIGNAL(pageLoadFinished(bool)),
   178             this, SLOT(setFinished(bool)));
   138             this, SLOT(setFinished(bool)));
   179 
   139 
       
   140     safe_connect(pageController, SIGNAL(pageCreated(WRT::WrtBrowserContainer*)),
       
   141             this, SLOT(setPageCreated()));
       
   142 
       
   143     safe_connect(pageController, SIGNAL(pageChanged(WRT::WrtBrowserContainer*, WRT::WrtBrowserContainer*)),
       
   144             this, SLOT(setPageChanged()));
       
   145 
   180     // Monitor view changes.
   146     // Monitor view changes.
   181 
   147 
   182     ViewController * viewController = chrome->viewController();
   148     ViewController * viewController = chrome->viewController();
   183 
   149 
   184     safe_connect(viewController, SIGNAL(currentViewChanged()),
   150     safe_connect(viewController, SIGNAL(currentViewChanged()),
   185             this, SLOT(viewChanged()));
   151             this, SLOT(viewChanged()));
   186 }
   152 
   187 
   153     safe_connect(ViewStack::getSingleton(), SIGNAL(currentViewChanged()),
   188 UrlSearchSnippet::~UrlSearchSnippet()
   154             this, SLOT(viewChanged()));
   189 {
   155 }
   190 }
   156 
   191 
   157 GUrlSearchItem::~GUrlSearchItem()
   192 bool UrlSearchSnippet::eventFilter(QObject * object, QEvent * event)
   158 {
   193 {
   159 }
   194     // Filter editor key events.
   160 
   195 
   161 //TODO: Shouldn't have to explicitly set the viewport sizes here
   196     if (object != m_editor) {
   162 
   197         return false;
   163 void GUrlSearchItem::resizeEvent(QGraphicsSceneResizeEvent * event)
   198     }
   164 {
   199 
   165     QSizeF size = event->newSize();
   200     if (event->type() != QEvent::KeyPress) {
   166 
   201         return false;
   167     m_viewPortWidth  = size.width()  - m_padding * 2;
   202     }
   168     m_viewPortHeight = size.height() - m_padding * 2;
   203 
   169     
   204     QKeyEvent * keyEvent = static_cast<QKeyEvent*>(event);
   170     m_viewPort->setGeometry(
   205 
       
   206     switch (keyEvent->key()) {
       
   207     case Qt::Key_Select:
       
   208     case Qt::Key_Return:
       
   209     case Qt::Key_Enter:
       
   210         // Signal that a carriage return-like key-press happened.
       
   211         emit activated();
       
   212         return true;
       
   213 
       
   214     case Qt::Key_Down:
       
   215     case Qt::Key_Up:
       
   216         // Swallow arrow up/down keys, editor has just one line.
       
   217         return true;
       
   218 
       
   219     default:
       
   220         return false;
       
   221     }
       
   222 }
       
   223 
       
   224 void UrlSearchSnippet::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
       
   225 {
       
   226     // Make sure any required horizontal scrolling happens
       
   227     // before rendering UrlEditorWidget.
       
   228 
       
   229     makeVisible(m_editor->cursorX());
       
   230 
       
   231     NativeChromeItem::paint(painter, option,widget);
       
   232 
       
   233     painter->save();
       
   234 
       
   235     painter->setRenderHint(QPainter::Antialiasing);
       
   236     painter->setBrush(m_backgroundColor);
       
   237 
       
   238     // First, do progress bar.
       
   239 
       
   240     QRectF g = boundingRect();
       
   241     g.setWidth(g.width() * m_percent / 100.0);
       
   242     painter->fillRect(g, QColor::fromRgb(0, 200, 200, 50));
       
   243 
       
   244     // Next, background matte.
       
   245 
       
   246     if (m_border > 0) {
       
   247         QPen pen;
       
   248         pen.setWidth(m_border);
       
   249         pen.setBrush(m_borderColor);
       
   250         painter->setPen(pen);
       
   251     }
       
   252 
       
   253     QPainterPath background;
       
   254     background.addRect(boundingRect());
       
   255     background.addRoundedRect(
       
   256             m_padding,
   171             m_padding,
   257             m_padding,
   172             m_padding,
   258             m_viewPortWidth,
   173             m_viewPortWidth,
   259             m_viewPortHeight,
   174             m_viewPortHeight);
   260             4,
   175 
   261             4);
   176     qreal w = m_iconWidth;
   262     painter->drawPath(background);
   177     qreal h = m_iconHeight;
   263 
   178 
   264     painter->restore();
   179     m_urlSearchBtn->setGeometry(
   265 }
   180 	    m_viewPortWidth - w - m_padding/2,
   266 
   181 	    (m_viewPortHeight - h)/2,
   267 void UrlSearchSnippet::resizeEvent(QGraphicsSceneResizeEvent * event)
   182 	    w,
   268 {
   183 	    h);
   269     QSizeF size = event->newSize();
   184 
   270 
   185     m_urlSearchEditor->setGeometry(0,
   271     m_viewPort->resize(size);
   186             0,
   272 
       
   273     m_viewPortWidth  = size.width()  - m_padding * 2;
       
   274     m_viewPortHeight = size.height() - m_padding * 2;
       
   275 
       
   276     m_viewPort->setGeometry(
       
   277             m_padding,
       
   278             (size.height() - m_editor->boundingRect().height()) / 2,
       
   279             m_viewPortWidth,
   187             m_viewPortWidth,
   280             m_viewPortHeight);
   188             m_viewPortHeight);
   281 
   189 
   282     m_editor->setTextWidth(m_viewPortWidth);
   190 }
   283 }
   191 
   284 
   192 void GUrlSearchItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
   285 void UrlSearchSnippet::resize()
   193 {
   286 {
   194     Q_UNUSED(option);
   287     QWebElement we = m_snippet->element();
   195     Q_UNUSED(widget);
   288 
   196 
   289     QRectF g = we.geometry();
   197     painter->save();
   290 
   198     painter->setRenderHint(QPainter::Antialiasing);
   291     qreal newWidth  = g.width();
   199 
   292 
   200     if (m_padding > 0 || m_border > 0) {
   293     qreal newHeight = g.height();
   201         QPainterPath border;
   294 
   202         border.addRect(boundingRect());
   295     QGraphicsWidget::resize(newWidth, newHeight);
   203         border.addRoundedRect(
   296 }
   204                 m_padding,
   297 
   205                 m_padding,
   298 void UrlSearchSnippet::setUrlText(const QString & text)
   206                 m_viewPortWidth,
   299 {
   207                 m_viewPortHeight,
   300     m_editor->setText(text);
   208                 4,
   301     m_editor->setPos(0, m_editor->pos().y());
   209                 4);
   302 
   210 
   303     makeVisible(m_editor->cursorX());
   211         if (m_padding > 0) {
   304 }
   212             painter->fillPath(border, m_borderColor);
   305 
   213         }
   306 void UrlSearchSnippet::setStarted()
   214 
   307 {
   215         if (m_border > 0) {
       
   216             QPen pen;
       
   217             pen.setWidth(m_border);
       
   218             pen.setBrush(m_borderColor);
       
   219             painter->setPen(pen);
       
   220             painter->drawPath(border);
       
   221         }
       
   222     }
       
   223 
       
   224     painter->restore();
       
   225     NativeChromeItem::paint(painter, option, widget);
       
   226 }
       
   227 
       
   228 void GUrlSearchItem::onChromeComplete()
       
   229 {
       
   230     setStarted();
       
   231 
       
   232     WRT::WindowFlowView* windowView = static_cast<WRT::WindowFlowView *>(m_chrome->viewController()->view("WindowView"));
       
   233     safe_connect(windowView, SIGNAL(newWindowTransitionComplete()), this, SLOT(onNewWindowTransitionComplete()));
       
   234 
       
   235     PageSnippet * suggestSnippet = qobject_cast<PageSnippet*>(m_chrome->getSnippet("SuggestsChromeId"));
       
   236 
       
   237     // instantiate items needed to display suggest page snippet
       
   238     if (suggestSnippet) {
       
   239         suggestSnippet->instantiate();
       
   240     }
       
   241 }
       
   242 
       
   243 void GUrlSearchItem::setStarted()
       
   244 {
       
   245     WebPageController * pageController = WebPageController::getSingleton();
       
   246     ViewController * viewController = m_chrome->viewController();
       
   247 
       
   248     m_urlSearchEditor->setText(pageController->currentRequestedUrl());
       
   249     ControllableViewBase* curView = viewController->currentView();
       
   250     if (curView && curView->type() == "webView") {
       
   251         GWebContentView * gView = qobject_cast<GWebContentView*> (curView);
       
   252         bool isSuperPage = gView ? gView->currentPageIsSuperPage() : false;
       
   253         if(!isSuperPage)
       
   254             m_chrome->slideView(100);
       
   255     }
   308     // Strictly speaking we should set progress to 0.
   256     // Strictly speaking we should set progress to 0.
   309     // But set it higher to give immediate visual feedback
   257     // But set it higher to give immediate visual feedback
   310     // that something is happening.
   258     // that something is happening.
   311 
   259 
   312     int progress = 0;
   260     int progress = 0;
   313 
   261 
   314     WebPageController * pageController = WebPageController::getSingleton();
       
   315 
       
   316     if (pageController->isPageLoading()) {
   262     if (pageController->isPageLoading()) {
   317         progress = 5;
   263         progress = 5;
   318     }
   264     }
   319 
   265 
   320     setProgress(progress);
   266     m_urlSearchEditor->setProgress(progress);
   321 }
   267     updateUrlSearchBtn();
   322 
   268 }
   323 void UrlSearchSnippet::setProgress(int percent)
   269 
   324 {
   270 void GUrlSearchItem::setProgress(int percent)
   325     m_percent = percent;
   271 {
   326     update();
   272     m_urlSearchEditor->setProgress(percent);
   327 }
   273 }
   328 
   274 
   329 // Wait a half-second before actually clearing the progress bar.
   275 // Wait a half-second before actually clearing the progress bar.
   330 //
   276 //
   331 // We have to be careful of the following two use cases:
   277 // We have to be careful of the following two use cases:
   348 //    We don't want to clear the progress bar in the first call to
   294 //    We don't want to clear the progress bar in the first call to
   349 //    clearProgress() because we want the progress bar to retain its
   295 //    clearProgress() because we want the progress bar to retain its
   350 //    appearance for the full timeout period.  We manage this by
   296 //    appearance for the full timeout period.  We manage this by
   351 //    tracking the number of pending calls to clearProgress() and
   297 //    tracking the number of pending calls to clearProgress() and
   352 //    only clearing the progress bar when that number becomes 0.
   298 //    only clearing the progress bar when that number becomes 0.
   353 
   299 void GUrlSearchItem::setFinished(bool ok)
   354 void UrlSearchSnippet::setFinished(bool ok)
   300 {
   355 {
   301     WebPageController * pageController = WebPageController::getSingleton();
   356     if (ok) {
   302     // If the load was finished normally and not due to user stopping it,
   357         setProgress(99);
   303     // simulate progress completion
   358     }
   304     if (!pageController->loadCanceled())
       
   305         m_urlSearchEditor->setProgress(100);
       
   306 
       
   307     if (ok)
       
   308         m_urlSearchEditor->setText(formattedUrl());
       
   309 
       
   310     m_urlSearchEditor->removeFocus();
       
   311 
       
   312     ViewController * viewController = m_chrome->viewController();
       
   313     ControllableViewBase* curView = viewController->currentView();
       
   314     if (curView && curView->type() == "webView" && pageController->contentsYPos() > 0)
       
   315         m_chrome->slideView(-100);
   359 
   316 
   360     ++m_pendingClearCalls;
   317     ++m_pendingClearCalls;
   361 
   318 
   362     QTimer::singleShot(500, this, SLOT(clearProgress()));
   319     QTimer::singleShot(500, this, SLOT(clearProgress()));
   363 }
   320 }
   364 
   321 
   365 void UrlSearchSnippet::clearProgress()
   322 void GUrlSearchItem::setPageCreated()
       
   323 {
       
   324     // remove slideview(100) since the new transition for the code-driven window
       
   325     //m_chrome->slideView(100);
       
   326 }
       
   327 
       
   328 void GUrlSearchItem::setPageChanged()
       
   329 {
       
   330     m_urlSearchEditor->setText(formattedUrl());
       
   331     updateUrlSearchBtn();
       
   332 
       
   333     WebPageController * pageController = WebPageController::getSingleton();
       
   334     int progress = pageController->loadProgressValue();
       
   335     if (progress == 100)
       
   336         m_urlSearchEditor->removeFocus();
       
   337 }
       
   338 
       
   339 void GUrlSearchItem::clearProgress()
   366 {
   340 {
   367     --m_pendingClearCalls;
   341     --m_pendingClearCalls;
   368 
   342 
   369     if (m_pendingClearCalls > 0) {
   343     if (m_pendingClearCalls > 0) {
   370         return;
   344         return;
   371     }
   345     }
   372 
   346 
   373     WebPageController * pageController = WebPageController::getSingleton();
   347     WebPageController * pageController = WebPageController::getSingleton();
   374 
       
   375     if (pageController->isPageLoading()) {
   348     if (pageController->isPageLoading()) {
   376         return;
   349         return;
   377     }
   350     }
   378 
   351     m_urlSearchEditor->setProgress(0);
   379     setProgress(0);
   352     updateUrlSearchBtn();
   380 }
   353 }
   381 
   354 
   382 void UrlSearchSnippet::viewChanged()
   355 void GUrlSearchItem::viewChanged()
   383 {
   356 {
   384     WebPageController * pageController = WebPageController::getSingleton();
   357     ViewController * viewController = m_chrome->viewController();
   385 
   358     WebPageController * pageController = WebPageController::getSingleton();
   386     setUrlText(pageController->currentDocUrl());
   359 
   387 
   360     ControllableViewBase* curView = viewController->currentView();
   388     int progress = pageController->loadProgressValue();
   361     GWebContentView * gView = qobject_cast<GWebContentView*> (curView);
   389     if (progress >= 100) {
   362     bool isSuperPage = gView ? gView->currentPageIsSuperPage() : false;
   390         progress = 0;
   363 
   391     }
   364     // view changes to web content view
   392     setProgress(progress);
   365     if (curView && curView->type() == "webView" && !isSuperPage) {
   393 }
   366         m_urlSearchEditor->setText(formattedUrl());
   394 
   367         int progress = pageController->loadProgressValue();
   395 // We divide the viewport into 3 distinct regions:
   368         if (progress >= 100)
   396 //
   369             progress = 0;
   397 //
   370         m_urlSearchEditor->setProgress(progress);
   398 //        [ left | middle | right ]
   371         updateUrlSearchBtn();
   399 //
   372 
   400 // [ editor, shifted left by editorShift pixels ]
   373         // place focus in urlsearch bar when returning from adding a new window in windows view
   401 //
   374         if (pageController->loadText() == "") {
   402 // When a cursor is in the middle section of the viewport we
   375             if (m_backFromNewWinTrans ) {
   403 // leave the editor shift unchanged, to preserve stability.
   376                 m_backFromNewWinTrans = false;
   404 //
   377                 WebPageController * pageController = WebPageController::getSingleton();
   405 // When a cursor is in the right section or beyond we shift
   378                 m_urlSearchEditor->setText(pageController->currentRequestedUrl());
   406 // the editor left until the cursor appears at the border
   379             }
   407 // between the middle and right sections.
   380             else {
   408 //
   381                 m_urlSearchEditor->grabFocus();
   409 // When a cursor is in the left section or beyond we shift
   382             }
   410 // the editor right until the cursor appears at the border
   383         }
   411 // between the left and middle sections.
   384         if (!isSuperPage  && (pageController->contentsYPos() <= 0 || pageController->isPageLoading())){
   412 //
   385             m_chrome->slideView(100);
   413 // We never shift the editor right of the viewport.
   386         } else {
   414 
   387             m_chrome->slideView(-100);
   415 void UrlSearchSnippet::makeVisible(qreal cursorX)
   388         }
   416 {
   389         m_backFromNewWinTrans = false;
   417     qreal leftScrollBorder  = 0;
   390     } else {
   418 
   391          pageController->urlTextChanged(m_urlSearchEditor->text());
   419     qreal rightScrollBorder = m_viewPortWidth - 10;
   392          // Remove progress bar and url text field value so that
   420 
   393          // incorrect values are not seen before we can update when we come back
   421     qreal editorShift = -1 * m_editor->pos().x();
   394          m_urlSearchEditor->setText("");
   422 
   395          m_urlSearchEditor->setProgress(0);
   423     qreal localX = cursorX - editorShift;
   396          m_chrome->slideView(-100);
   424 
   397     }
   425     if (localX < leftScrollBorder) {
   398 }
   426         // Before left section, scroll right.
   399 
   427         // In left section, scroll right.
   400 void GUrlSearchItem::urlSearchActivatedByEnterKey()
   428         qreal shift = qMin(leftScrollBorder - localX, editorShift);
   401 {
   429         m_editor->moveBy(shift, 0);
   402     m_urlSearchEditor->removeFocus();
   430         return;
   403     urlSearchActivated();
   431     }
   404 }
   432 
   405 
   433     if (localX >= rightScrollBorder) {
   406 void GUrlSearchItem::urlSearchActivated()
   434         // In right section, scroll left.
   407 {
   435         // After right section, scroll left.
   408     WebPageController * pageController = WebPageController::getSingleton();
   436         qreal shift = localX - rightScrollBorder;
   409     switch (pageController->loadState()) {
   437         m_editor->moveBy(-shift, 0);
   410         case WRT::LoadController::GotoModeLoading:
   438         return;
   411             pageController->currentStop();
   439     }
   412             ++m_pendingClearCalls;
   440 
   413             QTimer::singleShot(500, this, SLOT(clearProgress()));
   441     // In middle section, no scroll needed.
   414             break;
   442     return;
   415         case WRT::LoadController::GotoModeEditing:
       
   416             loadToMainWindow();
       
   417             break;
       
   418         case WRT::LoadController::GotoModeReloadable:
       
   419             if (pageController->currentDocUrl() == m_urlSearchEditor->text())
       
   420                 pageController->currentReload();
       
   421             else
       
   422                 loadToMainWindow();
       
   423             break;
       
   424         default:
       
   425             qDebug() << "Incorrect state";
       
   426             break;
       
   427     }
       
   428     updateUrlSearchBtn();
       
   429 }
       
   430 
       
   431 void GUrlSearchItem::updateUrlSearchBtn()
       
   432 {
       
   433     WebPageController * pageController = WebPageController::getSingleton();
       
   434     switch (pageController->loadState()) {
       
   435         case WRT::LoadController::GotoModeLoading:
       
   436             m_urlSearchBtn->addIcon(STOP_BUTTON_ICON);
       
   437             break;
       
   438         case WRT::LoadController::GotoModeEditing:
       
   439             m_urlSearchBtn->addIcon(GO_BUTTON_ICON);
       
   440             break;
       
   441         case WRT::LoadController::GotoModeReloadable:
       
   442             m_urlSearchBtn->addIcon(REFRESH_BUTTON_ICON);
       
   443             break;
       
   444         default:
       
   445             qDebug() << "Incorrect state";
       
   446             break;
       
   447     }
       
   448     m_urlSearchBtn->update();
       
   449     
       
   450     // notify suggest object of changes in load state
       
   451     PageSnippet * suggestSnippet = qobject_cast<PageSnippet*>(m_chrome->getSnippet("SuggestsChromeId"));
       
   452     if (suggestSnippet) {
       
   453         QString cmd = "searchSuggests.updateLoadState();";
       
   454         suggestSnippet->evaluateJavaScript(cmd);
       
   455     }
       
   456 }
       
   457 
       
   458 void GUrlSearchItem::loadToMainWindow()
       
   459 {
       
   460     QString url = m_urlSearchEditor->text();
       
   461     WebPageController * pageController = WebPageController::getSingleton();
       
   462     QString gotourl = pageController->guessUrlFromString(url);
       
   463     m_urlSearchEditor->setText(gotourl);
       
   464     pageController->currentLoad(gotourl);
       
   465     pageController->urlTextChanged(gotourl);
       
   466 }
       
   467 
       
   468 void GUrlSearchItem::updateLoadState()
       
   469 {
       
   470     WebPageController * pageController = WebPageController::getSingleton();
       
   471     if (pageController->loadState() == WRT::LoadController::GotoModeReloadable) {
       
   472         pageController->setLoadState(WRT::LoadController::GotoModeEditing);
       
   473         updateUrlSearchBtn();
       
   474     }
       
   475 }
       
   476 
       
   477 void GUrlSearchItem::updateLoadStateAndSuggest()
       
   478 {
       
   479     updateLoadState();
       
   480     PageSnippet * suggestSnippet = qobject_cast<PageSnippet*>(m_chrome->getSnippet("SuggestsChromeId"));
       
   481     if (suggestSnippet) {
       
   482         QString cmd = "searchSuggests.updateUserInput();";
       
   483         suggestSnippet->evaluateJavaScript(cmd);
       
   484     }
       
   485 }
       
   486 
       
   487 void GUrlSearchItem::tapped(QPointF& pos)
       
   488 {
       
   489     bool hitText = m_urlSearchEditor->tappedOnText(pos.x());
       
   490     if (!m_justFocusIn && !hitText)
       
   491         m_urlSearchEditor->unselect();
       
   492 
       
   493     if (m_justFocusIn) {
       
   494         m_justFocusIn = false;
       
   495         if (hitText && !m_urlSearchEditor->hasSelection())
       
   496             m_urlSearchEditor->selectAll();
       
   497     }
       
   498 }
       
   499 
       
   500 void GUrlSearchItem::focusChanged(bool focusIn)
       
   501 {
       
   502     if (focusIn)
       
   503         m_justFocusIn = true;
       
   504     else {
       
   505         m_justFocusIn = false;
       
   506         m_urlSearchEditor->unselect();
       
   507         m_urlSearchEditor->shiftToLeftEnd();
       
   508     }
       
   509 }
       
   510 
       
   511 void GUrlSearchItem::resize()
       
   512 {
       
   513     QWebElement we = m_snippet->element();
       
   514     QRectF g = we.geometry();
       
   515     qreal newWidth  = g.width();
       
   516     qreal newHeight = g.height();
       
   517     QGraphicsWidget::resize(newWidth, newHeight);
       
   518 }
       
   519 
       
   520 void GUrlSearchItem::onNewWindowTransitionComplete()
       
   521 {
       
   522     m_backFromNewWinTrans = true;
       
   523 }
       
   524 
       
   525 QString GUrlSearchItem::formattedUrl() const
       
   526 {
       
   527     WebPageController * pageController = WebPageController::getSingleton();
       
   528     return pageController->currentDocUrl().replace(" ","+");
       
   529 }
       
   530 
       
   531 GUrlSearchSnippet::GUrlSearchSnippet(const QString & elementId, ChromeWidget * chrome,
       
   532                          QGraphicsWidget * widget, const QWebElement & element)
       
   533   : ChromeSnippet(elementId, chrome, widget, element)
       
   534 {
       
   535 }
       
   536 
       
   537 inline GUrlSearchItem* GUrlSearchSnippet::urlSearchItem()
       
   538 {
       
   539     return static_cast<GUrlSearchItem *>(widget());
       
   540 }
       
   541 
       
   542 inline GUrlSearchItem const * GUrlSearchSnippet::constUrlSearchItem() const
       
   543 {
       
   544     return static_cast<GUrlSearchItem const *>(constWidget());
       
   545 }
       
   546 
       
   547 QString GUrlSearchSnippet::url() const
       
   548 {
       
   549     return constUrlSearchItem()->url();
       
   550 }
       
   551 
       
   552 void GUrlSearchSnippet::setUrl(const QString &url)
       
   553 {
       
   554     urlSearchItem()->setUrl(url);
   443 }
   555 }
   444 
   556 
   445 } // namespace GVA
   557 } // namespace GVA