krop's commit fixes my problem in a better way, reverting
[kdepim.git] / libkdepim / categoryhierarchyreader.cpp
blobc3bf8611b08aaf9d0546cc2e2db613083077f51e
1 /*
2 This file is part of libkdepim.
4 Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "categoryhierarchyreader.h"
23 #include "autochecktreewidget.h"
24 #include "kpimprefs.h"
26 #include <QComboBox>
27 #include <QStringList>
28 #include <Q3ListView>
30 using namespace KPIM;
32 inline QString &quote( QString &string )
34 Q_ASSERT( KPimPrefs::categorySeparator != "@" );
35 return string.replace( "@", "@0" ).replace( QString("\\") +
36 KPimPrefs::categorySeparator,
37 "@1" );
40 inline QStringList &unquote( QStringList &strings )
42 return strings.replaceInStrings( "@1", KPimPrefs::categorySeparator ).replaceInStrings( "@0", "@" );
45 QStringList CategoryHierarchyReader::path( QString string )
47 QStringList _path = quote( string).split( KPimPrefs::categorySeparator, QString::SkipEmptyParts );
48 return unquote( _path );
51 void CategoryHierarchyReader::read( QStringList categories )
53 clear();
54 QStringList::Iterator it;
56 // case insensitive sort
57 QMap<QString, QString> map;
58 foreach ( QString str, categories ) {
59 map.insert( str.toLower(), str );
62 categories = map.values();
64 QStringList last_path;
65 for ( it = categories.begin(); it != categories.end(); ++it ) {
66 QStringList _path = path( *it );
68 // we need to figure out where last item and the new one differ
69 QStringList::Iterator jt, kt;
70 int split_level = 0;
71 QStringList new_path = _path; // save it for later
72 for (jt = _path.begin(), kt = last_path.begin(); jt != _path.end() && kt != last_path.end(); ++jt, ++kt)
73 if (*jt == *kt) {
74 split_level++;
75 } else
76 break; // now we have first non_equal component in the iterators
78 // make a path relative to the shared ancestor
79 if ( jt != _path.begin() )
80 _path.erase( _path.begin(), jt );
81 last_path = new_path;
83 if ( _path.isEmpty() ) {
84 // something is wrong, we already have this node
85 continue;
88 // find that ancestor
89 while ( split_level < depth() ) {
90 goUp();
92 Q_ASSERT( split_level == depth() );
94 // make the node and any non-existent ancestors
95 while ( !_path.isEmpty() ) {
96 addChild( _path.first() );
97 _path.pop_front();
102 void CategoryHierarchyReaderQComboBox::clear()
104 mBox->clear();
107 void CategoryHierarchyReaderQComboBox::goUp()
109 mCurrentDepth--;
112 void CategoryHierarchyReaderQComboBox::addChild( const QString &label )
114 QString spaces;
115 spaces.fill( ' ', 2 * mCurrentDepth );
116 mBox->addItem( spaces + label );
117 mCurrentDepth++;
120 int CategoryHierarchyReaderQComboBox::depth() const
122 return mCurrentDepth;
125 void CategoryHierarchyReaderQTreeWidget::clear()
127 mTree->clear();
130 void CategoryHierarchyReaderQTreeWidget::goUp()
132 Q_ASSERT( mItem );
133 mItem = mItem->parent();
134 --mCurrentDepth;
137 void CategoryHierarchyReaderQTreeWidget::addChild( const QString &label )
139 if ( mItem ) {
140 mItem = new QTreeWidgetItem( mItem, QStringList() << label );
141 } else {
142 mItem = new QTreeWidgetItem( mTree, QStringList() << label );
145 mItem->setExpanded( true );
146 ++mCurrentDepth;
149 int CategoryHierarchyReaderQTreeWidget::depth() const
151 return mCurrentDepth;