diff --git a/utils/headerview.cpp b/utils/headerview.cpp index 1d10303d6..8246da6c9 100644 --- a/utils/headerview.cpp +++ b/utils/headerview.cpp @@ -1,219 +1,236 @@ /* -*- mode: c++; c-basic-offset:4 -*- utils/headerview.cpp This file is part of Kleopatra, the KDE keymanager Copyright (c) 2008 Klarälvdalens Datakonsult AB Kleopatra is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Kleopatra is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include #include "headerview.h" #include #include #include #include //#define ENABLE_HEADERVIEW_DEBUG #ifdef ENABLE_HEADERVIEW_DEBUG # define hvDebug kDebug #else # define hvDebug if ( true ) {} else kDebug #endif using namespace Kleo; class HeaderView::Private { + friend class ::Kleo::HeaderView; + HeaderView * const q; public: - Private() - : mousePressed( false ), + Private( HeaderView * qq ) + : q( qq ), + mousePressed( false ), modes(), sizes() { + connect( q, SIGNAL(sectionCountChanged(int,int)), + q, SLOT(_klhv_slotSectionCountChanged(int,int)) ); + } + void _klhv_slotSectionCountChanged( int oldCount, int newCount ) { + kDebug() << oldCount << "->" << newCount; + if ( oldCount > newCount ) + return; + ensureNumSections( newCount ); + for ( unsigned int i = 0, end = std::min( newCount, modes.size() ) ; i < end ; ++i ) + q->QHeaderView::setResizeMode( i, modes[i] ); } void ensureNumSections( unsigned int num ) { if ( num > modes.size() ) modes.resize( num, QHeaderView::Fixed ); } bool mousePressed : 1; std::vector modes; std::vector sizes; }; HeaderView::HeaderView( Qt::Orientation o, QWidget * p ) - : QHeaderView( o, p ), d( new Private ) + : QHeaderView( o, p ), d( new Private( this ) ) { } HeaderView::~HeaderView() {} static std::vector section_sizes( const QHeaderView * view ) { assert( view ); std::vector result; result.reserve( view->count() ); for ( int i = 0, end = view->count() ; i != end ; ++i ) result.push_back( view->sectionSize( i ) ); return result; } static void apply_section_sizes( QHeaderView * view, const std::vector & newSizes ) { assert( view ); for ( unsigned int i = 0, end = newSizes.size() ; i != end ; ++i ) view->resizeSection( i, newSizes[i] ); } namespace { template inline typename T_container::value_type lookup( const T_container & c, unsigned int i, const typename T_container::value_type & defaultValue ) { return i < c.size() ? c[i] : defaultValue ; } } template QDebug operator<<( QDebug debug, const std::vector & v ) { debug.nospace() << "std::vector("; for ( typename std::vector::size_type i = 0; i < v.size(); ++i ) { if (i) debug << ", "; debug << v[i]; } debug << ")"; return debug.space(); } - +#if 0 static std::vector calculate_section_sizes( const std::vector & oldSizes, int newLength, const std::vector & modes, int minSize ) { if ( oldSizes.empty() ) { hvDebug() << "no existing sizes"; return std::vector(); } int oldLength = 0, fixedLength = 0, stretchLength = 0; int numStretchSections = 0; for ( unsigned int i = 0, end = oldSizes.size() ; i != end ; ++i ) { oldLength += oldSizes[i]; if ( lookup( modes, i, QHeaderView::Fixed ) == QHeaderView::Stretch ) { stretchLength += oldSizes[i]; ++numStretchSections; } else { fixedLength += oldSizes[i]; } } if ( oldLength <= 0 ) { hvDebug() << "no existing lengths - returning equidistant sizes"; return std::vector( oldSizes.size(), newLength / oldSizes.size() ); } const int stretchableSpace = std::max( newLength - fixedLength, 0 ); std::vector newSizes; newSizes.reserve( oldSizes.size() ); for ( unsigned int i = 0, end = oldSizes.size() ; i != end ; ++i ) newSizes.push_back( std::max( minSize, lookup( modes, i, QHeaderView::Fixed ) == QHeaderView::Stretch ? stretchLength ? stretchableSpace * oldSizes[i] / stretchLength : stretchableSpace / numStretchSections : oldSizes[i] ) ); hvDebug() << "\noldSizes = " << oldSizes << "/" << oldLength << "\nnewSizes = " << newSizes << "/" << newLength; return newSizes; } - +#endif void HeaderView::setSectionSizes( const std::vector & sizes ) { hvDebug() << sizes; d->ensureNumSections( sizes.size() ); d->sizes = sizes; apply_section_sizes( this, sizes ); hvDebug() << "->" << sectionSizes(); } std::vector HeaderView::sectionSizes() const { return section_sizes( this ); } void HeaderView::setSectionResizeMode( unsigned int section, ResizeMode mode ) { d->ensureNumSections( section+1 ); d->modes[section] = mode; + if ( section < static_cast( count() ) ) + QHeaderView::setResizeMode( section, mode ); } +#if 0 void HeaderView::setModel( QAbstractItemModel * model ) { hvDebug() << "before" << section_sizes( this ); QHeaderView::setModel( model ); hvDebug() << "after " << section_sizes( this ); } void HeaderView::setRootIndex( const QModelIndex & idx ) { hvDebug() << "before" << section_sizes( this ); QHeaderView::setRootIndex( idx ); hvDebug() << "after " << section_sizes( this ); } void HeaderView::mousePressEvent( QMouseEvent * e ) { d->mousePressed = true; QHeaderView::mousePressEvent( e ); } void HeaderView::mouseReleaseEvent( QMouseEvent * e ) { d->mousePressed = false; QHeaderView::mouseReleaseEvent( e ); } void HeaderView::updateGeometries() { const std::vector oldSizes = d->mousePressed ? section_sizes( this ) : d->sizes ; hvDebug() << "before" << section_sizes( this ) << '(' << d->sizes << ')'; QHeaderView::updateGeometries(); hvDebug() << "after " << section_sizes( this ); const std::vector newSizes = calculate_section_sizes( oldSizes, width(), d->modes, minimumSectionSize() ); d->sizes = newSizes; apply_section_sizes( this, newSizes ); } +#endif #include "moc_headerview.cpp" diff --git a/utils/headerview.h b/utils/headerview.h index ed58156da..35a69b595 100644 --- a/utils/headerview.h +++ b/utils/headerview.h @@ -1,78 +1,79 @@ /* -*- mode: c++; c-basic-offset:4 -*- utils/headerview.h This file is part of Kleopatra, the KDE keymanager Copyright (c) 2008 Klarälvdalens Datakonsult AB Kleopatra is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Kleopatra is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #ifndef __KLEOPATRA_UTILS_HEADERVIEW_H__ #define __KLEOPATRA_UTILS_HEADERVIEW_H__ #include #include #include namespace Kleo { class HeaderView : public QHeaderView { Q_OBJECT public: explicit HeaderView( Qt::Orientation o, QWidget * parent=0 ); ~HeaderView(); void setSectionSizes( const std::vector & sizes ); std::vector sectionSizes() const; void setSectionResizeMode( unsigned int logicalIndex, ResizeMode mode ); ResizeMode sectionResizeMode( unsigned int logicalIndex ) const; - +#if 0 /* reimp */ void setModel( QAbstractItemModel * model ); /* reimp */ void setRootIndex( const QModelIndex & idx ); - +#endif private: //@{ /*! Defined, but not implemented, to catch at least some usage errors */ void setResizeMode( int, ResizeMode ); ResizeMode resizeMode() const; //@} - +#if 0 protected: /* reimp */ void updateGeometries(); /* reimp */ void mousePressEvent( QMouseEvent * e ); /* reimp */ void mouseReleaseEvent( QMouseEvent * e ); - +#endif private: class Private; kdtools::pimpl_ptr d; + Q_PRIVATE_SLOT( d, void _klhv_slotSectionCountChanged(int,int) ) }; } #endif /* __KLEOPATRA_UTILS_HEADERVIEW_H__ */