• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • utils
headerview.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  utils/headerview.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "headerview.h"
36 
37 #include <algorithm>
38 #include <numeric>
39 #include <cassert>
40 
41 #include <KDebug>
42 
43 //#define ENABLE_HEADERVIEW_DEBUG
44 
45 #ifdef ENABLE_HEADERVIEW_DEBUG
46 # define hvDebug kDebug
47 #else
48 # define hvDebug if ( true ) {} else kDebug
49 #endif
50 
51 using namespace Kleo;
52 
53 static std::vector<int> section_sizes( const QHeaderView * view ) {
54  assert( view );
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 ) );
59  return result;
60 }
61 
62 static void apply_section_sizes( QHeaderView * view, const std::vector<int> & newSizes ) {
63  assert( view );
64  for ( unsigned int i = 0, end = newSizes.size() ; i != end ; ++i )
65  view->resizeSection( i, newSizes[i] );
66 }
67 
68 namespace {
69 
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 ;
73  }
74 
75 }
76 
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 ) {
81  if (i)
82  debug << ", ";
83  debug << v[i];
84  }
85  debug << ")";
86  return debug.space();
87 }
88 
89 
90 class HeaderView::Private {
91  friend class ::Kleo::HeaderView;
92  HeaderView * const q;
93 public:
94  Private( HeaderView * qq )
95  : q( qq ),
96  mousePressed( false ),
97  modes(),
98  sizes()
99  {
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)) );
104  }
105 
106  void _klhv_slotSectionCountChanged( int oldCount, int newCount ) {
107  if ( newCount == oldCount )
108  return;
109  hvDebug() << oldCount << "->" << newCount;
110  if ( newCount < oldCount )
111  return;
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] );
115  apply_section_sizes( q, sizes );
116  }
117 
118  void _klhv_slotSectionResized( int idx, int oldSize, int newSize ) {
119  hvDebug() << idx << ':' << oldSize << "->" << newSize;
120  ensureNumSections( idx+1 );
121  sizes[idx] = newSize;
122  }
123 
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() );
129  }
130 
131  bool mousePressed : 1;
132  std::vector<QHeaderView::ResizeMode> modes;
133  std::vector<int> sizes;
134 };
135 
136 HeaderView::HeaderView( Qt::Orientation o, QWidget * p )
137  : QHeaderView( o, p ), d( new Private( this ) )
138 {
139 
140 }
141 
142 HeaderView::~HeaderView() {}
143 
144 #if 0
145 static std::vector<int> calculate_section_sizes( const std::vector<int> & oldSizes, int newLength, const std::vector<QHeaderView::ResizeMode> & modes, int minSize ) {
146 
147  if ( oldSizes.empty() ) {
148  hvDebug() << "no existing sizes";
149  return std::vector<int>();
150  }
151 
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;
159  } else {
160  fixedLength += oldSizes[i];
161  }
162  }
163 
164  if ( oldLength <= 0 ) {
165  hvDebug() << "no existing lengths - returning equidistant sizes";
166  return std::vector<int>( oldSizes.size(), newLength / oldSizes.size() );
167  }
168 
169  const int stretchableSpace = std::max( newLength - fixedLength, 0 );
170 
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
177  : oldSizes[i] ) );
178 
179  hvDebug() << "\noldSizes = " << oldSizes << "/" << oldLength
180  << "\nnewSizes = " << newSizes << "/" << newLength;
181 
182  return newSizes;
183 }
184 #endif
185 
186 void HeaderView::setSectionSizes( const std::vector<int> & sizes ) {
187  hvDebug() << sizes;
188  d->ensureNumSections( sizes.size() );
189  d->sizes = sizes;
190  apply_section_sizes( this, sizes );
191  hvDebug() << "->" << sectionSizes();
192 }
193 
194 std::vector<int> HeaderView::sectionSizes() const {
195  return section_sizes( this );
196 }
197 
198 void HeaderView::setSectionResizeMode( unsigned int section, ResizeMode mode ) {
199  d->ensureNumSections( section+1 );
200  d->modes[section] = mode;
201  if ( section < static_cast<unsigned int>( count() ) )
202  QHeaderView::setResizeMode( section, mode );
203 }
204 
205 #if 0
206 void HeaderView::setModel( QAbstractItemModel * model ) {
207 
208  hvDebug() << "before" << section_sizes( this );
209 
210  QHeaderView::setModel( model );
211 
212  hvDebug() << "after " << section_sizes( this );
213 
214 }
215 
216 void HeaderView::setRootIndex( const QModelIndex & idx ) {
217  hvDebug() << "before" << section_sizes( this );
218  QHeaderView::setRootIndex( idx );
219  hvDebug() << "after " << section_sizes( this );
220 }
221 
222 void HeaderView::mousePressEvent( QMouseEvent * e ) {
223  d->mousePressed = true;
224  QHeaderView::mousePressEvent( e );
225 }
226 
227 void HeaderView::mouseReleaseEvent( QMouseEvent * e ) {
228  d->mousePressed = false;
229  QHeaderView::mouseReleaseEvent( e );
230 }
231 
232 void HeaderView::updateGeometries() {
233 
234  const std::vector<int> oldSizes = d->mousePressed ? section_sizes( this ) : d->sizes ;
235 
236  hvDebug() << "before" << section_sizes( this ) << '(' << d->sizes << ')';
237 
238  QHeaderView::updateGeometries();
239 
240  hvDebug() << "after " << section_sizes( this );
241 
242  const std::vector<int> newSizes = calculate_section_sizes( oldSizes, width(), d->modes, minimumSectionSize() );
243  d->sizes = newSizes;
244 
245  apply_section_sizes( this, newSizes );
246 }
247 #endif
248 
249 #include "moc_headerview.cpp"
Kleo::HeaderView::HeaderView
HeaderView(Qt::Orientation o, QWidget *parent=0)
Definition: headerview.cpp:136
QWidget
section_sizes
static std::vector< int > section_sizes(const QHeaderView *view)
Definition: headerview.cpp:53
apply_section_sizes
static void apply_section_sizes(QHeaderView *view, const std::vector< int > &newSizes)
Definition: headerview.cpp:62
Kleo::HeaderView
Definition: headerview.h:44
Kleo::HeaderView::setSectionResizeMode
void setSectionResizeMode(unsigned int logicalIndex, ResizeMode mode)
Definition: headerview.cpp:198
Kleo::HeaderView::~HeaderView
~HeaderView()
Definition: headerview.cpp:142
d
#define d
Definition: adduseridcommand.cpp:90
QAbstractItemModel
headerview.h
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::HeaderView::setSectionSizes
void setSectionSizes(const std::vector< int > &sizes)
Definition: headerview.cpp:186
Kleo::HeaderView::sectionSizes
std::vector< int > sectionSizes() const
Definition: headerview.cpp:194
QHeaderView
hvDebug
#define hvDebug
Definition: headerview.cpp:48
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal