kdgantttreeviewrowcontroller.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002  ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB.  All rights reserved.
00003  **
00004  ** This file is part of the KD Gantt library.
00005  **
00006  ** This file may be distributed and/or modified under the terms of the
00007  ** GNU General Public License version 2 as published by the Free Software
00008  ** Foundation and appearing in the file LICENSE.GPL included in the
00009  ** packaging of this file.
00010  **
00011  ** Licensees holding valid commercial KD Gantt licenses may use this file in
00012  ** accordance with the KD Gantt Commercial License Agreement provided with
00013  ** the Software.
00014  **
00015  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017  **
00018  ** See http://www.kdab.net/kdgantt for
00019  **   information about KD Gantt Commercial License Agreements.
00020  **
00021  ** Contact info@kdab.net if any conditions of this
00022  ** licensing are not clear to you.
00023  **
00024  **********************************************************************/
00025 #include "kdgantttreeviewrowcontroller.h"
00026 #include "kdgantttreeviewrowcontroller_p.h"
00027 
00028 #include <QAbstractProxyModel>
00029 #include <QHeaderView>
00030 #include <QScrollBar>
00031 
00032 #include <cassert>
00033 
00034 using namespace KDGantt;
00035 
00041 TreeViewRowController::TreeViewRowController( QTreeView* tv,
00042                                               QAbstractProxyModel* proxy )
00043   : _d( new Private )
00044 {
00045     _d->treeview = static_cast<Private::HackTreeView*>(tv);
00046     _d->proxy = proxy;
00047 }
00048 
00049 TreeViewRowController::~TreeViewRowController()
00050 {
00051     delete _d; _d=0;
00052 }
00053 
00054 #define d d_func()
00055 
00056 int TreeViewRowController::headerHeight() const
00057 {
00058   //return d->treeview->header()->sizeHint().height();
00059     return d->treeview->viewport()->y()-d->treeview->frameWidth();
00060 }
00061 
00062 int TreeViewRowController::maximumItemHeight() const
00063 {
00064     return d->treeview->fontMetrics().height();
00065 }
00066 
00067 int TreeViewRowController::totalHeight() const
00068 {
00069     return d->treeview->verticalScrollBar()->maximum()+d->treeview->viewport()->height();
00070 }
00071 
00072 bool TreeViewRowController::isRowVisible( const QModelIndex& _idx ) const
00073 {
00074   //qDebug() << _idx.model()<<d->proxy << d->treeview->model();
00075     const QModelIndex idx = d->proxy->mapToSource( _idx );
00076     assert( idx.isValid() ? ( idx.model() == d->treeview->model() ):( true ) );
00077     return d->treeview->visualRect(idx).isValid();
00078 }
00079 
00080 bool TreeViewRowController::isRowExpanded( const QModelIndex& _idx ) const
00081 {
00082     const QModelIndex idx = d->proxy->mapToSource( _idx );
00083     assert( idx.isValid() ? ( idx.model() == d->treeview->model() ):( true ) );
00084     return d->treeview->isExpanded( idx );
00085 }
00086 
00087 Span TreeViewRowController::rowGeometry( const QModelIndex& _idx ) const
00088 {
00089     const QModelIndex idx = d->proxy->mapToSource( _idx );
00090     assert( idx.isValid() ? ( idx.model() == d->treeview->model() ):( true ) );
00091     QRect r = d->treeview->visualRect(idx).translated( QPoint( 0, d->treeview->verticalOffset() ) );
00092     return Span( r.y(), r.height() );
00093 }
00094 
00095 QModelIndex TreeViewRowController::indexAt( int height ) const
00096 {
00097   /* using indexAt( QPoint ) wont work here, since it does hit detection
00098    *   against the actual item text/icon, so we would return wrong values
00099    *   for items with no text etc.
00100    *
00101    *   The code below could cache for performance, but currently it doesn't
00102    *   seem to be the performance bottleneck at all.
00103    */
00104     if ( !d->treeview->model() ) return QModelIndex();
00105     int y = d->treeview->verticalOffset();
00106     QModelIndex idx = d->treeview->model()->index( 0, 0, d->treeview->rootIndex() );
00107     do {
00108         if ( y >= height ) break;
00109 #if QT_VERSION >= 0x040300
00110         y += d->treeview->rowHeight( idx );
00111 #else
00112         // Since TreeViewRowController is NOT using uniform row height
00113         // we can use this:
00114         y += d->treeview->indexRowSizeHint( idx );
00115 #endif
00116         idx = d->treeview->indexBelow( idx );
00117     } while ( idx.isValid() );
00118     return d->proxy->mapFromSource( idx );
00119 }
00120 
00121 QModelIndex TreeViewRowController::indexAbove( const QModelIndex& _idx ) const
00122 {
00123     const QModelIndex idx = d->proxy->mapToSource( _idx );
00124     return d->proxy->mapFromSource( d->treeview->indexAbove( idx ) );
00125 }
00126 
00127 QModelIndex TreeViewRowController::indexBelow( const QModelIndex& _idx ) const
00128 {
00129     const QModelIndex idx = d->proxy->mapToSource( _idx );
00130     return d->proxy->mapFromSource( d->treeview->indexBelow( idx ) );
00131 }

Generated on Thu Mar 4 23:19:13 2010 for KD Chart 2 by  doxygen 1.5.4