Logo Search packages:      
Sourcecode: qasconfig version File versions  Download package

tree_view_kv.cpp

//
// C++ Implementation:
//
// Description:
//
//
// Author: Sebastian Holtermann <sebholt@xwmw.org>, (C) 2011
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "tree_view_kv.hpp"

#include <QScrollBar>
#include <QHeaderView>
#include <QResizeEvent>
#include <iostream>


Tree_View_KV::Tree_View_KV (
      QWidget * parent ) :
QTreeView ( parent ),
_activate_current ( true )
{
      {
            const QFontMetrics & fmet ( fontMetrics() );
            _min_sec_width = 3*indentation() + fmet.averageCharWidth() * 8;
            _default_sec_width = 3*indentation() + fmet.averageCharWidth() * 16;
      }

      header()->setResizeMode ( QHeaderView::Interactive );
      header()->setMinimumSectionSize ( _min_sec_width );
}


void
Tree_View_KV::setModel (
      QAbstractItemModel * model_n )
{
      QTreeView::setModel ( model_n );
      adjust_first_column_width();
}


void
Tree_View_KV::set_activate_current (
      bool flag_n )
{
      _activate_current = flag_n;
}


void
Tree_View_KV::set_expanded_recursive (
      const QModelIndex & index_n,
      int depth_n,
      bool expanded_n )
{
      setExpanded ( index_n, expanded_n );

      if ( depth_n > 0 ) {
            const int rows ( model()->rowCount ( index_n ) );
            for ( int ii=0; ii < rows; ++ii ) {
                  const QModelIndex mi ( model()->index ( ii, 0, index_n ) );
                  set_expanded_recursive ( mi, depth_n - 1, expanded_n );
            }
      } else {
            collapse ( index_n );
      }
}


void
Tree_View_KV::expand_recursive (
      const QModelIndex & index_n,
      int depth_n )
{
      set_expanded_recursive ( index_n, depth_n, true );
}


void
Tree_View_KV::collapse_recursive (
      const QModelIndex & index_n,
      int depth_n )
{
      set_expanded_recursive ( index_n, depth_n, false );
}


void
Tree_View_KV::adjust_first_column_width ( )
{
      int hspace_free ( contentsRect().width() );
      if ( verticalScrollBar() != 0 ) {
            if ( verticalScrollBar()->isVisible() ) {
                  hspace_free -= verticalScrollBar()->width();
            }
      }
      QFontMetrics fm ( fontMetrics() );

      if ( (int)_default_sec_width < hspace_free ) {
            header()->resizeSection ( 0, _default_sec_width );
      } else if ( (int)_min_sec_width < hspace_free ) {
            header()->resizeSection ( 0, _min_sec_width );
      }
}


void
Tree_View_KV::currentChanged (
      const QModelIndex & current,
      const QModelIndex & previous )
{
      QTreeView::currentChanged ( current, previous );
      if ( _activate_current ) {
            emit activated ( current );
      }
}


void
Tree_View_KV::resizeEvent (
      QResizeEvent * event_n )
{
      QTreeView::resizeEvent ( event_n );
      adjust_first_column_width();
}

Generated by  Doxygen 1.6.0   Back to index