Sequence.cpp

Go to the documentation of this file.
00001 #include "Sequence.h"
00002 
00003 const int Sequence::MAX_ITEM = 5;
00004 const QString Sequence::itemString[] = { "a", "b", "c", "d", "e" };
00005 
00006 Sequence::Sequence( const QString& seqStr = QString::null ) : enabled( false ) {
00007     if( seqStr.isEmpty() )
00008         return;
00009 
00010     QString tempStr = seqStr;
00011     int indexOfColon = seqStr.find( ":" );
00012     if( indexOfColon != -1 ) {
00013         QString enabledStr = seqStr.mid( indexOfColon + 1 );
00014         enabled = ( enabledStr == "on" );
00015         tempStr = seqStr.mid( 0, indexOfColon );
00016     }
00017 
00018     QStringList groupStrings = QStringList::split( "-", tempStr );
00019     for( QStringList::ConstIterator it = groupStrings.begin(); it != groupStrings.end(); it++ ) {
00020         QStringList itemStrings = QStringList::split( "+", *it );
00021         ItemList itemList;
00022         for( QStringList::ConstIterator it2 = itemStrings.begin(); it2 != itemStrings.end(); it2++ ) {
00023             bool isOk;
00024             int item = (*it2).toInt( &isOk );
00025             if( isOk )
00026                 itemList.append( (Item)item );
00027         }
00028         groups.append( itemList );
00029     }
00030 }
00031 
00032 Sequence::Sequence( const Sequence& seq ) : enabled( seq.enabled ) {
00033     for( QValueList<ItemList>::ConstIterator it = seq.groups.begin(); it != seq.groups.end(); it++ ) {
00034         ItemList itemList;
00035 
00036         ItemList seqItemList = (ItemList)(*it);
00037         for( ItemList::ConstIterator it2 = seqItemList.begin(); it2 != seqItemList.end(); it2++ ) {
00038             Item seqItem = (Item)(*it2);
00039             itemList.append( seqItem ); 
00040         }
00041 
00042         groups.append( itemList );
00043     }
00044 }
00045 
00046 Sequence::~Sequence() {
00047 }
00048 
00049 bool Sequence::isEnabled() const {
00050     return( enabled );
00051 }
00052 
00053 void Sequence::setEnabled( bool isEnabled ) {
00054     enabled = isEnabled;
00055 }
00056 
00057 bool Sequence::contains( const Item& item ) const {
00058     for( QValueList<ItemList>::ConstIterator it = groups.begin(); it != groups.end(); it++ ) {
00059         ItemList itemList = (ItemList)(*it);
00060         if( itemList.contains( item ) )
00061             return( true );
00062     }
00063     return( false ); 
00064 }
00065 
00066 bool Sequence::isEmpty() const {
00067     return( groups.isEmpty() );
00068 }
00069 
00070 void Sequence::addGroup( ItemList group ) {
00071     groups.append( group );
00072 }
00073 
00074 void Sequence::removeLastGroup() {
00075     QValueList<ItemList>::Iterator it = groups.fromLast();
00076     groups.remove( it );
00077 }
00078 
00079 Sequence::ItemList Sequence::getGroupAt( int index ) const {
00080     return( groups[ index ] );
00081 }
00082 
00083 int Sequence::getGroupCount() const {
00084     return( groups.count() );
00085 }
00086 
00087 Sequence::Item Sequence::stringToItem( const QString& itemStr ) {
00088     for( int i = 0; i < Sequence::MAX_ITEM; i++ ) {
00089         if( itemStr == Sequence::itemString[ i ] ) 
00090             return( (Item)i );
00091     }
00092     return( INVALID ); 
00093 }
00094 
00095 QString Sequence::toString() const {
00096     QString str;
00097     QString groupDelimiter;
00098     for( QValueList<ItemList>::ConstIterator it = groups.begin(); it != groups.end(); it++ ) {
00099         ItemList itemList = (ItemList)(*it);
00100         qHeapSort( itemList );
00101         str += groupDelimiter;
00102         QString itemDelimiter;
00103         for( ItemList::ConstIterator it2 = itemList.begin(); it2 != itemList.end(); it2++ ) {
00104             Item item = (Item)(*it2);
00105             str += itemDelimiter + QString::number( item );
00106             itemDelimiter = "+";
00107         }
00108         groupDelimiter = "-";
00109     }
00110     str += QString( ":" ) + ( enabled ? "on" : "off" );
00111     return( str );
00112 }
00113 
00114 QString Sequence::toHumanReadableString() const {
00115     QString str;
00116     QString groupDelimiter;
00117     for( QValueList<ItemList>::ConstIterator it = groups.begin(); it != groups.end(); it++ ) {
00118         ItemList itemList = (ItemList)(*it);
00119         qHeapSort( itemList );
00120         str += groupDelimiter;
00121         QString itemDelimiter;
00122         for( ItemList::ConstIterator it2 = itemList.begin(); it2 != itemList.end(); it2++ ) {
00123             Item item = (Item)(*it2);
00124             str += itemDelimiter + itemString[ item ];
00125             itemDelimiter = "+";
00126         }
00127         groupDelimiter = " > ";
00128     }
00129     return( str );
00130 }
00131 
00132 QDataStream& operator<<( QDataStream& out, const Sequence& sequence ) {
00133     int enabledAsInt = ( sequence.enabled ? 1 : 0 );
00134     out << enabledAsInt;
00135   
00136     out << sequence.groups.count();
00137     for( QValueList<Sequence::ItemList>::ConstIterator it = sequence.groups.begin(); it != sequence.groups.end(); it++ ) {
00138         const Sequence::ItemList itemList = (const Sequence::ItemList)(*it);
00139         out << itemList.count();
00140         for( Sequence::ItemList::ConstIterator it2 = itemList.begin(); it2 != itemList.end(); it2++ ) {
00141             const Sequence::Item item = (const Sequence::Item)(*it2);
00142             out << (int)item;
00143         }
00144     }
00145 
00146     return( out );
00147 }
00148 
00149 QDataStream& operator>>( QDataStream& in, Sequence& sequence ) {
00150     int tempEnabledAsInt;
00151     QValueList<Sequence::ItemList> tempGroups;
00152 
00153     in >> tempEnabledAsInt;
00154     int tempItemListCount;
00155     in >> tempItemListCount;
00156     for( int i = 0; i < tempItemListCount; i++ ) {
00157         int tempItemCount;
00158         in >> tempItemCount;
00159         Sequence::ItemList itemList;
00160         for( int j = 0; j < tempItemCount; j++ ) {
00161             int tempItemAsInt;
00162             in >> tempItemAsInt;
00163             itemList.append( (Sequence::Item)tempItemAsInt );
00164         }
00165         tempGroups.append( itemList );
00166     }
00167 
00168     sequence = Sequence();
00169     sequence.enabled = ( tempEnabledAsInt == 1 );
00170     sequence.groups = tempGroups;
00171 
00172     return( in );
00173 }

Generated on Sun Mar 1 17:30:47 2009 for toMOTko by  doxygen 1.5.6