ginebra2/ChromeDOM.cpp
author hgs
Fri, 06 Aug 2010 17:23:08 -0400
changeset 9 b39122337a00
parent 3 0954f5dd2cd0
child 12 afcd8e6d025b
permissions -rw-r--r--
201031
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     1
/*
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     3
* All rights reserved.
3
hgs
parents: 0
diff changeset
     4
*
hgs
parents: 0
diff changeset
     5
* This program is free software: you can redistribute it and/or modify
hgs
parents: 0
diff changeset
     6
* it under the terms of the GNU Lesser General Public License as published by
hgs
parents: 0
diff changeset
     7
* the Free Software Foundation, version 2.1 of the License.
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     8
*
3
hgs
parents: 0
diff changeset
     9
* This program is distributed in the hope that it will be useful,
hgs
parents: 0
diff changeset
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
hgs
parents: 0
diff changeset
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
hgs
parents: 0
diff changeset
    12
* GNU Lesser General Public License for more details.
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    13
*
3
hgs
parents: 0
diff changeset
    14
* You should have received a copy of the GNU Lesser General Public License
hgs
parents: 0
diff changeset
    15
* along with this program.  If not,
hgs
parents: 0
diff changeset
    16
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    17
*
3
hgs
parents: 0
diff changeset
    18
* Description:
hgs
parents: 0
diff changeset
    19
* 
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    20
*
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    21
*/
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    22
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    23
#include <QtGui>
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    24
#include <QWebElement>
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    25
#include <QWebFrame>
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    26
#include <QWebPage>
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    27
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    28
#include "ChromeDOM.h"
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    29
#include "ChromeRenderer.h"
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    30
#include "ChromeSnippet.h"
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    31
#include "ChromeWidget.h"
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    32
#include "WebChromeSnippet.h"
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    33
#include "WebChromeContainerSnippet.h"
3
hgs
parents: 0
diff changeset
    34
#include "PageSnippet.h"
hgs
parents: 0
diff changeset
    35
#include "PageItem.h"
hgs
parents: 0
diff changeset
    36
#include "ProgressBarItem.h"
hgs
parents: 0
diff changeset
    37
#include "ProgressBarSnippet.h"
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    38
#include "ToolbarChromeItem.h"
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    39
#include "ContentToolbarChromeItem.h"
3
hgs
parents: 0
diff changeset
    40
#include "ContentToolbarSnippet.h"
hgs
parents: 0
diff changeset
    41
#include "WindowToolbarSnippet.h"
hgs
parents: 0
diff changeset
    42
#include "RecentUrlToolbarSnippet.h"
hgs
parents: 0
diff changeset
    43
#include "BookmarksToolbarSnippet.h"
hgs
parents: 0
diff changeset
    44
#include "SettingsToolbarSnippet.h"
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    45
#include "ActionButton.h"
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    46
#include "ActionButtonSnippet.h"
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    47
#include "mostvisitedpageview.h"
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    48
#include "mostvisitedsnippet.h"
3
hgs
parents: 0
diff changeset
    49
#include "UrlSearchSnippet.h"
hgs
parents: 0
diff changeset
    50
