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"
23 #include <QTreeWidget>
25 using namespace CalendarSupport
;
27 inline QString
"e( 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
)
38 strings
.replaceInStrings( QLatin1String("@1"), CategoryConfig::categorySeparator
).
39 replaceInStrings( QLatin1String("@0"), QLatin1String("@") );
42 QStringList
CategoryHierarchyReader::path( QString string
)
45 quote( string
).split( CategoryConfig::categorySeparator
, QString::SkipEmptyParts
);
46 return unquote( _path
);
49 void CategoryHierarchyReader::read( QStringList categories
)
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
;
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
) {
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
);
85 if ( _path
.isEmpty() ) {
86 // something is wrong, we already have this node
91 while ( split_level
< depth() ) {
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
) );
104 void CategoryHierarchyReaderQComboBox::clear()
109 void CategoryHierarchyReaderQComboBox::goUp()
114 void CategoryHierarchyReaderQComboBox::addChild( const QString
&label
, const QVariant
&userData
)
117 spaces
.fill( QLatin1Char(' '), 2 * mCurrentDepth
);
118 mBox
->addItem( spaces
+ label
, userData
);
122 int CategoryHierarchyReaderQComboBox::depth() const
124 return mCurrentDepth
;
127 #ifndef QT_NO_TREEWIDGET
129 void CategoryHierarchyReaderQTreeWidget::clear()
134 void CategoryHierarchyReaderQTreeWidget::goUp()
137 mItem
= mItem
->parent();
141 void CategoryHierarchyReaderQTreeWidget::addChild( const QString
&label
, const QVariant
&userData
)
143 Q_UNUSED( userData
);
146 mItem
= new QTreeWidgetItem( mItem
, QStringList() << label
);
148 mItem
= new QTreeWidgetItem( mTree
, QStringList() << label
);
151 mItem
->setExpanded( true );
155 int CategoryHierarchyReaderQTreeWidget::depth() const
157 return mCurrentDepth
;