00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kmlistview.h"
00021 #include "kmprinter.h"
00022 #include "kmobject.h"
00023
00024 #include <qheader.h>
00025 #include <qpainter.h>
00026 #include <klocale.h>
00027 #include <kiconloader.h>
00028 #include <kcursor.h>
00029
00030 class KMListViewItem : public QListViewItem, public KMObject
00031 {
00032 public:
00033 KMListViewItem(QListView *parent, const QString& txt);
00034 KMListViewItem(QListViewItem *parent, const QString& txt);
00035 KMListViewItem(QListViewItem *parent, KMPrinter *p);
00036
00037 virtual void paintCell(QPainter*, const QColorGroup&, int, int, int);
00038 void updatePrinter(KMPrinter *p);
00039 bool isClass() const { return m_isclass; }
00040
00041 protected:
00042 void init(KMPrinter *p = 0);
00043
00044 private:
00045 int m_state;
00046 bool m_isclass;
00047 };
00048
00049 KMListViewItem::KMListViewItem(QListView *parent, const QString& txt)
00050 : QListViewItem(parent,txt)
00051 {
00052 init();
00053 }
00054
00055 KMListViewItem::KMListViewItem(QListViewItem *parent, const QString& txt)
00056 : QListViewItem(parent,txt)
00057 {
00058 init();
00059 }
00060
00061 KMListViewItem::KMListViewItem(QListViewItem *parent, KMPrinter *p)
00062 : QListViewItem(parent)
00063 {
00064 init(p);
00065 }
00066
00067 void KMListViewItem::init(KMPrinter *p)
00068 {
00069 m_state = 0;
00070 if (p)
00071 updatePrinter(p);
00072 setSelectable(depth() == 2);
00073 }
00074
00075 void KMListViewItem::updatePrinter(KMPrinter *p)
00076 {
00077 bool update(false);
00078 if (p)
00079 {
00080 int oldstate = m_state;
00081 int st(p->isValid() ? (int)KIcon::DefaultState : (int)KIcon::LockOverlay);
00082 m_state = ((p->isHardDefault() ? 0x1 : 0x0) | (p->ownSoftDefault() ? 0x2 : 0x0) | (p->isValid() ? 0x4 : 0x0));
00083 update = (oldstate != m_state);
00084 QString name = (p->isVirtual() ? p->instanceName() : p->name());
00085 if (name != text(0))
00086 setText(0, name);
00087 setPixmap(0, SmallIcon(p->pixmap(), 0, st));
00088 m_isclass = p->isClass();
00089 }
00090 setDiscarded(false);
00091 if (update)
00092 repaint();
00093 }
00094
00095 void KMListViewItem::paintCell(QPainter *p, const QColorGroup& cg, int c, int w, int a)
00096 {
00097 if (m_state != 0)
00098 {
00099 QFont f(p->font());
00100 if (m_state & 0x1) f.setBold(true);
00101 if (m_state & 0x2) f.setItalic(true);
00102 p->setFont(f);
00103 }
00104 QListViewItem::paintCell(p,cg,c,w,a);
00105 }
00106
00107
00108
00109 KMListView::KMListView(QWidget *parent, const char *name)
00110 : QListView(parent,name)
00111 {
00112 m_items.setAutoDelete(false);
00113
00114 addColumn("");
00115 header()->hide();
00116 setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
00117 setLineWidth(1);
00118 setSorting(0);
00119
00120 connect(this,SIGNAL(contextMenuRequested(QListViewItem*,const QPoint&,int)),SLOT(slotRightButtonClicked(QListViewItem*,const QPoint&,int)));
00121 connect(this,SIGNAL(selectionChanged()),SLOT(slotSelectionChanged()));
00122 connect(this,SIGNAL(onItem(QListViewItem*)),SLOT(slotOnItem(QListViewItem*)));
00123 connect(this,SIGNAL(onViewport()),SLOT(slotOnViewport()));
00124
00125 m_root = new KMListViewItem(this,i18n("Print System"));
00126 m_root->setPixmap(0,SmallIcon("kdeprint_printer"));
00127 m_root->setOpen(true);
00128 m_classes = new KMListViewItem(m_root,i18n("Classes"));
00129 m_classes->setPixmap(0,SmallIcon("package"));
00130 m_classes->setOpen(true);
00131 m_printers = new KMListViewItem(m_root,i18n("Printers"));
00132 m_printers->setPixmap(0,SmallIcon("package"));
00133 m_printers->setOpen(true);
00134 m_specials = new KMListViewItem(m_root,i18n("Specials"));
00135 m_specials->setPixmap(0,SmallIcon("package"));
00136 m_specials->setOpen(true);
00137
00138 sort();
00139 }
00140
00141 KMListView::~KMListView()
00142 {
00143 }
00144
00145 void KMListView::slotRightButtonClicked(QListViewItem *item, const QPoint& p, int)
00146 {
00147 emit rightButtonClicked(item && item->depth() == 2 ? item->text(0) : QString::null, p);
00148 }
00149
00150 KMListViewItem* KMListView::findItem(KMPrinter *p)
00151 {
00152 if (p)
00153 {
00154 QPtrListIterator<KMListViewItem> it(m_items);
00155 bool isVirtual(p->isVirtual()), isClass(p->isClass());
00156 for (;it.current();++it)
00157 if (isVirtual)
00158 {
00159 if (it.current()->depth() == 3 && it.current()->text(0) == p->instanceName()
00160 && it.current()->parent()->text(0) == p->printerName())
00161 return it.current();
00162 }
00163 else
00164 {
00165 if (it.current()->isClass() == isClass && it.current()->text(0) == p->name())
00166 return it.current();
00167 }
00168 }
00169 return 0;
00170 }
00171
00172 KMListViewItem* KMListView::findItem(const QString& prname)
00173 {
00174 QPtrListIterator<KMListViewItem> it(m_items);
00175 for (; it.current(); ++it)
00176 if (it.current()->depth() == 2 && it.current()->text(0) == prname)
00177 return it.current();
00178 return 0;
00179 }
00180
00181 void KMListView::setPrinterList(QPtrList<KMPrinter> *list)
00182 {
00183 bool changed(false);
00184
00185 QPtrListIterator<KMListViewItem> it(m_items);
00186 for (;it.current();++it)
00187 it.current()->setDiscarded(true);
00188
00189 if (list)
00190 {
00191 QPtrListIterator<KMPrinter> it(*list);
00192 KMListViewItem *item (0);
00193 for (;it.current();++it)
00194 {
00195 item = findItem(it.current());
00196 if (!item)
00197 {
00198 if (it.current()->isVirtual())
00199 {
00200 KMListViewItem *pItem = findItem(it.current()->printerName());
00201 if (!pItem)
00202 continue;
00203 item = new KMListViewItem(pItem, it.current());
00204 pItem->setOpen(true);
00205 }
00206 else
00207 item = new KMListViewItem((it.current()->isSpecial() ? m_specials : (it.current()->isClass(false) ? m_classes : m_printers)),it.current());
00208 m_items.append(item);
00209 changed = true;
00210 }
00211 else
00212 item->updatePrinter(it.current());
00213 }
00214 }
00215
00216 QPtrList<KMListViewItem> deleteList;
00217 deleteList.setAutoDelete(true);
00218 for (uint i=0; i<m_items.count(); i++)
00219 if (m_items.at(i)->isDiscarded())
00220 {
00221
00222
00223 KMListViewItem *item = m_items.take(i);
00224 if (item->depth() == 2)
00225 deleteList.append(item);
00226 else
00227 deleteList.prepend(item);
00228 i--;
00229 changed = true;
00230 }
00231 deleteList.clear();
00232
00233 if (changed) sort();
00234 emit selectionChanged();
00235 }
00236
00237 void KMListView::slotSelectionChanged()
00238 {
00239 KMListViewItem *item = static_cast<KMListViewItem*>(currentItem());
00240 emit printerSelected((item && !item->isDiscarded() && item->depth() == 2 ? item->text(0) : QString::null));
00241 }
00242
00243 void KMListView::setPrinter(const QString& prname)
00244 {
00245 QPtrListIterator<KMListViewItem> it(m_items);
00246 for (;it.current();++it)
00247 if (it.current()->text(0) == prname)
00248 {
00249 setSelected(it.current(),true);
00250 break;
00251 }
00252 }
00253
00254 void KMListView::setPrinter(KMPrinter *p)
00255 {
00256 setPrinter(p ? p->name() : QString::null);
00257 }
00258
00259 void KMListView::slotOnItem(QListViewItem *)
00260 {
00261 setCursor(KCursor::handCursor());
00262 }
00263
00264 void KMListView::slotOnViewport()
00265 {
00266 setCursor(KCursor::arrowCursor());
00267 }
00268 #include "kmlistview.moc"