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"
27 #include <QStringList>
32 inline QString
"e( QString
&string
)
34 Q_ASSERT( KPimPrefs::categorySeparator
!= "@" );
35 return string
.replace( "@", "@0" ).replace( QString("\\") +
36 KPimPrefs::categorySeparator
,
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
)
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
;
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
)
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
);
83 if ( _path
.isEmpty() ) {
84 // something is wrong, we already have this node
89 while ( split_level
< depth() ) {
92 Q_ASSERT( split_level
== depth() );
94 // make the node and any non-existent ancestors
95 while ( !_path
.isEmpty() ) {
96 addChild( _path
.first() );
102 void CategoryHierarchyReaderQComboBox::clear()
107 void CategoryHierarchyReaderQComboBox::goUp()
112 void CategoryHierarchyReaderQComboBox::addChild( const QString
&label
)
115 spaces
.fill( ' ', 2 * mCurrentDepth
);
116 mBox
->addItem( spaces
+ label
);
120 int CategoryHierarchyReaderQComboBox::depth() const
122 return mCurrentDepth
;
125 void CategoryHierarchyReaderQTreeWidget::clear()
130 void CategoryHierarchyReaderQTreeWidget::goUp()
133 mItem
= mItem
->parent();
137 void CategoryHierarchyReaderQTreeWidget::addChild( const QString
&label
)
140 mItem
= new QTreeWidgetItem( mItem
, QStringList() << label
);
142 mItem
= new QTreeWidgetItem( mTree
, QStringList() << label
);
145 mItem
->setExpanded( true );
149 int CategoryHierarchyReaderQTreeWidget::depth() const
151 return mCurrentDepth
;