/*************************************************************************** * Copyright (C) 2005 by * * Jason Kivlighn (jkivlighn@gmail.com) * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #include "prepmethodcombobox.h" #include #include #include "backends/recipedb.h" #include "datablocks/elementlist.h" /** Completion object which allows completing completing items * the last item in a comma-separated list */ class PrepMethodCompletion : public TDECompletion { public: PrepMethodCompletion() : TDECompletion() {} virtual TQString makeCompletion( const TQString &string ) { kdDebug()<<"original makeCompletion( "<text(); ElementList prepMethodList; database->loadPrepMethods( &prepMethodList ); clear(); prepMethodComboRows.clear(); int row = 0; if ( !m_specialItem.isNull() ) { insertItem(m_specialItem); prepMethodComboRows.insert( row, -1 ); row++; } for ( ElementList::const_iterator it = prepMethodList.begin(); it != prepMethodList.end(); ++it, ++row ) { insertItem((*it).name); completionObject()->addItem((*it).name); prepMethodComboRows.insert( row,(*it).id ); } if ( editable() ) lineEdit()->setText( remember_text ); database->disconnect( this ); connect( database, TQ_SIGNAL( prepMethodCreated( const Element & ) ), TQ_SLOT( createPrepMethod( const Element & ) ) ); connect( database, TQ_SIGNAL( prepMethodRemoved( int ) ), TQ_SLOT( removePrepMethod( int ) ) ); } int PrepMethodComboBox::id( int row ) { return prepMethodComboRows[ row ]; } int PrepMethodComboBox::id( const TQString &ing ) { for ( int i = 0; i < count(); i++ ) { if ( ing == text( i ) ) return id(i); } kdDebug()<<"Warning: couldn't find the ID for "<text(); insertItem( element.name, row ); completionObject()->addItem(element.name); if ( editable() ) lineEdit()->setText( remember_text ); //now update the map by pushing everything after this item down TQMap new_map; for ( TQMap::iterator it = prepMethodComboRows.begin(); it != prepMethodComboRows.end(); ++it ) { if ( it.key() >= row ) { new_map.insert( it.key() + 1, it.data() ); } else new_map.insert( it.key(), it.data() ); } prepMethodComboRows = new_map; prepMethodComboRows.insert( row, element.id ); } void PrepMethodComboBox::removePrepMethod( int id ) { int row = -1; for ( TQMap::iterator it = prepMethodComboRows.begin(); it != prepMethodComboRows.end(); ++it ) { if ( it.data() == id ) { row = it.key(); completionObject()->removeItem( text(row) ); removeItem( row ); prepMethodComboRows.remove( it ); break; } } if ( row == -1 ) return ; //now update the map by pushing everything after this item up TQMap new_map; for ( TQMap::iterator it = prepMethodComboRows.begin(); it != prepMethodComboRows.end(); ++it ) { if ( it.key() > row ) { new_map.insert( it.key() - 1, it.data() ); } else new_map.insert( it.key(), it.data() ); } prepMethodComboRows = new_map; } int PrepMethodComboBox::findInsertionPoint( const TQString &name ) { for ( int i = 0; i < count(); i++ ) { if ( TQString::localeAwareCompare( name, text( i ) ) < 0 ) return i; } return count(); } void PrepMethodComboBox::setSelected( int prepID ) { //do a reverse lookup on the row->id map TQMap::const_iterator it; for ( it = prepMethodComboRows.begin(); it != prepMethodComboRows.end(); ++it ) { if ( it.data() == prepID ) { setCurrentItem(it.key()); break; } } } #include "prepmethodcombobox.moc"