ginebra2/TitleItem.cpp
changeset 12 afcd8e6d025b
child 16 3c88a81ff781
equal deleted inserted replaced
11:786160610b4d 12:afcd8e6d025b
       
     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 "TitleItem.h"
       
    23 #include "webpagecontroller.h"
       
    24 #include "Utilities.h"
       
    25 
       
    26 
       
    27 namespace GVA {
       
    28 
       
    29   // Methods for class TitleItem
       
    30 
       
    31   //GTitleItem extends QGraphicsTextItem 
       
    32 
       
    33   GTitleItem::GTitleItem(QGraphicsItem * parent)
       
    34   : QGraphicsTextItem(parent)
       
    35   , m_maxTextLength(0)
       
    36   {
       
    37     // Disable wrapping, force text to be stored and displayed
       
    38     // as a single line.
       
    39     QTextOption textOption = document()->defaultTextOption();
       
    40     textOption.setWrapMode(QTextOption::NoWrap);
       
    41     document()->setDefaultTextOption(textOption);
       
    42     // This is needed to initialize m_textLine.
       
    43     setText("");
       
    44 
       
    45     WebPageController * pageController = WebPageController::getSingleton();
       
    46   }
       
    47 
       
    48   GTitleItem::~GTitleItem()
       
    49   {
       
    50   }
       
    51 
       
    52   void GTitleItem::setText(const QString & text)
       
    53   {
       
    54     //qDebug() << "GTitleItem::setText " << text;
       
    55     QString newText = text;
       
    56     if(m_maxTextLength > 0 && text.length() > m_maxTextLength ) {
       
    57         newText = newText.left(m_maxTextLength);
       
    58     }
       
    59     setPlainText(newText);
       
    60     m_textLine = document()->begin().layout()->lineForTextPosition(0);
       
    61   }
       
    62 
       
    63   void GTitleItem::setMaxTextLength(int length)
       
    64   {
       
    65     if (m_maxTextLength <= 0 && length > 0) {
       
    66         QString text = toPlainText();
       
    67         if( text.length() > length ) {
       
    68           setPlainText(text.left(length));
       
    69         }
       
    70     }
       
    71     m_maxTextLength = length;
       
    72   }
       
    73 
       
    74 
       
    75   qreal GTitleItem::textWidth()
       
    76   {
       
    77     return m_textLine.naturalTextWidth();
       
    78   }
       
    79 
       
    80   void GTitleItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
       
    81   {
       
    82     QGraphicsTextItem::mousePressEvent(event);
       
    83     QPointF pos = event->pos();
       
    84     //qDebug() << "GTitleItem: received mouse press" << pos;
       
    85     emit tapped(pos);
       
    86   }
       
    87 
       
    88 } // namespace GVA