|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Message editor body text field |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "nmailuiwidgetsheaders.h" |
|
19 |
|
20 static const QString FILE_PATH_CSS_DEFAULT = ":nmeditortexteditblack.css"; |
|
21 static const QString FILE_PATH_CSS_SECONDARY = ":nmeditortexteditblue.css"; |
|
22 static const QString FILE_PATH_WIDGETML = ":nmeditortextedit.widgetml"; |
|
23 |
|
24 /*! |
|
25 Constructor |
|
26 */ |
|
27 NmEditorTextEdit::NmEditorTextEdit(QGraphicsItem *parent) : |
|
28 HbTextEdit(parent) |
|
29 { |
|
30 NM_FUNCTION; |
|
31 |
|
32 HbStyleLoader::registerFilePath(FILE_PATH_WIDGETML); |
|
33 HbStyleLoader::registerFilePath(FILE_PATH_CSS_DEFAULT); |
|
34 |
|
35 mCustomTextColor = QPair<bool, QColor>(false, Qt::black); |
|
36 |
|
37 // Disable HbTextEdit scrolling. Background scroll area will handle scrolling. |
|
38 setScrollable(false); |
|
39 scrollArea()->setScrollDirections(0); |
|
40 |
|
41 // set background colour to plain white |
|
42 QPixmap whitePixmap(10,10); |
|
43 whitePixmap.fill(Qt::white); |
|
44 QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem (whitePixmap); |
|
45 setBackgroundItem(pixmapItem); |
|
46 |
|
47 // Create custom palette based on the current one |
|
48 QPalette testPalette = QApplication::palette(); |
|
49 |
|
50 // Sets the selection background colour |
|
51 testPalette.setColor(QPalette::Active, QPalette::Highlight, Qt::cyan); |
|
52 testPalette.setColor(QPalette::Inactive, QPalette::Highlight, Qt::cyan); |
|
53 |
|
54 // Sets the link and visited link colours |
|
55 testPalette.setColor(QPalette::Active, QPalette::Link, Qt::darkBlue); |
|
56 testPalette.setColor(QPalette::Inactive, QPalette::Link, Qt::darkBlue); |
|
57 testPalette.setColor(QPalette::Active, QPalette::LinkVisited, Qt::darkMagenta); |
|
58 testPalette.setColor(QPalette::Inactive, QPalette::LinkVisited, Qt::darkMagenta); |
|
59 |
|
60 // Update custom palette for this widget |
|
61 setPalette(testPalette); |
|
62 } |
|
63 |
|
64 /*! |
|
65 Destructor |
|
66 */ |
|
67 NmEditorTextEdit::~NmEditorTextEdit() |
|
68 { |
|
69 NM_FUNCTION; |
|
70 |
|
71 HbStyleLoader::unregisterFilePath(FILE_PATH_WIDGETML); |
|
72 if (mCustomTextColor.first) { |
|
73 HbStyleLoader::unregisterFilePath(FILE_PATH_CSS_SECONDARY); |
|
74 } |
|
75 else { |
|
76 HbStyleLoader::unregisterFilePath(FILE_PATH_CSS_DEFAULT); |
|
77 } |
|
78 } |
|
79 |
|
80 /*! |
|
81 This slot applies custom text color for user - entered text |
|
82 It does not affect the text originally inserted into editor |
|
83 */ |
|
84 void NmEditorTextEdit::updateCustomTextColor() |
|
85 { |
|
86 NM_FUNCTION; |
|
87 |
|
88 if (mCustomTextColor.first) { |
|
89 QTextCursor tcursor = textCursor(); |
|
90 QTextCharFormat fmt; |
|
91 int pos = tcursor.position(); |
|
92 if (pos > 0) { |
|
93 // If there isn't any user-made selection - apply custom color |
|
94 if (!tcursor.hasSelection() && !tcursor.hasComplexSelection()) { |
|
95 // Select last added char and set its color to custom |
|
96 if (tcursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, 1) |
|
97 && tcursor.hasSelection()) { |
|
98 fmt = tcursor.charFormat(); |
|
99 fmt.setForeground(mCustomTextColor.second); |
|
100 tcursor.mergeCharFormat(fmt); |
|
101 } |
|
102 } |
|
103 } |
|
104 else { |
|
105 fmt = tcursor.charFormat(); |
|
106 fmt.setForeground(mCustomTextColor.second); |
|
107 tcursor.mergeCharFormat(fmt); |
|
108 setTextCursor(tcursor); |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 /*! |
|
114 Sets flag is custom text color should be used and sets the custom color. |
|
115 |
|
116 Function does not affect the color of existing content, only text that will be entered later. |
|
117 */ |
|
118 void NmEditorTextEdit::setCustomTextColor(const QPair<bool, QColor> &customColor) |
|
119 { |
|
120 NM_FUNCTION; |
|
121 |
|
122 mCustomTextColor = customColor; |
|
123 } |
|
124 |
|
125 /*! |
|
126 Reimplemented function \sa NmEditorTextEdit::setCustomTextColor(const QPair<bool, QColor> &customColor). |
|
127 |
|
128 \arg info about using of custom color |
|
129 \arg color to be used as custom. If \arg useCustom is set to false then color is not changed |
|
130 */ |
|
131 void NmEditorTextEdit::setCustomTextColor(bool useCustom, const QColor& color) |
|
132 { |
|
133 NM_FUNCTION; |
|
134 |
|
135 if (!mCustomTextColor.first && useCustom) { |
|
136 HbStyleLoader::unregisterFilePath(FILE_PATH_CSS_DEFAULT); |
|
137 HbStyleLoader::registerFilePath(FILE_PATH_CSS_SECONDARY); |
|
138 |
|
139 mCustomTextColor.first = useCustom; |
|
140 mCustomTextColor.second = color; |
|
141 } |
|
142 } |
|
143 |
|
144 /*! |
|
145 * Return current custom color and if it must be used. |
|
146 * |
|
147 * \return Current custem color. |
|
148 */ |
|
149 QPair<bool, QColor> NmEditorTextEdit::customTextColor() const |
|
150 { |
|
151 NM_FUNCTION; |
|
152 |
|
153 return mCustomTextColor; |
|
154 } |
|
155 |
|
156 /*! |
|
157 * Returns the rectangle for the cursor. |
|
158 */ |
|
159 QRectF NmEditorTextEdit::rectForCursorPosition() const |
|
160 { |
|
161 NM_FUNCTION; |
|
162 |
|
163 return HbTextEdit::rectForPosition(cursorPosition()); |
|
164 } |
|
165 |