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/kode/kwsdl/kung/listinputfield.cpp

190 lines
4.7 KiB

/*
This file is part of Kung.
Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <tqlayout.h>
#include <tqlistbox.h>
#include <tqpushbutton.h>
#include <klocale.h>
#include <schema/simpletype.h>
#include "inputdialog.h"
#include "inputfieldfactory.h"
#include "outputdialog.h"
#include "listinputfield.h"
ListInputField::ListInputField( const TQString &name, const TQString &typeName, const Schema::SimpleType *type )
: SimpleInputField( name, type ), mTypeName( typeName )
{
}
void ListInputField::setXMLData( const TQDomElement &element )
{
InputField::List::Iterator it;
for ( it = mFields.begin(); it != mFields.end(); ++it ) {
delete *it;
}
mFields.clear();
TQDomNode node = element.firstChild();
while ( !node.isNull() ) {
TQDomElement child = node.toElement();
if ( !child.isNull() ) {
InputField *field = InputFieldFactory::self()->createField( name(), mTypeName );
if ( field ) {
field->setXMLData( child );
appendChild( field );
} else
qDebug( "ListInputField: Unable to create field of type %s", mTypeName.latin1() );
}
node = node.nextSibling();
}
}
void ListInputField::xmlData( TQDomDocument &document, TQDomElement &tqparent )
{
InputField::List::Iterator it;
for ( it = mFields.begin(); it != mFields.end(); ++it ) {
(*it)->xmlData( document, tqparent );
}
}
void ListInputField::setData( const TQString& )
{
}
TQString ListInputField::data() const
{
return TQString();
}
TQWidget *ListInputField::createWidget( TQWidget *tqparent )
{
mInputWidget = new ListWidget( this, name(), mTypeName, tqparent );
return mInputWidget;
}
ListWidget::ListWidget( InputField *tqparentField, const TQString &name, const TQString &type, TQWidget *tqparent )
: TQWidget( tqparent ),
mParentField( tqparentField ), mName( name ), mType( type )
{
TQGridLayout *tqlayout = new TQGridLayout( this, 4, 2, 11, 6 );
mView = new TQListBox( this );
tqlayout->addMultiCellWidget( mView, 0, 3, 0, 0 );
mAddButton = new TQPushButton( i18n( "Add" ), this );
tqlayout->addWidget( mAddButton, 0, 1 );
mEditButton = new TQPushButton( i18n( "Edit..." ), this );
tqlayout->addWidget( mEditButton, 1, 1 );
mRemoveButton = new TQPushButton( i18n( "Remove" ), this );
tqlayout->addWidget( mRemoveButton, 2, 1 );
connect( mAddButton, TQT_SIGNAL( clicked() ), TQT_SLOT( add() ) );
connect( mEditButton, TQT_SIGNAL( clicked() ), TQT_SLOT( edit() ) );
connect( mRemoveButton, TQT_SIGNAL( clicked() ), TQT_SLOT( remove() ) );
update();
}
void ListWidget::update()
{
int pos = mView->currentItem();
mView->clear();
const InputField::List fields = mParentField->childFields();
InputField::List::ConstIterator it;
for ( it = fields.begin(); it != fields.end(); ++it )
mView->insertItem( (*it)->name() );
mView->setCurrentItem( pos );
updateButtons();
}
void ListWidget::add()
{
InputField *field = InputFieldFactory::self()->createField( mName, mType );
if ( !field ) {
qDebug( "ListInputField: Unable to create field of type %s", mType.latin1() );
return;
}
InputDialog dlg( field->createWidget( this ), this );
if ( dlg.exec() ) {
mParentField->appendChild( field );
update();
} else
delete field;
}
void ListWidget::edit()
{
int pos = mView->currentItem();
if ( pos == -1 )
return;
InputField *field = mParentField->childFields()[ pos ];
if ( !field )
return;
OutputDialog dlg( field->createWidget( this ), this );
dlg.exec();
update();
}
void ListWidget::remove()
{
int pos = mView->currentItem();
if ( pos == -1 )
return;
InputField *field = mParentField->childFields()[ pos ];
if ( !field )
return;
mParentField->removeChild( field );
delete field;
update();
}
void ListWidget::updateButtons()
{
bool enabled = (mParentField->childFields().count() > 0);
mEditButton->setEnabled( enabled );
mRemoveButton->setEnabled( enabled );
}
#include "listinputfield.moc"