23 #include <tqclipboard.h>
24 #include <tqlistbox.h>
25 #include <tqpopupmenu.h>
26 #include <tqapplication.h>
28 #include <kcompletionbox.h>
30 #include <kiconloader.h>
31 #include <kicontheme.h>
32 #include <klistviewsearchline.h>
33 #include <klineedit.h>
35 #include <knotifyclient.h>
36 #include <kpixmapprovider.h>
37 #include <kstdaccel.h>
43 #include "kcombobox.h"
47 class KComboBox::KComboBoxPrivate
50 KComboBoxPrivate() : klineEdit(0L)
61 : TQComboBox( parent, name ), d(new KComboBoxPrivate)
67 : TQComboBox( rw, parent, name ), d(new KComboBoxPrivate)
83 void KComboBox::init()
86 TQComboBox::setAutoCompletion(
false );
96 if ( _text.isEmpty() )
99 const int itemCount = count();
100 for (
int i = 0; i < itemCount; ++i )
102 if ( text(i) == _text )
128 d->klineEdit->setContextMenuEnabled( showMenu );
135 d->klineEdit->setURLDropsEnabled( enable );
140 return d->klineEdit && d->klineEdit->isURLDropsEnabled();
147 d->klineEdit->setCompletedText( text, marked );
153 d->klineEdit->setCompletedText( text );
159 d->klineEdit->makeCompletion( text );
163 if( text.isNull() || !listBox() )
166 const int index = listBox()->index( listBox()->findItem( text ) );
175 d->klineEdit->rotateText( type );
181 return TQComboBox::eventFilter( o, ev );
187 d->klineEdit->setTrapReturnKey( grab );
189 qWarning(
"KComboBox::setTrapReturnKey not supported with a non-KLineEdit.");
194 return d->klineEdit && d->klineEdit->trapReturnKey();
200 TQComboBox::setEditText( url.
prettyURL() );
205 TQComboBox::insertItem( url.
prettyURL(), index );
210 TQComboBox::insertItem( pixmap, url.
prettyURL(), index );
215 TQComboBox::changeItem( url.
prettyURL(), index );
220 TQComboBox::changeItem( pixmap, url.
prettyURL(), index );
226 d->klineEdit->setCompletedItems( items );
232 return d->klineEdit->completionBox( create );
239 TQComboBox::create(
id, initializeWindow, destroyOldWindow );
243 void KComboBox::wheelEvent( TQWheelEvent *ev )
246 TQComboBox::wheelEvent( ev );
251 if ( !editable() && edit &&
252 !qstrcmp( edit->className(), TQLINEEDIT_OBJECT_NAME_STRING ) )
260 edit =
new KLineEdit(
this,
"combo edit" );
263 TQComboBox::setLineEdit( edit );
264 d->klineEdit = tqt_dynamic_cast<
KLineEdit*>( edit );
277 connect( edit, TQT_SIGNAL( destroyed() ), TQT_SLOT( lineEditDeleted() ));
279 connect( d->klineEdit, TQT_SIGNAL(
returnPressed(
const TQString& )),
282 connect( d->klineEdit, TQT_SIGNAL(
completion(
const TQString& )),
288 connect( d->klineEdit,
292 connect( d->klineEdit,
296 connect( d->klineEdit,
300 connect( d->klineEdit,
301 TQT_SIGNAL( completionBoxActivated(
const TQString& )),
302 TQT_SIGNAL( activated(
const TQString& )) );
310 const int itemCount = count();
311 for (
int i = 0; i < itemCount; ++i)
320 if (sel == -1 && insert)
322 insertItem(item, index);
331 void KComboBox::lineEditDeleted()
347 class KHistoryCombo::KHistoryComboPrivate
350 KHistoryComboPrivate() : bHistoryEditorEnabled(false)
353 ~KHistoryComboPrivate()
357 bool bHistoryEditorEnabled;
362 :
KComboBox( true, parent, name ), d(new KHistoryComboPrivate)
369 TQWidget *parent,
const char *name )
370 :
KComboBox( true, parent, name ), d(new KHistoryComboPrivate)
372 init( useCompletion );
375 void KHistoryCombo::init(
bool useCompletion )
383 setInsertionPolicy( NoInsertion );
389 TQCString histControl = getenv(
"HISTCONTROL");
390 if ( histControl ==
"ignoredups" || histControl ==
"ignoreboth" )
391 setDuplicatesEnabled(
false );
394 TQT_SLOT(addContextMenuItems(TQPopupMenu*)) );
395 connect(
this, TQT_SIGNAL( activated(
int) ), TQT_SLOT( slotReset() ));
396 connect(
this, TQT_SIGNAL(
returnPressed(
const TQString&) ), TQT_SLOT(slotReset()));
401 delete myPixProvider;
405 bool setCompletionList )
410 const int itemCount = items.count();
411 const int toRemove = itemCount - maxCount();
413 if (toRemove >= itemCount) {
416 for (
int i = 0; i < toRemove; ++i)
436 const int itemCount = count();
437 for (
int i = 0; i < itemCount; ++i )
438 list.append( text( i ) );
445 const TQString temp = currentText();
452 void KHistoryCombo::addContextMenuItems( TQPopupMenu* menu )
456 menu->insertSeparator();
457 if (d->bHistoryEditorEnabled) {
458 int idedit = menu->insertItem( SmallIconSet(
"edit"), i18n(
"&Edit History..."),
this, TQT_SLOT( slotEdit()) );
459 menu->setItemEnabled(idedit, count());
461 int id = menu->insertItem( SmallIconSet(
"history_clear"), i18n(
"Clear &History"),
this, TQT_SLOT( slotClear()));
463 menu->setItemEnabled(
id,
false);
469 if ( item.isEmpty() || (count() > 0 && item == text(0) )) {
473 bool wasCurrent =
false;
475 if ( !duplicatesEnabled() ) {
477 int itemCount = count();
478 while ( i < itemCount ) {
479 if ( text( i ) == item ) {
481 wasCurrent = ( i == currentItem() );
494 insertItem( item, 0 );
501 const int last = count() - 1;
502 const int mc = maxCount();
503 const int stopAt = QMAX(mc, 0);
505 for (
int rmIndex = last; rmIndex >= stopAt; --rmIndex) {
509 const TQString rmItem = text( rmIndex );
510 removeItem( rmIndex );
511 if ( useComp && !
contains( rmItem ) )
521 if ( item.isEmpty() )
524 bool removed =
false;
525 const TQString temp = currentText();
527 int itemCount = count();
528 while ( i < itemCount ) {
529 if ( item == text( i ) ) {
545 void KHistoryCombo::rotateUp()
548 if ( myIterateIndex == -1 )
549 myText = currentText();
554 const int last = count() - 1;
555 const TQString currText = currentText();
557 while ( myIterateIndex < last &&
558 (currText == text( myIterateIndex ) ||
559 text( myIterateIndex ).isEmpty()) )
562 if ( myIterateIndex >= count() ) {
567 if ( count() > 0 && myText == text(0) )
570 setEditText( myText );
573 setEditText( text( myIterateIndex ));
576 void KHistoryCombo::rotateDown()
579 if ( myIterateIndex == -1 )
580 myText = currentText();
584 const TQString currText = currentText();
586 while ( myIterateIndex >= 0 &&
587 (currText == text( myIterateIndex ) ||
588 text( myIterateIndex ).isEmpty()) )
592 if ( myIterateIndex < 0 ) {
593 if ( myRotated && myIterateIndex == -2 ) {
595 myIterateIndex = count() - 1;
596 setEditText( text(myIterateIndex) );
599 if ( myIterateIndex == -2 ) {
601 i18n(
"No further item in the history."));
605 if ( currentText() != myText )
606 setEditText( myText );
610 setEditText( text( myIterateIndex ));
627 KComboBox::keyPressEvent( e );
633 TQListBox*
const lb = listBox();
634 if ( lb && lb->isVisible() )
636 TQApplication::sendEvent( lb, ev );
640 if ( ev->delta() > 0 ) {
648 void KHistoryCombo::slotReset()
657 if ( myPixProvider == prov )
660 delete myPixProvider;
661 myPixProvider = prov;
675 TQStringList::ConstIterator it = items.constBegin();
676 const TQStringList::ConstIterator itEnd = items.constEnd();
678 while ( it != itEnd ) {
679 const TQString item = *it;
680 if ( !item.isEmpty() ) {
691 void KHistoryCombo::slotClear()
697 void KHistoryCombo::slotEdit()
700 connect( &dlg, TQT_SIGNAL(
removeFromHistory(
const TQString&) ), TQT_SLOT( slotRemoveFromHistory(
const TQString&)) );
704 void KHistoryCombo::slotRemoveFromHistory(
const TQString &entry)
712 d->bHistoryEditorEnabled = enable;
715 bool KHistoryCombo::isHistoryEditorEnabled()
const
717 return d->bHistoryEditorEnabled;
720 void KComboBox::virtual_hook(
int id,
void* data )
721 { KCompletionBase::virtual_hook(
id, data ); }
723 void KHistoryCombo::virtual_hook(
int id,
void* data )
724 { KComboBox::virtual_hook(
id, data ); }
726 void KHistoryComboEditor::virtual_hook(
int id,
void* data )
727 { KDialogBase::virtual_hook(
id, data ); }
729 KHistoryComboEditor::KHistoryComboEditor(
const TQStringList& entries, TQWidget *parent )
730 :
KDialogBase( parent,
"khistorycomboeditor", true, i18n(
"History Editor" ),
732 KGuiItem( i18n(
"&Delete Entry" ),
"editdelete") ), d(0)
734 TQVBox* box =
new TQVBox(
this );
736 setMainWidget( box );
738 new TQLabel( i18n(
"This dialog allows you to delete unwanted history items." ), box );
741 TQHBox* searchbox =
new TQHBox( box );
744 TQToolButton *clearSearch =
new TQToolButton(searchbox);
745 clearSearch->setTextLabel(i18n(
"Clear Search"),
true);
746 clearSearch->setIconSet(SmallIconSet(TQApplication::reverseLayout() ?
"clear_left" :
"locationbar_erase"));
747 TQLabel* slbl =
new TQLabel(i18n(
"&Search:"), searchbox);
749 slbl->setBuddy(listViewSearch);
750 connect(clearSearch, TQT_SIGNAL(pressed()), listViewSearch, TQT_SLOT(
clear()));
755 m_pListView->setAllColumnsShowFocus(
true);
756 m_pListView->header()->hide();
757 m_pListView->addColumn(
"");
758 m_pListView->setRenameable( 0 );
760 box->setStretchFactor( m_pListView, 1 );
762 TQStringList newlist = entries;
763 for ( TQStringList::Iterator it = newlist.begin(); it != newlist.end(); ++it ) {
764 new TQListViewItem( m_pListView, *it );
767 m_pListView->setMinimumSize( m_pListView->sizeHint() );
769 connect( m_pListView, TQT_SIGNAL( selectionChanged( TQListViewItem * ) ),
770 this, TQT_SLOT( slotSelectionChanged( TQListViewItem * ) ) );
774 resize( sizeHint() );
777 KHistoryComboEditor::~KHistoryComboEditor()
781 void KHistoryComboEditor::slotUser1()
783 TQListViewItem *item = m_pListView->selectedItem();
786 emit removeFromHistory( item->text(0) );
787 m_pListView->takeItem( item );
792 void KHistoryComboEditor::slotSelectionChanged( TQListViewItem * item )
797 #include "kcombobox.moc"