21 #include <tqstringlist.h>
22 #include <tqpushbutton.h>
24 #include <tqgroupbox.h>
25 #include <tqlistbox.h>
26 #include <tqwhatsthis.h>
29 #include <kcombobox.h>
32 #include <klineedit.h>
34 #include <tdeapplication.h>
35 #include <knotifyclient.h>
37 #include "keditlistbox.h"
41 class KEditListBoxPrivate
44 bool m_checkAtEntering;
49 bool checkAtEntering,
int buttons )
50 :TQGroupBox(parent, name ), d(new KEditListBoxPrivate)
52 init( checkAtEntering,
buttons );
56 const char *name,
bool checkAtEntering,
int buttons)
57 :TQGroupBox(title, parent, name ), d(new KEditListBoxPrivate)
59 init( checkAtEntering,
buttons );
63 TQWidget *parent,
const char *name,
64 bool checkAtEntering,
int buttons)
65 :TQGroupBox(title, parent, name ), d(new KEditListBoxPrivate)
67 m_lineEdit = custom.lineEdit();
68 init( checkAtEntering,
buttons, custom.representationWidget() );
71 KEditListBox::~KEditListBox()
76 void KEditListBox::init(
bool checkAtEntering,
int buttons,
77 TQWidget *representationWidget )
79 d->m_checkAtEntering = checkAtEntering;
81 servNewButton = servRemoveButton = servUpButton = servDownButton = 0L;
82 setSizePolicy(TQSizePolicy(TQSizePolicy::MinimumExpanding,
83 TQSizePolicy::MinimumExpanding));
85 TQGridLayout * grid =
new TQGridLayout(
this, 7, 2,
88 grid->addRowSpacing(0, fontMetrics().lineSpacing());
89 grid->setRowStretch( 6, 1 );
93 if ( representationWidget )
94 representationWidget->reparent(
this, TQPoint(0,0) );
98 m_listBox =
new TQListBox(
this);
100 TQWidget *editingWidget = representationWidget ?
101 representationWidget : m_lineEdit;
102 grid->addMultiCellWidget(editingWidget,1,1,0,1);
103 grid->addMultiCellWidget(m_listBox, 2, 6, 0, 0);
108 connect(m_lineEdit,TQ_SIGNAL(textChanged(
const TQString&)),
this,TQ_SLOT(typedSomething(
const TQString&)));
110 connect(m_lineEdit,TQ_SIGNAL(returnPressed()),
this,TQ_SLOT(addItem()));
111 connect(m_listBox, TQ_SIGNAL(highlighted(
int)), TQ_SLOT(enableMoveButtons(
int)));
114 typedSomething( m_lineEdit->text() );
122 TQGridLayout* grid =
static_cast<TQGridLayout *
>( layout() );
123 if ( (
buttons & Add ) && !servNewButton ) {
124 servNewButton =
new TQPushButton(i18n(
"&Add"),
this);
125 servNewButton->setEnabled(
false);
126 servNewButton->show();
127 connect(servNewButton, TQ_SIGNAL(clicked()), TQ_SLOT(addItem()));
129 grid->addWidget(servNewButton, 2, 1);
130 }
else if ( (
buttons & Add ) == 0 && servNewButton ) {
131 delete servNewButton;
135 if ( (
buttons & Remove ) && !servRemoveButton ) {
136 servRemoveButton =
new TQPushButton(i18n(
"&Remove"),
this);
137 servRemoveButton->setEnabled(
false);
138 servRemoveButton->show();
139 connect(servRemoveButton, TQ_SIGNAL(clicked()), TQ_SLOT(removeItem()));
141 grid->addWidget(servRemoveButton, 3, 1);
142 }
else if ( (
buttons & Remove ) == 0 && servRemoveButton ) {
143 delete servRemoveButton;
144 servRemoveButton = 0;
147 if ( (
buttons & UpDown ) && !servUpButton ) {
148 servUpButton =
new TQPushButton(i18n(
"Move &Up"),
this);
149 servUpButton->setEnabled(
false);
150 servUpButton->show();
151 connect(servUpButton, TQ_SIGNAL(clicked()), TQ_SLOT(moveItemUp()));
153 servDownButton =
new TQPushButton(i18n(
"Move &Down"),
this);
154 servDownButton->setEnabled(
false);
155 servDownButton->show();
156 connect(servDownButton, TQ_SIGNAL(clicked()), TQ_SLOT(moveItemDown()));
158 grid->addWidget(servUpButton, 4, 1);
159 grid->addWidget(servDownButton, 5, 1);
160 }
else if ( (
buttons & UpDown ) == 0 && servUpButton ) {
161 delete servUpButton; servUpButton = 0;
162 delete servDownButton; servDownButton = 0;
168 void KEditListBox::typedSomething(
const TQString& text)
176 bool block = m_listBox->signalsBlocked();
179 m_listBox->blockSignals(
true );
180 m_listBox->changeItem(
text, item);
181 m_listBox->blockSignals( block );
188 if ( !servNewButton )
191 if (!d->m_checkAtEntering)
192 servNewButton->setEnabled(!
text.isEmpty());
197 servNewButton->setEnabled(
false);
201 StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
202 bool enable = (!m_listBox->findItem(
text, mode ));
203 servNewButton->setEnabled( enable );
208 void KEditListBox::moveItemUp()
210 if (!m_listBox->isEnabled())
216 const unsigned int selIndex = m_listBox->currentItem();
223 TQListBoxItem *selItem = m_listBox->item(selIndex);
224 m_listBox->takeItem(selItem);
225 m_listBox->insertItem(selItem, selIndex-1);
226 m_listBox->setCurrentItem(selIndex - 1);
231 void KEditListBox::moveItemDown()
233 if (!m_listBox->isEnabled())
239 unsigned int selIndex = m_listBox->currentItem();
240 if (selIndex == m_listBox->count() - 1)
246 TQListBoxItem *selItem = m_listBox->item(selIndex);
247 m_listBox->takeItem(selItem);
248 m_listBox->insertItem(selItem, selIndex+1);
249 m_listBox->setCurrentItem(selIndex + 1);
254 void KEditListBox::addItem()
259 if ( !servNewButton || !servNewButton->isEnabled() )
262 const TQString& currentTextLE=m_lineEdit->text();
263 bool alreadyInList(
false);
265 if (!d->m_checkAtEntering)
268 if ( m_listBox->currentText() == currentTextLE )
269 alreadyInList =
true;
272 StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
273 alreadyInList =(m_listBox->findItem(currentTextLE, mode) );
278 servNewButton->setEnabled(
false);
280 bool block = m_lineEdit->signalsBlocked();
281 m_lineEdit->blockSignals(
true);
283 m_lineEdit->blockSignals(block);
286 m_listBox->setSelected(item,
false);
290 block = m_listBox->signalsBlocked();
291 m_listBox->blockSignals(
true );
292 m_listBox->insertItem(currentTextLE);
293 m_listBox->blockSignals( block );
295 emit
added( currentTextLE );
296 emit
added( item, currentTextLE );
302 int nr = m_listBox->currentItem();
303 if(nr >= 0 && !m_listBox->item(nr)->isSelected())
return -1;
307 void KEditListBox::removeItem()
309 int item = m_listBox->currentItem();
313 TQString removedText = m_listBox->currentText();
315 m_listBox->removeItem( item );
317 m_listBox->setSelected( TQMIN( item,
count() - 1 ),
true );
321 emit
removed( item, removedText );
324 if ( servRemoveButton && m_listBox->currentItem() == -1 )
325 servRemoveButton->setEnabled(
false);
328 void KEditListBox::enableMoveButtons(
int index)
334 bool moveEnabled = servUpButton && servDownButton;
338 if (m_listBox->count() <= 1)
340 servUpButton->setEnabled(
false);
341 servDownButton->setEnabled(
false);
343 else if ((uint) index == (m_listBox->count() - 1))
345 servUpButton->setEnabled(
true);
346 servDownButton->setEnabled(
false);
350 servUpButton->setEnabled(
false);
351 servDownButton->setEnabled(
true);
355 servUpButton->setEnabled(
true);
356 servDownButton->setEnabled(
true);
360 if ( servRemoveButton )
361 servRemoveButton->setEnabled(
true);
373 m_listBox->insertStringList(list,index);
378 m_listBox->insertStrList(list,index);
383 m_listBox->insertStrList(list,index);
388 m_listBox->insertStrList(list,numStrings,index);
394 for (TQListBoxItem
const * i = m_listBox->firstItem(); i != 0; i = i->next() )
395 list.append( i->text());
403 m_listBox->insertStringList(
items, 0);
411 void KEditListBox::virtual_hook(
int,
void* )
418 KEditListBox::CustomEditor::CustomEditor(
KComboBox *combo )
420 m_representationWidget = combo;
421 m_lineEdit =
dynamic_cast<KLineEdit*
>( combo->lineEdit() );
422 assert( m_lineEdit );
425 #include "keditlistbox.moc"
static int marginHint()
Return the number of pixels you shall use between a dialog edge and the outermost widget(s) according...
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
KEditListBox(TQWidget *parent=0, const char *name=0, bool checkAtEntering=false, int buttons=All)
Create an editable listbox.
void setItems(const TQStringList &items)
Clears the listbox and sets the contents to items.
int count() const
See TQListBox::count()
TQString currentText() const
See TQListBox::currentText()
void renamed(const TQString &from, const TQString &to)
This signal is emitted when the user renames a list item.
int currentItem() const
See TQListBox::currentItem()
int buttons() const
Returns which buttons are visible.
void removed(const TQString &text)
This signal is emitted when the user removes a string from the list.
void added(const TQString &text)
This signal is emitted when the user adds a new string to the list.
void insertStringList(const TQStringList &list, int index=-1)
See TQListBox::insertStringList()
TQStringList items() const
void setButtons(uint buttons)
Specifies which buttons should be visible.
TQString text(int index) const
See TQListBox::text()
void clear()
Clears both the listbox and the line edit.
void insertStrList(const TQStrList *list, int index=-1)
See TQListBox::insertStringList()
An enhanced TQLineEdit widget for inputting text.
void setTrapReturnKey(bool trap)
By default, KLineEdit recognizes Key_Return and Key_Enter and emits the returnPressed() signals,...
virtual void setText(const TQString &)
Re-implemented to enable text squeezing.
virtual void clear()
Reimplemented to workaround a buggy TQLineEdit::clear() (changing the clipboard to the text we just h...
void beep(const TQString &reason=TQString::null)