20 #include "qeditlistbox.h" 22 #include "compat_tools.h" 24 #include <klineedit.h> 26 #include <tqpushbutton.h> 28 #include <tqgroupbox.h> 29 #include <tqlistbox.h> 30 #include <tqwhatsthis.h> 32 #include <tqcombobox.h> 33 #include <tqapplication.h> 34 #include <tqstringlist.h> 42 class QEditListBoxPrivate
45 bool m_checkAtEntering;
49 QEditListBox::QEditListBox(TQWidget *parent,
const char *name,
50 bool checkAtEntering,
int buttons )
51 :TQGroupBox(parent, name )
53 init( checkAtEntering, buttons );
56 QEditListBox::QEditListBox(
const TQString& title, TQWidget *parent,
57 const char *name,
bool checkAtEntering,
int buttons)
58 :TQGroupBox(title, parent, name )
60 init( checkAtEntering, buttons );
63 QEditListBox::QEditListBox(
const TQString& title,
const CustomEditor& custom,
64 TQWidget *parent,
const char *name,
65 bool checkAtEntering,
int buttons)
66 :TQGroupBox(title, parent, name )
68 m_lineEdit = custom.lineEdit();
69 init( checkAtEntering, buttons, custom.representationWidget() );
72 QEditListBox::~QEditListBox()
78 void QEditListBox::init(
bool checkAtEntering,
int buttons,
79 TQWidget *representationWidget )
81 d=
new QEditListBoxPrivate;
82 d->m_checkAtEntering=checkAtEntering;
86 if ( (buttons & Add) == 0 )
88 if ( (buttons & Remove) == 0 )
90 if ( (buttons & UpDown) == 0 )
94 servNewButton = servRemoveButton = servUpButton = servDownButton = 0L;
95 setSizePolicy(TQSizePolicy(TQSizePolicy::MinimumExpanding,
96 TQSizePolicy::MinimumExpanding));
99 TQGridLayout * grid =
new TQGridLayout(gb, 7 - lostButtons, 2,
102 grid->addRowSpacing(0, fontMetrics().lineSpacing());
103 for (
int i = 1; i < 7 - lostButtons; i++ )
104 grid->setRowStretch(i, 1);
108 if ( representationWidget )
109 representationWidget->reparent( gb, TQPoint(0,0) );
111 m_lineEdit=
new KLineEdit(gb);
113 m_listBox =
new TQListBox(gb);
115 TQWidget *editingWidget = representationWidget ?
116 representationWidget : m_lineEdit;
117 grid->addMultiCellWidget(editingWidget,1,1,0,1);
118 grid->addMultiCellWidget(m_listBox, 2, 6 - lostButtons, 0, 0);
120 if ( buttons & Add ) {
121 servNewButton =
new TQPushButton(i18n(
"&Add"), gb);
122 servNewButton->setEnabled(
false);
123 connect(servNewButton, TQT_SIGNAL(clicked()), TQT_SLOT(addItem()));
125 grid->addWidget(servNewButton, row++, 1);
128 if ( buttons & Remove ) {
129 servRemoveButton =
new TQPushButton(i18n(
"&Remove"), gb);
130 servRemoveButton->setEnabled(
false);
131 connect(servRemoveButton, TQT_SIGNAL(clicked()), TQT_SLOT(removeItem()));
133 grid->addWidget(servRemoveButton, row++, 1);
136 if ( buttons & UpDown ) {
137 servUpButton =
new TQPushButton(i18n(
"Move &Up"), gb);
138 servUpButton->setEnabled(
false);
139 connect(servUpButton, TQT_SIGNAL(clicked()), TQT_SLOT(moveItemUp()));
141 servDownButton =
new TQPushButton(i18n(
"Move &Down"), gb);
142 servDownButton->setEnabled(
false);
143 connect(servDownButton, TQT_SIGNAL(clicked()), TQT_SLOT(moveItemDown()));
145 grid->addWidget(servUpButton, row++, 1);
146 grid->addWidget(servDownButton, row++, 1);
149 connect(m_lineEdit,TQT_SIGNAL(textChanged(
const TQString&)),
this,TQT_SLOT(typedSomething(
const TQString&)));
151 connect(m_lineEdit,TQT_SIGNAL(returnPressed()),
this,TQT_SLOT(addItem()));
152 connect(m_listBox, TQT_SIGNAL(highlighted(
int)), TQT_SLOT(enableMoveButtons(
int)));
155 typedSomething( m_lineEdit->text() );
158 void QEditListBox::typedSomething(
const TQString& text)
160 if(currentItem() >= 0) {
161 if(currentText() != m_lineEdit->text())
166 bool block = m_listBox->signalsBlocked();
167 m_listBox->blockSignals(
true );
168 m_listBox->changeItem(text, currentItem());
169 m_listBox->blockSignals( block );
174 if ( !servNewButton )
177 if (!d->m_checkAtEntering)
178 servNewButton->setEnabled(!text.isEmpty());
183 servNewButton->setEnabled(
false);
187 StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
188 bool enable = (m_listBox->findItem( text, mode ) == 0L);
189 servNewButton->setEnabled( enable );
194 void QEditListBox::moveItemUp()
196 if (!m_listBox->isEnabled())
202 unsigned int selIndex = m_listBox->currentItem();
209 TQListBoxItem *selItem = m_listBox->item(selIndex);
210 m_listBox->takeItem(selItem);
211 m_listBox->insertItem(selItem, selIndex-1);
212 m_listBox->setCurrentItem(selIndex - 1);
217 void QEditListBox::moveItemDown()
219 if (!m_listBox->isEnabled())
225 unsigned int selIndex = m_listBox->currentItem();
226 if (selIndex == m_listBox->count() - 1)
232 TQListBoxItem *selItem = m_listBox->item(selIndex);
233 m_listBox->takeItem(selItem);
234 m_listBox->insertItem(selItem, selIndex+1);
235 m_listBox->setCurrentItem(selIndex + 1);
240 void QEditListBox::addItem()
245 if ( !servNewButton || !servNewButton->isEnabled() )
248 const TQString& currentTextLE=m_lineEdit->text();
249 bool alreadyInList(
false);
251 if (!d->m_checkAtEntering)
254 if ( m_listBox->currentText() == currentTextLE )
255 alreadyInList =
true;
258 StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
259 alreadyInList =(m_listBox->findItem(currentTextLE, mode) != 0);
264 servNewButton->setEnabled(
false);
266 bool block = m_lineEdit->signalsBlocked();
267 m_lineEdit->blockSignals(
true);
269 m_lineEdit->blockSignals(block);
271 m_listBox->setSelected(currentItem(),
false);
275 block = m_listBox->signalsBlocked();
276 m_listBox->blockSignals(
true );
277 m_listBox->insertItem(currentTextLE);
278 m_listBox->blockSignals( block );
280 emit added( currentTextLE );
284 int QEditListBox::currentItem()
const 286 int nr = m_listBox->currentItem();
287 if(nr >= 0 && !m_listBox->item(nr)->isSelected())
return -1;
291 void QEditListBox::removeItem()
293 int selected = m_listBox->currentItem();
297 TQString removedText = m_listBox->currentText();
299 m_listBox->removeItem( selected );
301 m_listBox->setSelected( TQMIN( selected, count() - 1 ),
true );
304 emit removed( removedText );
307 if ( servRemoveButton && m_listBox->currentItem() == -1 )
308 servRemoveButton->setEnabled(
false);
311 void QEditListBox::enableMoveButtons(
int index)
314 if(currentText() != m_lineEdit->text())
315 m_lineEdit->setText(currentText());
317 bool moveEnabled = servUpButton && servDownButton;
321 if (m_listBox->count() <= 1)
323 servUpButton->setEnabled(
false);
324 servDownButton->setEnabled(
false);
326 else if ((uint) index == (m_listBox->count() - 1))
328 servUpButton->setEnabled(
true);
329 servDownButton->setEnabled(
false);
333 servUpButton->setEnabled(
false);
334 servDownButton->setEnabled(
true);
338 servUpButton->setEnabled(
true);
339 servDownButton->setEnabled(
true);
343 if ( servRemoveButton )
344 servRemoveButton->setEnabled(
true);
347 void QEditListBox::clear()
354 void QEditListBox::insertStringList(
const TQStringList& list,
int index)
356 m_listBox->insertStringList(list,index);
359 void QEditListBox::insertStrList(
const TQStrList* list,
int index)
361 m_listBox->insertStrList(list,index);
364 void QEditListBox::insertStrList(
const TQStrList& list,
int index)
366 m_listBox->insertStrList(list,index);
369 void QEditListBox::insertStrList(
const char ** list,
int numStrings,
int index)
371 m_listBox->insertStrList(list,numStrings,index);
374 TQStringList QEditListBox::items()
const 377 for ( uint i = 0; i < m_listBox->count(); i++ )
378 list.append( m_listBox->text( i ));
383 void QEditListBox::setItems(
const TQStringList& items)
386 m_listBox->insertStringList(items, 0);
389 void QEditListBox::virtual_hook(
int,
void* )
396 QEditListBox::CustomEditor::CustomEditor( TQComboBox *combo )
398 m_representationWidget = combo;
399 m_lineEdit =
dynamic_cast<KLineEdit*
>( combo->lineEdit() );
400 assert( m_lineEdit );
403 #include "qeditlistbox.moc"