ginebra2/EditorWidget.h
changeset 3 0954f5dd2cd0
child 9 b39122337a00
equal deleted inserted replaced
1:b0dd75e285d2 3:0954f5dd2cd0
       
     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 #ifndef __EDITOR_WIDGET_H__
       
    23 #define __EDITOR_WIDGET_H__
       
    24 
       
    25 #include "ActionButton.h"
       
    26 #include "NativeChromeItem.h"
       
    27 
       
    28 #include <QGraphicsTextItem>
       
    29 #include <QGraphicsWidget>
       
    30 #include <QtGui>
       
    31 
       
    32 namespace GVA {
       
    33 
       
    34   class ChromeSnippet;
       
    35   class ChromeWidget;
       
    36 
       
    37   class GTextLineItem : public QGraphicsTextItem
       
    38   {
       
    39     Q_OBJECT
       
    40 
       
    41   public:
       
    42     GTextLineItem(QGraphicsItem * parent);
       
    43     virtual ~GTextLineItem();
       
    44 
       
    45     qreal cursorX();
       
    46     qreal anchorX();
       
    47     QRectF selectionRectF();
       
    48     void setText(const QString & text);
       
    49     void setMaxTextLength(int length);
       
    50     void selectAll();
       
    51     void unselect();
       
    52     qreal textWidth();
       
    53     void setCursorPosition(int pos);
       
    54     bool hasSelection() { return (cursorX()!= anchorX()); }
       
    55     Qt::InputMethodHints inputMethodHints() const { return m_hints; }
       
    56     void setInputMethodHints(Qt::InputMethodHints hints);
       
    57 
       
    58   protected:
       
    59     virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
       
    60     virtual void keyPressEvent(QKeyEvent * event);
       
    61     virtual void keyReleaseEvent(QKeyEvent * event);
       
    62     virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * event);
       
    63     virtual void mousePressEvent(QGraphicsSceneMouseEvent * event);
       
    64     virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
       
    65     virtual void focusInEvent(QFocusEvent * event);
       
    66     virtual void focusOutEvent(QFocusEvent * event);
       
    67     virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
       
    68 
       
    69   private slots:
       
    70     void contentsChange(int position, int charsRemoved, int charsAdded);
       
    71 
       
    72   signals:
       
    73     void cursorXChanged(qreal newx);
       
    74     void textMayChanged();
       
    75     void focusChanged(bool focusIn);
       
    76     void tapped(QPointF& pos);
       
    77 
       
    78   private:
       
    79     QTextLine m_textLine;
       
    80     int m_defaultStartDragDistance;
       
    81     int m_maxTextLength;
       
    82     Qt::InputMethodHints m_hints;
       
    83   };
       
    84 
       
    85   class GLineEditor : public QGraphicsWidget
       
    86   {
       
    87     Q_OBJECT
       
    88 
       
    89   public:
       
    90     GLineEditor(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent = 0);
       
    91     virtual ~GLineEditor();
       
    92 
       
    93     void selectAll() { m_editor->selectAll(); }
       
    94     void unselect() { m_editor->unselect(); }
       
    95 
       
    96     void setTextColor(QColor & color);
       
    97     void setBackgroundColor(QColor & color) {m_backgroundColor = color;}
       
    98     void setPadding(qreal padding);
       
    99     void setRightTextMargin(qreal margin);
       
   100     QString text() const;
       
   101     void updateEditor();
       
   102     bool tappedOnText(qreal x) const;
       
   103     void grabFocus() { m_editor->setFocus(); }
       
   104     void removeFocus() { m_editor->clearFocus(); }
       
   105     void setCursorPosition (int pos) { m_editor->setCursorPosition(pos); }
       
   106     void shiftToLeftEnd() { setCursorPosition(0); }
       
   107     int characterCount() { return m_editor->document()->characterCount(); }
       
   108     bool hasSelection() { return m_editor->hasSelection(); }
       
   109     Qt::InputMethodHints inputMethodHints() { return m_editor->inputMethodHints(); }
       
   110     // Calling this function will overwrite the existing hints
       
   111     void setInputMethodHints(Qt::InputMethodHints hints) { m_editor->setInputMethodHints(hints); }
       
   112     void setMaxTextLength(int length) { m_editor->setMaxTextLength(length); }
       
   113 
       
   114   protected:
       
   115     virtual bool eventFilter(QObject * object, QEvent * event);
       
   116     virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
       
   117     virtual void resizeEvent(QGraphicsSceneResizeEvent * event);
       
   118 
       
   119   signals:
       
   120     void activated();
       
   121     void textMayChanged();
       
   122     void focusChanged(bool focusIn);
       
   123     void tapped(QPointF& pos);
       
   124 
       
   125   private slots:
       
   126     void makeVisible(qreal cursorX);
       
   127 
       
   128   public slots:
       
   129     void setText(const QString & text);
       
   130 
       
   131   private:
       
   132     void internalScroll(qreal deltaX);
       
   133 
       
   134   protected:
       
   135     ChromeWidget * m_chrome;
       
   136 
       
   137     // Cached values used for painting and scrolling.
       
   138     qreal m_viewPortWidth;
       
   139     qreal m_viewPortHeight;
       
   140 
       
   141     // At runtime, UrlSearchSnippet is parent to a QGraphicsWidget
       
   142     // (m_viewPort) that is parent to a UrlEditorWidget (m_editor).
       
   143     QGraphicsWidget * m_viewPort;
       
   144     GTextLineItem * m_editor;
       
   145 
       
   146     // Attributes
       
   147     QString m_text;
       
   148     QColor m_textColor;
       
   149     QColor m_backgroundColor;
       
   150     qreal m_padding;
       
   151     //TODO: add left margin too
       
   152     qreal m_rightTextMargin;
       
   153 
       
   154   };
       
   155 
       
   156   class GTextEditor : public GLineEditor
       
   157   {
       
   158     Q_OBJECT
       
   159 
       
   160   public:
       
   161     GTextEditor(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent = 0);
       
   162     virtual ~GTextEditor();
       
   163     void setBorderColor(QColor & color) { m_borderColor = color; }
       
   164 
       
   165   protected:
       
   166     virtual void paintBorder(QPainter * painter);
       
   167     virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
       
   168   private:
       
   169     QColor m_borderColor;
       
   170   };
       
   171 
       
   172 
       
   173   class GProgressEditor : public GTextEditor
       
   174   {
       
   175     Q_OBJECT
       
   176 
       
   177   public:
       
   178     GProgressEditor(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent = 0);
       
   179     virtual ~GProgressEditor();
       
   180 
       
   181     void setProgressColor(QColor & color) { m_progressColor = color; }
       
   182 
       
   183     public slots:
       
   184     void setProgress(int percent);
       
   185 
       
   186   protected:
       
   187     virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
       
   188 
       
   189   private:
       
   190     int m_percent;
       
   191     QColor m_progressColor;
       
   192   };
       
   193 
       
   194   class TextEditItem : public NativeChromeItem 
       
   195   {
       
   196     Q_OBJECT
       
   197   public:
       
   198     TextEditItem(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent = 0);
       
   199     virtual ~TextEditItem();
       
   200     GTextEditor * editor() { return m_textEditor; } 
       
   201     QString text() { return m_textEditor->text(); }
       
   202     void setText(const QString & text){ m_textEditor->setText(text); }
       
   203     int characterCount() { return m_textEditor->characterCount(); }
       
   204     void setCursorPosition(int pos) { m_textEditor->setCursorPosition(pos); }
       
   205     void selectAll() { m_textEditor->selectAll(); }
       
   206     void unselect() { m_textEditor->unselect(); }
       
   207     int getTextOptions() { return (int) m_textEditor->inputMethodHints(); }
       
   208     // Calling this function will overwrite the existing options
       
   209     void setTextOptions (int flag);
       
   210     void setMaxTextLength(int length) { m_textEditor->setMaxTextLength(length); }
       
   211  
       
   212   private slots:	
       
   213 	void tapped(QPointF&);
       
   214 	void focusChanged(bool focusIn);
       
   215 
       
   216   protected:
       
   217     virtual void resizeEvent(QGraphicsSceneResizeEvent * ev);
       
   218   private:
       
   219     GTextEditor * m_textEditor;
       
   220 	 bool m_justFocusIn;
       
   221   };
       
   222 
       
   223 } // namespace GVA
       
   224 
       
   225 #endif // __EDITOR_WIDGET_H__