Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

opimsortvector.h

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003                              Copyright (C) 2004 Holger Freyther <freyther@handhelds.org>
00004               =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
00005             .=l.
00006            .>+-=
00007  _;:,     .>    :=|.         This program is free software; you can
00008 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00009 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00010 .="- .-=="i,     .._         License as published by the Free Software
00011  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00012      ._= =}       :          or (at your option) any later version.
00013     .%`+i>       _;_.
00014     .i_,=:_.      -<s.       This program is distributed in the hope that
00015      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00016     : ..    .:,     . . .    without even the implied warranty of
00017     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00018   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00019 ..}^=.=       =       ;      Library General Public License for more
00020 ++=   -.     .`     .:       details.
00021  :     =  ...= . :.=-
00022  -.   .:....=;==+<;          You should have received a copy of the GNU
00023   -_. . .   )=.  =           Library General Public License along with
00024     --        :-=`           this library; see the file COPYING.LIB.
00025                              If not, write to the Free Software Foundation,
00026                              Inc., 59 Temple Place - Suite 330,
00027                              Boston, MA 02111-1307, USA.
00028 */
00029 
00030 #ifndef OPIE_PIM_SORT_VECTOR_H
00031 #define OPIE_PIM_SORT_VECTOR_H
00032 
00033 #include <opie2/opimglobal.h>
00034 
00035 #include <qvector.h>
00036 
00037 namespace Opie {
00038 namespace Internal {
00039 template<class T>
00040 struct OPimSortVectorContainer {
00041     T item;
00042 };
00043 
00044 template<class T>
00045 class  OPimSortVector : public QVector<OPimSortVectorContainer<T> > {
00046     typedef OPimSortVectorContainer<T> VectorItem;
00047 public:
00048     OPimSortVector( uint size, bool asc, int sort );
00049     int compareItems( QCollection::Item d1, QCollection::Item d2 );
00050     bool insert( uint, const T& t );
00051     UID uidAt( uint i )const;
00052 
00053 protected:
00054     int testString( const QString&, const QString& )const;
00055     int testDate( const QDate&, const QDate& )const;
00056     int testTime( const QTime&, const QTime& )const;
00057     int testDateTime( const QDateTime& left,
00058                       const QDateTime& right )const;
00059 protected:
00060     bool sortAscending()const;
00061     int  sortOrder()const;
00062 
00063 private:
00064     bool m_ascending : 1;
00065     int m_sort;
00066     virtual int compareItems( const T& item1, const T& item2 ) = 0;
00067 };
00068 
00069 template<class T>
00070 OPimSortVector<T>::OPimSortVector( uint size, bool asc, int sort )
00071     : QVector<VectorItem>( size ), m_ascending( asc ),
00072       m_sort( sort ) {
00073     this->setAutoDelete( true );
00074 }
00075 
00092 template<class T>
00093 int OPimSortVector<T>::compareItems( QCollection::Item d1, QCollection::Item d2 ) {
00094     return compareItems( ((VectorItem*)d1)->item,
00095                          ((VectorItem*)d2)->item );
00096 }
00097 
00098 template<class T>
00099 bool OPimSortVector<T>::sortAscending()const {
00100     return m_ascending;
00101 }
00102 
00103 template<class T>
00104 int OPimSortVector<T>::sortOrder()const {
00105     return m_sort;
00106 }
00107 
00108 template<class T>
00109 bool OPimSortVector<T>::insert( uint i, const T& record ) {
00110     VectorItem *item = new VectorItem;
00111     item->item = record;
00112     return QVector<VectorItem>::insert( i, item );
00113 }
00114 
00115 template<class T>
00116 UID OPimSortVector<T>::uidAt( uint index )const {
00117     return this->at( index )->item.uid();
00118 }
00119 
00120 template<class T>
00121 inline int OPimSortVector<T>::testString( const QString& left,
00122                                           const QString& right )const {
00123     return QString::compare( left, right );
00124 }
00125 
00126 
00127 template<class T>
00128 inline int OPimSortVector<T>::testDate( const QDate& left,
00129                                         const QDate& right )const {
00130     int ret = 0;
00131     if ( !left .isValid() ) ret++;
00132     if ( !right.isValid() ) ret--;
00133 
00134     if ( left.isValid() && right.isValid() )
00135         ret += left < right ? -1 : 1;
00136 
00137     return ret;
00138 }
00139 
00140 template<class T>
00141 inline int OPimSortVector<T>::testTime( const QTime& left,
00142                                         const QTime& right )const {
00143     int ret = 0;
00144     if ( !left .isValid() ) ret++;
00145     if ( !right.isValid() ) ret--;
00146 
00147     if ( left.isValid() && right.isValid() ){
00148             ret += left < right ? -1 : 1;    
00149     }
00150 
00151     return ret;
00152 }
00153 
00154 template<class T>
00155 inline int OPimSortVector<T>::testDateTime( const QDateTime& left,
00156                                         const QDateTime& right )const {
00157     int ret = 0;
00158     if ( !left .isValid() ) ret++;
00159     if ( !right.isValid() ) ret--;
00160 
00161     if ( left.isValid() && right.isValid() ){
00162             ret += left < right ? -1 : 1;    
00163     }
00164 
00165     return ret;
00166 
00167 }
00168 
00169 }
00170 }
00171 
00172 #endif

Generated on Sat Nov 5 16:16:23 2005 for OPIE by  doxygen 1.4.2