src/hbwidgets/editors/hbselectionhandle_p.cpp
changeset 1 f7ac710697a9
parent 0 16d8024aca5e
child 2 06ff229162e9
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 //
       
    27 //  W A R N I N G
       
    28 //  -------------
       
    29 //
       
    30 // This file is not part of the Hb API.  It exists purely as an
       
    31 // implementation detail.  This file may change from version to
       
    32 // version without notice, or even be removed.
       
    33 //
       
    34 // We mean it.
       
    35 //
       
    36 
       
    37 #include "hbselectionhandle_p.h"
       
    38 #include "hbstyleoption.h"
       
    39 
       
    40 #include "hbicon.h"
       
    41 #include "hbmenu.h"
       
    42 #include "hbaction.h"
       
    43 
       
    44 #include "hbinstance.h"
       
    45 
       
    46 #include <QtDebug>
       
    47 
       
    48 #include <QTextCursor>
       
    49 #include <QTextDocument>
       
    50 #include <QTextBlock>
       
    51 #include <QGraphicsItem>
       
    52 #include <QGraphicsSceneMouseEvent>
       
    53 #include <QPointF>
       
    54 #include <QPainter>
       
    55 #include <QApplication>
       
    56 #include <QClipboard>
       
    57 
       
    58 const QSizeF HandleSize = QSizeF(10, 10);
       
    59 const QRectF MouseTreshold = QRectF(-5, -5, 5, 5);
       
    60 const QPointF MenuOffset = QPointF(10, 50);
       
    61 
       
    62 HbSelectionHandle::HbSelectionHandle (HandleType type, HbTextControl *control, QGraphicsItem *parent) :
       
    63     QGraphicsItem(parent), mControl(control), mType(type)
       
    64 {
       
    65     hide(); // defaults to hidden
       
    66 
       
    67     if (mType == Cursor) {
       
    68         mIcon = HbIcon("qtg_graf_editor_handle_end.svg");
       
    69     } else {
       
    70         mIcon = HbIcon("qtg_graf_editor_handle_begin.svg");
       
    71     }
       
    72 
       
    73     connect(mControl, SIGNAL(selectionChanged()), this, SLOT(cursorChanged()));
       
    74 
       
    75     cursorChanged();
       
    76 }
       
    77 
       
    78 void HbSelectionHandle::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
    79 {
       
    80     Q_UNUSED(widget);
       
    81     Q_UNUSED(option);
       
    82 
       
    83     painter->save();
       
    84     mIcon.setSize(HandleSize);
       
    85     mIcon.paint(painter, boundingRect());
       
    86     painter->restore();
       
    87 }
       
    88 
       
    89 QRectF HbSelectionHandle::boundingRect() const
       
    90 {
       
    91     return QRectF(QPointF(0, 0), HandleSize);
       
    92 }
       
    93 
       
    94 void HbSelectionHandle::mouseMoveEvent (QGraphicsSceneMouseEvent *event)
       
    95 {
       
    96     float x1 = event->scenePos().x();
       
    97     float x2 = mMousePos.x();
       
    98 
       
    99     if (qAbs(x1 - x2) > HandleSize.width()) {
       
   100         mMousePos = event->scenePos();
       
   101         if (x1 > x2) {
       
   102             movePosition(QTextCursor::NextCharacter);
       
   103         } else if(x1 < x2) {
       
   104             movePosition(QTextCursor::PreviousCharacter);
       
   105         }
       
   106     }
       
   107 
       
   108     float y1 = event->scenePos().y();
       
   109     float y2 = mMousePos.y();
       
   110 
       
   111     if (qAbs(y1 - y2) > HandleSize.height()) {
       
   112         mMousePos = event->scenePos();
       
   113         if (y1 > y2) {
       
   114             movePosition(QTextCursor::Down);
       
   115         } else if(y1 < y2) {
       
   116             movePosition(QTextCursor::Up);
       
   117         }
       
   118     }
       
   119 }
       
   120 
       
   121 void HbSelectionHandle::movePosition(QTextCursor::MoveOperation op)
       
   122 {
       
   123     if (mType == Cursor) {
       
   124         QTextCursor cursor = mControl->textCursor();
       
   125         cursor.movePosition(op, QTextCursor::KeepAnchor);
       
   126         mControl->setTextCursor(cursor);
       
   127     } else {
       
   128         QTextCursor cursor = mControl->textCursor();
       
   129         QTextCursor c = QTextCursor(mControl->document());
       
   130         c.setPosition(cursor.position());
       
   131         c.setPosition(cursor.anchor(), QTextCursor::KeepAnchor);
       
   132         c.movePosition(op, QTextCursor::KeepAnchor);
       
   133         cursor.setPosition(c.position());
       
   134         cursor.setPosition(c.anchor(), QTextCursor::KeepAnchor);
       
   135         mControl->setTextCursor(cursor);
       
   136     }
       
   137 }
       
   138 
       
   139 void HbSelectionHandle::mousePressEvent (QGraphicsSceneMouseEvent *event)
       
   140 {
       
   141     mMousePressPos = mMousePos = event->scenePos();
       
   142     event->accept();
       
   143 }
       
   144 
       
   145 void HbSelectionHandle::mouseReleaseEvent (QGraphicsSceneMouseEvent *event)
       
   146 {
       
   147     event->accept();
       
   148     if (MouseTreshold.contains(mMousePressPos - event->scenePos())) {
       
   149         showMenu(event->scenePos() + MenuOffset);
       
   150     }
       
   151 }
       
   152 
       
   153 void HbSelectionHandle::cursorChanged ()
       
   154 {
       
   155     QRectF r;
       
   156     QPointF p;
       
   157 
       
   158     int c = mControl->textCursor().position();
       
   159     int a = mControl->textCursor().anchor();
       
   160 
       
   161     if (mType == Cursor) {
       
   162         r = rectForPosition(c);
       
   163         p = r.topRight();
       
   164         if (c < a) {
       
   165             p.rx() -= HandleSize.width();
       
   166         }
       
   167     } else {
       
   168         r = rectForPosition(mControl->textCursor().anchor());
       
   169         p = r.topLeft();
       
   170         if (c >= a) {
       
   171             p.rx() -= HandleSize.width();
       
   172         }
       
   173     }
       
   174 
       
   175     if (c >= a) {
       
   176         mIcon.setMirroringMode(HbIcon::Prevented);
       
   177     } else {
       
   178         mIcon.setMirroringMode(HbIcon::Forced);
       
   179     }
       
   180 
       
   181     if (QApplication::layoutDirection() == Qt::RightToLeft) {
       
   182         p.rx() = parentItem()->boundingRect().right() - p.x();
       
   183     }
       
   184     setPos(p);
       
   185 }
       
   186 
       
   187 void HbSelectionHandle::showMenu(QPointF position)
       
   188 {
       
   189     HbMenu *menu = new HbMenu();
       
   190     HbMenuItem *mi;
       
   191 
       
   192     mi = menu->addAction("Cut");
       
   193     connect(mi->action(), SIGNAL(triggered()), SLOT(cut()));
       
   194 
       
   195     mi = menu->addAction("Copy");
       
   196     connect(mi->action(), SIGNAL(triggered()), SLOT(copy()));
       
   197 
       
   198     mi = menu->addAction("Paste");
       
   199     connect(mi->action(), SIGNAL(triggered()), SLOT(paste()));
       
   200 
       
   201     menu->setMenuType(HbMenu::MenuContext);
       
   202     menu->exec(position);
       
   203 }
       
   204 
       
   205 void HbSelectionHandle::copy ()
       
   206 {
       
   207    QString selectedText = mControl->textCursor().selectedText();
       
   208 #ifndef QT_NO_CLIPBOARD
       
   209    QClipboard *clipboard = QApplication::clipboard();
       
   210    clipboard->setText(selectedText);
       
   211 #else
       
   212    mClipboard = selectedText;
       
   213 #endif
       
   214 }
       
   215 
       
   216 void HbSelectionHandle::cut ()
       
   217 {
       
   218     copy();
       
   219     mControl->textCursor().removeSelectedText();
       
   220 }
       
   221 
       
   222 void HbSelectionHandle::paste ()
       
   223 {
       
   224 #ifndef QT_NO_CLIPBOARD
       
   225    QClipboard *clipboard = QApplication::clipboard();
       
   226    QString clipText = clipboard->text();
       
   227 #else
       
   228    QString clipText = mClipboard;
       
   229 #endif
       
   230    mControl->textCursor().removeSelectedText();
       
   231    mControl->textCursor().insertText(clipText);
       
   232 }
       
   233 
       
   234 #include <QAbstractTextDocumentLayout>
       
   235 #include <QTextLayout>
       
   236 #include <QTextLine>
       
   237 
       
   238 QRectF HbSelectionHandle::rectForPosition(int position) const
       
   239 {
       
   240     const QTextBlock block = mControl->document()->findBlock(position);
       
   241 
       
   242     if (!block.isValid())
       
   243         return QRectF();
       
   244 
       
   245     const QTextLayout *layout = block.layout();
       
   246 
       
   247     const QPointF layoutPos = blockBoundingRect(block).topLeft();
       
   248 
       
   249     int relativePos = position - block.position();
       
   250 
       
   251     QTextLine line = layout->lineForTextPosition(relativePos);
       
   252 
       
   253     QRectF r;
       
   254     qreal w = 1;
       
   255 
       
   256     if (line.isValid()) {
       
   257         qreal x = line.cursorToX(relativePos);
       
   258         r = QRectF(layoutPos.x() + x, layoutPos.y() + line.y(),
       
   259                    w, line.height());
       
   260     } else {
       
   261         r = QRectF(layoutPos.x(), layoutPos.y(), w, HandleSize.height()); // #### correct height
       
   262     }
       
   263 
       
   264     return r;
       
   265 }
       
   266 
       
   267 QRectF HbSelectionHandle::blockBoundingRect(const QTextBlock &block) const
       
   268 {
       
   269     return mControl->document()->documentLayout()->blockBoundingRect(block);
       
   270 }