french -> French
[kdepim.git] / knode / treewidget.h
blob2880b3dbfb5ea8241fa736c14733224766ea5ee6
1 #ifndef KDEPIM_TREEWIDGET_H
2 #define KDEPIM_TREEWIDGET_H
4 /******************************************************************************
6 * This file is part of libkdepim.
8 * Copyright (C) 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public License
21 * along with this library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
25 *****************************************************************************/
27 /**
28 * @file treewidget.h A common QTreeWidget extension
31 #include "knode_export.h"
32 #include <QTreeWidget>
34 class KMenu;
35 class KConfig;
36 class KConfigGroup;
38 class QAction;
40 namespace KPIM
43 /**
44 * @brief A QTreeWidget with expanded capabilities
46 * This class extends the functionality provided by the standard QTreeWidget
47 * by adding toggleable columns and a layout save/restore facility.
49 * At the time of writing the usage of QTreeWidget is a bit against the
50 * reccomended Qt4 approach. On the other hand in many cases you just want
51 * a simple tree of items and don't want to care about anything complex like
52 * a "data model".
54 * @author Szymon Tomasz Stefanek <pragma@kvirc.net>
56 class KNODE_EXPORT TreeWidget : public QTreeWidget
58 Q_OBJECT
59 public:
60 /**
61 * Constructs a TreeWidget. Sets up the default values
62 * for properties.
64 explicit TreeWidget( QWidget *parent, const char *name = 0 );
66 public:
69 // Layout Save/Restore facility
72 /**
73 * Stores the layout of this tree view to the specified KConfigGroup.
74 * The stored data includes visible columns, column width and order.
76 * @param group The KConfigGroup to write the layout data to.
77 * @param keyName The key to use, "TreeWidgetLayout" by default
79 * @returns true on success and false on failure (if keyName is 0, for example).
81 bool saveLayout(
82 KConfigGroup &group,
83 const QString &keyName = QString()
84 ) const;
86 /**
87 * Stores the layout of this tree view to the specified key of
88 * the specified config group.
89 * The stored data includes visible columns, column width and order.
91 * @param configGroup The KConfig group to write the layout data to.
92 * @param keyName The key to use, "TreeWidgetLayout" by default
94 * @returns true on success and false on failure (if you can't write
95 * to the configGroup).
97 bool saveLayout(
98 KConfig *config,
99 const QString &groupName,
100 const QString &keyName = QString()
101 ) const;
104 * Attempts to restore the layout of this tree from the specified
105 * key of the specified KConfigGroup. The layout must have been
106 * previously saved by a call to saveLayout().
108 * Please note that you must call this function after having added
109 * all the relevant columns to the view. On the other hand if you're
110 * setting some of the view properties programmatically (like default
111 * section size) then you should set them after restoring the layout
112 * because otherwise they might get overwritten by the stored data.
113 * The proper order is: add columns, restore layout, set header properties.
115 * @param group The KConfigGroup to read the layout data from.
116 * @param keyName The key to use, "TreeWidgetLayout" by default
118 * @returns true if the layout data has been successfully read and
119 * restored. Returns false if the specified key of the config
120 * group is empty, or the data contained inside is not valid.
122 bool restoreLayout(
123 KConfigGroup &group,
124 const QString &keyName = QString()
128 * Attempts to restore the layout of this tree from the specified
129 * key of the specified group. The layout must have been
130 * previously saved by a call to saveLayout().
132 * Please note that you must call this function after having added
133 * all the relevant columns to the view. On the other hand if you're
134 * setting some of the view properties programmatically (like default
135 * section size) then you should set them after restoring the layout
136 * because otherwise they might get overwritten by the stored data.
137 * The proper order is: add columns, restore layout, set header properties.
139 * @param configGroup The name of the KConfig group to read the layout data from.
140 * @param keyName The key to use, "TreeWidgetLayout" by default
142 * @returns true if the layout data has been successfully read and
143 * restored. Returns false if the specified key of the config
144 * group is empty, or the data contained inside is not valid.
146 bool restoreLayout(
147 KConfig *config,
148 const QString &groupName,
149 const QString &keyName = QString()
153 // Manual and programmatic column hiding
157 * Enables or disables manual column hiding by the means of a contextual menu.
158 * All columns but the first can be hidden by the user by toggling the
159 * check on the popup menu that is displayed by right clicking the header
160 * of this view.
162 * By default column hiding is enabled.
164 void setManualColumnHidingEnabled( bool enable );
167 * @returns true if manual column hiding is currently enabled, false otherwise.
169 bool manualColumnHidingEnabled() const
170 { return mEnableManualColumnHiding; };
173 * Hides or shows the column specified by logicalIndex.
174 * The column is hidden if hide is true and shown otherwise.
176 void setColumnHidden( int logicalIndex, bool hide );
179 * Returns true if the column specified by logicalIndex is actually hidden.
181 bool isColumnHidden( int logicalIndex ) const;
184 * Toggle the visibility state of the specified column.
186 void toggleColumn( int logicalIndex );
189 * Called by this class to fill the contextual menu shown when the
190 * user right clicks on one of the header columns and column hiding
191 * is enabled.
193 * If you override this, remember to call the base class implementation
194 * if you want to keep column hiding working.
196 * You should return true if you want the menu to be shown or false
197 * if you don't want it to be shown. The default implementation returns
198 * true unless you have no columns at all, column hiding is disabled
199 * or you pass some weird parameter.
201 virtual bool fillHeaderContextMenu( KMenu *menu, const QPoint &clickPoint );
204 // Facilities for adding and manipulating columns
208 * Convenience function that adds a column to this tree widget
209 * and returns its logical index. Please note that in fact
210 * this function manipulates the header item which in turn
211 * plays with the underlying data model.
213 * The alignment flags refer to the header text only. For individual
214 * items you must set the alignment manually.
216 * @param label The label for the column
217 * @param alignment The alignment of the header text for this column
219 int addColumn(
220 const QString &label,
221 int headerAlignment = Qt::AlignLeft | Qt::AlignVCenter
225 * Convenience function that changes the header text for the specified column.
226 * Returns true if the text can be successfully changed or false
227 * if the specified column index is out of range.
229 bool setColumnText( int columnIndex, const QString &label );
232 * Convenience function that returns the last item in the view.
234 QTreeWidgetItem *lastItem() const;
237 * Convenience function that returns the first item in the view.
238 * This, in fact, is roughly equivalent to topLevelItem( 0 ).
239 * It is included for symmetry with lastItem().
241 QTreeWidgetItem *firstItem() const;
243 Q_SIGNALS:
245 * This signal is emitted when an existing column is shown or hidden
246 * either by the means of the popup menu or by a call to setColumnHidden().
247 * See also setManualColumnHidingEnabled().
249 void columnVisibilityChanged( int logicalIndex );
251 protected:
253 * Reimplemented in order to catch style change events
254 * and explicitly disable animations.
256 virtual void changeEvent( QEvent *e );
258 private Q_SLOTS:
260 * Internal slot connected to the customContextMenuRequested()
261 * signal of the header().
263 void slotHeaderContextMenuRequested( const QPoint &clickPoint );
266 * Internal slot connected to the show/hide actions in the
267 * header contextual menu.
269 void slotToggleColumnActionTriggered( bool );
271 private:
272 bool mEnableManualColumnHiding; ///< Is manual column hiding currently enabled ?
276 } // namespace KPIM
278 #endif //!__TREEWIDGET_H__