#include "EditorSnippet.h"
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    51
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    52
#include <QDebug>
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    53
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    54
//TODO: Replace JS strings with DOM api. Make stateful: i.e. get the doc element from the current page
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    55
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    56
namespace GVA {
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    57
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    58
  ChromeDOM::ChromeDOM(QWebPage * page, ChromeWidget * chrome)
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    59
    : m_page(page),
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    60
      m_chrome(chrome),
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    61
      m_height(0),
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    62
      m_bytes(0)
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    63
  {
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    64
    m_renderer = m_chrome->renderer();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    65
    m_renderer->clearRenderList();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    66
  }
3
hgs
parents: 0
diff changeset
    67
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    68
  ChromeDOM::~ChromeDOM()
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    69
  {
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    70
  }
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    71
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    72
  QVariant ChromeDOM::evalInChromeContext(QString js){
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    73
    return m_page->mainFrame()->evaluateJavaScript(js);
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    74
  }
3
hgs
parents: 0
diff changeset
    75
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    76
  QWebElement ChromeDOM::getElementById(const QString &id)
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    77
  {
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    78
    return m_page->mainFrame()->documentElement().findFirst("#" + id);
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    79
  }
3
hgs
parents: 0
diff changeset
    80
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    81
  QRect ChromeDOM::getElementRect(const QString &id)
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    82
  {
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    83
    return getElementById(id).geometry();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    84
  }
3
hgs
parents: 0
diff changeset
    85
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    86
  QSize ChromeDOM::getElementSize(const QString &id)
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    87
  {
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    88
    QRect rect = getElementRect(id);
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    89
    return QSize(rect.width(), rect.height());
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    90
  }
3
hgs
parents: 0
diff changeset
    91
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    92
  QString ChromeDOM::getElementAttribute(const QString &id, const QString &attribute)
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    93
  {
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    94
    return getElementById(id).attribute(attribute);
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    95
  }
3
hgs
parents: 0
diff changeset
    96
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    97
  //Get the cacheable script element. Only one is allowed so get the first one.
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    98
  /*QString ChromeDOM::getCacheableScript()
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    99
  {
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   100
    QWebElement doc = m_page->mainFrame()->documentElement();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   101
    return doc.findAll("script.GinebraCacheable").toList()[0].toPlainText();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   102
  }
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   103
  */
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   104
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   105
  //Get a list of cached handlers for a snippet
3
hgs
parents: 0
diff changeset
   106
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   107
  QList<CachedHandler> ChromeDOM::getCachedHandlers(const QString &elementId, const QRectF & ownerArea)
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   108
  {
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   109
    QWebElement snippet = getElementById(elementId);
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   110
    QList <QWebElement> controls =  snippet.findAll(".GinebraCached").toList();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   111
    QList <CachedHandler> handlers;
3
hgs
parents: 0
diff changeset
   112
    for (int i = 0; i < controls.size(); i++){
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   113
      QWebElement elem = controls.at(i);
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   114
      //Element rectangle relative to snippet, so we can handle mouse events relative to snippet
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   115
      //qDebug() << "====> Owner X: " << ownerArea.x() << " Owner Width: " << ownerArea.width() << " Elem X: " << elem.geometry().x() << " Elem Width: " << elem.geometry().width();
3
hgs
parents: 0
diff changeset
   116
      QRectF elemRect(elem.geometry().x() - ownerArea.x(), elem.geometry().y() - ownerArea.y(), elem.geometry().width(), elem.geometry().height());
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   117
      //NB: For now we handle only onclick from cache. Should add at least long-press too.
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   118
      CachedHandler handler(elem.attribute("id"), elem.attribute("data-GinebraOnClick"), elemRect, m_chrome, elem.attribute("data-GinebraTargetView"));
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   119
      //qDebug() << "Cached handler" << handler.elementId() << ": "  << handler.script() << ": "  << handler.rect();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   120
      handlers.append(handler);
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   121
    }
3
hgs
parents: 0
diff changeset
   122
    return handlers;
hgs
parents: 0
diff changeset
   123
  }
hgs
parents: 0
diff changeset
   124
  
hgs
parents: 0
diff changeset
   125
hgs
parents: 0
diff changeset
   126
//TODO: Get rid of rectangle argument to snippets. This is redundant with the element argument!!
hgs
parents: 0
diff changeset
   127
//TODO: Rewrite using function table
hgs
parents: 0
diff changeset
   128
  
hgs
parents: 0
diff changeset
   129
  ChromeSnippet * ChromeDOM::nativeSnippetForClassName(const QString & className, const QString elementId,  QWebElement element)
hgs
parents: 0
diff changeset
   130
  {
hgs
parents: 0
diff changeset
   131
      QRectF rect = element.geometry();
hgs
parents: 0
diff changeset
   132
      
hgs
parents: 0
diff changeset
   133
      if (className == "ContentToolbar") {
hgs
parents: 0
diff changeset
   134
          return ContentToolbarSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   135
      }
hgs
parents: 0
diff changeset
   136
      else if (className == "WindowToolbar") {
hgs
parents: 0
diff changeset
   137
          return WindowToolbarSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   138
      }
hgs
parents: 0
diff changeset
   139
      else if (className == "RecentUrlToolbar") {
hgs
parents: 0
diff changeset
   140
          return RecentUrlToolbarSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   141
      }
hgs
parents: 0
diff changeset
   142
      else if (className == "BookmarksToolbar") {
hgs
parents: 0
diff changeset
   143
          return BookmarksToolbarSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   144
      }
hgs
parents: 0
diff changeset
   145
      else if (className == "SettingsToolbar") {
hgs
parents: 0
diff changeset
   146
          return SettingsToolbarSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   147
      }
hgs
parents: 0
diff changeset
   148
      else if (className == "MostVisitedPagesWidget") {
hgs
parents: 0
diff changeset
   149
          return MostVisitedSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   150
      }
hgs
parents: 0
diff changeset
   151
      else if (className == "ActionButton") {
hgs
parents: 0
diff changeset
   152
          return ActionButtonSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   153
      }
hgs
parents: 0
diff changeset
   154
      else if (className == "PageSnippet") {
hgs
parents: 0
diff changeset
   155
          return PageSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   156
      }
hgs
parents: 0
diff changeset
   157
      else if (className == "UrlSearchSnippet") {
hgs
parents: 0
diff changeset
   158
          return GUrlSearchSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   159
      }
hgs
parents: 0
diff changeset
   160
      else if (className == "TextEditSnippet") {
hgs
parents: 0
diff changeset
   161
          return EditorSnippet::instance(elementId, m_chrome, element);
hgs
parents: 0
diff changeset
   162
      }
hgs
parents: 0
diff changeset
   163
      else {
hgs
parents: 0
diff changeset
   164
          ChromeSnippet* result = new ChromeSnippet(elementId, m_chrome, 0, element);
hgs
parents: 0
diff changeset
   165
          result->setChromeWidget(new QGraphicsWidget());
hgs
parents: 0
diff changeset
   166
          return result;
hgs
parents: 0
diff changeset
   167
      }
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   168
  }
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   169
  
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   170
  ChromeSnippet *ChromeDOM::getSnippet(const QString &docElementId, QGraphicsItem* parent) {
3
hgs
parents: 0
diff changeset
   171
    Q_UNUSED(parent)
hgs
parents: 0
diff changeset
   172
hgs
parents: 0
diff changeset
   173
    ChromeSnippet * snippet = 0;
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   174
    QWebElement doc = m_page->mainFrame()->documentElement();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   175
    QWebElement element = doc.findFirst("#" + docElementId);
3
hgs
parents: 0
diff changeset
   176
    QRect rect = element.geometry();
hgs
parents: 0
diff changeset
   177
    //TODO: This may not be accurate since final heights may not have been computed at this point!!
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   178
    m_height += rect.height();
3
hgs
parents: 0
diff changeset
   179
    
hgs
parents: 0
diff changeset
   180
    //    qDebug() << "Snippet: ID: " << docElementId << " Owner Area: " << rect << " Element Rect: " << element.geometry();
hgs
parents: 0
diff changeset
   181
  
hgs
parents: 0
diff changeset
   182
    if (!rect.isNull()) {
hgs
parents: 0
diff changeset
   183
        QString className = element.attribute("data-GinebraNativeClass", "__NO_CLASS__");
hgs
parents: 0
diff changeset
   184
        if (className == "__NO_CLASS__") {
hgs
parents: 0
diff changeset
   185
            if (element.attribute("data-GinebraContainer", "false") == "true") {
hgs
parents: 0
diff changeset
   186
                snippet = new WebChromeContainerSnippet(docElementId, m_chrome, element);
hgs
parents: 0
diff changeset
   187
                snippet->setChromeWidget(new ChromeItem(snippet));
hgs
parents: 0
diff changeset
   188
            }
hgs
parents: 0
diff changeset
   189
            else {
hgs
parents: 0
diff changeset
   190
                snippet = new WebChromeSnippet(docElementId, m_chrome, element);
hgs
parents: 0
diff changeset
   191
                m_renderer->addRenderItem((static_cast<WebChromeSnippet*> (snippet))->item());
hgs
parents: 0
diff changeset
   192
            }
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   193
        }
3
hgs
parents: 0
diff changeset
   194
        else {
hgs
parents: 0
diff changeset
   195
            snippet = nativeSnippetForClassName(className, docElementId, element);
hgs
parents: 0
diff changeset
   196
            //TODO: Is the following still needed?
hgs
parents: 0
diff changeset
   197
            QGraphicsWidget * widget = snippet->widget();
hgs
parents: 0
diff changeset
   198
            //Have snippet determine its own size when in anchor layout. Again, these will not
hgs
parents: 0
diff changeset
   199
            //necessarily be accurate at this point.
hgs
parents: 0
diff changeset
   200
            widget->resize(rect.width(), rect.height());
hgs
parents: 0
diff changeset
   201
            widget->setPreferredSize(rect.width(), rect.height());
hgs
parents: 0
diff changeset
   202
            widget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
hgs
parents: 0
diff changeset
   203
            //Have snippet determine its own location when NOT in anchor layout
hgs
parents: 0
diff changeset
   204
            widget->setPos(rect.x(), rect.y());
hgs
parents: 0
diff changeset
   205
            
hgs
parents: 0
diff changeset
   206
        }
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   207
3
hgs
parents: 0
diff changeset
   208
        QWebElement parentElem;
hgs
parents: 0
diff changeset
   209
hgs
parents: 0
diff changeset
   210
        if (!(parentElem = findChromeParent(element)).isNull()) {
hgs
parents: 0
diff changeset
   211
            snippet->setParentId(parentElem.attribute("id"));
hgs
parents: 0
diff changeset
   212
        }
hgs
parents: 0
diff changeset
   213
        //Set auto-layout attributes
hgs
parents: 0
diff changeset
   214
        snippet->setAnchor(element.attribute("data-GinebraAnchor", "AnchorNone"), false);
hgs
parents: 0
diff changeset
   215
        snippet->setAnchorOffset(element.attribute("data-GinebraAnchorOffset", "0").toInt());
hgs
parents: 0
diff changeset
   216
        snippet->setInitiallyVisible(element.attribute("data-GinebraVisible", "false") == "true");
hgs
parents: 0
diff changeset
   217
        snippet->setHidesContent(element.attribute("data-GinebraHidesContent", "false") == "true");
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   218
    }
3
hgs
parents: 0
diff changeset
   219
    return snippet;
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   220
  }
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   221
  
3
hgs
parents: 0
diff changeset
   222
  QWebElement ChromeDOM::findChromeParent(QWebElement element)
hgs
parents: 0
diff changeset
   223
  {
hgs
parents: 0
diff changeset
   224
    while(!(element = element.parent()).isNull()){
hgs
parents: 0
diff changeset
   225
      if (element.attribute("class") == "GinebraSnippet"){
hgs
parents: 0
diff changeset
   226
	return element;
hgs
parents: 0
diff changeset
   227
      }
hgs
parents: 0
diff changeset
   228
    }
hgs
parents: 0
diff changeset
   229
    return element;
hgs
parents: 0
diff changeset
   230
  }
hgs
parents: 0
diff changeset
   231
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   232
  QList <QWebElement> ChromeDOM::getInitialElements()
3
hgs
parents: 0
diff changeset
   233
  {
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   234
    m_renderer->clearRenderList();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   235
    QWebElement test = getElementById("TestTableCell9");
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   236
    //qDebug() << "TEST ELEMENT:" << test.toPlainText();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   237
    m_height = 0;
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   238
    QWebElement doc = m_page->mainFrame()->documentElement();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   239
#if QT_VERSION < 0x040600 //TBD: Do we care, given that the dom api is not officially supported before 4.6?
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   240
    return doc.findAll(".GinebraSnippet");
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   241
#else
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   242
    return doc.findAll(".GinebraSnippet").toList();
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   243
#endif
3
hgs
parents: 0
diff changeset
   244
  }
hgs
parents: 0
diff changeset
   245
0
1450b09d0cfd Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   246
} // end of namespace GVA