27 #include <tqobjectlist.h>
29 #include <tqsplitter.h>
30 #include <tqtabwidget.h>
32 #include <tqwidgetstack.h>
33 #include <tqpainter.h>
36 #include <tdeapplication.h>
39 #include <tdeglobal.h>
40 #include <tdeglobalsettings.h>
41 #include <kseparator.h>
43 #include "kjanuswidget.h"
44 #include <tdelistview.h>
45 #include "kpushbutton.h"
48 class KJanusWidget::IconListItem :
public TQListBoxItem
51 IconListItem( TQListBox *listbox,
const TQPixmap &pixmap,
52 const TQString &text );
53 virtual int height(
const TQListBox *lb )
const;
54 virtual int width(
const TQListBox *lb )
const;
55 int expandMinimumWidth(
int width );
56 void highlight(
bool erase );
59 const TQPixmap &defaultPixmap();
60 void paint( TQPainter *painter );
63 void paintContents( TQPainter *painter );
69 class KJanusWidget::KJanusWidgetPrivate
72 KJanusWidgetPrivate() : mNextPageIndex(0), mListFrame( 0 ) { }
77 TQMap<int,TQWidget*> mIntToPage;
79 TQMap<TQWidget*,int> mPageToInt;
81 TQMap<int, TQString> mIntToTitle;
83 TQWidget * mListFrame;
84 TQSplitter * mSplitter;
87 template class TQPtrList<TQListViewItem>;
91 : TQWidget( parent, name, 0 ),
92 mValid(false), mPageList(0),
93 mTitleList(0), mFace(face), mTitleLabel(0), mActivePageWidget(0),
94 mShowIconsInTreeList(false), d(0)
96 TQVBoxLayout *topLayout =
new TQVBoxLayout(
this );
100 d =
new KJanusWidgetPrivate;
106 d->mSplitter =
new TQSplitter(
this );
107 topLayout->addWidget( d->mSplitter, 10 );
108 mTreeListResizeMode = TQSplitter::KeepSize;
110 d->mListFrame =
new TQWidget( d->mSplitter );
112 dummy->setAutoAdd(
true );
115 mTreeList->header()->hide();
116 mTreeList->setRootIsDecorated(
true);
118 connect( mTreeList, TQ_SIGNAL(selectionChanged()), TQ_SLOT(slotShowPage()) );
119 connect( mTreeList, TQ_SIGNAL(clicked(TQListViewItem *)), TQ_SLOT(slotItemClicked(TQListViewItem *)));
125 TQFrame *p =
new TQFrame( d->mSplitter );
127 TQHBoxLayout *hbox =
new TQHBoxLayout( p, 0, 0 );
129 page =
new TQFrame( p );
130 hbox->addWidget( page, 10 );
134 TQHBoxLayout *hbox =
new TQHBoxLayout( topLayout );
135 d->mListFrame =
new TQWidget(
this );
136 hbox->addWidget( d->mListFrame );
138 (
new TQVBoxLayout( d->mListFrame, 0, 0 ) )->setAutoAdd(
true );
139 mIconList =
new IconListBox( d->mListFrame );
141 TQFont listFont( mIconList->font() );
142 listFont.setBold(
true );
143 mIconList->setFont( listFont );
145 mIconList->verticalScrollBar()->installEventFilter(
this );
146 connect( mIconList, TQ_SIGNAL(selectionChanged()), TQ_SLOT(slotShowPage()));
147 connect( mIconList, TQ_SIGNAL(onItem(TQListBoxItem *)), TQ_SLOT(slotOnItem(TQListBoxItem *)));
150 page =
new TQFrame(
this );
151 hbox->addWidget( page, 10 );
161 mTitleLabel =
new TQLabel( i18n(
"Empty Page"), page,
"KJanusWidgetTitleLabel" );
162 vbox->addWidget( mTitleLabel, 0, TQApplication::reverseLayout() ? AlignRight : AlignLeft );
164 TQFont titleFont( mTitleLabel->font() );
165 titleFont.setBold(
true );
166 mTitleLabel->setFont( titleFont );
169 mTitleSep->setFrameStyle( TQFrame::HLine|TQFrame::Plain );
170 vbox->addWidget( mTitleSep );
172 mPageStack =
new TQWidgetStack( page );
173 connect(mPageStack, TQ_SIGNAL(aboutToShow(TQWidget *)),
175 vbox->addWidget( mPageStack, 10 );
177 else if( mFace ==
Tabbed )
179 d =
new KJanusWidgetPrivate;
181 mTabControl =
new TQTabWidget(
this );
183 connect(mTabControl, TQ_SIGNAL(currentChanged(TQWidget *)),
185 topLayout->addWidget( mTabControl, 10 );
189 mSwallowPage =
new TQWidget(
this );
190 topLayout->addWidget( mSwallowPage, 10 );
195 mPlainPage =
new TQFrame(
this );
196 topLayout->addWidget( mPlainPage, 10 );
200 connect(kapp,TQ_SIGNAL(tdedisplayFontChanged()),TQ_SLOT(slotFontChanged()));
230 TQWidget *KJanusWidget::FindParent()
241 const TQPixmap &pixmap )
249 TQFrame *page =
new TQFrame( FindParent(),
"page" );
250 addPageWidget( page, items, header, pixmap );
255 void KJanusWidget::pageGone( TQObject *obj )
260 void KJanusWidget::slotReopen( TQListViewItem * item )
263 item->setOpen(
true );
267 const TQPixmap &pixmap )
271 return addPage(items, header, pixmap);
277 const TQString &header,
278 const TQPixmap &pixmap )
286 TQVBox *page =
new TQVBox(FindParent() ,
"page" );
288 addPageWidget( page, items, header, pixmap );
294 const TQString &header,
295 const TQPixmap &pixmap )
303 const TQString &header,
304 const TQPixmap &pixmap )
311 TQHBox *page =
new TQHBox(FindParent(),
"page");
313 addPageWidget( page, items, header, pixmap );
319 const TQString &header,
320 const TQPixmap &pixmap )
328 const TQStringList &items,
329 const TQString &header,
330 const TQPixmap &pixmap )
338 TQGrid *page =
new TQGrid( n, dir, FindParent(),
"page" );
340 addPageWidget( page, items, header, pixmap );
347 const TQString &itemName,
348 const TQString &header,
349 const TQPixmap &pixmap )
356 void KJanusWidget::InsertTreeListItem(
const TQStringList &items,
const TQPixmap &pixmap, TQFrame *page)
359 TQListViewItem *curTop = 0, *child, *last, *newChild;
360 unsigned int index = 1;
361 TQStringList curPath;
363 for ( TQStringList::ConstIterator it = items.begin(); it != items.end(); ++it, index++ ) {
364 TQString name = (*it);
365 bool isPath = ( index != items.count() );
369 child = mTreeList->firstChild();
372 child = curTop->firstChild();
377 for (last = 0; child && child->text(0) !=
name ; last = child, child = child->nextSibling());
379 if (!last && !child) {
383 newChild =
new TQListViewItem(mTreeList, name);
385 newChild =
new TQListViewItem(curTop, name);
391 kdDebug() <<
"The element inserted was already in the TreeList box!" <<
endl;
402 newChild =
new TQListViewItem(mTreeList, last, name);
404 newChild =
new TQListViewItem(curTop, last, name);
410 newChild->setExpandable(
true);
415 TQString
key = curPath.join(
"_/_");
416 if (mFolderIconMap.contains(key)) {
417 TQPixmap p = mFolderIconMap[
key];
418 newChild->setPixmap(0,p);
422 if (mShowIconsInTreeList) {
423 newChild->setPixmap(0, pixmap);
425 mTreeListToPageStack.insert(newChild, page);
430 void KJanusWidget::addPageWidget( TQFrame *page,
const TQStringList &items,
431 const TQString &header,
const TQPixmap &pixmap )
433 connect(page, TQ_SIGNAL(destroyed(TQObject*)), TQ_SLOT(pageGone(TQObject*)));
437 mTabControl->addTab (page, items.last());
438 d->mIntToPage[d->mNextPageIndex] = page;
439 d->mPageToInt[page] = d->mNextPageIndex;
444 d->mIntToPage[d->mNextPageIndex] = page;
445 d->mPageToInt[page] = d->mNextPageIndex;
446 mPageStack->addWidget( page, 0 );
448 if (items.isEmpty()) {
449 kdDebug() <<
"Invalid TQStringList, with zero items" <<
endl;
455 InsertTreeListItem(items, pixmap, page);
459 TQString itemName = items.last();
460 IconListItem *item =
new IconListItem( mIconList, pixmap, itemName );
461 mIconListToPageStack.insert(item, page);
462 mIconList->invalidateHeight();
463 mIconList->invalidateWidth();
465 if (mIconList->isVisible())
466 mIconList->updateWidth();
472 TQString lastName = items.last();
473 const TQString &title = (!header.isNull() ? header : lastName);
474 TQRect r = mTitleLabel->fontMetrics().boundingRect( title );
475 if( mTitleLabel->minimumWidth() < r.width() )
477 mTitleLabel->setMinimumWidth( r.width() );
479 d->mIntToTitle[d->mNextPageIndex] = title;
480 if( d->mIntToTitle.count() == 1 )
488 kdDebug() <<
"KJanusWidget::addPageWidget: can only add a page in Tabbed, TreeList or IconList modes" <<
endl;
495 TQString key = path.join(
"_/_");
496 mFolderIconMap.insert(key,pixmap);
503 if( mFace !=
Swallow || !mValid )
511 delete mSwallowPage->layout();
513 TQGridLayout *gbox =
new TQGridLayout( mSwallowPage, 1, 1, 0 );
518 TQObjectList l = mSwallowPage->childrenListObject();
519 for( uint i=0; i < l.count(); i++ )
521 TQObject *o = l.at(i);
522 if( o->isWidgetType() )
524 ((TQWidget*)o)->hide();
533 gbox->addRowSpacing(0,100);
534 gbox->addColSpacing(0,100);
535 mSwallowPage->setMinimumSize(100,100);
539 if( widget->parent() != mSwallowPage )
541 widget->reparent( mSwallowPage, 0, TQPoint(0,0) );
543 gbox->addWidget(widget, 0, 0 );
545 mSwallowPage->setMinimumSize( widget->minimumSize() );
551 bool KJanusWidget::slotShowPage()
560 TQListViewItem *node = mTreeList->selectedItem();
561 if( !node ) {
return false; }
563 TQWidget *stackItem = mTreeListToPageStack[node];
565 return showPage(d->mPageToInt[stackItem]);
569 TQListBoxItem *node = mIconList->item( mIconList->currentItem() );
570 if( !node ) {
return false; }
571 TQWidget *stackItem = mIconListToPageStack[node];
573 return showPage(d->mPageToInt[stackItem]);
588 return showPage(d->mIntToPage[index]);
602 mPageStack->raiseWidget( w );
603 mActivePageWidget = w;
605 int index = d->mPageToInt[w];
606 mTitleLabel->setText( d->mIntToTitle[index] );
609 TQMap<TQListViewItem *, TQWidget *>::Iterator it;
610 for (it = mTreeListToPageStack.begin(); it != mTreeListToPageStack.end(); ++it){
611 TQListViewItem *
key = it.key();
612 TQWidget *val = it.data();
614 mTreeList->setSelected(key,
true );
621 TQMap<TQListBoxItem *, TQWidget *>::Iterator it;
622 for (it = mIconListToPageStack.begin(); it != mIconListToPageStack.end(); ++it){
623 TQListBoxItem *
key = it.key();
624 TQWidget *val = it.data();
626 mIconList->setSelected( key,
true );
632 else if( mFace ==
Tabbed )
634 mTabControl->showPage(w);
635 mActivePageWidget = w;
649 TQListViewItem *node = mTreeList->selectedItem();
650 if( !node ) {
return -1; }
651 TQWidget *stackItem = mTreeListToPageStack[node];
652 return d->mPageToInt[stackItem];
655 TQListBoxItem *node = mIconList->item( mIconList->currentItem() );
656 if( !node ) {
return false; }
657 TQWidget *stackItem = mIconListToPageStack[node];
658 return d->mPageToInt[stackItem];
660 else if( mFace ==
Tabbed ) {
661 TQWidget *widget = mTabControl->currentPage();
662 return ( !widget ? -1 : d->mPageToInt[widget] );
678 return d->mPageToInt[widget];
680 else if( mFace ==
Tabbed )
687 if( widget->isA(
"TQFrame") )
689 return d->mPageToInt[widget->parentWidget()];
693 return d->mPageToInt[widget];
702 void KJanusWidget::slotFontChanged()
707 TQFont titleFont( mTitleLabel->font() );
708 titleFont.setBold(
true );
709 mTitleLabel->setFont( titleFont );
714 TQFont listFont( mIconList->font() );
715 listFont.setBold(
true );
716 mIconList->setFont( listFont );
717 mIconList->invalidateHeight();
718 mIconList->invalidateWidth();
723 void KJanusWidget::slotItemClicked(TQListViewItem *it)
725 if(it && (it->childCount()>0))
726 it->setOpen(!it->isOpen());
730 void KJanusWidget::slotOnItem(TQListBoxItem *qitem)
732 mIconList->slotOnItem( qitem );
737 if( !mValid ) {
return; }
740 mTreeList->setFocus();
744 mIconList->setFocus();
746 else if( mFace ==
Tabbed )
748 mTabControl->setFocus();
752 mSwallowPage->setFocus();
754 else if( mFace ==
Plain )
756 mPlainPage->setFocus();
768 TQSize s4( mPageStack->sizeHint() );
772 s1.rwidth() += style().pixelMetric( TQStyle::PM_SplitterWidth );
773 s2 = mTreeList->minimumSize();
777 mIconList->updateMinimumHeight();
778 mIconList->updateWidth();
779 s2 = mIconList->minimumSize();
782 if( mTitleLabel->isVisible() )
784 s3 += mTitleLabel->sizeHint();
785 s3.rheight() += mTitleSep->minimumSize().height();
791 int h1 = s1.rheight() + s3.rheight() + s4.height();
792 int h2 = TQMAX( h1, s2.rheight() );
794 return TQSize( s1.width()+s2.width()+TQMAX(s3.width(),s4.width()), h2 );
796 else if( mFace ==
Tabbed )
798 return mTabControl->sizeHint();
802 return mSwallowPage->minimumSize();
804 else if( mFace ==
Plain )
806 return mPlainPage->sizeHint();
810 return TQSize( 100, 100 );
826 mTreeListResizeMode = !state ?
827 TQSplitter::KeepSize : TQSplitter::Stretch;
829 d->mSplitter->setResizeMode( d->mListFrame, mTreeListResizeMode );
838 mIconList->setShowAll( state );
844 mShowIconsInTreeList = state;
850 mTreeList->setRootIsDecorated(state);
859 connect( mTreeList, TQ_SIGNAL( collapsed( TQListViewItem * ) ),
this, TQ_SLOT( slotReopen( TQListViewItem * ) ) );
861 disconnect( mTreeList, TQ_SIGNAL( collapsed( TQListViewItem * ) ),
this, TQ_SLOT( slotReopen( TQListViewItem * ) ) );
863 for( TQListViewItem * item = mTreeList->firstChild(); item; item = item->itemBelow() )
864 item->setOpen(
true );
872 widget->reparent( d->mListFrame, TQPoint() );
880 TQPushButton * button =
new TQPushButton( text, d->mListFrame,
"KJanusWidget::buttonBelowList" );
881 connect( button, TQ_SIGNAL( clicked() ), recv, slot );
890 connect( button, TQ_SIGNAL( clicked() ), recv, slot );
899 d->mSplitter->setResizeMode( d->mListFrame, mTreeListResizeMode );
911 if( e->type() == TQEvent::Show )
913 IconListItem *item = (IconListItem*)mIconList->item(0);
916 int lw = item->width( mIconList );
917 int sw = mIconList->verticalScrollBar()->sizeHint().width();
918 mIconList->setFixedWidth( lw+sw+mIconList->frameWidth()*2 );
921 else if( e->type() == TQEvent::Hide )
923 IconListItem *item = (IconListItem*)mIconList->item(0);
926 int lw = item->width( mIconList );
927 mIconList->setFixedWidth( lw+mIconList->frameWidth()*2 );
930 return TQWidget::eventFilter( o, e );
940 KJanusWidget::IconListBox::IconListBox( TQWidget *parent,
const char *name,
942 :
TDEListBox( parent, name, f ), mShowAll(false), mHeightValid(false),
948 void KJanusWidget::IconListBox::updateMinimumHeight()
950 if( mShowAll && !mHeightValid )
952 int h = frameWidth()*2;
953 for( TQListBoxItem *i = item(0); i; i = i->next() )
955 h += i->height(
this );
957 setMinimumHeight( h );
963 void KJanusWidget::IconListBox::updateWidth()
968 for( TQListBoxItem *i = item(0); i; i = i->next() )
970 int w = ((IconListItem *)i)->width(
this);
971 maxWidth = TQMAX( w, maxWidth );
974 for( TQListBoxItem *i = item(0); i; i = i->next() )
976 ((IconListItem *)i)->expandMinimumWidth( maxWidth );
979 if( verticalScrollBar()->isVisible() )
981 maxWidth += verticalScrollBar()->sizeHint().width();
984 setFixedWidth( maxWidth + frameWidth()*2 );
990 void KJanusWidget::IconListBox::invalidateHeight()
992 mHeightValid =
false;
996 void KJanusWidget::IconListBox::invalidateWidth()
1002 void KJanusWidget::IconListBox::setShowAll(
bool showAll )
1005 mHeightValid =
false;
1009 void KJanusWidget::IconListBox::leaveEvent( TQEvent *ev )
1011 TDEListBox::leaveEvent( ev );
1013 if ( mOldItem && !mOldItem->isSelected() )
1015 ((KJanusWidget::IconListItem *) mOldItem)->highlight(
true );
1021 void KJanusWidget::IconListBox::slotOnItem(TQListBoxItem *qitem)
1023 TDEListBox::slotOnItem( qitem );
1025 if ( qitem == mOldItem )
1030 if ( mOldItem && !mOldItem->isSelected() )
1032 ((KJanusWidget::IconListItem *) mOldItem)->highlight(
true );
1035 KJanusWidget::IconListItem *item =
dynamic_cast< KJanusWidget::IconListItem *
>( qitem );
1036 if ( item && !item->isSelected() )
1038 item->highlight(
false );
1049 KJanusWidget::IconListItem::IconListItem( TQListBox *listbox,
const TQPixmap &pixmap,
1050 const TQString &text )
1051 : TQListBoxItem( listbox )
1054 if( mPixmap.isNull() )
1056 mPixmap = defaultPixmap();
1059 setCustomHighlighting(
true );
1064 int KJanusWidget::IconListItem::expandMinimumWidth(
int width )
1066 mMinimumWidth = TQMAX( mMinimumWidth, width );
1067 return mMinimumWidth;
1071 void KJanusWidget::IconListItem::highlight(
bool erase )
1077 TQRect r = listBox()->itemRect(
this );
1078 r.addCoords( 1, 1, -1, -1 );
1080 TQPainter p( listBox()->viewport() );
1081 p.setClipRegion( r );
1083 const TQColorGroup &cg = listBox()->colorGroup();
1086 p.setPen( cg.base() );
1087 p.setBrush( cg.base() );
1092 p.setBrush( cg.highlight().light( 120 ) );
1095 p.setPen( cg.highlight().dark( 140 ) );
1099 p.setPen( cg.foreground() );
1100 p.translate( r.x() - 1, r.y() - 1 );
1101 paintContents( &p );
1105 const TQPixmap &KJanusWidget::IconListItem::defaultPixmap()
1107 static TQPixmap *pix=0;
1110 pix =
new TQPixmap( 32, 32 );
1112 p.eraseRect( 0, 0, pix->width(), pix->height() );
1113 p.setPen( TQt::red );
1114 p.drawRect ( 0, 0, pix->width(), pix->height() );
1117 TQBitmap mask( pix->width(), pix->height(),
true );
1118 mask.fill( TQt::black );
1120 p.setPen( TQt::white );
1121 p.drawRect ( 0, 0, pix->width(), pix->height() );
1124 pix->setMask( mask );
1130 void KJanusWidget::IconListItem::paint( TQPainter *painter )
1132 TQRect itemPaintRegion( listBox()->itemRect(
this ) );
1133 TQRect r( 1, 1, itemPaintRegion.width() - 2, itemPaintRegion.height() - 2);
1137 painter->eraseRect( r );
1140 painter->setPen( listBox()->colorGroup().highlight().dark( 160 ) );
1141 painter->drawRect( r );
1145 paintContents( painter );
1149 void KJanusWidget::IconListItem::paintContents( TQPainter *painter )
1151 TQFontMetrics fm = painter->fontMetrics();
1152 int ht = fm.boundingRect( 0, 0, 0, 0, TQt::AlignCenter, text() ).height();
1153 int wp = mPixmap.width();
1154 int hp = mPixmap.height();
1155 painter->drawPixmap( (mMinimumWidth - wp) / 2, 5, mPixmap );
1157 if( !text().isEmpty() )
1159 painter->drawText( 1, hp + 7, mMinimumWidth - 2, ht, TQt::AlignCenter, text() );
1163 int KJanusWidget::IconListItem::height(
const TQListBox *lb )
const
1165 if( text().isEmpty() )
1167 return mPixmap.height();
1171 int ht = lb->fontMetrics().boundingRect( 0, 0, 0, 0, TQt::AlignCenter, text() ).height();
1172 return (mPixmap.height() + ht + 10);
1177 int KJanusWidget::IconListItem::width(
const TQListBox *lb )
const
1179 int wt = lb->fontMetrics().boundingRect( 0, 0, 0, 0, TQt::AlignCenter, text() ).width() + 10;
1180 int wp = mPixmap.width() + 10;
1181 int w = TQMAX( wt, wp );
1182 return TQMAX( w, mMinimumWidth );
1186 void KJanusWidget::virtual_hook(
int,
void* )
1194 if (!d || !d->mPageToInt.contains(page))
1197 int index = d->mPageToInt[page];
1201 TQMap<TQListViewItem*, TQWidget *>::Iterator i;
1202 for( i = mTreeListToPageStack.begin(); i != mTreeListToPageStack.end(); ++i )
1206 mPageStack->removeWidget(page);
1207 mTreeListToPageStack.remove(i);
1208 d->mIntToTitle.remove(index);
1209 d->mPageToInt.remove(page);
1210 d->mIntToPage.remove(index);
1216 TQMap<TQListBoxItem*, TQWidget *>::Iterator i;
1217 for( i = mIconListToPageStack.begin(); i != mIconListToPageStack.end(); ++i )
1221 mPageStack->removeWidget(page);
1222 mIconListToPageStack.remove(i);
1223 d->mIntToTitle.remove(index);
1224 d->mPageToInt.remove(page);
1225 d->mIntToPage.remove(index);
1231 mTabControl->removePage(page);
1232 d->mPageToInt.remove(page);
1233 d->mIntToPage.remove(index);
1240 if (!d || !d->mIntToTitle.contains(index))
1241 return TQString::null;
1243 return d->mIntToTitle[index];
1249 if (!d || !d->mIntToPage.contains(index))
1252 return d->mIntToPage[index];
1255 #include "kjanuswidget.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...
An abstract class for GUI data such as ToolTip and Icon.
Standard horizontal or vertical separator.
static TQFont generalFont()
A variant of TQListBox that honors KDE's system-wide settings.
This Widget extends the functionality of TQListView to honor the system wide settings for Single Clic...
virtual int addColumn(const TQString &label, int width=-1)
Reimplemented for full width support.
virtual void setSorting(int column, bool ascending=true)
Reimplemented to remember the current sort column and order.
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
TQString name(StdAccel id)