You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tdepim/korn/keditlistboxman.cpp

302 lines
8.5 KiB

/*
* Copyright (C) 2004, Mart Kelder (mart.kde@hccnet.nl)
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "keditlistboxman.h"
#include <kconfig.h>
#include <kdebug.h>
#include <tqmap.h>
#include <tqstring.h>
KEditListBoxManager::KEditListBoxManager( TQWidget *tqparent, const char *name,
bool checkAtEntering, int buttons )
: KEditListBox( tqparent, name, checkAtEntering, buttons ),
_config( 0 ),
_groupName( 0 ),
_subGroupName( 0 ),
_prevCount( 0 )
{
init();
}
KEditListBoxManager::KEditListBoxManager( const TQString& title, TQWidget *tqparent,
const char *name, bool checkAtEntering,
int buttons)
: KEditListBox( title, tqparent, name, checkAtEntering, buttons ),
_config( 0 ),
_groupName( 0 ),
_subGroupName( 0 ),
_prevCount( 0 )
{
init();
}
KEditListBoxManager::KEditListBoxManager( const TQString& title,
const KEditListBox::CustomEditor &customEditor,
TQWidget *tqparent, const char *name,
bool checkAtEntering, int buttons )
: KEditListBox( title, customEditor, tqparent, name, checkAtEntering, buttons ),
_config( 0 ),
_groupName( 0 ),
_subGroupName( 0 ),
_prevCount( 0 )
{
init();
}
KEditListBoxManager::~KEditListBoxManager()
{
delete _groupName;
}
void KEditListBoxManager::setConfig( KConfig* config )
{
_config = config;
if( _groupName )
readNames();
}
void KEditListBoxManager::setGroupName( const TQString& name )
{
if( _groupName )
*_groupName = name;
else
_groupName = new TQString( name );
if( _config )
readNames();
}
void KEditListBoxManager::setSubGroupName( const TQString& name )
{
if( _subGroupName )
*_subGroupName = name;
else
_subGroupName = new TQString( name );
if( _config && _groupName )
readNames();
}
void KEditListBoxManager::init()
{
connect( this, TQT_SIGNAL( changed() ), this, TQT_SLOT( slotChanged() ) );
connect( this, TQT_SIGNAL( added( const TQString& ) ), this, TQT_SLOT( slotAdded( const TQString& ) ) );
connect( this, TQT_SIGNAL( removed( const TQString& ) ), this, TQT_SLOT( slotRemoved( const TQString& ) ) );
connect( this->listBox(), TQT_SIGNAL( doubleClicked( TQListBoxItem * ) ), this, TQT_SLOT( slotActivated( TQListBoxItem * ) ) );
connect( this->listBox(), TQT_SIGNAL( returnPressed( TQListBoxItem * ) ), this, TQT_SLOT( slotActivated( TQListBoxItem * ) ) );
}
void KEditListBoxManager::readNames()
{
int number = 0;
this->clear();
while( _config->hasGroup( _groupName->tqarg( number ) ) )
{
_config->setGroup( _groupName->tqarg( number ) );
this->insertItem( _config->readEntry( "name", TQString() ) );
++number;
}
_prevCount = this->count();
}
void KEditListBoxManager::slotChanged()
{
/* Three thing could be hapened:
* 1. the text is changed;
* 2. the item has moved up;
* 3. the item has moved down.
*/
//_prevCount is invariant under all of these operation
//if _prevCount is changed, is wasn't one of those operations.
if( _prevCount != this->count() )
return;
if( !_config || !_groupName )
return;
//First check if the item was moved up
_config->setGroup( _groupName->tqarg( this->currentItem() ) );
if( this->currentItem() > 0 && this->text( this->currentItem() - 1 ) == _config->readEntry( "name", TQString() ) )
changeItem( this->currentItem() - 1, this->currentItem() ); //moved down
else if( this->currentItem() < this->count() - 1 &&
this->text( this->currentItem() + 1 ) == _config->readEntry( "name", TQString() ) )
changeItem( this->currentItem(), this->currentItem() + 1 ); //moved up
else if( this->currentText() != _config->readEntry( "name", TQString() ) )
changedText(); //changed
}
void KEditListBoxManager::slotAdded( const TQString& name )
{
//Update _prevCount
_prevCount = this->count();
if( !_config || !_groupName )
return;
int number = 0;
while( _config->hasGroup( _groupName->tqarg( number ) ) )
++number;
_config->setGroup( _groupName->tqarg( number ) );
_config->writeEntry( "name", name );
emit setDefaults( name, number, _config );
}
void KEditListBoxManager::slotRemoved( const TQString& name )
{
//Update prevCount
_prevCount = this->count();
if( !_config || !_groupName )
return;
//First: search the item number.
int number = 0;
int subnumber = 0;
while( true )
{
if( !_config->hasGroup( _groupName->tqarg( number ) ) )
{
number = -1; //not found
break;
}
_config->setGroup( _groupName->tqarg( number ) );
if( name == _config->readEntry( "name", TQString() ) )
break; //found
++number; //Try next group
}
if( number < 0 ) //failure
return; //do nothing
_config->deleteGroup( _groupName->tqarg( number ), true, false );
emit elementDeleted( number );
while( _subGroupName && _config->hasGroup( _subGroupName->tqarg( number ).tqarg( subnumber ) ) )
{
_config->deleteGroup( _subGroupName->tqarg( number ).tqarg( subnumber ) );
++subnumber;
}
//rotate groups
while( _config->hasGroup( _groupName->tqarg( number + 1 ) ) )
{
moveItem( number + 1, number );
++number;
}
}
void KEditListBoxManager::slotActivated( TQListBoxItem* item )
{
if( item )
emit activated( item->text() );
}
void KEditListBoxManager::moveItem( int src, int dest )
{
TQMap<TQString, TQString> *srcList = new TQMap<TQString, TQString >;
TQMap<TQString, TQString>::iterator it;
int subnumber = 0;
*srcList = _config->entryMap( _groupName->tqarg( src ) );
_config->deleteGroup( _groupName->tqarg( src ) );
_config->setGroup( _groupName->tqarg( dest ) );
for( it = srcList->begin(); it != srcList->end(); ++it )
_config->writeEntry( it.key(), it.data() );
while( _subGroupName && _config->hasGroup( _subGroupName->tqarg( src ).tqarg( subnumber ) ) )
{
_config->deleteGroup( _subGroupName->tqarg( dest ).tqarg( subnumber ) );
_config->setGroup( _subGroupName->tqarg( dest ).tqarg( subnumber ) );
for( it = srcList->begin(); it != srcList->end(); ++it )
_config->writeEntry( it.key(), it.data() );
++subnumber;
}
emit elementsSwapped( src, dest );
delete srcList;
}
void KEditListBoxManager::changeItem( int first, int last )
{
TQMap<TQString, TQString> *firstList = new TQMap<TQString, TQString >;
TQMap<TQString, TQString> *lastList = new TQMap<TQString, TQString >;
TQMap<TQString, TQString>::iterator it;
int subnumber = 0;
*firstList = _config->entryMap( _groupName->tqarg( first ) );
*lastList = _config->entryMap( _groupName->tqarg( last ) );
_config->deleteGroup( _groupName->tqarg( first ) );
_config->deleteGroup( _groupName->tqarg( last ) );
_config->setGroup( _groupName->tqarg( last ) );
for( it = firstList->begin(); it != firstList->end(); ++it )
_config->writeEntry( it.key(), it.data() );
_config->setGroup( _groupName->tqarg( first ) );
for( it = lastList->begin(); it != lastList->end(); ++it )
_config->writeEntry( it.key(), it.data() );
while( _subGroupName && (
_config->hasGroup( _subGroupName->tqarg( first ).tqarg( subnumber ) ) ||
_config->hasGroup( _subGroupName->tqarg( last ).tqarg( subnumber ) ) ) )
{
*firstList = _config->entryMap( _subGroupName->tqarg( first ).tqarg( subnumber ) );
*lastList = _config->entryMap( _subGroupName->tqarg( last ).tqarg( subnumber ) );
_config->deleteGroup( _subGroupName->tqarg( first ).tqarg( subnumber ) );
_config->deleteGroup( _subGroupName->tqarg( last ).tqarg( subnumber ) );
_config->setGroup( _subGroupName->tqarg( last ).tqarg( subnumber ) );
for( it = firstList->begin(); it != firstList->end(); ++it )
_config->writeEntry( it.key(), it.data() );
_config->setGroup( _subGroupName->tqarg( first ).tqarg( subnumber ) );
for( it = lastList->begin(); it != lastList->end(); ++it )
_config->writeEntry( it.key(), it.data() );
++subnumber;
}
emit elementsSwapped( first, last );
delete firstList;
delete lastList;
}
void KEditListBoxManager::changedText()
{
_config->setGroup( _groupName->tqarg( this->currentItem() ) );
_config->writeEntry( "name", this->currentText() );
}
#include "keditlistboxman.moc"