ginebra2/CopyCutPasteSnippet.cpp
changeset 16 3c88a81ff781
equal deleted inserted replaced
14:6aeb7a756187 16:3c88a81ff781
       
     1 /*
       
     2  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     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.
       
     8  *
       
     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  *
       
    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:
       
    19  *
       
    20  */
       
    21 
       
    22 #include "CopyCutPasteSnippet.h"
       
    23 
       
    24 #define BUTTON_WIDTH 85
       
    25 #define BUTTON_HEIGHT 50
       
    26 #define BUTTON_PADDING 0
       
    27 #define TOP_PADDING 5
       
    28 #define FIRST_BUTTON_OFFSET 0
       
    29 #define SECOND_BUTTON_OFFSET BUTTON_WIDTH+BUTTON_PADDING
       
    30 #define THIRD_BUTTON_OFFSET SECOND_BUTTON_OFFSET+SECOND_BUTTON_OFFSET
       
    31 #ifdef BROWSER_LAYOUT_TENONE
       
    32   #define URLSEARCH_CHROME_ID "TitleUrlId"
       
    33 #else
       
    34   #define URLSEARCH_CHROME_ID "UrlSearchChromeId"
       
    35 #endif
       
    36 #define MENU_DISPLAY_DURATION 5000
       
    37 #define BACKGROUND_COLOR "#FFFFDD"
       
    38 
       
    39 namespace GVA {
       
    40 
       
    41     CopyCutPasteButton::CopyCutPasteButton(const QString & text, QWidget * parent)
       
    42         : QObject(parent) 
       
    43         , m_text(text)
       
    44         , m_mousePressed(false)
       
    45         , m_size(QSize())
       
    46         , m_disabled(false)
       
    47     {
       
    48         
       
    49     }
       
    50     
       
    51     void CopyCutPasteButton::mousePressEvent(QGraphicsSceneMouseEvent * event)
       
    52     {
       
    53         Q_UNUSED(event);
       
    54         m_mousePressed = true;
       
    55     }
       
    56     
       
    57     void CopyCutPasteButton::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
       
    58     {
       
    59         Q_UNUSED(event);
       
    60         if (m_mousePressed)
       
    61             emit clicked();
       
    62         m_mousePressed = false; 
       
    63     }
       
    64 
       
    65     void CopyCutPasteButton::render(QPainter * painter, const QPoint & targetOffset)
       
    66     {
       
    67        painter->save();
       
    68        QFont textFont = painter->font();
       
    69        textFont.setPixelSize(30);
       
    70        textFont.setBold(true);
       
    71        painter->setFont(textFont);
       
    72        if (m_mousePressed) {
       
    73            painter->setPen(Qt::white);
       
    74            painter->setBrush(Qt::black);
       
    75            painter->drawRoundedRect(QRect(targetOffset.x(), TOP_PADDING, BUTTON_WIDTH, BUTTON_HEIGHT), 10,10);
       
    76            painter->setPen(QColor(BACKGROUND_COLOR));
       
    77        } else {
       
    78            if (m_disabled)
       
    79                painter->setPen(Qt::lightGray);
       
    80            else
       
    81                painter->setPen(Qt::black);
       
    82        } 
       
    83 
       
    84        painter->drawText(QRect(targetOffset.x(), TOP_PADDING, BUTTON_WIDTH, BUTTON_HEIGHT), Qt::AlignCenter, m_text);
       
    85        painter->restore();
       
    86     }
       
    87 
       
    88     CopyCutPasteItem::CopyCutPasteItem(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent)
       
    89       : NativeChromeItem(snippet, parent)
       
    90       , m_editorSnippet(NULL)
       
    91     {
       
    92         Q_UNUSED(chrome);
       
    93         m_cutButton = new CopyCutPasteButton(QString("cut"));//(qtTrId("txt_browser_content_view_menu_cut"));
       
    94         m_copyButton = new CopyCutPasteButton(QString("copy"));//(qtTrId("txt_browser_content_view_menu_copy"));
       
    95         m_pasteButton = new CopyCutPasteButton(QString("paste"));//(qtTrId("txt_browser_content_view_menu_paste"));
       
    96 
       
    97         m_cutButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
       
    98         m_copyButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
       
    99         m_pasteButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
       
   100         
       
   101         connect(m_cutButton, SIGNAL(clicked()), this, SLOT(cut()));
       
   102         connect(m_copyButton, SIGNAL(clicked()), this, SLOT(copy()));
       
   103         connect(m_pasteButton, SIGNAL(clicked()), this, SLOT(paste()));
       
   104     }
       
   105 
       
   106     CopyCutPasteItem::~CopyCutPasteItem() 
       
   107     {
       
   108         delete m_cutButton;
       
   109         delete m_copyButton;
       
   110         delete m_pasteButton;
       
   111     }
       
   112 
       
   113     void CopyCutPasteItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
       
   114     {
       
   115         NativeChromeItem::paint(painter, option, widget);
       
   116         painter->save();
       
   117 
       
   118         painter->setPen(Qt::black);
       
   119         painter->setBrush(QColor(BACKGROUND_COLOR));
       
   120 
       
   121         painter->drawRoundedRect(QRect(FIRST_BUTTON_OFFSET, TOP_PADDING, THIRD_BUTTON_OFFSET + BUTTON_WIDTH, BUTTON_HEIGHT), 10,10);
       
   122 
       
   123         // draw split
       
   124         painter->setOpacity(0.3);
       
   125         painter->setPen(Qt::black);
       
   126         painter->drawLine(BUTTON_WIDTH, TOP_PADDING + 10, BUTTON_WIDTH, TOP_PADDING + BUTTON_HEIGHT - 10);
       
   127         painter->drawLine(SECOND_BUTTON_OFFSET + BUTTON_WIDTH, TOP_PADDING + 10, SECOND_BUTTON_OFFSET + BUTTON_WIDTH, TOP_PADDING + BUTTON_HEIGHT - 10);
       
   128         painter->setOpacity(1);
       
   129         
       
   130         // draw buttons
       
   131         m_copyButton->render(painter, QPoint(FIRST_BUTTON_OFFSET,TOP_PADDING));
       
   132         m_cutButton->render(painter, QPoint(SECOND_BUTTON_OFFSET,TOP_PADDING));
       
   133         m_pasteButton->render(painter, QPoint(THIRD_BUTTON_OFFSET,TOP_PADDING));
       
   134 
       
   135         painter->restore();
       
   136     }
       
   137 
       
   138     void CopyCutPasteItem::reset()
       
   139     {
       
   140         m_copyButton->setDisabled(true);
       
   141         m_cutButton->setDisabled(true);
       
   142         m_pasteButton->setDisabled(true);
       
   143         m_copyButton->setMousePressed(false);
       
   144         m_cutButton->setMousePressed(false);
       
   145         m_pasteButton->setMousePressed(false);
       
   146         m_editorSnippet = NULL;
       
   147     }
       
   148 
       
   149     void CopyCutPasteItem::cut() 
       
   150     {
       
   151         if (m_editorSnippet)
       
   152             m_editorSnippet->cut();
       
   153         m_snippet->setVisible(false);
       
   154         // set the focus back
       
   155         m_editorSnippet->grabFocus();
       
   156     }
       
   157 
       
   158     void CopyCutPasteItem::copy() 
       
   159     {
       
   160         if (m_editorSnippet)
       
   161             m_editorSnippet->copy();
       
   162         m_snippet->setVisible(false);
       
   163         // set the focus back
       
   164         m_editorSnippet->grabFocus();
       
   165     }
       
   166 
       
   167     void CopyCutPasteItem::paste() 
       
   168     {
       
   169         if (m_editorSnippet)
       
   170             m_editorSnippet->paste();
       
   171         m_snippet->setVisible(false);
       
   172         // set the focus back
       
   173         m_editorSnippet->grabFocus();
       
   174     }
       
   175 
       
   176     void CopyCutPasteItem::buildMenu(bool isContentSelected)
       
   177     {
       
   178         reset();
       
   179         
       
   180         m_copyButton->setDisabled(!isContentSelected);
       
   181         m_cutButton->setDisabled(!isContentSelected);
       
   182 
       
   183         bool canPaste = false;
       
   184         const QClipboard *clipboard = QApplication::clipboard();
       
   185         if (clipboard) {
       
   186             const QMimeData *mimeData = clipboard->mimeData();
       
   187             if (mimeData)
       
   188                 canPaste = mimeData->hasText();
       
   189         }
       
   190         m_pasteButton->setDisabled(!canPaste);
       
   191     }
       
   192 
       
   193     void CopyCutPasteItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
       
   194     {
       
   195         int x = event->pos().x();
       
   196         if (x > 0 && x < SECOND_BUTTON_OFFSET && m_copyButton->isEnabled())
       
   197             m_copyButton->mousePressEvent(event);
       
   198         else if (x >= SECOND_BUTTON_OFFSET && x < THIRD_BUTTON_OFFSET && m_cutButton->isEnabled())
       
   199             m_cutButton->mousePressEvent(event);
       
   200         else if (x >= THIRD_BUTTON_OFFSET && m_pasteButton->isEnabled())
       
   201             m_pasteButton->mousePressEvent(event);
       
   202         update();
       
   203     }
       
   204 
       
   205     void CopyCutPasteItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
       
   206     {
       
   207         int x = event->pos().x();
       
   208         if (x > 0 && x < SECOND_BUTTON_OFFSET && m_copyButton->isEnabled())
       
   209             m_copyButton->mouseReleaseEvent(event);
       
   210         else if (x >= SECOND_BUTTON_OFFSET && x < THIRD_BUTTON_OFFSET && m_cutButton->isEnabled())
       
   211             m_cutButton->mouseReleaseEvent(event);
       
   212         else if (x >= THIRD_BUTTON_OFFSET && m_pasteButton->isEnabled())
       
   213             m_pasteButton->mouseReleaseEvent(event);
       
   214         // reset all the mouse
       
   215         m_copyButton->setMousePressed(false);
       
   216         m_cutButton->setMousePressed(false);
       
   217         m_pasteButton->setMousePressed(false);
       
   218         update();
       
   219     }
       
   220 
       
   221     CopyCutPasteSnippet::CopyCutPasteSnippet( const QString & elementId, ChromeWidget * chrome, QGraphicsWidget * widget, const QWebElement & element )
       
   222     : ChromeSnippet( elementId, chrome, widget, element )
       
   223     , m_timer(new QTimer(this))
       
   224     , m_externalEventCharm(NULL)
       
   225     {
       
   226         connectAll();
       
   227     }
       
   228 
       
   229     void CopyCutPasteSnippet::setChromeWidget(QGraphicsWidget * widget)
       
   230     {
       
   231         ChromeSnippet::setChromeWidget(widget);
       
   232     }
       
   233 
       
   234     CopyCutPasteSnippet * CopyCutPasteSnippet::instance(const QString& elementId, ChromeWidget * chrome, const QWebElement & element)
       
   235     {
       
   236         CopyCutPasteSnippet* that = new CopyCutPasteSnippet(elementId, chrome, 0, element);
       
   237         CopyCutPasteItem * item = new CopyCutPasteItem(that, chrome);
       
   238         that->setChromeWidget(item);
       
   239         return that;
       
   240     }
       
   241 
       
   242     void CopyCutPasteSnippet::connectAll()
       
   243     {
       
   244         connect(m_chrome, SIGNAL(chromeComplete()), this, SLOT(onChromeComplete()));
       
   245         connect(m_chrome, SIGNAL(aspectChanged(int)), this, SLOT(onAspectChanged(int)));
       
   246         connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
       
   247     }
       
   248 
       
   249     void CopyCutPasteSnippet::onChromeComplete()
       
   250     {
       
   251         ChromeSnippet* urlSnippet = m_chrome->getSnippet(URLSEARCH_CHROME_ID);
       
   252         if (urlSnippet)
       
   253             connect(urlSnippet, SIGNAL(contextEvent(bool, QString)), this, SLOT(onContextEvent(bool, QString)));
       
   254 
       
   255          m_externalEventCharm = new ExternalEventCharm(copyCutPasteItem());
       
   256 
       
   257          connect(m_externalEventCharm,
       
   258                     SIGNAL(externalMouseEvent(QEvent *, const QString &, const QString &)),
       
   259                     this,
       
   260                     SLOT(onExternalMouseEvent(QEvent *, const QString &, const QString &)));
       
   261     }
       
   262     
       
   263     void CopyCutPasteSnippet::onAspectChanged(int aspect)
       
   264     {
       
   265         Q_UNUSED(aspect);
       
   266         if (isVisible()) {
       
   267             ChromeSnippet* editorSnippet = copyCutPasteItem()->editorSnippet();
       
   268             if (!editorSnippet)
       
   269                 return;
       
   270             int x = (m_chrome->viewSize().width() - (BUTTON_WIDTH + THIRD_BUTTON_OFFSET)) / 2;
       
   271             int y = editorSnippet->widget()->size().height() + 1;
       
   272             anchorTo(URLSEARCH_CHROME_ID, x, y);
       
   273             copyCutPasteItem()->update();
       
   274         }
       
   275     }
       
   276 
       
   277     void CopyCutPasteSnippet::onContextEvent(bool isContentSelected, QString snippetId)
       
   278     {
       
   279         ChromeSnippet* editorSnippet = m_chrome->getSnippet(snippetId);
       
   280         if (!editorSnippet)
       
   281             return;
       
   282         int x = (m_chrome->viewSize().width() - (BUTTON_WIDTH + THIRD_BUTTON_OFFSET)) / 2;
       
   283         int y = editorSnippet->widget()->size().height() + 1;
       
   284         anchorTo(URLSEARCH_CHROME_ID, x, y);
       
   285         copyCutPasteItem()->buildMenu(isContentSelected);
       
   286         copyCutPasteItem()->setEditorSnippet(editorSnippet);
       
   287         copyCutPasteItem()->update();
       
   288         setVisible(true);
       
   289         m_timer->start(MENU_DISPLAY_DURATION);
       
   290     }
       
   291 
       
   292     void CopyCutPasteSnippet::onTimeout()
       
   293     {
       
   294         setVisible(false);
       
   295     }
       
   296 
       
   297     void  CopyCutPasteSnippet::onExternalMouseEvent(QEvent * ev, const QString & name, const QString & description)
       
   298     {
       
   299         Q_UNUSED(ev);
       
   300         Q_UNUSED(description);
       
   301         if (name == "MouseClick")
       
   302             setVisible(false);
       
   303     }
       
   304 
       
   305     void CopyCutPasteSnippet::setVisible(bool visiblity, bool animate)
       
   306     {
       
   307         // let snippet know menu shows or hides
       
   308         if (copyCutPasteItem()->editorSnippet())
       
   309             copyCutPasteItem()->editorSnippet()->setContextMenuStatus(visiblity); 
       
   310         ChromeSnippet::setVisible(visiblity, animate);
       
   311     }
       
   312 
       
   313     CopyCutPasteItem * CopyCutPasteSnippet::copyCutPasteItem()
       
   314     {
       
   315         return qobject_cast<CopyCutPasteItem*>(m_widget);
       
   316     }
       
   317 }