javauis/eswt_qt/org.eclipse.swt/Eclipse_SWT_PI/qt/library/listdatamodel.h
changeset 21 2a9601315dfc
child 26 dc7c549001d5
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     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 ListViewItem : public ListItem
       
    46 	{
       
    47 public:
       
    48 	ListViewItem(const QString& aText, const QPixmap* aImage);
       
    49 	~ListViewItem();
       
    50 	const QPixmap* image() const;
       
    51 	void setImage(const QPixmap* pixmap);
       
    52 	
       
    53 private:	
       
    54 	const QPixmap* img;
       
    55 	};
       
    56 
       
    57 class ListBoxItem : public ListItem
       
    58 	{
       
    59 public:
       
    60 	ListBoxItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount,  
       
    61 		const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount );
       
    62 	~ListBoxItem();
       
    63 	const QString& headingText() const;
       
    64 	const QList<QPixmap>& detailImages() const ;
       
    65 	const QList<QPixmap>& headingImages() const;
       
    66 	int detailImageCount() const;
       
    67 	int headingImageCount() const;
       
    68 	
       
    69 private:
       
    70 	const QString headingTxt;
       
    71 	QList<QPixmap> detailImgs;
       
    72 	QList<QPixmap> headingImgs;
       
    73 	};	
       
    74 
       
    75 class ListModel : public QAbstractListModel
       
    76 	{
       
    77 public:
       
    78 
       
    79      ListModel( int type, int style, QListView *parent );
       
    80      ~ListModel();
       
    81       
       
    82      
       
    83     /** 
       
    84 	 * from QAbstractListModel
       
    85 	 */
       
    86 	int rowCount(const QModelIndex& parent) const;
       
    87    	QVariant data(const QModelIndex& index, int role) const;
       
    88 	Qt::ItemFlags flags ( const QModelIndex& index ) const;
       
    89  
       
    90  	/**
       
    91  	 * called by JNI, these calls are delegated to protected methods from QAbstractListModel
       
    92  	 */
       
    93  	void beginInsertRows( const QModelIndex& parent, int first, int last );
       
    94 	void endInsertRows(); 
       
    95 	void beginRemoveRows( const QModelIndex& parent, int first, int last );
       
    96 	void endRemoveRows(); 
       
    97 	void emitLayoutAboutToBeChanged();
       
    98 	void emitLayoutChanged();
       
    99 
       
   100  	/**
       
   101  	 * called by JNI
       
   102  	 */
       
   103     static ListModel* createDataModel( int type, int style, QListView *parent );
       
   104 	const QString& itemString( const int row ) const;
       
   105 	jobjectArray itemStrings( JNIEnv* aJniEnv );
       
   106 	int indexOf( const QString& string, const int start ) const;
       
   107 	int insertPosition(const QString& string, const bool ascent = true );
       
   108 	void setCheckState( const QItemSelection& selection, Qt::CheckState state );
       
   109 	void remove( const int row );
       
   110 	void appendItem( const QString& string );
       
   111 	void appendItem( const QString& string, const QPixmap* pixmap );
       
   112 	void setItem( const int row, const QString& string );
       
   113 	void setItem( const int row, const QString& string, const QPixmap* pixmap );
       
   114 	void insertItem( const int row, const QString& string );
       
   115 	void insertItem( const int row, const QString& string, const QPixmap* pixmap );
       
   116 	virtual void appendItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount,  
       
   117 		const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount );
       
   118 	virtual int itemHeight();
       
   119 	virtual void clearList();
       
   120 	
       
   121  	/**
       
   122  	 * called by other native classes
       
   123  	 */
       
   124 	int style() const;
       
   125 	
       
   126 protected:
       
   127  	virtual ListItem* createItem( const QString& string );
       
   128  	virtual ListItem* createItem( const QString& string, const QPixmap* pixmap );
       
   129 	virtual ListItem* createItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount,  
       
   130 		const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount );
       
   131 
       
   132 protected:
       
   133 	const int listType;
       
   134 	const int layoutStyle;
       
   135  	/**
       
   136  	 * data body
       
   137  	 */
       
   138 	QList<ListItem*> list;
       
   139 	};	
       
   140 
       
   141 
       
   142 class ListViewModel : public ListModel
       
   143 	{
       
   144 public:
       
   145      ListViewModel( int type, int style, QListView *parent );
       
   146      ~ListViewModel();
       
   147      
       
   148 protected: 
       
   149  	/**
       
   150  	 * from ListModel
       
   151  	 */
       
   152  	ListItem* createItem( const QString& string );
       
   153  	ListItem* createItem( const QString& string, const QPixmap* pixmap );
       
   154 	};
       
   155 
       
   156 
       
   157 class ListBoxModel : public ListModel
       
   158 	{
       
   159 public:
       
   160 	ListBoxModel( int type, int style, QListView *parent );
       
   161 	~ListBoxModel();
       
   162 	
       
   163  	/**
       
   164  	 * from ListModel
       
   165  	 */
       
   166 	void clearList();
       
   167 	void appendItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount,  
       
   168 		const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount );
       
   169 
       
   170  	/**
       
   171  	 * Below 5 methods provide item data
       
   172   	 */
       
   173 	const QString& headingText(const int row) const;
       
   174 	int detailImageCount( const int row ) const;
       
   175 	int headingImageCount( const int row ) const;	
       
   176 	const QList<QPixmap>& detailImages(const int row ) const;
       
   177 	const QList<QPixmap>& headingImages( const int row ) const;
       
   178 		
       
   179  	/**
       
   180  	 * called by ListBoxDelegate to save cached layout data to ListModel
       
   181  	 * so that the ListBoxDelegate does not have to compute layout data from scratch
       
   182  	 * whenever drawing a list item.
       
   183  	 */
       
   184 	void updateLayoutData( const int row, const int rowHeight, const QPoint detailTxtSize, 
       
   185 		const QPoint headingTxtSize, const int detailImgWidth, const int headingImgWidth ) const;
       
   186 	
       
   187  	/**
       
   188  	 * Below 10 methods provide cached layout data 
       
   189   	 */
       
   190 	const int& detailImageWidth(const int index ) const;
       
   191 	const int& headingImageWidth(const int index ) const;
       
   192 	QPoint detailTextSize() const;
       
   193 	QPoint headingTextSize() const;
       
   194 	int detailImageCount() const;
       
   195 	int headingImageCount() const;
       
   196 	QPoint detailImageSize(const int row) const;
       
   197 	QPoint headingImageSize(const int row) const;
       
   198 	int rowWithMaxDetailTextSize() const;
       
   199 	int rowWithMaxHeadingTextSize() const;
       
   200 	
       
   201 protected:	
       
   202  	/**
       
   203  	 * from ListModel
       
   204   	 */
       
   205 	ListItem* createItem(const QString& aDetailText, const QPixmap** aDetailImages, const int aDetailImageCount,  
       
   206 		const QString& aHeadingText, const QPixmap** aHeadingImages, const int aHeadingImageCount );
       
   207 private:
       
   208  	/**
       
   209  	 * layout data
       
   210   	 */
       
   211   	 
       
   212  	/**
       
   213  	 * holds the index which item has the largest detail text size
       
   214   	 */
       
   215 	int detailTxtIndex;
       
   216 	
       
   217  	/**
       
   218  	 * holds the index which item has the largest heading text size
       
   219   	 */
       
   220 	int headingTxtIndex;
       
   221 	
       
   222  	/**
       
   223  	 * holds the largest  detail text size
       
   224   	 */
       
   225 	mutable QPoint detailTxtSize;
       
   226 	
       
   227  	/**
       
   228  	 * holds the largest  heading text size
       
   229   	 */
       
   230 	mutable QPoint headingTxtSize;
       
   231 	
       
   232  	/**
       
   233  	 * holds the amount of columns for detail images
       
   234   	 */
       
   235 	int detailImgCount;
       
   236 	
       
   237  	/**
       
   238  	 * holds the amount of columns for heading images
       
   239   	 */
       
   240 	int headingImgCount;
       
   241 	
       
   242  	/**
       
   243  	 * holds maximum height of images 
       
   244   	 */
       
   245 	int imgHeight;
       
   246 	
       
   247  	/**
       
   248  	 * holds widths for every detail image columns 
       
   249   	 */
       
   250 	int* detailImgWidths;
       
   251 	
       
   252  	/**
       
   253  	 * holds widths for every heading image columns 
       
   254   	 */
       
   255 	int* headingImgWidths;
       
   256 	
       
   257  	/**
       
   258  	 * holds heights for every rows
       
   259   	 */
       
   260 	mutable int* rowHeights;
       
   261 	
       
   262  	/**
       
   263  	 * holds entire width for all of detail images.
       
   264   	 */
       
   265 	mutable int detailImgWidth;
       
   266 	
       
   267  	/**
       
   268  	 * holds entire width for all of heading images.
       
   269   	 */
       
   270 	mutable int headingImgWidth;
       
   271 	};
       
   272 
       
   273  
       
   274  class ListBoxItemDelegate : public QItemDelegate
       
   275 	{
       
   276 	Q_OBJECT
       
   277 public:
       
   278 	ListBoxItemDelegate( QObject* parent );
       
   279 	
       
   280 	// from QItemDelegate
       
   281 	void paint(QPainter * painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;	
       
   282     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
       
   283     
       
   284     // called from JNI
       
   285 	void setHeadingFont( QFont* font );
       
   286 
       
   287 private:
       
   288 
       
   289  	/**
       
   290  	 * compute sizes of checkbox, detailtext, headingtext, detailImage and  headingImage.
       
   291   	 */
       
   292 	void computeSizes( const QModelIndex& index, const QStyleOptionViewItem& option, QSize* checkSize, 
       
   293 	QSize *headingImageSize, QSize* detailImageSize, QSize* headingTextSize, QSize* detailTextSize) const;
       
   294 	
       
   295  	/**
       
   296  	 * add margins to checkbox, detailtext, headingtext, detailImage and  headingImage.
       
   297   	 */
       
   298 	void addMargins( const QStyleOptionViewItem &option, QSize* checkSize, 
       
   299 		QSize* headingImageSize, QSize* detailImageSize, QSize* headingTextSize, QSize* detailTextSize ) const;
       
   300 		
       
   301 	/**	
       
   302 	 * compute item entire size	for the item
       
   303 	 */
       
   304 	QSize computeSize(  const QStyleOptionViewItem &option, const QSize& check, const QSize& headingImage, 
       
   305 		const QSize& detailImage, const QSize& headingText, const QSize& detailText ) const;
       
   306 		
       
   307 	/**	
       
   308 	 * layout checkbox, detailtext, headingtext, detailImage and headingImage within a given rectangle
       
   309 	 * and adjust sizes and positions according to alignments from option	
       
   310 	 */
       
   311 	void layoutRects( const QStyleOptionViewItem &option, QRect* check, QRect *headingImage, 
       
   312 		QRect* detailImage, QRect* headingText, QRect* detailText, bool hint ) const;
       
   313 	
       
   314 	/**	
       
   315 	 * layout checkbox, detailtext, headingtext, detailImage and  headingImage within a given rectangle
       
   316 	 */
       
   317 	void doLayout( const QStyleOptionViewItem &option, QRect* checkRect, QRect* headingImageRect,
       
   318 		QRect* detailImageRect, QRect* headingTextRect, QRect* detailTextRect ) const;
       
   319     	QStyleOptionViewItem setOptions(const QModelIndex &index, const QStyleOptionViewItem &option) const;
       
   320     
       
   321 	QRect textRectangle(const int width, const QFont &font, const QString &text) const;
       
   322 	
       
   323 	int margin( const QStyleOptionViewItem &option ) const;
       
   324 
       
   325 private:
       
   326     void drawImages( QPainter* painter, const QStyleOptionViewItem &option, 
       
   327             const QRect imageRect, const QList<QPixmap>& images, const int* imageWidths, const int imageCount ) const;
       
   328 	
       
   329 private:	
       
   330 
       
   331 	/**	
       
   332 	 * not own
       
   333 	 */
       
   334 	const ListBoxModel* dataModel;
       
   335     mutable QTextLayout textLayout;
       
   336     QFont* headingFont;
       
   337 	};
       
   338 	
       
   339  }}
       
   340 #endif // SWT_LISTDATAMODEL_H