summaryrefslogtreecommitdiffstats
path: root/kdeprint/management/cjanuswidget.cpp
blob: af04ee1705d98ccc546394fb1f2af79664f146aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include "cjanuswidget.h"

#include <tqwidgetstack.h>
#include <tqlabel.h>
#include <tqpainter.h>
#include <klistbox.h>
#include <tqlayout.h>
#include <kseparator.h>

class CJanusWidget::CPage
{
public:
	TQWidget	*m_widget;
	TQString	m_text;
	TQString	m_header;
	TQPixmap	m_pixmap;
	CListBoxItem	*m_item;
};

//***********************************************************************************

class CJanusWidget::CListBoxItem : public TQListBoxItem
{
public:
	CListBoxItem(TQListBox *lb, TQListBoxItem *after, const TQPixmap& pix, const TQString& text);
	int height(const TQListBox*) const;
	int width(const TQListBox*) const;

protected:
	void paint(TQPainter*);

private:
	TQPixmap	m_pix;
};

CJanusWidget::CListBoxItem::CListBoxItem(TQListBox *lb, TQListBoxItem *after, const TQPixmap& pix, const TQString& text)
: TQListBoxItem(lb, after), m_pix(pix)
{
	setText(text);
}

int CJanusWidget::CListBoxItem::height(const TQListBox *lb) const
{
	return (m_pix.height() + lb->fontMetrics().lineSpacing() + 12);
}

int CJanusWidget::CListBoxItem::width(const TQListBox *lb) const
{
	int	w = TQMAX(lb->fontMetrics().width(text()),m_pix.width());
	return (w + 10);
}

void CJanusWidget::CListBoxItem::paint(TQPainter *p)
{
	int	w1 = (listBox()->contentsWidth()-m_pix.width())/2;

	p->drawPixmap(w1,5,m_pix);
	p->drawText(0,7+m_pix.height(),listBox()->contentsWidth(),p->fontMetrics().lineSpacing(),Qt::AlignHCenter,text());
}

//***********************************************************************************

class CJanusWidget::CListBox : public KListBox
{
public:
	CListBox(TQWidget *parent = 0, const char *name = 0);
	~CListBox();

	void computeWidth();

protected:
	virtual bool eventFilter(TQObject*, TQEvent*);
};

CJanusWidget::CListBox::CListBox(TQWidget *parent, const char *name)
: KListBox(parent,name)
{
	verticalScrollBar()->installEventFilter(this);
}

CJanusWidget::CListBox::~CListBox()
{
}

bool CJanusWidget::CListBox::eventFilter(TQObject *o, TQEvent *e)
{
	if (e->type() == TQEvent::Show || e->type() == TQEvent::Hide)
		computeWidth();
	return KListBox::eventFilter(o,e);
}

void CJanusWidget::CListBox::computeWidth()
{
	TQListBoxItem	*item = firstItem();
	int	w(40);
	while (item)
	{
		w = TQMAX(w,item->width(this));
		item = item->next();
	}
	if (verticalScrollBar()->isVisible())
		w += verticalScrollBar()->tqsizeHint().width();
	w += (frameWidth()*2);
	setFixedWidth(w);
}

//***********************************************************************************

CJanusWidget::CJanusWidget(TQWidget *parent, const char *name)
: TQWidget(parent,name)
{
	m_pages.setAutoDelete(true);

	m_stack = new TQWidgetStack(this);
	m_header = new TQLabel(this);
	TQFont	f(m_header->font());
	f.setBold(true);
	m_header->setFont(f);

	KSeparator* sep = new KSeparator( KSeparator::HLine, this);
	sep->setFixedHeight(5);

	m_iconlist = new CListBox(this);
	f = m_iconlist->font();
	f.setBold(true);
	m_iconlist->setFont(f);
	connect(m_iconlist,TQT_SIGNAL(selectionChanged(TQListBoxItem*)),TQT_SLOT(slotSelected(TQListBoxItem*)));

	m_empty = new TQWidget(this, "Empty");
	m_stack->addWidget(m_empty,0);

	TQHBoxLayout	*main_ = new TQHBoxLayout(this, 0, 10);
	TQVBoxLayout	*sub_ = new TQVBoxLayout(0, 0, 5);
	main_->addWidget(m_iconlist,0);
	main_->addLayout(sub_,1);
	sub_->addWidget(m_header,0);
	sub_->addWidget(sep,0);
	sub_->addWidget(m_stack,1);
}

CJanusWidget::~CJanusWidget()
{
}

void CJanusWidget::addPage(TQWidget *w, const TQString& text, const TQString& header, const TQPixmap& pix)
{
	CPage	*page = new CPage();
	m_pages.append(page);
	page->m_widget = w;
	page->m_text = text;
	page->m_header = header;
	page->m_pixmap = pix;
	page->m_item = new CListBoxItem(m_iconlist,findPrevItem(page),pix,text);
	m_iconlist->computeWidth();
	m_stack->addWidget(w,m_pages.count());

	if (m_iconlist->count() == 1)
		m_iconlist->setSelected(page->m_item,true);
}

void CJanusWidget::enablePage(TQWidget *w)
{
	CPage	*page = findPage(w);
	if (page && !page->m_item)
	{
		page->m_item = new CListBoxItem(m_iconlist,findPrevItem(page),page->m_pixmap,page->m_text);
		m_iconlist->computeWidth();
		if (m_iconlist->count() == 1)
			m_iconlist->setSelected(page->m_item,true);
	}
}

void CJanusWidget::disablePage(TQWidget *w)
{
	CPage	*page = findPage(w);
	if (page && page->m_item)
	{
		bool	needReselect(m_iconlist->isSelected(page->m_item));
		delete page->m_item;
		page->m_item = 0;
		m_iconlist->computeWidth();
		if (needReselect)
			if (m_iconlist->count() > 0)
				m_iconlist->setSelected(m_iconlist->firstItem(),true);
			else
				slotSelected(0);
	}
}

void CJanusWidget::slotSelected(TQListBoxItem *item)
{
	CPage	*page = findPage(item);
	if (page)
	{
		m_stack->raiseWidget(page->m_widget);
		m_header->setText(page->m_header);
	}
	else
	{
		m_header->setText("");
		m_stack->raiseWidget(m_empty);
	}
}

CJanusWidget::CPage* CJanusWidget::findPage(TQWidget *w)
{
	TQPtrListIterator<CPage>	it(m_pages);
	for (;it.current();++it)
		if (it.current()->m_widget == w)
			return it.current();
	return 0;
}

CJanusWidget::CPage* CJanusWidget::findPage(TQListBoxItem *i)
{
	TQPtrListIterator<CPage>	it(m_pages);
	for (;it.current();++it)
		if (it.current()->m_item == i)
			return it.current();
	return 0;
}

TQListBoxItem* CJanusWidget::findPrevItem(CPage *p)
{
	if (m_pages.findRef(p) == -1)
		m_pages.last();
	else
		m_pages.prev();
	for (;m_pages.current();m_pages.prev())
		if (m_pages.current()->m_item)
			return m_pages.current()->m_item;
	return 0;
}

void CJanusWidget::clearPages()
{
	TQPtrListIterator<CPage>	it(m_pages);
	for (;it.current(); ++it)
	{
		delete it.current()->m_widget;
		delete it.current()->m_item;
	}
	m_pages.clear();
}

#include "cjanuswidget.moc"