20 #include "cjanuswidget.h"
22 #include <tqwidgetstack.h>
24 #include <tqpainter.h>
25 #include <tdelistbox.h>
27 #include <kseparator.h>
29 class CJanusWidget::CPage
41 class CJanusWidget::CListBoxItem :
public TQListBoxItem
44 CListBoxItem(TQListBox *lb, TQListBoxItem *after,
const TQPixmap& pix,
const TQString& text);
45 int height(
const TQListBox*)
const;
46 int width(
const TQListBox*)
const;
49 void paint(TQPainter*);
55 CJanusWidget::CListBoxItem::CListBoxItem(TQListBox *lb, TQListBoxItem *after,
const TQPixmap& pix,
const TQString& text)
56 : TQListBoxItem(lb, after), m_pix(pix)
61 int CJanusWidget::CListBoxItem::height(
const TQListBox *lb)
const
63 return (m_pix.height() + lb->fontMetrics().lineSpacing() + 12);
66 int CJanusWidget::CListBoxItem::width(
const TQListBox *lb)
const
68 int w = TQMAX(lb->fontMetrics().width(text()),m_pix.width());
72 void CJanusWidget::CListBoxItem::paint(TQPainter *p)
74 int w1 = (listBox()->contentsWidth()-m_pix.width())/2;
76 p->drawPixmap(w1,5,m_pix);
77 p->drawText(0,7+m_pix.height(),listBox()->contentsWidth(),p->fontMetrics().lineSpacing(),TQt::AlignHCenter,text());
82 class CJanusWidget::CListBox :
public TDEListBox
85 CListBox(TQWidget *parent = 0,
const char *name = 0);
91 virtual bool eventFilter(TQObject*, TQEvent*);
94 CJanusWidget::CListBox::CListBox(TQWidget *parent,
const char *name)
95 : TDEListBox(parent,name)
97 verticalScrollBar()->installEventFilter(
this);
100 CJanusWidget::CListBox::~CListBox()
104 bool CJanusWidget::CListBox::eventFilter(TQObject *o, TQEvent *e)
106 if (e->type() == TQEvent::Show || e->type() == TQEvent::Hide)
108 return TDEListBox::eventFilter(o,e);
111 void CJanusWidget::CListBox::computeWidth()
113 TQListBoxItem *item = firstItem();
117 w = TQMAX(w,item->width(
this));
120 if (verticalScrollBar()->isVisible())
121 w += verticalScrollBar()->sizeHint().width();
122 w += (frameWidth()*2);
128 CJanusWidget::CJanusWidget(TQWidget *parent,
const char *name)
129 : TQWidget(parent,name)
131 m_pages.setAutoDelete(
true);
133 m_stack =
new TQWidgetStack(
this);
134 m_header =
new TQLabel(
this);
135 TQFont f(m_header->font());
137 m_header->setFont(f);
139 KSeparator* sep =
new KSeparator( KSeparator::HLine,
this);
140 sep->setFixedHeight(5);
142 m_iconlist =
new CListBox(
this);
143 f = m_iconlist->font();
145 m_iconlist->setFont(f);
146 connect(m_iconlist,TQ_SIGNAL(selectionChanged(TQListBoxItem*)),TQ_SLOT(slotSelected(TQListBoxItem*)));
148 m_empty =
new TQWidget(
this,
"Empty");
149 m_stack->addWidget(m_empty,0);
151 TQHBoxLayout *main_ =
new TQHBoxLayout(
this, 0, 10);
152 TQVBoxLayout *sub_ =
new TQVBoxLayout(0, 0, 5);
153 main_->addWidget(m_iconlist,0);
154 main_->addLayout(sub_,1);
155 sub_->addWidget(m_header,0);
156 sub_->addWidget(sep,0);
157 sub_->addWidget(m_stack,1);
160 CJanusWidget::~CJanusWidget()
164 void CJanusWidget::addPage(TQWidget *w,
const TQString& text,
const TQString& header,
const TQPixmap& pix)
166 CPage *page =
new CPage();
167 m_pages.append(page);
170 page->m_header = header;
171 page->m_pixmap = pix;
172 page->m_item =
new CListBoxItem(m_iconlist,findPrevItem(page),pix,text);
173 m_iconlist->computeWidth();
174 m_stack->addWidget(w,m_pages.count());
176 if (m_iconlist->count() == 1)
177 m_iconlist->setSelected(page->m_item,
true);
180 void CJanusWidget::enablePage(TQWidget *w)
182 CPage *page = findPage(w);
183 if (page && !page->m_item)
185 page->m_item =
new CListBoxItem(m_iconlist,findPrevItem(page),page->m_pixmap,page->m_text);
186 m_iconlist->computeWidth();
187 if (m_iconlist->count() == 1)
188 m_iconlist->setSelected(page->m_item,
true);
192 void CJanusWidget::disablePage(TQWidget *w)
194 CPage *page = findPage(w);
195 if (page && page->m_item)
197 bool needReselect(m_iconlist->isSelected(page->m_item));
200 m_iconlist->computeWidth();
202 if (m_iconlist->count() > 0)
203 m_iconlist->setSelected(m_iconlist->firstItem(),
true);
209 void CJanusWidget::slotSelected(TQListBoxItem *item)
211 CPage *page = findPage(item);
214 m_stack->raiseWidget(page->m_widget);
215 m_header->setText(page->m_header);
219 m_header->setText(
"");
220 m_stack->raiseWidget(m_empty);
224 CJanusWidget::CPage* CJanusWidget::findPage(TQWidget *w)
226 TQPtrListIterator<CPage> it(m_pages);
227 for (;it.current();++it)
228 if (it.current()->m_widget == w)
233 CJanusWidget::CPage* CJanusWidget::findPage(TQListBoxItem *i)
235 TQPtrListIterator<CPage> it(m_pages);
236 for (;it.current();++it)
237 if (it.current()->m_item == i)
242 TQListBoxItem* CJanusWidget::findPrevItem(CPage *p)
244 if (m_pages.findRef(p) == -1)
248 for (;m_pages.current();m_pages.prev())
249 if (m_pages.current()->m_item)
250 return m_pages.current()->m_item;
254 void CJanusWidget::clearPages()
256 TQPtrListIterator<CPage> it(m_pages);
257 for (;it.current(); ++it)
259 delete it.current()->m_widget;
260 delete it.current()->m_item;
265 #include "cjanuswidget.moc"