• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Widgets Library
 

KDevelop Widgets Library

  • lib
  • widgets
qcomboview.cpp
1/**********************************************************************
2**
3**
4** Implementation of QComboView widget class
5**
6** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
7** Copyright (C) 2003 Alexander Dymo <cloudtemple@mksat.net>
8**
9** This file may be distributed and/or modified under the terms of the
10** GNU General Public License version 2 as published by the Free Software
11** Foundation and appearing in the file LICENSE.GPL included in the
12** packaging of this file.
13**
14**********************************************************************/
15
16#include "qcomboview.h"
17#include <tdeversion.h>
18#ifndef TQT_NO_COMBOBOX
19#include "tqpopupmenu.h"
20#include "tqlistview.h"
21#include "tqpainter.h"
22#include "tqdrawutil.h"
23#include "tqstrlist.h"
24#include "tqpixmap.h"
25#include "tqtimer.h"
26#include "tqapplication.h"
27#include "tqlineedit.h"
28#include "tqbitmap.h"
29#include "tqeffects_p.h"
30#include "tqstringlist.h"
31#include "tqcombobox.h"
32#include "tqstyle.h"
33#include "tqheader.h"
34#include <limits.h>
35
36class QComboViewData
37{
38public:
39 QComboViewData( QComboView *cb ): current(0), lView( 0 ), combo( cb )
40 {
41 duplicatesEnabled = true;
42 cb->setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Fixed ) );
43 }
44
45 inline TQListView * listView() { return lView; }
46 void updateLinedGeometry();
47
48 void setListView( TQListView *l ) { lView = l ;
49 l->setMouseTracking( true );}
50
51 TQListViewItem *current;
52 int maxCount;
53 int sizeLimit;
54 QComboView::Policy p;
55 bool autoresize;
56 bool poppedUp;
57 bool mouseWasInsidePopup;
58 bool arrowPressed;
59 bool arrowDown;
60 bool discardNextMousePress;
61 bool shortClick;
62 bool useCompletion;
63 bool completeNow;
64 int completeAt;
65 bool duplicatesEnabled;
66 int fullHeight, currHeight;
67
68 TQLineEdit * ed; // /bin/ed rules!
69 TQTimer *completionTimer;
70
71 TQSize sizeHint;
72
73private:
74 bool usinglView;
75 TQListView *lView;
76 QComboView *combo;
77
78};
79
80void QComboViewData::updateLinedGeometry()
81{
82 if ( !ed || !combo )
83 return;
84 TQRect r = TQStyle::visualRect( combo->style().querySubControlMetrics(TQStyle::CC_ComboBox, combo,
85 TQStyle::SC_ComboBoxEditField), combo );
86
87// tqWarning("updateLinedGeometry(): currentItem is %d", combo->currentItem() == 0 ? 0 : 1);
88 const TQPixmap *pix = combo->currentItem() ? combo->currentItem()->pixmap(0) : 0;
89 if ( pix && pix->width() < r.width() )
90 r.setLeft( r.left() + pix->width() + 4 );
91 if ( r != ed->geometry() )
92 ed->setGeometry( r );
93}
94
95static inline bool checkInsertIndex( const char *method, const char * name,
96 int count, int *index)
97{
98 bool range_err = (*index > count);
99#if defined(TQT_CHECK_RANGE)
100 if ( range_err )
101 tqWarning( "QComboView::%s: (%s) Index %d out of range",
102 method, name ? name : "<no name>", *index );
103#else
104 Q_UNUSED( method )
105 Q_UNUSED( name )
106#endif
107 if ( *index < 0 ) // append
108 *index = count;
109 return !range_err;
110}
111
112
113static inline bool checkIndex( const char *method, const char * name,
114 int count, int index )
115{
116 bool range_err = (index >= count);
117#if defined(TQT_CHECK_RANGE)
118 if ( range_err )
119 tqWarning( "QComboView::%s: (%s) Index %i out of range",
120 method, name ? name : "<no name>", index );
121#else
122 Q_UNUSED( method )
123 Q_UNUSED( name )
124#endif
125 return !range_err;
126}
127
128
141QComboView::QComboView( bool rw, TQWidget *parent, const char *name )
142 : TQWidget( parent, name, WResizeNoErase )
143{
144 d = new QComboViewData( this );
145 setUpListView();
146
147 d->current = 0;
148 d->maxCount = INT_MAX;
149 setSizeLimit(10);
150 d->p = AtBottom;
151 d->autoresize = false;
152 d->poppedUp = false;
153 d->arrowDown = false;
154 d->discardNextMousePress = false;
155 d->shortClick = false;
156 d->useCompletion = false;
157 d->completeAt = 0;
158 d->completeNow = false;
159 d->completionTimer = new TQTimer( this );
160
161 setFocusPolicy( TQWidget::StrongFocus );
162
163 d->ed = 0;
164 if ( rw )
165 setUpLineEdit();
166 setBackgroundMode( PaletteButton, PaletteBase );
167}
168
169
170
175QComboView::~QComboView()
176{
177 delete d;
178}
179
180void QComboView::setDuplicatesEnabled( bool enable )
181{
182 d->duplicatesEnabled = enable;
183}
184
185bool QComboView::duplicatesEnabled() const
186{
187 return d->duplicatesEnabled;
188}
189
190int QComboView::childCount() const
191{
192 return d->listView()->childCount();
193}
194
195
200void QComboView::clear()
201{
202 d->listView()->resize( 0, 0 );
203 d->listView()->clear();
204
205 d->current = 0;
206 if ( d->ed ) {
207 d->ed->setText( TQString::fromLatin1("") );
208 d->updateLinedGeometry();
209 }
210 currentChanged();
211}
212
213TQListViewItem *QComboView::currentItem() const
214{
215 return d->current;
216}
217
218void QComboView::setCurrentItem( TQListViewItem *item )
219{
220 if ( item == d->current && !d->ed ) {
221 return;
222 }
223
224 if (!item)
225 {
226 d->current = 0;
227 if ( d->ed ) {
228// d->ed->setText( "" );
229 d->updateLinedGeometry();
230 }
231 return;
232 }
233
234 d->current = item;
235 d->completeAt = 0;
236 if ( d->ed ) {
237 d->ed->setText( item->text(0) );
238// tqWarning("setCurrentItem( %s )", item->text(0).latin1());
239 d->updateLinedGeometry();
240 }
241 if ( d->listView() ) {
242 d->listView()->setCurrentItem( item );
243 } else {
244 internalHighlight( item );
245 // internalActivate( item ); ### this leads to weird behavior, as in 3.0.1
246 }
247
248 currentChanged();
249
250 d->listView()->ensureItemVisible(item);
251}
252
253bool QComboView::autoResize() const
254{
255 return d->autoresize;
256}
257
258void QComboView::setAutoResize( bool enable )
259{
260 if ( (bool)d->autoresize != enable ) {
261 d->autoresize = enable;
262 if ( enable )
263 adjustSize();
264 }
265}
266
267
275TQSize QComboView::sizeHint() const
276{
277 if ( isVisible() && d->sizeHint.isValid() )
278 return d->sizeHint;
279
280 constPolish();
281// int i, w;
282 TQFontMetrics fm = fontMetrics();
283
284 int maxW = childCount() ? 18 : 7 * fm.width(TQChar('x')) + 18;
285 int maxH = TQMAX( fm.lineSpacing(), 14 ) + 2;
286
287/* for( i = 0; i < count(); i++ ) {
288 w = d->listView()->item( i )->width( d->listView() );
289 if ( w > maxW )
290 maxW = w;
291 }
292*/
293 d->sizeHint = (style().sizeFromContents(TQStyle::CT_ComboBox, this,
294 TQSize(maxW, maxH)).expandedTo(TQApplication::globalStrut()));
295
296 return d->sizeHint;
297}
298
299
306void QComboView::internalActivate( TQListViewItem * item )
307{
308 if (!item)
309 {
310 d->current = 0;
311 if ( d->ed ) {
312// d->ed->setText( "" );
313 d->updateLinedGeometry();
314 }
315 return;
316 }
317 popDownListView();
318 d->poppedUp = false;
319
320 d->current = item;
321
322 TQString t( item->text(0) );
323 if ( d->ed ) {
324 d->ed->setText( t );
325// tqWarning("internalActivate( %s )", item->text(0).latin1());
326 d->updateLinedGeometry();
327 }
328 emit activated( item );
329 emit activated( t );
330
331// item->setOpen(true);
332}
333
340void QComboView::internalHighlight( TQListViewItem * item )
341{
342 if (!item)
343 {
344 d->current = 0;
345 if ( d->ed ) {
346// d->ed->setText( "" );
347 d->updateLinedGeometry();
348 }
349 return;
350 }
351 emit highlighted( item );
352 TQString t = item->text(0);
353 if ( !t.isNull() )
354 emit highlighted( t );
355}
356
362void QComboView::internalClickTimeout()
363{
364 d->shortClick = false;
365}
366
372void QComboView::setPalette( const TQPalette &palette )
373{
374 TQWidget::setPalette( palette );
375 if( d ) {
376 if(d->listView())
377 d->listView()->setPalette( palette );
378 }
379}
380
386void QComboView::setFont( const TQFont &font )
387{
388 d->sizeHint = TQSize(); // invalidate size hint
389 TQWidget::setFont( font );
390 d->listView()->setFont( font );
391 if (d->ed)
392 d->ed->setFont( font );
393 if ( d->autoresize )
394 adjustSize();
395}
396
397
401void QComboView::resizeEvent( TQResizeEvent * e )
402{
403 if ( d->ed )
404 d->updateLinedGeometry();
405 d->listView()->resize( width(), d->listView()->height() );
406 TQWidget::resizeEvent( e );
407}
408
412void QComboView::paintEvent( TQPaintEvent * )
413{
414 TQPainter p( this );
415 const TQColorGroup & g = colorGroup();
416 p.setPen(g.text());
417
418 TQStyle::SFlags flags = TQStyle::Style_Default;
419 if (isEnabled())
420 flags |= TQStyle::Style_Enabled;
421 if (hasFocus())
422 flags |= TQStyle::Style_HasFocus;
423
424 if ( width() < 5 || height() < 5 ) {
425 qDrawShadePanel( &p, rect(), g, false, 2,
426 &g.brush( TQColorGroup::Button ) );
427 return;
428 }
429
430// bool reverse = TQApplication::reverseLayout();
431 style().drawComplexControl( TQStyle::CC_ComboBox, &p, this, rect(), g,
432 flags, TQStyle::SC_All,
433 (d->arrowDown ?
434 TQStyle::SC_ComboBoxArrow :
435 TQStyle::SC_None ));
436
437 TQRect re = style().querySubControlMetrics( TQStyle::CC_ComboBox, this,
438 TQStyle::SC_ComboBoxEditField );
439 re = TQStyle::visualRect(re, this);
440 p.setClipRect( re );
441
442 if ( !d->ed ) {
443 TQListViewItem * item = d->current;
444 if ( item ) {
445 // we calculate the TQListBoxTexts height (ignoring strut)
446 int itemh = d->listView()->fontMetrics().lineSpacing() + 2;
447 p.translate( re.x(), re.y() + (re.height() - itemh)/2 );
448 item->paintCell( &p, d->listView()->colorGroup(), 0, width(), AlignLeft | AlignVCenter );
449 }
450 } else if ( d->listView() && d->listView()->currentItem( ) && d->current ) {
451 TQListViewItem * item = d->current ;
452 const TQPixmap *pix = item->pixmap(0);
453 if ( pix ) {
454 p.fillRect( re.x(), re.y(), pix->width() + 4, re.height(),
455 colorGroup().brush( TQColorGroup::Base ) );
456 p.drawPixmap( re.x() + 2, re.y() +
457 ( re.height() - pix->height() ) / 2, *pix );
458 }
459 }
460 p.setClipping( false );
461}
462
463
467void QComboView::mousePressEvent( TQMouseEvent *e )
468{
469 if ( e->button() != TQt::LeftButton )
470 return;
471 if ( d->discardNextMousePress ) {
472 d->discardNextMousePress = false;
473 return;
474 }
475 TQRect arrowRect = style().querySubControlMetrics( TQStyle::CC_ComboBox, this,
476 TQStyle::SC_ComboBoxArrow);
477 arrowRect = TQStyle::visualRect(arrowRect, this);
478
479 // Correction for motif style, where arrow is smaller
480 // and thus has a rect that doesn't fit the button.
481 arrowRect.setHeight( TQMAX( height() - (2 * arrowRect.y()), arrowRect.height() ) );
482
483 if ( childCount() && ( !editable() || arrowRect.contains( e->pos() ) ) ) {
484 d->arrowPressed = false;
485 listView()->blockSignals( true );
486 tqApp->sendEvent( listView(), e ); // trigger the listbox's autoscroll
487 listView()->blockSignals( false );
488 popup();
489 if ( arrowRect.contains( e->pos() ) ) {
490 d->arrowPressed = true;
491 d->arrowDown = true;
492 repaint( false );
493 }
494 TQTimer::singleShot( 200, this, TQ_SLOT(internalClickTimeout()));
495 d->shortClick = true;
496 }
497}
498
502void QComboView::mouseMoveEvent( TQMouseEvent * )
503{
504}
505
509void QComboView::mouseReleaseEvent( TQMouseEvent * )
510{
511}
512
516void QComboView::mouseDoubleClickEvent( TQMouseEvent *e )
517{
518 mousePressEvent( e );
519}
520
521
525void QComboView::keyPressEvent( TQKeyEvent *e )
526{
527 TQListViewItem *c = currentItem();
528 if ( ( e->key() == Key_F4 && e->state() == 0 ) ||
529 ( e->key() == Key_Down && (e->state() & AltButton) ) ||
530 ( !d->ed && e->key() == Key_Space ) ) {
531 if ( childCount() ) {
532 popup();
533 }
534 return;
535 } else if ( e->key() == Key_Up ) {
536/* if ((!c) && (listView()->firstChild()))
537 setCurrentItem(listView()->firstChild());*/
538 if (c && c->itemAbove() )
539 setCurrentItem( c->itemAbove() );
540 else
541 return;
542 } else if ( e->key() == Key_Down ) {
543 if ((!c) && (listView()->firstChild()))
544 {
545 setCurrentItem(listView()->firstChild());
546 return;
547 }
548 if ( c && c->itemBelow() )
549 setCurrentItem( c->itemBelow() );
550 else
551 return;
552 } else if ( e->key() == Key_Home && ( !d->ed || !d->ed->hasFocus() ) ) {
553 if (listView()->firstChild())
554 setCurrentItem( listView()->firstChild() );
555 else
556 return;
557 } else if ( e->key() == Key_End && ( !d->ed || !d->ed->hasFocus() ) ) {
558 if (listView()->lastItem())
559 setCurrentItem( listView()->lastItem() );
560 else
561 return;
562 } else if ( !d->ed && e->ascii() >= 32 && !e->text().isEmpty() ) {
563 if ( !d->completionTimer->isActive() ) {
564 d->completeAt = 0;
565 c = completionIndex( e->text(), c->itemBelow() );
566 if ( c ) {
567 setCurrentItem( c );
568 d->completeAt = e->text().length();
569 }
570 else
571 return;
572 } else {
573 d->completionTimer->stop();
574 TQString ct = currentText().left( d->completeAt ) + e->text();
575 c = completionIndex( ct, c );
576 if ( c == 0 && d->completeAt > 0 ) {
577 c = completionIndex( e->text(), listView()->firstChild() );
578 ct = e->text();
579 }
580 d->completeAt = 0;
581 if ( c ) {
582 setCurrentItem( c );
583 d->completeAt = ct.length();
584 }
585 else
586 return;
587 }
588 d->completionTimer->start( 400, true );
589 } else {
590 e->ignore();
591 return;
592 }
593
594 c = currentItem();
595 if ( childCount() && c && !c->text(0).isNull() )
596 emit activated( c->text(0) );
597 emit activated( c );
598}
599
600TQString QComboView::currentText() const
601{
602 if ( d->ed )
603 return d->ed->text();
604 else if ( d->current )
605 return currentItem()->text(0);
606 else
607 return TQString();
608}
609
613void QComboView::focusInEvent( TQFocusEvent * e )
614{
615 TQWidget::focusInEvent( e );
616 d->completeNow = false;
617 d->completeAt = 0;
618
619 emit focusGranted();
620}
621
625void QComboView::focusOutEvent( TQFocusEvent * e )
626{
627 TQWidget::focusOutEvent( e );
628 d->completeNow = false;
629 d->completeAt = 0;
630
631 emit focusLost();
632}
633
637void QComboView::wheelEvent( TQWheelEvent *e )
638{
639 if ( d->poppedUp ) {
640 TQApplication::sendEvent( d->listView(), e );
641 } else {
642 if ( e->delta() > 0 ) {
643 TQListViewItem *c = currentItem();
644 if ( c && c->itemAbove() ) {
645 setCurrentItem( c->itemAbove() );
646 emit activated( currentItem() );
647 emit activated( currentText() );
648 }
649 } else {
650 TQListViewItem *c = currentItem();
651 if ( c && c->itemBelow() ) {
652 setCurrentItem( c->itemBelow() );
653 emit activated( currentItem() );
654 emit activated( currentText() );
655 }
656 }
657 e->accept();
658 }
659}
660
661int childCount(TQListViewItem *it)
662{
663 int count = 1;
664 TQListViewItem * myChild = it->firstChild();
665 while( myChild ) {
666 count += childCount(myChild);
667 myChild = myChild->nextSibling();
668 }
669 return count;
670}
671
672int childCount(TQListView *lv)
673{
674 int count = 0;
675 TQListViewItem * myChild = lv->firstChild();
676 while( myChild ) {
677 count += childCount(myChild);
678// count += 1;
679 myChild = myChild->nextSibling();
680 }
681 return count;
682}
683
689static int listHeight( TQListView *l, int /*sl*/ )
690{
691/* if ( l->childCount() > 0 )
692 return TQMIN( l->childCount(), (uint)sl) * l->firstChild()->height();
693 else*/
694
695 int prefH = 0;
696 int ch = childCount(l);
697 ch = TQMIN(ch, 10);
698 if (l->firstChild())
699 {
700 prefH = ch * l->firstChild()->height();
701 }
702 else
703 prefH = l->sizeHint().height();
704
705 if (l->header()->isVisible())
706 prefH += l->header()->sizeHint().height();
707
708// return prefH < l->sizeHint().height() ? prefH : l->sizeHint().height();
709
710 return prefH+2;
711}
712
719void QComboView::popup()
720{
721 if ( !childCount() )
722 return;
723
724 // Send all listbox events to eventFilter():
725 TQListView* lb = d->listView();
726 lb->triggerUpdate( );
727 lb->installEventFilter( this );
728 lb->viewport()->installEventFilter( this );
729 d->mouseWasInsidePopup = false;
730// int w = lb->variableWidth() ? lb->sizeHint().width() : width();
731 int w = width();
732 int h = listHeight( lb, d->sizeLimit );
733 TQRect screen = TQApplication::desktop()->availableGeometry( const_cast<QComboView*>(this) );
734
735 int sx = screen.x(); // screen pos
736 int sy = screen.y();
737 int sw = screen.width(); // screen width
738 int sh = screen.height(); // screen height
739 TQPoint pos = mapToGlobal( TQPoint(0,height()) );
740 // ## Similar code is in TQPopupMenu
741 int x = pos.x();
742 int y = pos.y();
743
744 // the complete widget must be visible
745 if ( x + w > sx + sw )
746 x = sx+sw - w;
747 if ( x < sx )
748 x = sx;
749 if (y + h > sy+sh && y - h - height() >= 0 )
750 y = y - h - height();
751 TQRect rect =
752 style().querySubControlMetrics( TQStyle::CC_ComboBox, this,
753 TQStyle::SC_ComboBoxListBoxPopup,
754 TQStyleOption( x, y, w, h ) );
755 if ( rect.isNull() )
756 rect.setRect( x, y, w, h );
757 lb->setGeometry( rect );
758
759 lb->raise();
760 bool block = lb->signalsBlocked();
761 lb->blockSignals( true );
762 TQListViewItem *currentLBItem = d->current ;
763 lb->setCurrentItem( currentLBItem );
764 // set the current item to also be the selected item if it isn't already
765 if ( currentLBItem && currentLBItem->isSelectable() && !currentLBItem->isSelected() )
766 lb->setSelected( currentLBItem, true );
767 lb->blockSignals( block );
768 lb->setVScrollBarMode(TQScrollView::Auto);
769
770//#ifndef TQT_NO_EFFECTS
771/* if ( TQApplication::isEffectEnabled( UI_AnimateCombo ) ) {
772 if ( lb->y() < mapToGlobal(TQPoint(0,0)).y() )
773 qScrollEffect( lb, TQEffects::UpScroll );
774 else
775 qScrollEffect( lb );
776 } else*/
777//#endif
778 lb->show();
779 d->poppedUp = true;
780}
781
782
786void QComboView::updateMask()
787{
788 TQBitmap bm( size() );
789 bm.fill( color0 );
790
791 {
792 TQPainter p( &bm, this );
793 style().drawComplexControlMask(TQStyle::CC_ComboBox, &p, this, rect());
794 }
795
796 setMask( bm );
797}
798
803void QComboView::popDownListView()
804{
805 d->listView()->removeEventFilter( this );
806 d->listView()->viewport()->removeEventFilter( this );
807 d->listView()->hide();
808 d->listView()->setCurrentItem( d->current );
809 if ( d->arrowDown ) {
810 d->arrowDown = false;
811 repaint( false );
812 }
813 d->poppedUp = false;
814}
815
816
822void QComboView::reIndex()
823{
824}
825
831void QComboView::currentChanged()
832{
833 if ( d->autoresize )
834 adjustSize();
835 update();
836}
837
849bool QComboView::eventFilter( TQObject *object, TQEvent *event )
850{
851 if ( !event )
852 return true;
853 else if ( object == d->ed ) {
854 if ( event->type() == TQEvent::KeyPress ) {
855 bool isAccepted = ( (TQKeyEvent*)event )->isAccepted();
856 keyPressEvent( (TQKeyEvent *)event );
857 if ( ((TQKeyEvent *)event)->isAccepted() ) {
858 d->completeNow = false;
859 return true;
860 } else if ( ((TQKeyEvent *)event)->key() != Key_End ) {
861 d->completeNow = true;
862 d->completeAt = d->ed->cursorPosition();
863 }
864 if ( isAccepted )
865 ( (TQKeyEvent*)event )->accept();
866 else
867 ( (TQKeyEvent*)event )->ignore();
868 } else if ( event->type() == TQEvent::KeyRelease ) {
869 d->completeNow = false;
870 keyReleaseEvent( (TQKeyEvent *)event );
871 return ((TQKeyEvent *)event)->isAccepted();
872 } else if ( event->type() == TQEvent::FocusIn ) {
873 focusInEvent( (TQFocusEvent *)event );
874 } else if ( event->type() == TQEvent::FocusOut ) {
875 focusOutEvent( (TQFocusEvent *)event );
876 } else if ( d->useCompletion && d->completeNow ) {
877 if ( !d->ed->text().isNull() &&
878 d->ed->cursorPosition() > d->completeAt &&
879 d->ed->cursorPosition() == (int)d->ed->text().length() ) {
880 d->completeNow = false;
881 TQString ct( d->ed->text() );
882 TQListViewItem *i = completionIndex( ct, currentItem() );
883 if ( i ) {
884 TQString it = i->text(0);
885 d->ed->validateAndSet( it, ct.length(),
886 ct.length(), it.length() );
887 }
888 }
889 }
890 } else if ( ( object == d->listView() ||
891 object == d->listView()->viewport() )) {
892 TQMouseEvent *e = (TQMouseEvent*)event;
893 switch( event->type() ) {
894 case TQEvent::MouseMove:
895 if ( !d->mouseWasInsidePopup ) {
896// tqWarning("!d->mouseWasInsidePopup");
897 TQPoint pos = e->pos();
898 if ( d->listView()->rect().contains( pos ) )
899 d->mouseWasInsidePopup = true;
900 // Check if arrow button should toggle
901 if ( d->arrowPressed ) {
902 TQPoint comboPos;
903 comboPos = mapFromGlobal( d->listView()->mapToGlobal(pos) );
904 TQRect arrowRect =
905 style().querySubControlMetrics( TQStyle::CC_ComboBox, this,
906 TQStyle::SC_ComboBoxArrow);
907 arrowRect = TQStyle::visualRect(arrowRect, this);
908 if ( arrowRect.contains( comboPos ) ) {
909 if ( !d->arrowDown ) {
910 d->arrowDown = true;
911 repaint( false );
912 }
913 } else {
914 if ( d->arrowDown ) {
915 d->arrowDown = false;
916 repaint( false );
917 }
918 }
919 }
920 } else if ((e->state() & ( TQt::RightButton | TQt::LeftButton | TQt::MidButton ) ) == 0 &&
921 style().styleHint(TQStyle::SH_ComboBox_ListMouseTracking, this)) {
922// tqWarning("event filter:: emu");
923 TQWidget *mouseW = TQApplication::widgetAt( e->globalPos(), true );
924// if ( mouseW == d->listView()->viewport() ) { //###
925 if ( mouseW == d->listView()->viewport() ) {
926 TQListViewItem *sel = d->listView()->itemAt(e->pos());
927 if (sel)
928 {
929 d->listView()->setCurrentItem(sel);
930 d->listView()->setSelected(sel, true);
931 }
932 return true;
933 }
934 }
935
936 break;
937 case TQEvent::MouseButtonRelease:
938 if ( d->listView()->rect().contains( e->pos() ) ) {
939 TQMouseEvent tmp( TQEvent::MouseButtonDblClick,
940 e->pos(), e->button(), e->state() ) ;
941 // will hide popup
942 TQApplication::sendEvent( object, &tmp );
943 return true;
944 } else {
945 if ( d->mouseWasInsidePopup ) {
946 popDownListView();
947 } else {
948 d->arrowPressed = false;
949 if ( d->arrowDown ) {
950 d->arrowDown = false;
951 repaint( false );
952 }
953 }
954 }
955 break;
956 case TQEvent::MouseButtonDblClick:
957 case TQEvent::MouseButtonPress:
958 if ( !d->listView()->rect().contains( e->pos() ) ) {
959 TQPoint globalPos = d->listView()->mapToGlobal(e->pos());
960 if ( TQApplication::widgetAt( globalPos, true ) == this ) {
961 d->discardNextMousePress = true;
962 // avoid popping up again
963 }
964 popDownListView();
965 return true;
966 }
967 break;
968 case TQEvent::KeyPress:
969 switch( ((TQKeyEvent *)event)->key() ) {
970 case Key_Up:
971 case Key_Down:
972 if ( !(((TQKeyEvent *)event)->state() & AltButton) )
973 break;
974 case Key_F4:
975 case Key_Escape:
976 if ( d->poppedUp ) {
977 popDownListView();
978 return true;
979 }
980 break;
981 case Key_Enter:
982 case Key_Return:
983 // work around TQDialog's enter handling
984 return false;
985 default:
986 break;
987 }
988 break;
989 case TQEvent::Hide:
990 popDownListView();
991 break;
992 default:
993 break;
994 }
995 }
996 return TQWidget::eventFilter( object, event );
997}
998
999
1006TQListViewItem *QComboView::completionIndex( const TQString & prefix,
1007 TQListViewItem *startingAt ) const
1008{
1009 TQListViewItem *start = startingAt;
1010/* if ( start < 0 || start >= count() )
1011 start = 0;
1012 if ( start >= count() )
1013 return -1;*/
1014 if (!start)
1015 start = listView()->firstChild();
1016 if (!start)
1017 return 0;
1018/* if (!start->itemBelow())
1019 return 0;*/
1020 TQString match = prefix.lower();
1021 if ( match.length() < 1 )
1022 return start;
1023
1024 TQString current;
1025 TQListViewItem *i = start;
1026 do {
1027 current = i->text(0).lower();
1028 if ( current.startsWith( match ) )
1029 return i;
1030 i = i->itemBelow();
1031 if ( i )
1032 i = listView()->firstChild();
1033 } while ( i != start );
1034 return 0;
1035}
1036
1037
1038int QComboView::sizeLimit() const
1039{
1040 return d ? d->sizeLimit : INT_MAX;
1041}
1042
1043void QComboView::setSizeLimit( int lines )
1044{
1045 d->sizeLimit = lines;
1046}
1047
1048
1049/*int QComboView::maxCount() const
1050{
1051 return d ? d->maxCount : INT_MAX;
1052}
1053
1054void QComboView::setMaxCount( int count )
1055{
1056 int l = this->count();
1057 while( --l > count )
1058 removeItem( l );
1059 d->maxCount = count;
1060}
1061*/
1062QComboView::Policy QComboView::insertionPolicy() const
1063{
1064 return d->p;
1065}
1066
1067void QComboView::setInsertionPolicy( Policy policy )
1068{
1069 d->p = policy;
1070}
1071
1072
1073
1078void QComboView::returnPressed()
1079{
1080 TQString s( d->ed->text() );
1081
1082 if ( s.isEmpty() )
1083 return;
1084
1085 TQListViewItem *c = 0;
1086 bool doInsert = true;
1087 if ( !d->duplicatesEnabled ) {
1088 c = listView()->findItem(s, 0);
1089 if ( c )
1090 doInsert = false;
1091 }
1092
1093 if ( doInsert ) {
1094 if ( insertionPolicy() != NoInsertion ) {
1095/* int cnt = count();
1096 while ( cnt >= d->maxCount ) {
1097 removeItem( --cnt );
1098 }*/
1099 }
1100
1101 switch ( insertionPolicy() ) {
1102 case AtCurrent:
1103 if ( s != currentItem()->text(0) )
1104 currentItem()->setText(0, s);
1105 emit activated( currentItem() );
1106 emit activated( s );
1107 return;
1108 case NoInsertion:
1109 emit activated( s );
1110 return;
1111 case AtTop:
1112 c = 0;
1113 return;
1114// break;
1115 case AtBottom:
1116 c = new TQListViewItem(listView(), listView()->lastItem(), s);
1117 break;
1118 case BeforeCurrent:
1119 if (currentItem() && currentItem()->itemAbove())
1120 c = new TQListViewItem(listView(), currentItem()->itemAbove(), s);
1121 else
1122 {
1123 c = 0;
1124 return;
1125 }
1126 break;
1127 case AfterCurrent:
1128 if (currentItem() && currentItem()->itemBelow())
1129 c = new TQListViewItem(listView(), currentItem()->itemBelow(), s);
1130 else
1131 {
1132 c = 0;
1133 return;
1134 }
1135 break;
1136 }
1137 }
1138
1139 if (c)
1140 {
1141 setCurrentItem( c );
1142 emit activated( c );
1143 emit activated( s );
1144 }
1145}
1146
1147
1151void QComboView::setEnabled( bool enable )
1152{
1153 TQWidget::setEnabled( enable );
1154}
1155
1156
1157
1167void QComboView::setValidator( const TQValidator * v )
1168{
1169 if ( d && d->ed )
1170 d->ed->setValidator( v );
1171}
1172
1173
1181const TQValidator * QComboView::validator() const
1182{
1183 return d && d->ed ? d->ed->validator() : 0;
1184}
1185
1186
1191void QComboView::clearValidator()
1192{
1193 if ( d && d->ed )
1194 d->ed->setValidator( 0 );
1195}
1196
1197
1208void QComboView::setListView( TQListView * newListView )
1209{
1210 clear();
1211
1212 delete d->listView();
1213
1214 newListView->reparent( this, WType_Popup, TQPoint(0,0), false );
1215 d->setListView( newListView );
1216 d->listView()->setFont( font() );
1217 d->listView()->setPalette( palette() );
1218/* d->listView()->setVScrollBarMode(TQScrollView::AlwaysOff);
1219 d->listView()->setHScrollBarMode(TQScrollView::AlwaysOff);*/
1220 d->listView()->setFrameStyle( TQFrame::Box | TQFrame::Plain );
1221 d->listView()->setLineWidth( 1 );
1222/* d->listView()->setRootIsDecorated( true );
1223 d->listView()->setAllColumnsShowFocus(true);*/
1224 d->listView()->resize( 100, 10 );
1225
1226 if (d->listView()->firstChild())
1227 d->current = d->listView()->firstChild();
1228
1229// d->listView()->header()->hide();
1230
1231
1232/* d->listView()->setFont( font() );
1233 d->listView()->setPalette( palette() );
1234 d->listView()->setVScrollBarMode( TQScrollView::AlwaysOff );
1235 d->listView()->setHScrollBarMode( TQScrollView::AlwaysOff );
1236 d->listView()->setFrameStyle( TQFrame::Box | TQFrame::Plain );
1237 d->listView()->setLineWidth( 1 );
1238 d->listView()->setRootIsDecorated( true );
1239 d->listView()->setAllColumnsShowFocus(true);
1240 d->listView()->addColumn("");
1241 d->listView()->resize( 100, 10 );
1242*/
1243
1244 connect( d->listView(), TQ_SIGNAL(returnPressed(TQListViewItem*)),
1245 TQ_SLOT(internalActivate(TQListViewItem*)));
1246 connect( d->listView(), TQ_SIGNAL(doubleClicked(TQListViewItem*)),
1247 TQ_SLOT(internalActivate(TQListViewItem*)));
1248 connect( d->listView(), TQ_SIGNAL(doubleClicked(TQListViewItem*)),
1249 TQ_SLOT(checkState(TQListViewItem*)));
1250 connect( d->listView(), TQ_SIGNAL(currentChanged(TQListViewItem*)),
1251 TQ_SLOT(internalHighlight(TQListViewItem*)));
1252 connect( d->listView(), TQ_SIGNAL(selectionChanged(TQListViewItem*)),
1253 TQ_SLOT(internalHighlight(TQListViewItem*)));
1254}
1255
1256
1265TQListView * QComboView::listView() const
1266{
1267 return d ? d->listView() : 0;
1268}
1269
1275TQLineEdit* QComboView::lineEdit() const
1276{
1277 return d->ed;
1278}
1279
1280
1281
1294void QComboView::clearEdit()
1295{
1296 if ( d && d->ed )
1297 d->ed->clear();
1298}
1299
1300
1312void QComboView::setEditText( const TQString &newText )
1313{
1314 if ( d && d->ed ) {
1315 d->updateLinedGeometry();
1316 d->ed->setText( newText );
1317 }
1318}
1319
1320void QComboView::setAutoCompletion( bool enable )
1321{
1322 d->useCompletion = enable;
1323 d->completeNow = false;
1324}
1325
1326
1327bool QComboView::autoCompletion() const
1328{
1329 return d->useCompletion;
1330}
1331
1334void QComboView::styleChange( TQStyle& s )
1335{
1336 d->sizeHint = TQSize(); // invalidate size hint...
1337 if ( d->ed )
1338 d->updateLinedGeometry();
1339 TQWidget::styleChange( s );
1340}
1341
1342bool QComboView::editable() const
1343{
1344 return d->ed != 0;
1345}
1346
1347void QComboView::setEditable( bool y )
1348{
1349 if ( y == editable() )
1350 return;
1351 if ( y ) {
1352 setUpListView();
1353 setUpLineEdit();
1354 d->ed->show();
1355 if ( currentItem() )
1356 setEditText( currentText() );
1357 } else {
1358 delete d->ed;
1359 d->ed = 0;
1360 }
1361
1362 setFocusPolicy( TQWidget::StrongFocus );
1363 updateGeometry();
1364 update();
1365}
1366
1367
1368void QComboView::setUpListView()
1369{
1370 d->setListView( new TQListView( this, "in-combo", WType_Popup ) );
1371
1372 d->listView()->setFont( font() );
1373 d->listView()->setPalette( palette() );
1374/* d->listView()->setVScrollBarMode( TQScrollView::AlwaysOff );
1375 d->listView()->setHScrollBarMode( TQScrollView::AlwaysOff );*/
1376 d->listView()->setFrameStyle( TQFrame::Box | TQFrame::Plain );
1377 d->listView()->setLineWidth( 1 );
1378 d->listView()->setRootIsDecorated( false );
1379 d->listView()->setAllColumnsShowFocus(true);
1380 d->listView()->addColumn("");
1381 d->listView()->resize( 100, 10 );
1382 d->listView()->setResizeMode(TQListView::LastColumn);
1383
1384 if (d->listView()->firstChild())
1385 d->current = d->listView()->firstChild();
1386
1387 d->listView()->header()->hide();
1388
1389 connect( d->listView(), TQ_SIGNAL(returnPressed(TQListViewItem*)),
1390 TQ_SLOT(internalActivate(TQListViewItem*)));
1391 connect( d->listView(), TQ_SIGNAL(doubleClicked(TQListViewItem*)),
1392 TQ_SLOT(internalActivate(TQListViewItem*)));
1393 connect( d->listView(), TQ_SIGNAL(doubleClicked(TQListViewItem*)),
1394 TQ_SLOT(checkState(TQListViewItem*)));
1395 connect( d->listView(), TQ_SIGNAL(currentChanged(TQListViewItem*)),
1396 TQ_SLOT(internalHighlight(TQListViewItem*)));
1397 connect( d->listView(), TQ_SIGNAL(selectionChanged(TQListViewItem*)),
1398 TQ_SLOT(internalHighlight(TQListViewItem*)));
1399}
1400
1401
1402void QComboView::setUpLineEdit()
1403{
1404 if ( !d->ed )
1405 setLineEdit( new TQLineEdit( this, "combo edit" ) );
1406}
1407
1412void QComboView::setLineEdit( TQLineEdit *edit )
1413{
1414 if ( !edit ) {
1415#if defined(TQT_CHECK_NULL)
1416 Q_ASSERT( edit != 0 );
1417#endif
1418 return;
1419 }
1420
1421 edit->setText( currentText() );
1422 if ( d->ed ) {
1423 int start = 0, end = 0;
1424 d->ed->getSelection( &start, &end );
1425 edit->setSelection( start, end );
1426 edit->setCursorPosition( d->ed->cursorPosition() );
1427 edit->setEdited( d->ed->edited() );
1428 delete d->ed;
1429 }
1430
1431 d->ed = edit;
1432
1433 if ( edit->parent() != this ) {
1434 edit->reparent( this, TQPoint(0,0), false );
1435 edit->setFont( font() );
1436 }
1437
1438 connect (edit, TQ_SIGNAL( textChanged( const TQString& ) ),
1439 this, TQ_SIGNAL( textChanged( const TQString& ) ) );
1440 connect( edit, TQ_SIGNAL(returnPressed()), TQ_SLOT(returnPressed()) );
1441
1442 edit->setFrame( false );
1443 d->updateLinedGeometry();
1444 edit->installEventFilter( this );
1445 setFocusProxy( edit );
1446 setFocusPolicy( TQWidget::StrongFocus );
1447
1448 setUpListView();
1449
1450 if ( isVisible() )
1451 edit->show();
1452
1453 updateGeometry();
1454 update();
1455}
1456
1457void QComboView::setCurrentText( const TQString& txt )
1458{
1459 TQListViewItem *i;
1460 i = listView()->findItem(txt, 0);
1461 if ( i )
1462 setCurrentItem( i );
1463 else if ( d->ed )
1464 d->ed->setText( txt );
1465 else if (currentItem())
1466 currentItem()->setText(0, txt);
1467}
1468
1469void QComboView::checkState( TQListViewItem * item)
1470{
1471 item->setOpen(!item->isOpen());
1472}
1473
1474void QComboView::setCurrentActiveItem( TQListViewItem * item )
1475{
1476 if ( item == d->current && !d->ed ) {
1477 return;
1478 }
1479
1480 d->current = item;
1481 d->completeAt = 0;
1482 if ( d->ed ) {
1483 d->ed->setText( item->text(0) );
1484 d->ed->setCursorPosition(0);
1485// tqWarning("setCurrentActiveItem( %s )", item->text(0).latin1());
1486 d->updateLinedGeometry();
1487 }
1488 if ( d->listView() ) {
1489 d->listView()->setCurrentItem( item );
1490 emit activated( item );
1491 emit activated( item->text(0) );
1492 } else {
1493 internalHighlight( item );
1494 internalActivate( item );
1495 }
1496
1497 currentChanged();
1498
1499 d->listView()->ensureItemVisible(item);
1500}
1501
1502#include "qcomboview.moc"
1503
1504#endif // TQT_NO_COMBOBOX
1505
QComboView
QComboView - a combo with a TQListView as a popup widget.
Definition qcomboview.h:42
QComboView::setPalette
void setPalette(const TQPalette &)
Definition qcomboview.cpp:372
QComboView::focusInEvent
void focusInEvent(TQFocusEvent *e)
Definition qcomboview.cpp:613
QComboView::popup
virtual void popup()
Definition qcomboview.cpp:719
QComboView::setEditText
virtual void setEditText(const TQString &)
Definition qcomboview.cpp:1312
QComboView::wheelEvent
void wheelEvent(TQWheelEvent *e)
Definition qcomboview.cpp:637
QComboView::eventFilter
bool eventFilter(TQObject *object, TQEvent *event)
Definition qcomboview.cpp:849
QComboView::lineEdit
TQLineEdit * lineEdit() const
Definition qcomboview.cpp:1275
QComboView::mousePressEvent
void mousePressEvent(TQMouseEvent *)
Definition qcomboview.cpp:467
QComboView::updateMask
void updateMask()
Definition qcomboview.cpp:786
QComboView::sizeHint
TQSize sizeHint() const
Definition qcomboview.cpp:275
QComboView::clearValidator
void clearValidator()
Definition qcomboview.cpp:1191
QComboView::mouseDoubleClickEvent
void mouseDoubleClickEvent(TQMouseEvent *)
Definition qcomboview.cpp:516
QComboView::setEnabled
void setEnabled(bool)
Definition qcomboview.cpp:1151
QComboView::~QComboView
~QComboView()
Definition qcomboview.cpp:175
QComboView::setFont
void setFont(const TQFont &)
Definition qcomboview.cpp:386
QComboView::setListView
virtual void setListView(TQListView *)
Definition qcomboview.cpp:1208
QComboView::setLineEdit
virtual void setLineEdit(TQLineEdit *edit)
Definition qcomboview.cpp:1412
QComboView::focusOutEvent
void focusOutEvent(TQFocusEvent *e)
Definition qcomboview.cpp:625
QComboView::setValidator
virtual void setValidator(const TQValidator *)
Definition qcomboview.cpp:1167
QComboView::QComboView
QComboView(bool rw, TQWidget *parent=0, const char *name=0)
Definition qcomboview.cpp:141
QComboView::styleChange
void styleChange(TQStyle &)
Definition qcomboview.cpp:1334
QComboView::clear
virtual void clear()
Definition qcomboview.cpp:200
QComboView::validator
const TQValidator * validator() const
Definition qcomboview.cpp:1181
QComboView::listView
TQListView * listView() const
Definition qcomboview.cpp:1265
QComboView::clearEdit
void clearEdit()
Definition qcomboview.cpp:1294
QComboView::keyPressEvent
void keyPressEvent(TQKeyEvent *e)
Definition qcomboview.cpp:525
QComboView::resizeEvent
void resizeEvent(TQResizeEvent *)
Definition qcomboview.cpp:401
QComboView::paintEvent
void paintEvent(TQPaintEvent *)
Definition qcomboview.cpp:412
QComboView::mouseReleaseEvent
void mouseReleaseEvent(TQMouseEvent *)
Definition qcomboview.cpp:509
QComboView::mouseMoveEvent
void mouseMoveEvent(TQMouseEvent *)
Definition qcomboview.cpp:502
qcomboview.h
QComboView class.

KDevelop Widgets Library

Skip menu "KDevelop Widgets Library"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

KDevelop Widgets Library

Skip menu "KDevelop Widgets Library"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for KDevelop Widgets Library by doxygen 1.9.8
This website is maintained by Timothy Pearson.