kpilot Library API Documentation

listCat.cc

00001 /* listCat.cc           KPilot
00002 **
00003 ** Copyright (C) 2000-2001 by Adriaan de Groot
00004 **
00005 ** This file defines a specialization of KListView that can
00006 ** be used to sort some fixed set of object into some fixed
00007 ** set of categories.
00008 */
00009 
00010 /*
00011 ** This program is free software; you can redistribute it and/or modify
00012 ** it under the terms of the GNU General Public License as published by
00013 ** the Free Software Foundation; either version 2 of the License, or
00014 ** (at your option) any later version.
00015 **
00016 ** This program is distributed in the hope that it will be useful,
00017 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 ** GNU General Public License for more details.
00020 **
00021 ** You should have received a copy of the GNU General Public License
00022 ** along with this program in a file called COPYING; if not, write to
00023 ** the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
00024 ** MA 02111-1307, USA.
00025 */
00026 
00027 /*
00028 ** Bug reports and questions can be sent to kde-pim@kde.org
00029 */
00030 
00031 static const char *listCat_id =
00032     "$Id: listCat.cc,v 1.11 2003/03/08 01:43:07 waba Exp $";
00033 
00034 #include "options.h"
00035 
00036 #include <qpainter.h>
00037 #include <klocale.h>
00038 
00039 #include "listCat.moc"
00040 
00041 
00042 ListCategorizer::ListCategorizer(QWidget * parent,
00043     const char *name) :
00044     KListView(parent, name), 
00045     fStartOpen(false)
00046 {
00047     FUNCTIONSETUP;
00048     setupWidget();
00049     (void) listCat_id;
00050 }
00051 
00052 ListCategorizer::ListCategorizer(const QStringList & i,
00053     bool startOpen,
00054     QWidget * parent,
00055     const char *name) :
00056     KListView(parent, name), 
00057     fStartOpen(startOpen)
00058 {
00059     FUNCTIONSETUP;
00060     addCategories(i);
00061 }
00062 
00063 void ListCategorizer::addCategories(const QStringList & l)
00064 {
00065     FUNCTIONSETUP;
00066     QStringList::ConstIterator i;
00067 
00068     for (i = l.begin(); i != l.end(); ++i)
00069     {
00070         (void) addCategory(*i);
00071     }
00072 }
00073 
00074 QListViewItem *ListCategorizer::addCategory(const QString & name,
00075     const QString & desc)
00076 {
00077     FUNCTIONSETUP;
00078     QListViewItem *m = new QListViewItem(this, name, desc);
00079 
00080     m->setSelectable(false);
00081     m->setOpen(fStartOpen);
00082     return m;
00083 }
00084 
00085 void ListCategorizer::setupWidget()
00086 {
00087     FUNCTIONSETUP;
00088     addColumn(i18n("Category"));
00089     addColumn(i18n("Description"));
00090     setItemsMovable(false);
00091     setDragEnabled(true);
00092     setAcceptDrops(true);
00093     setDropVisualizer(true);
00094     setRootIsDecorated(true);
00095 }
00096 
00097 /* virtual */ bool ListCategorizer::acceptDrag(QDropEvent * event) const
00098 {
00099     FUNCTIONSETUP;
00100     if (!(event->source()))
00101         return false;
00102     QListViewItem *p = itemAt(event->pos());
00103 
00104     if (!p)
00105         return false;
00106 
00107     return true;
00108 }
00109 
00110 /* virtual */ void ListCategorizer::contentsDropEvent(QDropEvent * e)
00111 {
00112     FUNCTIONSETUP;
00113     cleanDropVisualizer();
00114 
00115     if (!acceptDrag(e))
00116         return;
00117     e->accept();
00118 
00119     QListViewItem *p = itemAt(e->pos());
00120     QListViewItem *selection = currentItem();
00121 
00122     if (!p)
00123     {
00124         kdWarning() << "Drop without a category!" << endl;
00125         return;
00126     }
00127 
00128     QListViewItem *category = p->parent();
00129 
00130     if (!category)
00131     {
00132         category = p;
00133     }
00134 
00135     moveItem(selection, category, 0L);
00136 }
00137 
00138 /* virtual */ void ListCategorizer::startDrag()
00139 {
00140     FUNCTIONSETUP;
00141     QListViewItem *p = currentItem();
00142 
00143     if (!p || !p->parent())
00144         return;
00145 
00146     KListView::startDrag();
00147 }
00148 
00149 QStringList ListCategorizer::listSiblings(const QListViewItem * p, int column) const
00150 {
00151     FUNCTIONSETUP;
00152     QStringList l;
00153 
00154     while (p)
00155     {
00156         l.append(p->text(column));
00157         p = p->nextSibling();
00158     }
00159 
00160     return l;
00161 }
00162 
00163 QListViewItem *ListCategorizer::findCategory(const QString & category) const
00164 {
00165     FUNCTIONSETUP;
00166     QListViewItem *p = firstChild();
00167 
00168     while (p)
00169     {
00170         if (p->text(0) == category)
00171             return p;
00172         p = p->nextSibling();
00173     }
00174 
00175     return 0L;
00176 }
00177 
00178 QListViewItem *ListCategorizer::addItem(const QString & category,
00179     const QString & name, const QString & description)
00180 {
00181     FUNCTIONSETUP;
00182     QListViewItem *p = findCategory(category);
00183 
00184     if (!p)
00185         return 0L;
00186 
00187     return new QListViewItem(p, name, description);
00188 }
00189 
00190 #define RVPAD   (4)
00191 
00192 RichListViewItem::RichListViewItem(QListViewItem *p,
00193     QString l,
00194     int c) :
00195     QListViewItem(p,l)
00196 {
00197     FUNCTIONSETUP;
00198 
00199     fColumns=c;
00200     fIsRich = new bool[c];
00201     fRect = new QRect[c];
00202 
00203     for (int i=0; i<c; i++)
00204     {
00205         fIsRich[i]=false;
00206     }
00207 }
00208 
00209 RichListViewItem::~RichListViewItem()
00210 {
00211     FUNCTIONSETUP;
00212 
00213     delete[] fIsRich;
00214     delete[] fRect;
00215 }
00216 
00217 void RichListViewItem::computeHeight(int c)
00218 {
00219     FUNCTIONSETUP;
00220 
00221     if (!fIsRich[c]) return;
00222 
00223     QListView *v = listView();
00224     
00225     fRect[c] = v->fontMetrics().boundingRect(v->itemMargin()+RVPAD,0+RVPAD,
00226         v->columnWidth(c)-v->itemMargin()-RVPAD,300,
00227         AlignLeft | AlignTop | WordBreak,
00228         text(c));
00229 }
00230 
00231 
00232 /* virtual */ void RichListViewItem::setup()
00233 {
00234     FUNCTIONSETUP;
00235 
00236     QListViewItem::setup();
00237 
00238     int h = height();
00239 
00240     for (int i=0; i<fColumns; i++)
00241     {
00242         computeHeight(i);
00243         h = QMAX(h,fRect[i].height()+2*RVPAD);
00244     }
00245 
00246     setHeight(h);
00247 }
00248 
00249 
00250 /* virtual */ void RichListViewItem::paintCell(QPainter *p,
00251     const QColorGroup &gc,
00252     int column,
00253     int width,
00254     int alignment)
00255 {
00256     FUNCTIONSETUP;
00257 
00258     if ((!column) || (!fIsRich[column]))
00259     {
00260         QListViewItem::paintCell(p,gc,column,width,alignment);
00261         return;
00262     }
00263 
00264     QListView *v = listView();
00265 
00266     p->eraseRect(0,0,width,height());
00267     p->setBackgroundColor(gc.background());
00268     p->eraseRect(RVPAD,RVPAD,width-RVPAD,height()-RVPAD);
00269     p->setPen(gc.text());
00270     p->drawText(v->itemMargin()+RVPAD,0+RVPAD,
00271         width-v->itemMargin()-RVPAD,height()-RVPAD,
00272         AlignTop | AlignLeft | WordBreak,
00273         text(column),
00274         -1,
00275         &fRect[column]);
00276 }
KDE Logo
This file is part of the documentation for kpilot Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:48 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003