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