ginebra2/PopupWebChromeItem.cpp
changeset 6 1c3b8676e58c
parent 5 0f2326c2a325
equal deleted inserted replaced
5:0f2326c2a325 6:1c3b8676e58c
    20 */
    20 */
    21 
    21 
    22 #include "PopupWebChromeItem.h"
    22 #include "PopupWebChromeItem.h"
    23 #include "ChromeWidget.h"
    23 #include "ChromeWidget.h"
    24 #include "WebChromeSnippet.h"
    24 #include "WebChromeSnippet.h"
       
    25 #include "ExternalEventCharm.h"
    25 
    26 
    26 namespace GVA {
    27 namespace GVA {
    27 
    28 
    28 PopupWebChromeItem::PopupWebChromeItem(
    29 PopupWebChromeItem::PopupWebChromeItem(
    29         const QRectF & ownerArea,
       
    30         ChromeWidget * chrome,
    30         ChromeWidget * chrome,
    31         const QWebElement & element,
    31         const QWebElement & element,
    32         QGraphicsItem * parent,
    32         QGraphicsItem * parent,
    33         bool modal)
    33         bool modal)
    34 : WebChromeItem(ownerArea, chrome, element, parent),
    34 : WebChromeItem(chrome, element, parent),
    35   m_modal(modal)
    35   m_modal(modal),
       
    36   m_externalEventCharm(0)
    36 {
    37 {
    37 }
    38 }
    38 
    39 
    39 PopupWebChromeItem::~PopupWebChromeItem()
    40 PopupWebChromeItem::~PopupWebChromeItem()
    40 {}
    41 {
       
    42     delete m_externalEventCharm;
       
    43 }
    41 
    44 
    42 void PopupWebChromeItem::init(WebChromeSnippet * snippet)
    45 void PopupWebChromeItem::init(WebChromeSnippet * snippet)
    43 {
    46 {
    44     WebChromeItem::init(snippet);
    47     WebChromeItem::init(snippet);
       
    48     m_externalEventCharm = new ExternalEventCharm(this);
    45 
    49 
    46     // Forward externalMouseEvent signals from context items.
    50     // Forward externalMouseEvent signals from context items.
    47     QObject::connect(
    51     QObject::connect(
    48             this,
    52             m_externalEventCharm,
    49             SIGNAL(externalMouseEvent(int, const QString &, const QString &)),
    53             SIGNAL(externalMouseEvent(QEvent *, const QString &, const QString &)),
    50             snippet,
    54             snippet,
    51             SIGNAL(externalMouseEvent(int, const QString &, const QString &)));
    55             SIGNAL(externalMouseEvent(QEvent *, const QString &, const QString &)));
    52 }
    56 }
    53 
    57 
    54 bool PopupWebChromeItem::event(QEvent * e)
    58 bool PopupWebChromeItem::event(QEvent * e)
    55 {
    59 {
    56     // Check for external events grabbed by this item.
       
    57 
       
    58     checkForExternalEvent(this, e);
       
    59 
       
    60     switch (e->type()) {
    60     switch (e->type()) {
    61       case QEvent::Show:
    61       case QEvent::Show:
    62         scene()->installEventFilter(this);
       
    63         if(snippet() && m_modal) {
    62         if(snippet() && m_modal) {
    64             chrome()->emitPopupShown(snippet()->objectName());
    63             chrome()->emitPopupShown(snippet()->objectName());
    65         }
    64         }
    66         break;
    65         break;
    67       case QEvent::Hide:
    66       case QEvent::Hide:
    68         scene()->removeEventFilter(this);
       
    69         if(snippet() && m_modal) {
    67         if(snippet() && m_modal) {
    70             chrome()->emitPopupHidden(snippet()->objectName());
    68             chrome()->emitPopupHidden(snippet()->objectName());
    71         }
    69         }
    72         break;
    70         break;
    73       default: break;
    71       default: break;
    76     // Let the parent class handle the event.
    74     // Let the parent class handle the event.
    77 
    75 
    78     return WebChromeItem::event(e);
    76     return WebChromeItem::event(e);
    79 }
    77 }
    80 
    78 
    81 bool PopupWebChromeItem::eventFilter(QObject * o, QEvent * e)
       
    82 {
       
    83     // Check for external events NOT grabbed by this item.
       
    84 
       
    85     checkForExternalEvent(o, e);
       
    86 
       
    87     // Don't filter any events.
       
    88 
       
    89     return false;
       
    90 }
       
    91 
       
    92 void PopupWebChromeItem::checkForExternalEvent(QObject * o, QEvent * e)
       
    93 {
       
    94     Q_UNUSED(o);
       
    95 
       
    96     // Ignore all events when this item is not showing.
       
    97 
       
    98     if (!isVisible()) {
       
    99         return;
       
   100     }
       
   101 
       
   102     // Ignore all but a few mouse press events.
       
   103 
       
   104     switch (e->type()) {
       
   105     case QEvent::GraphicsSceneMousePress:
       
   106     case QEvent::GraphicsSceneMouseRelease:
       
   107     case QEvent::GraphicsSceneMouseDoubleClick:
       
   108 // Commented out because new context menu resizes itself, don't want externalMouseEvents
       
   109 // in that case.
       
   110 //    case QEvent::GraphicsSceneResize:
       
   111         break;
       
   112     default:
       
   113         return;
       
   114     }
       
   115 
       
   116     // Check where the mouse press event occurred.
       
   117     // If it was outside this item's bounding rectangle,
       
   118     // then tell the world.
       
   119 
       
   120 // Commented out because new context menu resizes itself, don't want externalMouseEvents
       
   121 // in that case.
       
   122 //    if (e->type() == QEvent::GraphicsSceneResize)
       
   123 //    {
       
   124 //        QGraphicsSceneResizeEvent *resizeEvent = static_cast<QGraphicsSceneResizeEvent *>(e);
       
   125 //        qDebug() << "PopupWebChromeItem::checkForExternalEvent: " << resizeEvent->newSize() << resizeEvent->oldSize();
       
   126 //        if (resizeEvent->newSize() != resizeEvent->oldSize())
       
   127 //          emitExternalEvent(e);
       
   128 //      return;
       
   129 //    }
       
   130 
       
   131     QGraphicsSceneMouseEvent * me = static_cast<QGraphicsSceneMouseEvent*>(e);
       
   132 
       
   133     QPointF eventPosition = me->scenePos();
       
   134 
       
   135     QRectF itemGeometry = sceneBoundingRect();
       
   136 
       
   137     if (!itemGeometry.contains(eventPosition)) {
       
   138         emitExternalEvent(e);
       
   139     }
       
   140 }
       
   141 
       
   142 void PopupWebChromeItem::emitExternalEvent(QEvent * e)
       
   143 {
       
   144     QString description;
       
   145 
       
   146     QDebug stream(&description);
       
   147     stream << e;
       
   148 
       
   149     QString name = description;
       
   150     name.truncate(name.indexOf('('));
       
   151 
       
   152     emit externalMouseEvent(e->type(), name, description.trimmed());
       
   153 }
       
   154 
       
   155 } // end of namespace GVA
    79 } // end of namespace GVA