20 #include "kmiconview.h"
21 #include "kmprinter.h"
23 #include <tqpainter.h>
24 #include <kiconloader.h>
27 KMIconViewItem::KMIconViewItem(TQIconView *parent, KMPrinter *p)
28 : TQIconViewItem(parent)
31 m_mode = parent->itemTextPos();
32 m_pixmap = TQString();
34 updatePrinter(p, m_mode);
37 void KMIconViewItem::paintItem(TQPainter *p,
const TQColorGroup& cg)
42 if (m_state & 0x1) f.setBold(
true);
43 if (m_state & 0x2) f.setItalic(
true);
46 TQIconViewItem::paintItem(p,cg);
49 void KMIconViewItem::calcRect(
const TQString&)
51 TQRect ir(rect()), pr, tr;
54 pr.setWidth(pixmap()->width());
55 pr.setHeight(pixmap()->height());
58 TQFont f(iconView()->font());
59 if (m_state & 0x1) f.setBold(
true);
60 if (m_state & 0x2) f.setItalic(
true);
62 if (m_mode == TQIconView::Bottom)
63 tr = fm.boundingRect(0, 0, iconView()->maxItemWidth(), 0xFFFFFF, AlignHCenter|AlignTop|WordBreak|BreakAnywhere, text()+
"X");
65 tr = fm.boundingRect(0, 0, 0xFFFFFF, 0xFFFFFF, AlignLeft|AlignTop, text()+
"X");
68 if (m_mode == TQIconView::Bottom)
70 ir.setHeight(pr.height() + tr.height() + 15);
71 ir.setWidth(TQMAX(pr.width(), tr.width()) + 10);
72 pr = TQRect((ir.width()-pr.width())/2, 5, pr.width(), pr.height());
73 tr = TQRect((ir.width()-tr.width())/2, 10+pr.height(), tr.width(), tr.height());
77 ir.setHeight(TQMAX(pr.height(), tr.height()) + 4);
78 ir.setWidth(pr.width() + tr.width() + 6);
79 pr = TQRect(2, (ir.height()-pr.height())/2, pr.width(), pr.height());
80 tr = TQRect(4+pr.width(), (ir.height()-tr.height())/2, tr.width(), tr.height());
89 void KMIconViewItem::updatePrinter(KMPrinter *p,
int mode)
92 int oldstate = m_state;
95 m_state = ((p->isHardDefault() ? 0x1 : 0x0) | (p->ownSoftDefault() ? 0x2 : 0x0) | (p->isValid() ? 0x4 : 0x0));
96 update = (oldstate != m_state);
97 if (p->name() != text() || update)
99 setText(TQString::null);
102 setKey(TQString::fromLatin1(
"%1_%2").arg((p->isSpecial() ?
"special" : (p->isClass(
false) ?
"class" :
"printer"))).arg(p->name()));
103 m_isclass = p->isClass(
false);
105 if (mode != m_mode || ((oldstate&0x4) != (m_state&0x4)) || (p && p->pixmap() != m_pixmap))
107 int iconstate = (m_state&0x4 ? (int)TDEIcon::DefaultState : (
int)TDEIcon::LockOverlay);
109 m_pixmap = p->pixmap();
111 if (m_mode == TQIconView::Bottom)
112 setPixmap(DesktopIcon(m_pixmap, 0, iconstate));
114 setPixmap(SmallIcon(m_pixmap, 0, iconstate));
121 KMIconView::KMIconView(TQWidget *parent,
const char *name)
122 : TDEIconView(parent,name)
124 setMode(TDEIconView::Select);
125 setSelectionMode(TQIconView::Single);
126 setItemsMovable(
false);
127 setResizeMode(TQIconView::Adjust);
129 m_items.setAutoDelete(
false);
130 setViewMode(KMIconView::Big);
132 connect(
this,TQ_SIGNAL(contextMenuRequested(TQIconViewItem*,
const TQPoint&)),TQ_SLOT(slotRightButtonClicked(TQIconViewItem*,
const TQPoint&)));
133 connect(
this,TQ_SIGNAL(selectionChanged()),TQ_SLOT(slotSelectionChanged()));
136 KMIconView::~KMIconView()
140 KMIconViewItem* KMIconView::findItem(KMPrinter *p)
144 TQPtrListIterator<KMIconViewItem> it(m_items);
145 for (;it.current();++it)
146 if (it.current()->text() == p->name()
147 && it.current()->isClass() == p->isClass())
153 void KMIconView::setPrinterList(TQPtrList<KMPrinter> *list)
157 TQPtrListIterator<KMIconViewItem> it(m_items);
158 for (;it.current();++it)
159 it.current()->setDiscarded(
true);
163 TQPtrListIterator<KMPrinter> it(*list);
164 KMIconViewItem *item(0);
165 for (;it.current();++it)
168 if (!it.current()->instanceName().isEmpty())
170 item = findItem(it.current());
173 item =
new KMIconViewItem(
this,it.current());
174 m_items.append(item);
178 item->updatePrinter(it.current(), itemTextPos());
182 for (uint i=0; i<m_items.count(); i++)
183 if (m_items.at(i)->isDiscarded())
185 delete m_items.take(i);
191 emit selectionChanged();
194 void KMIconView::setViewMode(ViewMode m)
197 bool big = (m == KMIconView::Big);
198 int mode = (big ? TQIconView::Bottom : TQIconView::Right);
200 TQPtrListIterator<KMIconViewItem> it(m_items);
201 for (;it.current();++it)
202 it.current()->updatePrinter(0, mode);
204 setArrangement((big ? TQIconView::LeftToRight : TQIconView::TopToBottom));
205 setItemTextPos((TQIconView::ItemTextPos)mode);
207 setWordWrapIconText(
true);
210 void KMIconView::slotRightButtonClicked(TQIconViewItem *item,
const TQPoint& p)
212 emit rightButtonClicked(item ? item->text() : TQString::null, p);
215 void KMIconView::slotSelectionChanged()
217 KMIconViewItem *item =
static_cast<KMIconViewItem*
>(currentItem());
218 emit printerSelected((item && !item->isDiscarded() && item->isSelected() ? item->text() : TQString::null));
221 void KMIconView::setPrinter(
const TQString& prname)
223 TQPtrListIterator<KMIconViewItem> it(m_items);
224 for (; it.current(); ++it)
225 if (it.current()->text() == prname)
227 setSelected(it.current(),
true);
232 void KMIconView::setPrinter(KMPrinter *p)
234 setPrinter(p ? p->name() : TQString::null);
237 #include "kmiconview.moc"