|
1 /******************************************************************************* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. This program and the accompanying materials |
|
4 * are made available under the terms of the Eclipse Public License v1.0 |
|
5 * which accompanies this distribution, and is available at |
|
6 * http://www.eclipse.org/legal/epl-v10.html |
|
7 * |
|
8 * Contributors: |
|
9 * Nokia Corporation - initial implementation |
|
10 *******************************************************************************/ |
|
11 |
|
12 |
|
13 #ifndef SWT_LISTDATAMODEL_H |
|
14 #define SWT_LISTDATAMODEL_H |
|
15 |
|
16 #include <QAbstractListModel> |
|
17 #include <QItemDelegate> |
|
18 #include <QTextLayout> |
|
19 #include <QItemSelection> |
|
20 #include <QList> |
|
21 #include "jniutils.h" |
|
22 |
|
23 |
|
24 class QListView; |
|
25 |
|
26 namespace Java { namespace eSWT { |
|
27 |
|
28 |
|
29 class ListItem |
|
30 { |
|
31 public: |
|
32 ListItem( const QString& aText ); |
|
33 virtual ~ListItem(); |
|
34 const QString& text() const; |
|
35 virtual const QPixmap* image() const; |
|
36 void setText(const QString& string); |
|
37 Qt::CheckState checkState() const; |
|
38 void setCheckState( Qt::CheckState state ); |
|
39 |
|
40 protected: |
|
41 Qt::CheckState state; |
|
42 QString txt; |
|
43 }; |
|
44 |
|
45 class ListBoxItem : public ListItem |
|
46 { |
|
47 public: |
|
48 ListBoxItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount, |
|
49 const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount ); |
|
50 ~ListBoxItem(); |
|
51 const QString& headingText() const; |
|
52 const QList<QPixmap>& detailImages() const ; |
|
53 const QList<QPixmap>& headingImages() const; |
|
54 int detailImageCount() const; |
|
55 int headingImageCount() const; |
|
56 void setContentsToNull(); |
|
57 |
|
58 |
|
59 private: |
|
60 QString headingTxt; |
|
61 QList<QPixmap> detailImgs; |
|
62 QList<QPixmap> headingImgs; |
|
63 }; |
|
64 |
|
65 class ListModel : public QAbstractListModel |
|
66 { |
|
67 public: |
|
68 |
|
69 ListModel( int type, int style, QListView *parent ); |
|
70 ~ListModel(); |
|
71 |
|
72 |
|
73 /** |
|
74 * from QAbstractListModel |
|
75 */ |
|
76 int rowCount(const QModelIndex& parent) const; |
|
77 QVariant data(const QModelIndex& index, int role) const; |
|
78 Qt::ItemFlags flags ( const QModelIndex& index ) const; |
|
79 |
|
80 /** |
|
81 * called by JNI, these calls are delegated to protected methods from QAbstractListModel |
|
82 */ |
|
83 void beginInsertRows( const QModelIndex& parent, int first, int last ); |
|
84 void endInsertRows(); |
|
85 void beginRemoveRows( const QModelIndex& parent, int first, int last ); |
|
86 void endRemoveRows(); |
|
87 void emitLayoutAboutToBeChanged(); |
|
88 void emitLayoutChanged(); |
|
89 |
|
90 /** |
|
91 * called by JNI |
|
92 */ |
|
93 static ListModel* createDataModel( int type, int style, QListView *parent ); |
|
94 const QString& itemString( const int row ) const; |
|
95 jobjectArray itemStrings( JNIEnv* aJniEnv ); |
|
96 int indexOf( const QString& string, const int start ) const; |
|
97 int insertPosition(const QString& string, const bool ascent = true ); |
|
98 void setCheckState( const QItemSelection& selection, Qt::CheckState state ); |
|
99 void remove( const int row ); |
|
100 void appendItem( const QString& string ); |
|
101 void appendItem( const QString& string, const QPixmap* pixmap ); |
|
102 void insertItem( const int row, const QString& string ); |
|
103 void insertItem( const int row, const QString& string, const QPixmap* pixmap ); |
|
104 virtual void appendItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount, |
|
105 const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount ); |
|
106 virtual int itemHeight(); |
|
107 virtual void clearList(); |
|
108 virtual void setItemContentsToNull( const int row ); |
|
109 |
|
110 /** |
|
111 * called by other native classes |
|
112 */ |
|
113 int style() const; |
|
114 |
|
115 protected: |
|
116 virtual ListItem* createItem( const QString& string ); |
|
117 virtual ListItem* createItem( const QString& string, const QPixmap* pixmap ); |
|
118 virtual ListItem* createItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount, |
|
119 const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount ); |
|
120 |
|
121 protected: |
|
122 const int listType; |
|
123 const int layoutStyle; |
|
124 /** |
|
125 * data body |
|
126 */ |
|
127 QList<ListItem*> list; |
|
128 }; |
|
129 |
|
130 |
|
131 class ListBoxModel : public ListModel |
|
132 { |
|
133 public: |
|
134 ListBoxModel( int type, int style, QListView *parent ); |
|
135 ~ListBoxModel(); |
|
136 |
|
137 /** |
|
138 * from ListModel |
|
139 */ |
|
140 void clearList(); |
|
141 void appendItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount, |
|
142 const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount ); |
|
143 |
|
144 /** |
|
145 * Below 5 methods provide item data |
|
146 */ |
|
147 const QString& headingText(const int row) const; |
|
148 int detailImageCount( const int row ) const; |
|
149 int headingImageCount( const int row ) const; |
|
150 const QList<QPixmap>& detailImages(const int row ) const; |
|
151 const QList<QPixmap>& headingImages( const int row ) const; |
|
152 |
|
153 /** |
|
154 * called by ListBoxDelegate to save cached layout data to ListModel |
|
155 * so that the ListBoxDelegate does not have to compute layout data from scratch |
|
156 * whenever drawing a list item. |
|
157 */ |
|
158 void updateLayoutData( const int row, const int rowHeight, const QPoint detailTxtSize, |
|
159 const QPoint headingTxtSize, const int detailImgWidth, const int headingImgWidth ) const; |
|
160 |
|
161 /** |
|
162 * Below 10 methods provide cached layout data |
|
163 */ |
|
164 const int& detailImageWidth(const int index ) const; |
|
165 const int& headingImageWidth(const int index ) const; |
|
166 QPoint detailTextSize() const; |
|
167 QPoint headingTextSize() const; |
|
168 int detailImageCount() const; |
|
169 int headingImageCount() const; |
|
170 QPoint detailImageSize(const int row) const; |
|
171 QPoint headingImageSize(const int row) const; |
|
172 int rowWithMaxDetailTextSize() const; |
|
173 int rowWithMaxHeadingTextSize() const; |
|
174 void setItemContentsToNull( const int row ); |
|
175 |
|
176 protected: |
|
177 /** |
|
178 * from ListModel |
|
179 */ |
|
180 ListItem* createItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount, |
|
181 const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount ); |
|
182 private: |
|
183 /** |
|
184 * layout data |
|
185 */ |
|
186 |
|
187 /** |
|
188 * holds the index which item has the largest detail text size |
|
189 */ |
|
190 int detailTxtIndex; |
|
191 |
|
192 /** |
|
193 * holds the index which item has the largest heading text size |
|
194 */ |
|
195 int headingTxtIndex; |
|
196 |
|
197 /** |
|
198 * holds the largest detail text size |
|
199 */ |
|
200 mutable QPoint detailTxtSize; |
|
201 |
|
202 /** |
|
203 * holds the largest heading text size |
|
204 */ |
|
205 mutable QPoint headingTxtSize; |
|
206 |
|
207 /** |
|
208 * holds the amount of columns for detail images |
|
209 */ |
|
210 int detailImgCount; |
|
211 |
|
212 /** |
|
213 * holds the amount of columns for heading images |
|
214 */ |
|
215 int headingImgCount; |
|
216 |
|
217 /** |
|
218 * holds maximum height of images |
|
219 */ |
|
220 int imgHeight; |
|
221 |
|
222 /** |
|
223 * holds widths for every detail image columns |
|
224 */ |
|
225 int* detailImgWidths; |
|
226 |
|
227 /** |
|
228 * holds widths for every heading image columns |
|
229 */ |
|
230 int* headingImgWidths; |
|
231 |
|
232 /** |
|
233 * holds heights for every rows |
|
234 */ |
|
235 mutable int* rowHeights; |
|
236 |
|
237 /** |
|
238 * holds entire width for all of detail images. |
|
239 */ |
|
240 mutable int detailImgWidth; |
|
241 |
|
242 /** |
|
243 * holds entire width for all of heading images. |
|
244 */ |
|
245 mutable int headingImgWidth; |
|
246 }; |
|
247 |
|
248 |
|
249 class ListBoxItemDelegate : public QItemDelegate |
|
250 { |
|
251 Q_OBJECT |
|
252 public: |
|
253 ListBoxItemDelegate( QObject* parent ); |
|
254 |
|
255 // from QItemDelegate |
|
256 void paint(QPainter * painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; |
|
257 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; |
|
258 |
|
259 // called from JNI |
|
260 void setHeadingFont( QFont* font ); |
|
261 |
|
262 private: |
|
263 |
|
264 /** |
|
265 * compute sizes of checkbox, detailtext, headingtext, detailImage and headingImage. |
|
266 */ |
|
267 void computeSizes( const QModelIndex& index, const QStyleOptionViewItem& option, QSize* checkSize, |
|
268 QSize *headingImageSize, QSize* detailImageSize, QSize* headingTextSize, QSize* detailTextSize) const; |
|
269 |
|
270 /** |
|
271 * add margins to checkbox, detailtext, headingtext, detailImage and headingImage. |
|
272 */ |
|
273 void addMargins( const QStyleOptionViewItem &option, QSize* checkSize, |
|
274 QSize* headingImageSize, QSize* detailImageSize, QSize* headingTextSize, QSize* detailTextSize ) const; |
|
275 |
|
276 /** |
|
277 * compute item entire size for the item |
|
278 */ |
|
279 QSize computeSize( const QStyleOptionViewItem &option, const QSize& check, const QSize& headingImage, |
|
280 const QSize& detailImage, const QSize& headingText, const QSize& detailText ) const; |
|
281 |
|
282 /** |
|
283 * layout checkbox, detailtext, headingtext, detailImage and headingImage within a given rectangle |
|
284 * and adjust sizes and positions according to alignments from option |
|
285 */ |
|
286 void layoutRects( const QStyleOptionViewItem &option, QRect* check, QRect *headingImage, |
|
287 QRect* detailImage, QRect* headingText, QRect* detailText, bool hint ) const; |
|
288 |
|
289 /** |
|
290 * layout checkbox, detailtext, headingtext, detailImage and headingImage within a given rectangle |
|
291 */ |
|
292 void doLayout( const QStyleOptionViewItem &option, QRect* checkRect, QRect* headingImageRect, |
|
293 QRect* detailImageRect, QRect* headingTextRect, QRect* detailTextRect ) const; |
|
294 QStyleOptionViewItem setOptions(const QModelIndex &index, const QStyleOptionViewItem &option) const; |
|
295 |
|
296 QRect textRectangle(const int width, const QFont &font, const QString &text) const; |
|
297 |
|
298 int margin( const QStyleOptionViewItem &option ) const; |
|
299 |
|
300 private: |
|
301 void drawImages( QPainter* painter, const QStyleOptionViewItem &option, |
|
302 const QRect imageRect, const QList<QPixmap>& images, const int* imageWidths, const int imageCount ) const; |
|
303 |
|
304 private: |
|
305 |
|
306 /** |
|
307 * not own |
|
308 */ |
|
309 const ListBoxModel* dataModel; |
|
310 mutable QTextLayout textLayout; |
|
311 QFont* headingFont; |
|
312 }; |
|
313 |
|
314 }} |
|
315 #endif // SWT_LISTDATAMODEL_H |