summaryrefslogtreecommitdiffstats
path: root/kode/kwsdl/kung/listinputfield.cpp
blob: 539e3de2c05b91cf15952541e63f641ff5397dcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
    This file is part of Kung.

    Copyright (c) 2005 Tobias Koenig <[email protected]>

    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 <tdelocale.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
        tqDebug( "ListInputField: Unable to create field of type %s", mTypeName.latin1() );
    }

    node = node.nextSibling();
  }
}

void ListInputField::xmlData( TQDomDocument &document, TQDomElement &parent )
{
  InputField::List::Iterator it;
  for ( it = mFields.begin(); it != mFields.end(); ++it ) {
    (*it)->xmlData( document, parent );
  }
}

void ListInputField::setData( const TQString& )
{
}

TQString ListInputField::data() const
{
  return TQString();
}

TQWidget *ListInputField::createWidget( TQWidget *parent )
{
  mInputWidget = new ListWidget( this, name(), mTypeName, parent );

  return mInputWidget;
}


ListWidget::ListWidget( InputField *parentField, const TQString &name, const TQString &type, TQWidget *parent )
  : TQWidget( parent ),
    mParentField( parentField ), mName( name ), mType( type )
{
  TQGridLayout *layout = new TQGridLayout( this, 4, 2, 11, 6 );

  mView = new TQListBox( this );
  layout->addMultiCellWidget( mView, 0, 3, 0, 0 );

  mAddButton = new TQPushButton( i18n( "Add" ), this );
  layout->addWidget( mAddButton, 0, 1 );

  mEditButton = new TQPushButton( i18n( "Edit..." ), this );
  layout->addWidget( mEditButton, 1, 1 );

  mRemoveButton = new TQPushButton( i18n( "Remove" ), this );
  layout->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 ) {
    tqDebug( "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"