29 #include <QHeaderView>
30 #include <QMouseEvent>
31 #include "QY2ListView.h"
33 #define YUILogComponent "qt-pkg"
34 #include <yui/YUILog.h>
37 : QTreeWidget( parent )
38 , _mousePressedItem(0)
39 , _mousePressedCol( -1 )
40 , _mousePressedButton( Qt::NoButton )
41 , _sortByInsertionSequence( false )
43 , _mouseButton1PressedInHeader( false )
44 , _finalSizeChangeExpected( false )
47 setRootIsDecorated(
false);
50 _toolTip =
new QY2ListViewToolTip(
this );
55 header()->installEventFilter(
this );
56 header()->setStretchLastSection(
false );
59 connect( header(), SIGNAL( sectionResized (
int,
int,
int ) ),
62 connect(
this, SIGNAL( itemExpanded ( QTreeWidgetItem *) ),
65 connect(
this, SIGNAL( itemCollapsed ( QTreeWidgetItem *) ),
83 QTreeWidgetItemIterator it(
this );
89 if ( item && (item->flags() & Qt::ItemIsSelectable) )
91 item->setSelected(
true);
103 QTreeWidget::clear();
111 QTreeWidgetItemIterator it(
this );
128 QTreeWidgetItemIterator it(
this );
145 if ( ! listViewItem )
146 return QString::null;
157 return item->
toolTip( column );
162 return checkListItem->
toolTip( column );
164 return QString::null;
171 _savedColumnWidth.clear();
172 _savedColumnWidth.reserve( columnCount() );
174 for (
int i = 0; i < columnCount(); i++ )
176 int size = header()->sectionSize(i);
178 _savedColumnWidth.push_back( size );
186 if ( _savedColumnWidth.size() != (unsigned) columnCount() )
189 for (
int i = 0; i < columnCount(); i++ )
190 resizeColumnToContents(i);
195 for (
int i = 0; i < columnCount(); i++ )
197 header()->resizeSection( i, _savedColumnWidth[ i ] );
200 yuiDebug() <<
"Restoring size " << _savedColumnWidth[i]
201 <<
" for section " << i
202 <<
" now " << header()->sectionSize(i)
214 QTreeWidgetItem * item = itemAt( ev->pos() );
217 if ( item && ( item->flags() & Qt::ItemIsEnabled ) )
219 _mousePressedItem = item;
220 _mousePressedCol = header()->logicalIndexAt( ev->pos().x() );
221 _mousePressedButton = ev->button();
225 _mousePressedItem = 0;
226 _mousePressedCol = -1;
227 _mousePressedButton = -1;
231 QTreeWidget::mousePressEvent( ev );
239 QTreeWidgetItem * item = itemAt( ev->pos() );
241 if ( item && ( item->flags() & Qt::ItemIsEnabled ) && item == _mousePressedItem )
243 int col = header()->logicalIndexAt( ev->pos().x() );
245 if ( item == _mousePressedItem &&
246 col == _mousePressedCol &&
247 ev->button() == _mousePressedButton )
249 emit(
columnClicked( ev->button(), item, col, ev->globalPos() ) );
256 _mousePressedItem = 0;
257 _mousePressedCol = -1;
258 _mousePressedButton = Qt::NoButton;
261 QTreeWidget::mouseReleaseEvent( ev );
268 QTreeWidgetItem * item = itemAt( mapToGlobal( ev->pos() ) );
270 if ( item && ( item->flags() & Qt::ItemIsEnabled ) )
272 int col = header()->logicalIndexAt( ev->pos().x() );
278 _mousePressedItem = 0;
279 _mousePressedCol = -1;
280 _mousePressedButton = Qt::NoButton;
283 QTreeWidget::mouseDoubleClickEvent( ev );
301 if ( _mouseButton1PressedInHeader || _finalSizeChangeExpected )
308 if ( ! _mouseButton1PressedInHeader )
309 _finalSizeChangeExpected =
false;
318 if ( event && obj && obj == header() )
320 if ( event->type() == QEvent::MouseButtonPress )
322 QMouseEvent * mouseEvent = (QMouseEvent *) event;
324 if ( mouseEvent->button() == 1 )
326 _mouseButton1PressedInHeader =
true;
327 _finalSizeChangeExpected =
false;
330 else if ( event->type() == QEvent::MouseButtonRelease )
332 QMouseEvent * mouseEvent = (QMouseEvent *) event;
334 if ( mouseEvent->button() == 1 )
336 _finalSizeChangeExpected =
true;
337 _mouseButton1PressedInHeader =
false;
342 return QTreeWidget::eventFilter( obj, event );
349 return QSize( 0, 0 );
358 header()->setClickable( ! _sortByInsertionSequence );
368 const QString & text )
369 : QTreeWidgetItem( parentListView, QStringList(text), 1)
376 const QString & text )
377 : QTreeWidgetItem( parentItem, QStringList(text), 1 )
383 if ( parentListView )
397 bool sortByInsertionSequence =
false;
400 if ( parentListView )
403 if ( sortByInsertionSequence )
416 if ( otherCheckListItem )
418 return ( this->
serial() < otherCheckListItem->
serial() );
424 int column = treeWidget()->sortColumn();
425 QString text1=text(column).trimmed();
426 QString text2=otherListViewItem.text(column).trimmed();
428 text1=text1.left(text1.indexOf(QChar(
' ')));
429 text2=text2.left(text2.indexOf(QChar(
' ')));
432 bool retval = text1.toInt(&ok1) < text2.toInt(&ok2);
436 else if (ok1 && !ok2)
438 else if (!ok1 && ok2)
442 return QTreeWidgetItem::operator<(otherListViewItem);
447 const QString & text )
450 setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
451 setCheckState(0, Qt::Unchecked);
457 const QString & text )
463 setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
464 setCheckState(0, Qt::Unchecked);
466 if ( parentListView )
477 #if FIXME_ITEM_COLORS
479 QY2CheckListItem::paintCell( QPainter * painter,
480 const QColorGroup & colorGroup,
485 QColorGroup cg = colorGroup;
487 if ( _textColor.isValid() ) cg.setColor( QColorGroup::Text, _textColor );
488 if ( _backgroundColor.isValid() ) cg.setColor( QColorGroup::Base, _backgroundColor );
490 QTreeWidgetItem::paintCell( painter, cg, column, width, alignment );
497 QY2ListViewToolTip::maybeTip(
const QPoint & pos )
499 Q3Header * header = _listView->header();
500 QTreeWidgetItem * item = _listView->itemAt( pos );
505 int x = _listView->viewportToContents( pos ).x();
506 int column = header->sectionAt(x);
511 indent = item->depth() + ( _listView->rootIsDecorated() ? 1 : 0 );
512 indent *= _listView->treeStepSize();
514 if ( pos.x() < indent )
518 QString text = _listView->toolTip( item, column );
520 if ( ! text.isEmpty() )
522 QRect rect( _listView->itemRect( item ) );
527 rect.setWidth( indent );
531 QPoint topLeft( header->sectionPos( column ), 0 );
532 topLeft = _listView->contentsToViewport( topLeft );
533 rect.setX( topLeft.x() );
534 rect.setWidth( header->sectionSize( column ) );
546 if ( columnCount() == 1 && header() && header()->isHidden() )
547 resizeColumnToContents( 0 );
553 if ( columnCount() == 1 && header() && header()->isHidden())
554 resizeColumnToContents( 0 );
560 #include "QY2ListView.moc"
bool sortByInsertionSequence() const
virtual void setSortByInsertionSequence(bool sortByInsertionSequence)
virtual bool operator<(const QTreeWidgetItem &other) const
QY2ListViewItem(QY2ListView *parentListView, const QString &text=QString::null)
void columnWidthChanged(int col, int oldSize, int newSize)
QY2ListView(QWidget *parent)
virtual QSize minimumSizeHint() const
void columnClicked(int button, QTreeWidgetItem *item, int col, const QPoint &pos)
virtual void mouseDoubleClickEvent(QMouseEvent *)
virtual void updateData()
virtual QString toolTip(QTreeWidgetItem *item, int column)
virtual bool eventFilter(QObject *obj, QEvent *event)
virtual ~QY2CheckListItem()
virtual QString toolTip(int column)
void treeCollapsed(QTreeWidgetItem *listViewItem)
void columnDoubleClicked(int button, QTreeWidgetItem *item, int col, const QPoint &pos)
virtual QString toolTip(int column)
QY2CheckListItem(QY2ListView *parentListView, const QString &text)
void treeExpanded(QTreeWidgetItem *listViewItem)
virtual ~QY2ListViewItem()
virtual void mouseReleaseEvent(QMouseEvent *)
virtual void selectSomething()
virtual void mousePressEvent(QMouseEvent *e)
void restoreColumnWidths()
virtual void updateStatus()