french -> French
[kdepim.git] / calendarsupport / categoryhierarchyreader.cpp
blobdf55d451c18b37fd47b816803a2613bf6ac09718
1 /*
2 Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "categoryhierarchyreader.h"
21 #include "categoryconfig.h"
22 #include <KComboBox>
23 #include <QTreeWidget>
25 using namespace CalendarSupport;
27 inline QString &quote( QString &string )
29 Q_ASSERT( CategoryConfig::categorySeparator != QLatin1String("@") );
30 return string.replace( QLatin1Char('@'), QLatin1String("@0") ).replace( QLatin1Char( '\\' ) +
31 CategoryConfig::categorySeparator,
32 QLatin1String("@1") );
35 inline QStringList &unquote( QStringList &strings )
37 return
38 strings.replaceInStrings( QLatin1String("@1"), CategoryConfig::categorySeparator ).
39 replaceInStrings( QLatin1String("@0"), QLatin1String("@") );
42 QStringList CategoryHierarchyReader::path( QString string )
44 QStringList _path =
45 quote( string ).split( CategoryConfig::categorySeparator, QString::SkipEmptyParts );
46 return unquote( _path );
49 void CategoryHierarchyReader::read( QStringList categories )
51 clear();
52 QStringList::Iterator it;
54 // case insensitive sort
55 QMap<QString, QString> map;
56 foreach ( const QString &str, categories ) {
57 map.insert( str.toLower(), str );
60 categories = map.values();
62 QStringList last_path;
63 for ( it = categories.begin(); it != categories.end(); ++it ) {
64 QStringList _path = path( *it );
66 // we need to figure out where last item and the new one differ
67 QStringList::Iterator jt, kt;
68 int split_level = 0;
69 QStringList new_path = _path; // save it for later
70 for ( jt = _path.begin(), kt = last_path.begin();
71 jt != _path.end() && kt != last_path.end(); ++jt, ++kt ) {
72 if ( *jt == *kt ) {
73 split_level++;
74 } else {
75 break; // now we have first non_equal component in the iterators
79 // make a path relative to the shared ancestor
80 if ( jt != _path.begin() ) {
81 _path.erase( _path.begin(), jt );
83 last_path = new_path;
85 if ( _path.isEmpty() ) {
86 // something is wrong, we already have this node
87 continue;
90 // find that ancestor
91 while ( split_level < depth() ) {
92 goUp();
94 Q_ASSERT( split_level == depth() );
96 // make the node and any non-existent ancestors
97 while ( !_path.isEmpty() ) {
98 addChild( _path.first(), QVariant( *it ) );
99 _path.pop_front();
104 void CategoryHierarchyReaderQComboBox::clear()
106 mBox->clear();
109 void CategoryHierarchyReaderQComboBox::goUp()
111 mCurrentDepth--;
114 void CategoryHierarchyReaderQComboBox::addChild( const QString &label, const QVariant &userData )
116 QString spaces;
117 spaces.fill( QLatin1Char(' '), 2 * mCurrentDepth );
118 mBox->addItem( spaces + label, userData );
119 mCurrentDepth++;
122 int CategoryHierarchyReaderQComboBox::depth() const
124 return mCurrentDepth;
127 #ifndef QT_NO_TREEWIDGET
129 void CategoryHierarchyReaderQTreeWidget::clear()
131 mTree->clear();
134 void CategoryHierarchyReaderQTreeWidget::goUp()
136 Q_ASSERT( mItem );
137 mItem = mItem->parent();
138 --mCurrentDepth;
141 void CategoryHierarchyReaderQTreeWidget::addChild( const QString &label, const QVariant &userData )
143 Q_UNUSED( userData );
145 if ( mItem ) {
146 mItem = new QTreeWidgetItem( mItem, QStringList() << label );
147 } else {
148 mItem = new QTreeWidgetItem( mTree, QStringList() << label );
151 mItem->setExpanded( true );
152 ++mCurrentDepth;
155 int CategoryHierarchyReaderQTreeWidget::depth() const
157 return mCurrentDepth;
159 #endif