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