kmail

simplefoldertree.h
1 /*
2  Copyright (c) 2007 Volker Krause <vkrause@kde.org>
3  Copyright (c) 2003 Andreas Gungl <a.gungl@gmx.de>
4  Copyright (c) Stefan Taferner <taferner@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef KMAIL_SIMPLEFOLDERTREE_H
22 #define KMAIL_SIMPLEFOLDERTREE_H
23 
24 #include "kmfolder.h"
25 #include "kmfoldertree.h"
26 #include "treebase.h"
27 
28 #include <kdebug.h>
29 #include <tdelistview.h>
30 #include <tdepopupmenu.h>
31 #include <kiconloader.h>
32 
33 class KMFolder;
34 class KMFolderTree;
35 
36 namespace KMail {
37 
38 static int recurseFilter( TQListViewItem * item, const TQString& filter, int column )
39 {
40  if ( item == 0 )
41  return 0;
42 
43  TQListViewItem * child;
44  child = item->firstChild();
45 
46  int enabled = 0;
47  while ( child ) {
48  enabled += recurseFilter( child, filter, column );
49  child = child->nextSibling();
50  }
51 
52  if ( filter.length() == 0 ||
53  item->text( column ).find( filter, 0, false ) >= 0 ) {
54  item->setVisible( true );
55  ++enabled;
56  }
57  else {
58  item->setVisible( !!enabled );
59  item->setEnabled( false );
60  }
61 
62  return enabled;
63 }
64 
65 class TreeItemBase
66 {
67  public :
68  TreeItemBase()
69  : mFolder( 0 )
70  {
71  kdDebug(5006) << k_funcinfo << endl;
72  }
73  virtual ~TreeItemBase() { }
74 
75  void setFolder( KMFolder * folder ) { mFolder = folder; };
76  const KMFolder * folder() { return mFolder; };
77 
78  // Set the flag which determines if this is an alternate row
79  void setAlternate ( bool alternate ) {
80  mAlternate = alternate;
81  }
82 
83  private:
84  KMFolder * mFolder;
85  bool mAlternate;
86 
87 };
88 
89 template <class T> class SimpleFolderTreeItem : public T, public TreeItemBase
90 {
91  public:
92  SimpleFolderTreeItem( TQListView * listView ) :
93  T( listView ), TreeItemBase()
94  {
95  kdDebug(5006) << k_funcinfo << endl;
96  }
97  SimpleFolderTreeItem( TQListView * listView, TQListViewItem * afterListViewItem ) :
98  T( listView, afterListViewItem ) , TreeItemBase()
99  {
100  kdDebug(5006) << k_funcinfo << endl;
101  }
102  SimpleFolderTreeItem( TQListViewItem * listViewItem ) : T( listViewItem ) , TreeItemBase()
103  {
104  kdDebug(5006) << k_funcinfo << endl;
105  }
106 
107  SimpleFolderTreeItem( TQListViewItem * listViewItem, TQListViewItem * afterListViewItem ) :
108  T( listViewItem, afterListViewItem ) , TreeItemBase()
109  {
110  kdDebug(5006) << k_funcinfo << endl;
111  }
112 
113 };
114 
115 template <> class SimpleFolderTreeItem<TQCheckListItem> : public TQCheckListItem, public TreeItemBase
116 {
117  public:
118  SimpleFolderTreeItem( TQListView * listView ) :
119  TQCheckListItem( listView, TQString(), CheckBox ), TreeItemBase() {}
120  SimpleFolderTreeItem( TQListView * listView, TQListViewItem * afterListViewItem ) :
121  TQCheckListItem( listView, afterListViewItem, TQString(), CheckBox ), TreeItemBase() {}
122  SimpleFolderTreeItem( TQListViewItem * listViewItem ) :
123  TQCheckListItem( listViewItem, TQString(), CheckBox ) {}
124  SimpleFolderTreeItem( TQListViewItem * listViewItem, TQListViewItem * afterListViewItem ) :
125  TQCheckListItem( listViewItem, afterListViewItem, TQString(), CheckBox ) {}
126 
127 };
128 
129 
130 template <class T> class SimpleFolderTreeBase : public TreeBase
131 {
132 
133  public:
134 
135 
136  inline SimpleFolderTreeBase( TQWidget * parent, KMFolderTree *folderTree,
137  const TQString &preSelection, bool mustBeReadWrite )
138  : TreeBase( parent, folderTree, preSelection, mustBeReadWrite )
139  {
140  assert( folderTree );
141  setFolderColumn( addColumn( i18n( "Folder" ) ) );
142  setPathColumn( addColumn( i18n( "Path" ) ) );
143 
144  setRootIsDecorated( true );
145  setSorting( -1 );
146 
147  reload( mustBeReadWrite, true, true, preSelection );
148 
149  }
150 
151  virtual SimpleFolderTreeItem<T>* createItem( TQListView * parent )
152  {
153  return new SimpleFolderTreeItem<T>( parent );
154  }
155 
156  virtual SimpleFolderTreeItem<T>* createItem( TQListView * parent, TQListViewItem* afterListViewItem )
157  {
158  return new SimpleFolderTreeItem<T>( parent, afterListViewItem );
159  }
160 
161  virtual SimpleFolderTreeItem<T>* createItem( TQListViewItem * parent, TQListViewItem* afterListViewItem )
162  {
163  return new SimpleFolderTreeItem<T>( parent, afterListViewItem );
164  }
165 
166  virtual SimpleFolderTreeItem<T>* createItem( TQListViewItem * parent )
167  {
168  return new SimpleFolderTreeItem<T>( parent );
169  }
170 
171  inline void keyPressEvent( TQKeyEvent *e )
172  {
173  const char ascii = e->ascii();
174  if ( ascii == 8 || ascii == 127 ) {
175  if ( mFilter.length() > 0 ) {
176  mFilter.truncate( mFilter.length()-1 );
177  applyFilter( mFilter );
178  }
179  } else if ( !e->text().isEmpty() && e->text().length() == 1 && e->text().at( 0 ).isPrint() ) {
180  applyFilter( mFilter + e->text() );
181  } else {
182  TDEListView::keyPressEvent( e );
183  }
184  }
185 
186  void applyFilter( const TQString& filter )
187  {
188  kdDebug(5006) << k_funcinfo << filter << endl ;
189  // Reset all items to visible, enabled, and open
190  TQListViewItemIterator clean( this );
191  while ( clean.current() ) {
192  TQListViewItem * item = clean.current();
193  item->setEnabled( true );
194  item->setVisible( true );
195  item->setOpen( true );
196  ++clean;
197  }
198 
199  mFilter = filter;
200 
201  if ( filter.isEmpty() ) {
202  setColumnText( pathColumn(), i18n("Path") );
203  return;
204  }
205 
206  // Set the visibility and enabled status of each list item.
207  // The recursive algorithm is necessary because visiblity
208  // changes are automatically applied to child nodes by TQt.
209  TQListViewItemIterator it( this );
210  while ( it.current() ) {
211  TQListViewItem * item = it.current();
212  if ( item->depth() <= 0 )
213  recurseFilter( item, filter, pathColumn() );
214  ++it;
215  }
216 
217  // Recolor the rows appropriately
218  recolorRows();
219 
220  // Iterate through the list to find the first selectable item
221  TQListViewItemIterator first ( this );
222  while ( first.current() ) {
223  SimpleFolderTreeItem<T> * item = static_cast< SimpleFolderTreeItem<T> * >( first.current() );
224 
225  if ( item->isVisible() && item->isSelectable() ) {
226  setSelected( item, true );
227  ensureItemVisible( item );
228  break;
229  }
230 
231  ++first;
232  }
233 
234  // Display and save the current filter
235  if ( filter.length() > 0 )
236  setColumnText( pathColumn(), i18n("Path") + " ( " + filter + " )" );
237  else
238  setColumnText( pathColumn(), i18n("Path") );
239 
240  mFilter = filter;
241  }
242 
243 };
244 
245 typedef SimpleFolderTreeBase<TDEListViewItem> SimpleFolderTree;
246 
247 }
248 
249 #endif
Mail folder.
Definition: kmfolder.h:69
folderdiaquotatab.h
Definition: aboutdata.cpp:40