kmiconview.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "kmiconview.h"
00021 #include "kmprinter.h"
00022 
00023 #include <qpainter.h>
00024 #include <kiconloader.h>
00025 #include <kdebug.h>
00026 
00027 KMIconViewItem::KMIconViewItem(QIconView *parent, KMPrinter *p)
00028 : QIconViewItem(parent)
00029 {
00030     m_state = 0;
00031     m_mode = parent->itemTextPos();
00032     m_pixmap = QString::null;
00033     m_isclass = false;
00034     updatePrinter(p, m_mode);
00035 }
00036 
00037 void KMIconViewItem::paintItem(QPainter *p, const QColorGroup& cg)
00038 {
00039     if (m_state != 0)
00040     {
00041         QFont   f(p->font());
00042         if (m_state & 0x1) f.setBold(true);
00043         if (m_state & 0x2) f.setItalic(true);
00044         p->setFont(f);
00045     }
00046     QIconViewItem::paintItem(p,cg);
00047 }
00048 
00049 void KMIconViewItem::calcRect(const QString&)
00050 {
00051     QRect   ir(rect()), pr, tr;
00052 
00053     // pixmap rect
00054     pr.setWidth(pixmap()->width());
00055     pr.setHeight(pixmap()->height());
00056 
00057     // text rect
00058     QFont   f(iconView()->font());
00059     if (m_state & 0x1) f.setBold(true);
00060     if (m_state & 0x2) f.setItalic(true);
00061     QFontMetrics    fm(f);
00062     if (m_mode == QIconView::Bottom)
00063         tr = fm.boundingRect(0, 0, iconView()->maxItemWidth(), 0xFFFFFF, AlignHCenter|AlignTop|WordBreak|BreakAnywhere, text()+"X");
00064     else
00065         tr = fm.boundingRect(0, 0, 0xFFFFFF, 0xFFFFFF, AlignLeft|AlignTop, text()+"X");
00066 
00067     // item rect
00068     if (m_mode == QIconView::Bottom)
00069     {
00070         ir.setHeight(pr.height() + tr.height() + 15);
00071         ir.setWidth(QMAX(pr.width(), tr.width()) + 10);
00072         pr = QRect((ir.width()-pr.width())/2, 5, pr.width(), pr.height());
00073         tr = QRect((ir.width()-tr.width())/2, 10+pr.height(), tr.width(), tr.height());
00074     }
00075     else
00076     {
00077         ir.setHeight(QMAX(pr.height(), tr.height()) + 4);
00078         ir.setWidth(pr.width() + tr.width() + 6);
00079         pr = QRect(2, (ir.height()-pr.height())/2, pr.width(), pr.height());
00080         tr = QRect(4+pr.width(), (ir.height()-tr.height())/2, tr.width(), tr.height());
00081     }
00082 
00083     // set rects
00084     setItemRect(ir);
00085     setTextRect(tr);
00086     setPixmapRect(pr);
00087 }
00088 
00089 void KMIconViewItem::updatePrinter(KMPrinter *p, int mode)
00090 {
00091     bool    update(false);
00092     int oldstate = m_state;
00093     if (p)
00094     {
00095         m_state = ((p->isHardDefault() ? 0x1 : 0x0) | (p->ownSoftDefault() ? 0x2 : 0x0) | (p->isValid() ? 0x4 : 0x0));
00096         update = (oldstate != m_state);
00097         if (p->name() != text() || update)
00098         {
00099             setText(QString::null);
00100             setText(p->name());
00101         }
00102         setKey(QString::fromLatin1("%1_%2").arg((p->isSpecial() ? "special" : (p->isClass(false) ? "class" : "printer"))).arg(p->name()));
00103         m_isclass = p->isClass(false);
00104     }
00105     if (mode != m_mode || ((oldstate&0x4) != (m_state&0x4)) || (p && p->pixmap() != m_pixmap))
00106     {
00107         int iconstate = (m_state&0x4 ? (int)KIcon::DefaultState : (int)KIcon::LockOverlay);
00108         if (p)
00109             m_pixmap = p->pixmap();
00110         m_mode = mode;
00111         if (m_mode == QIconView::Bottom)
00112             setPixmap(DesktopIcon(m_pixmap, 0, iconstate));
00113         else
00114             setPixmap(SmallIcon(m_pixmap, 0, iconstate));
00115     }
00116     //if (update)
00117     //  repaint();
00118     setDiscarded(false);
00119 }
00120 
00121 KMIconView::KMIconView(QWidget *parent, const char *name)
00122 : KIconView(parent,name)
00123 {
00124     setMode(KIconView::Select);
00125     setSelectionMode(QIconView::Single);
00126     setItemsMovable(false);
00127     setResizeMode(QIconView::Adjust);
00128 
00129     m_items.setAutoDelete(false);
00130     setViewMode(KMIconView::Big);
00131 
00132     connect(this,SIGNAL(contextMenuRequested(QIconViewItem*,const QPoint&)),SLOT(slotRightButtonClicked(QIconViewItem*,const QPoint&)));
00133     connect(this,SIGNAL(selectionChanged()),SLOT(slotSelectionChanged()));
00134 }
00135 
00136 KMIconView::~KMIconView()
00137 {
00138 }
00139 
00140 KMIconViewItem* KMIconView::findItem(KMPrinter *p)
00141 {
00142     if (p)
00143     {
00144         QPtrListIterator<KMIconViewItem>    it(m_items);
00145         for (;it.current();++it)
00146             if (it.current()->text() == p->name()
00147                 && it.current()->isClass() == p->isClass())
00148                 return it.current();
00149     }
00150     return 0;
00151 }
00152 
00153 void KMIconView::setPrinterList(QPtrList<KMPrinter> *list)
00154 {
00155     bool    changed(false);
00156 
00157     QPtrListIterator<KMIconViewItem>    it(m_items);
00158     for (;it.current();++it)
00159         it.current()->setDiscarded(true);
00160 
00161     if (list)
00162     {
00163         QPtrListIterator<KMPrinter> it(*list);
00164         KMIconViewItem          *item(0);
00165         for (;it.current();++it)
00166         {
00167                         // only keep real printers (no instances)
00168                         if (!it.current()->instanceName().isEmpty())
00169                                 continue;
00170             item = findItem(it.current());
00171             if (!item)
00172             {
00173                 item = new KMIconViewItem(this,it.current());
00174                 m_items.append(item);
00175                 changed = true;
00176             }
00177             else
00178                 item->updatePrinter(it.current(), itemTextPos());
00179         }
00180     }
00181 
00182     for (uint i=0; i<m_items.count(); i++)
00183         if (m_items.at(i)->isDiscarded())
00184         {
00185             delete m_items.take(i);
00186             i--;
00187             changed = true;
00188         }
00189 
00190     if (changed) sort();
00191     emit selectionChanged();
00192 }
00193 
00194 void KMIconView::setViewMode(ViewMode m)
00195 {
00196     m_mode = m;
00197     bool    big = (m == KMIconView::Big);
00198     int mode = (big ? QIconView::Bottom : QIconView::Right);
00199 
00200     QPtrListIterator<KMIconViewItem>    it(m_items);
00201     for (;it.current();++it)
00202         it.current()->updatePrinter(0, mode);
00203 
00204     setArrangement((big ? QIconView::LeftToRight : QIconView::TopToBottom));
00205     setItemTextPos((QIconView::ItemTextPos)mode);
00206     //setGridX((big ? 60 : -1));
00207     setWordWrapIconText(true);
00208 }
00209 
00210 void KMIconView::slotRightButtonClicked(QIconViewItem *item, const QPoint& p)
00211 {
00212     emit rightButtonClicked(item ? item->text() : QString::null, p);
00213 }
00214 
00215 void KMIconView::slotSelectionChanged()
00216 {
00217     KMIconViewItem  *item = static_cast<KMIconViewItem*>(currentItem());
00218     emit printerSelected((item && !item->isDiscarded() && item->isSelected() ? item->text() : QString::null));
00219 }
00220 
00221 void KMIconView::setPrinter(const QString& prname)
00222 {
00223     QPtrListIterator<KMIconViewItem>    it(m_items);
00224     for (; it.current(); ++it)
00225         if (it.current()->text() == prname)
00226         {
00227             setSelected(it.current(), true);
00228             break;
00229         }
00230 }
00231 
00232 void KMIconView::setPrinter(KMPrinter *p)
00233 {
00234     setPrinter(p ? p->name() : QString::null);
00235 }
00236 
00237 #include "kmiconview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys