Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[modules/kernel.git] / src / SALOMEGUI / QAD_ObjectBrowserItem.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : QAD_ObjectBrowserItem.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "QAD_ObjectBrowserItem.h"
30
31 #include <qcolor.h>
32 #include <qlistview.h>
33 #include <qpixmap.h>
34 #include <qstringlist.h>
35 #include <qpainter.h>
36 using namespace std;
37
38 /*!
39   Constructors
40 */
41 QAD_ObjectBrowserItem::QAD_ObjectBrowserItem( QListView *theParent )
42   : QListViewItem(theParent)
43 {
44   myType = BlackItem;
45   myCurrent = false;
46 }
47 QAD_ObjectBrowserItem::QAD_ObjectBrowserItem( QListView* theParent, QAD_ObjectBrowserItem* after )
48   : QListViewItem(theParent, after)
49 {
50   myType = BlackItem;
51   myCurrent = false;
52 }
53 QAD_ObjectBrowserItem::QAD_ObjectBrowserItem( QAD_ObjectBrowserItem *theParent )
54   : QListViewItem(theParent)           
55 {
56   myType = BlackItem;
57   myCurrent = false;
58 }
59 QAD_ObjectBrowserItem::QAD_ObjectBrowserItem( QAD_ObjectBrowserItem *theParent, QAD_ObjectBrowserItem* after )
60   : QListViewItem(theParent, after)
61 {
62   myType = BlackItem;
63   myCurrent = false;
64 }
65
66 /*!
67   Destructor
68 */
69 QAD_ObjectBrowserItem::~QAD_ObjectBrowserItem()
70 {
71 }
72
73 /*!
74   Sets type
75 */
76 void QAD_ObjectBrowserItem::setType( int type )
77 {
78   myType = type;
79   repaint();
80 }
81
82 /*!
83   Sets text color
84 */
85 void QAD_ObjectBrowserItem::setTextColor( const QColor& color )
86 {
87   myTextColor = color;
88   repaint();
89 }
90
91 /*!
92   Sets text highlight color
93 */
94 void QAD_ObjectBrowserItem::setTextHighlightColor(const QColor& color)
95 {  
96   myTextHighlightColor = color;
97 }
98
99 /*!
100   Gets item name
101 */
102 QString QAD_ObjectBrowserItem::getName() const
103 {
104   return QListViewItem::text( 0 );
105 }
106
107 /*!
108   Sets name
109 */
110 void  QAD_ObjectBrowserItem::setName( const QString& name )
111 {
112   setText( 0, name );
113 }
114
115 /*!
116   Gets value
117 */
118 QString QAD_ObjectBrowserItem::getValue() const
119 {
120   return QListViewItem::text( 1 );
121 }
122
123 /*!
124   Sets value
125 */
126 void QAD_ObjectBrowserItem::setValue( const QString& value )
127 {
128   setText( 1, value );
129 }
130
131 /*!
132   Gets entry
133 */
134 QString QAD_ObjectBrowserItem::getEntry() const
135 {
136   return QListViewItem::text( 2 );
137 }
138
139 /*!
140   Sets entry
141 */
142 void QAD_ObjectBrowserItem::setEntry( const QString& entry )
143 {
144   setText( 2, entry );
145 }
146
147 /*!
148   Gets IOR
149 */
150 QString QAD_ObjectBrowserItem::getIOR() const
151 {
152   return QListViewItem::text( 3 );
153 }
154
155 /*!
156   Sets IOR
157 */
158   void QAD_ObjectBrowserItem::setIOR( const QString& IOR )
159 {
160   setText( 3, IOR );
161 }
162
163 /*!
164   Gets reference
165 */
166 QString QAD_ObjectBrowserItem::getReference() const
167 {
168   return QListViewItem::text( 4 );
169 }
170
171 /*!
172   Sets reference
173 */
174 void QAD_ObjectBrowserItem::setReference( const QString& ref )
175 {
176   setText( 4, ref );
177 }
178 /*!
179   Returns true if this item is current (for UseCase browser - it is bold)
180 */
181 bool QAD_ObjectBrowserItem::isCurrent() const
182 {
183   return myCurrent;
184 }
185 /*!
186   Sets this item to be current (for UseCase browser - it is bold)
187 */
188 void QAD_ObjectBrowserItem::setCurrent( bool on )
189 {
190   myCurrent = on;
191   repaint();
192 }
193
194 /*!
195   Paints cell [ redefined ]
196 */
197 void QAD_ObjectBrowserItem::paintCell( QPainter*          thePainter,
198                                        const QColorGroup& theColorGroup,
199                                        int                theColumn,
200                                        int                theWidth,
201                                        int                theAlignment )
202
203   QColorGroup aColorGroup(theColorGroup);
204   QColor      aColor          = aColorGroup.text();
205   QColor      aColorHighlight = aColorGroup.highlight();
206   
207   switch (myType)
208     {
209     case 1 :
210       {
211         aColorGroup.setColor(QColorGroup::Text,       Qt::blue);
212         aColorGroup.setColor(QColorGroup::Highlight,  Qt::blue);
213         break;
214       }
215     case 2 :
216       {
217         aColorGroup.setColor(QColorGroup::Text,       Qt::red);
218         aColorGroup.setColor(QColorGroup::Highlight,  Qt::red);
219         break;
220       }
221     default :
222       {
223         aColorGroup.setColor(QColorGroup::Text,       Qt::black);
224         aColorGroup.setColor(QColorGroup::Highlight,  Qt::black);
225         break;
226       }
227     }
228       
229   if (myTextColor.isValid()) {
230     aColorGroup.setColor(QColorGroup::Text,            myTextColor);
231     aColorGroup.setColor(QColorGroup::HighlightedText, myTextColor);
232   }
233   if (myTextHighlightColor.isValid())
234     aColorGroup.setColor(QColorGroup::Highlight,  myTextHighlightColor);
235   if (myCurrent) { // UseCase's current entry 
236     QFont font = thePainter->font();
237     font.setBold(true); 
238     thePainter->setFont(font);
239   }
240   
241   QListViewItem::paintCell(thePainter,
242                            aColorGroup,
243                            theColumn,
244                            theWidth,
245                            theAlignment);
246   aColorGroup.setColor(QColorGroup::Text,       aColor);
247   aColorGroup.setColor(QColorGroup::Highlight,  aColorHighlight);
248 }
249
250 /*!
251   Returns item's column text
252   Overrided to protect access outside
253 */
254 QString QAD_ObjectBrowserItem::text( int column ) const
255 {
256   return QListViewItem::text( column );
257 }
258
259 /*!
260   Compares two item to sort list view data by column
261 */
262 int QAD_ObjectBrowserItem::compare( QListViewItem* i, int col, bool ascending ) const
263 {
264   // Additional check is necessary for Entry or Reference entry (2 and 4) columns
265   if ( ( col == 2 || col == 4 ) && !key( col, ascending ).isEmpty() && !i->key( col, ascending ).isEmpty() ) { 
266     QStringList mylist = QStringList::split( ":", key( col, ascending ),    false );
267     QStringList ilist  = QStringList::split( ":", i->key( col, ascending ), false );
268     if ( mylist.count() != ilist.count() || mylist.count() == 0 )
269       return ( mylist.count() < ilist.count() ) ? -1 : 1;
270     for ( int i = 0; i < mylist.count(); i++ ) {
271       if ( mylist[i].toInt() < ilist[i].toInt() )
272         return -1;
273       else if ( mylist[i].toInt() > ilist[i].toInt() )
274         return 1;
275     }
276     return 0;
277   }
278   return QListViewItem::compare( i, col, ascending );
279 }