|
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 selectAll(); |
|
50 void unselect(); |
|
51 qreal textWidth(); |
|
52 void setCursorPosition(int pos); |
|
53 void setAutoUppercase(bool enable) { m_autoUppercase = enable; } |
|
54 bool hasSelection() { return (cursorX()!= anchorX()); } |
|
55 |
|
56 protected: |
|
57 virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); |
|
58 virtual void keyPressEvent(QKeyEvent * event); |
|
59 virtual void keyReleaseEvent(QKeyEvent * event); |
|
60 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * event); |
|
61 virtual void mousePressEvent(QGraphicsSceneMouseEvent * event); |
|
62 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * event); |
|
63 virtual void focusInEvent(QFocusEvent * event); |
|
64 virtual void focusOutEvent(QFocusEvent * event); |
|
65 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); |
|
66 |
|
67 signals: |
|
68 void cursorXChanged(qreal newx); |
|
69 void textMayChanged(); |
|
70 void focusChanged(bool focusIn); |
|
71 void tapped(QPointF& pos); |
|
72 |
|
73 private: |
|
74 QTextLine m_textLine; |
|
75 int m_defaultStartDragDistance; |
|
76 bool m_predictionDisabled; |
|
77 bool m_autoUppercase; |
|
78 }; |
|
79 |
|
80 class GLineEditor : public QGraphicsWidget |
|
81 { |
|
82 Q_OBJECT |
|
83 |
|
84 public: |
|
85 GLineEditor(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent = 0); |
|
86 virtual ~GLineEditor(); |
|
87 |
|
88 void selectAll() { m_editor->selectAll(); } |
|
89 void unselect() { m_editor->unselect(); } |
|
90 |
|
91 void setTextColor(QColor & color); |
|
92 void setBackgroundColor(QColor & color) {m_backgroundColor = color;} |
|
93 void setPadding(qreal padding); |
|
94 void setRightTextMargin(qreal margin); |
|
95 QString text() const; |
|
96 void updateEditor(); |
|
97 bool tappedOnText(qreal x) const; |
|
98 void grabFocus() { m_editor->setFocus(); } |
|
99 void removeFocus() { m_editor->clearFocus(); } |
|
100 void setCursorPosition (int pos) { m_editor->setCursorPosition(pos); } |
|
101 void shiftToLeftEnd() { setCursorPosition(0); } |
|
102 int characterCount() { return m_editor->document()->characterCount(); } |
|
103 bool hasSelection() { return m_editor->hasSelection(); } |
|
104 |
|
105 protected: |
|
106 virtual bool eventFilter(QObject * object, QEvent * event); |
|
107 virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); |
|
108 virtual void resizeEvent(QGraphicsSceneResizeEvent * event); |
|
109 |
|
110 signals: |
|
111 void activated(); |
|
112 void textMayChanged(); |
|
113 void focusChanged(bool focusIn); |
|
114 void tapped(QPointF& pos); |
|
115 |
|
116 private slots: |
|
117 void makeVisible(qreal cursorX); |
|
118 |
|
119 public slots: |
|
120 void setText(const QString & text); |
|
121 |
|
122 private: |
|
123 void internalScroll(qreal deltaX); |
|
124 |
|
125 protected: |
|
126 ChromeWidget * m_chrome; |
|
127 |
|
128 // Cached values used for painting and scrolling. |
|
129 qreal m_viewPortWidth; |
|
130 qreal m_viewPortHeight; |
|
131 |
|
132 // At runtime, UrlSearchSnippet is parent to a QGraphicsWidget |
|
133 // (m_viewPort) that is parent to a UrlEditorWidget (m_editor). |
|
134 QGraphicsWidget * m_viewPort; |
|
135 GTextLineItem * m_editor; |
|
136 |
|
137 // Attributes |
|
138 QString m_text; |
|
139 QColor m_textColor; |
|
140 QColor m_backgroundColor; |
|
141 qreal m_padding; |
|
142 //TODO: add left margin too |
|
143 qreal m_rightTextMargin; |
|
144 |
|
145 }; |
|
146 |
|
147 class GTextEditor : public GLineEditor |
|
148 { |
|
149 Q_OBJECT |
|
150 |
|
151 public: |
|
152 GTextEditor(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent = 0); |
|
153 virtual ~GTextEditor(); |
|
154 void setBorderColor(QColor & color) { m_borderColor = color; } |
|
155 |
|
156 protected: |
|
157 virtual void paintBorder(QPainter * painter); |
|
158 virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); |
|
159 private: |
|
160 QColor m_borderColor; |
|
161 }; |
|
162 |
|
163 |
|
164 class GProgressEditor : public GTextEditor |
|
165 { |
|
166 Q_OBJECT |
|
167 |
|
168 public: |
|
169 GProgressEditor(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent = 0); |
|
170 virtual ~GProgressEditor(); |
|
171 |
|
172 void setProgressColor(QColor & color) { m_progressColor = color; } |
|
173 |
|
174 public slots: |
|
175 void setProgress(int percent); |
|
176 |
|
177 protected: |
|
178 virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); |
|
179 |
|
180 private: |
|
181 int m_percent; |
|
182 QColor m_progressColor; |
|
183 }; |
|
184 |
|
185 class TextEditItem : public NativeChromeItem |
|
186 { |
|
187 Q_OBJECT |
|
188 public: |
|
189 TextEditItem(ChromeSnippet * snippet, ChromeWidget * chrome, QGraphicsItem * parent = 0); |
|
190 virtual ~TextEditItem(); |
|
191 GTextEditor * editor() { return m_textEditor; } |
|
192 QString text() { return m_textEditor->text(); } |
|
193 void setText(const QString & text){ m_textEditor->setText(text); } |
|
194 int characterCount() { return m_textEditor->characterCount(); } |
|
195 void setCursorPosition(int pos) { m_textEditor->setCursorPosition(pos); } |
|
196 void selectAll() { m_textEditor->selectAll(); } |
|
197 void unselect() { m_textEditor->unselect(); } |
|
198 protected: |
|
199 virtual void resizeEvent(QGraphicsSceneResizeEvent * ev); |
|
200 private: |
|
201 GTextEditor * m_textEditor; |
|
202 }; |
|
203 |
|
204 } // namespace GVA |
|
205 |
|
206 #endif // __EDITOR_WIDGET_H__ |