33 #include <config-kleopatra.h>
45 #ifdef ENABLE_HEADERVIEW_DEBUG
46 # define hvDebug kDebug
48 # define hvDebug if ( true ) {} else kDebug
55 std::vector<int> result;
56 result.reserve( view->count() );
57 for (
int i = 0, end = view->count() ; i != end ; ++i )
58 result.push_back( view->sectionSize( i ) );
64 for (
unsigned int i = 0, end = newSizes.size() ; i != end ; ++i )
65 view->resizeSection( i, newSizes[i] );
70 template <
typename T_container>
71 inline typename T_container::value_type lookup(
const T_container & c,
unsigned int i,
const typename T_container::value_type & defaultValue ) {
72 return i < c.size() ? c[i] : defaultValue ;
77 template <
typename T,
typename A>
78 QDebug operator<<( QDebug debug, const std::vector<T,A> & v ) {
79 debug.nospace() <<
"std::vector(";
80 for (
typename std::vector<T,A>::size_type i = 0; i < v.size(); ++i ) {
90 class HeaderView::Private {
91 friend class ::Kleo::HeaderView;
96 mousePressed( false ),
100 connect(
q, SIGNAL(sectionCountChanged(
int,
int)),
101 q, SLOT(_klhv_slotSectionCountChanged(
int,
int)) );
102 connect(
q, SIGNAL(sectionResized(
int,
int,
int)),
103 q, SLOT(_klhv_slotSectionResized(
int,
int,
int)) );
106 void _klhv_slotSectionCountChanged(
int oldCount,
int newCount ) {
107 if ( newCount == oldCount )
109 hvDebug() << oldCount <<
"->" << newCount;
110 if ( newCount < oldCount )
112 ensureNumSections( newCount );
113 for (
unsigned int i = 0, end = std::min<unsigned int>( newCount, modes.size() ) ; i < end ; ++i )
114 q->QHeaderView::setResizeMode( i, modes[i] );
118 void _klhv_slotSectionResized(
int idx,
int oldSize,
int newSize ) {
119 hvDebug() << idx <<
':' << oldSize <<
"->" << newSize;
120 ensureNumSections( idx+1 );
121 sizes[idx] = newSize;
124 void ensureNumSections(
unsigned int num ) {
125 if ( num > modes.size() )
126 modes.resize( num, QHeaderView::Interactive );
127 if ( num > sizes.size() )
128 sizes.resize( num,
q->defaultSectionSize() );
131 bool mousePressed : 1;
132 std::vector<QHeaderView::ResizeMode> modes;
133 std::vector<int> sizes;
145 static std::vector<int> calculate_section_sizes(
const std::vector<int> & oldSizes,
int newLength,
const std::vector<QHeaderView::ResizeMode> & modes,
int minSize ) {
147 if ( oldSizes.empty() ) {
148 hvDebug() <<
"no existing sizes";
149 return std::vector<int>();
152 int oldLength = 0, fixedLength = 0, stretchLength = 0;
153 int numStretchSections = 0;
154 for (
unsigned int i = 0, end = oldSizes.size() ; i != end ; ++i ) {
155 oldLength += oldSizes[i];
156 if ( lookup( modes, i, QHeaderView::Fixed ) == QHeaderView::Stretch ) {
157 stretchLength += oldSizes[i];
158 ++numStretchSections;
160 fixedLength += oldSizes[i];
164 if ( oldLength <= 0 ) {
165 hvDebug() <<
"no existing lengths - returning equidistant sizes";
166 return std::vector<int>( oldSizes.size(), newLength / oldSizes.size() );
169 const int stretchableSpace = std::max( newLength - fixedLength, 0 );
171 std::vector<int> newSizes;
172 newSizes.reserve( oldSizes.size() );
173 for (
unsigned int i = 0, end = oldSizes.size() ; i != end ; ++i )
174 newSizes.push_back( std::max( minSize,
175 lookup( modes, i, QHeaderView::Fixed ) == QHeaderView::Stretch
176 ? stretchLength ? stretchableSpace * oldSizes[i] / stretchLength : stretchableSpace / numStretchSections
179 hvDebug() <<
"\noldSizes = " << oldSizes <<
"/" << oldLength
180 <<
"\nnewSizes = " << newSizes <<
"/" << newLength;
188 d->ensureNumSections( sizes.size() );
199 d->ensureNumSections( section+1 );
200 d->modes[section] = mode;
201 if ( section < static_cast<unsigned int>( count() ) )
202 QHeaderView::setResizeMode( section, mode );
210 QHeaderView::setModel( model );
216 void HeaderView::setRootIndex(
const QModelIndex & idx ) {
218 QHeaderView::setRootIndex( idx );
222 void HeaderView::mousePressEvent( QMouseEvent * e ) {
223 d->mousePressed =
true;
224 QHeaderView::mousePressEvent( e );
227 void HeaderView::mouseReleaseEvent( QMouseEvent * e ) {
228 d->mousePressed =
false;
229 QHeaderView::mouseReleaseEvent( e );
232 void HeaderView::updateGeometries() {
234 const std::vector<int> oldSizes = d->mousePressed ?
section_sizes(
this ) : d->sizes ;
238 QHeaderView::updateGeometries();
242 const std::vector<int> newSizes = calculate_section_sizes( oldSizes, width(), d->modes, minimumSectionSize() );
249 #include "moc_headerview.cpp"