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