lib

editoritem.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
00004    Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "editoritem.h"
00023 #include "editor.h"
00024 #include "property.h"
00025 #include "widget.h"
00026 #include "factory.h"
00027 
00028 #include <qpainter.h>
00029 #include <qpixmap.h>
00030 #include <qheader.h>
00031 #include <qstyle.h>
00032 
00033 #ifdef QT_ONLY
00034 #else
00035 #include <kdebug.h>
00036 #include <kiconloader.h>
00037 #include <kstyle.h>
00038 #endif
00039 
00040 #define BRANCHBOX_SIZE 9
00041 
00042 namespace KoProperty {
00043 class EditorItemPrivate
00044 {
00045     public:
00046         EditorItemPrivate()
00047         : property(0) {}
00048         ~EditorItemPrivate() {}
00049 
00050         Property  *property;
00051         Editor  *editor;
00052 };
00053 }
00054 
00055 using namespace KoProperty;
00056 
00057 EditorItem::EditorItem(Editor *editor, EditorItem *parent, Property *property, QListViewItem *after)
00058  : KListViewItem(parent, after, 
00059     property->captionForDisplaying().isEmpty() ? property->name() : property->captionForDisplaying())
00060 {
00061     d = new EditorItemPrivate();
00062     d->property = property;
00063     d->editor = editor;
00064 
00065     setMultiLinesEnabled(true);
00066     //setHeight(static_cast<Editor*>(listView())->baseRowHeight()*3);
00067 /*
00068     if (property && !property->caption().isEmpty()) {
00069             QSimpleRichText srt(property->caption(), font());
00070             srt.setWidth(columnWidth(0)-KPROPEDITOR_ITEM_MARGIN*2-20+1);
00071             int oldHeight = it.current()->height();
00072             int textHeight = srt.height()+KPROPEDITOR_ITEM_MARGIN;
00073             int textLines = textHeight / d->baseRowHeight + (((textHeight % d->baseRowHeight) > 0) ? 1 : 0);
00074             kdDebug() << " textLines: " << textLines << endl;
00075             if (textLines != newNumLines) {
00076                 dynamic_cast<EditorItem*>(it.current())->setHeight(newNumLines * d->baseRowHeight);
00077             }
00078             kdDebug() << it.current()->text(0) << ":  "  << oldHeight << " -> " << newHeight << endl;
00079         }
00080 */
00081 }
00082 
00083 EditorItem::EditorItem(KListView *parent)
00084  : KListViewItem(parent)
00085 {
00086     d = new EditorItemPrivate();
00087     d->property = 0;
00088     d->editor = 0;
00089     setMultiLinesEnabled(true);
00090 }
00091 
00092 EditorItem::EditorItem(EditorItem *parent, const QString &text)
00093  : KListViewItem(parent, text)
00094 {
00095     d = new EditorItemPrivate();
00096     d->property = 0;
00097     d->editor = 0;
00098     setMultiLinesEnabled(true);
00099 }
00100 
00101 EditorItem::~EditorItem()
00102 {
00103     delete d;
00104 }
00105 
00106 Property*
00107 EditorItem::property()
00108 {
00109     return d->property;
00110 }
00111 
00112 void
00113 EditorItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int width, int align)
00114 {
00115     //int margin = static_cast<Editor*>(listView())->itemMargin();
00116     if(!d->property)
00117         return;
00118 
00119     if(column == 0)
00120     {
00121         QFont font = listView()->font();
00122         if(d->property->isModified())
00123             font.setBold(true);
00124         p->setFont(font);
00125         p->setBrush(cg.highlight());
00126         p->setPen(cg.highlightedText());
00127 #ifdef QT_ONLY
00128         QListViewItem::paintCell(p, cg, column, width, align);
00129 #else
00130         KListViewItem::paintCell(p, cg, column, width, align);
00131 #endif
00132         p->fillRect(parent() ? 0 : 50, 0, width, height()-1, 
00133             QBrush(isSelected() ? cg.highlight() : backgroundColor()));
00134         p->setPen(isSelected() ? cg.highlightedText() : cg.text());
00135         int delta = -20+KPROPEDITOR_ITEM_MARGIN;
00136         if ((firstChild() && dynamic_cast<EditorGroupItem*>(parent()))) {
00137             delta = -KPROPEDITOR_ITEM_MARGIN-1;
00138         }
00139         if (dynamic_cast<EditorDummyItem*>(parent())) {
00140             delta = KPROPEDITOR_ITEM_MARGIN*2;
00141         }
00142         else if (parent() && dynamic_cast<EditorDummyItem*>(parent()->parent())) {
00143             if (dynamic_cast<EditorGroupItem*>(parent()))
00144                 delta += KPROPEDITOR_ITEM_MARGIN*2;
00145             else
00146                 delta += KPROPEDITOR_ITEM_MARGIN*5;
00147         }
00148         p->drawText(
00149             QRect(delta,2, width+listView()->columnWidth(1)-KPROPEDITOR_ITEM_MARGIN*2, height()), 
00150             Qt::AlignLeft | Qt::AlignTop /*| Qt::SingleLine*/, text(0)); 
00151 
00152         p->setPen( KPROPEDITOR_ITEM_BORDER_COLOR );
00153         p->drawLine(width-1, 0, width-1, height()-1);
00154 
00155         p->setPen( KPROPEDITOR_ITEM_BORDER_COLOR ); 
00156         if (dynamic_cast<EditorDummyItem*>(parent()))
00157             p->drawLine(0, 0, 0, height()-1 );
00158     }
00159     else if(column == 1)
00160     {
00161         QColorGroup icg(cg);
00162 #ifdef QT_ONLY
00163         icg.setColor(QColorGroup::Background, cg.base());
00164 #else
00165         icg.setColor(QColorGroup::Background, backgroundColor());
00166         p->setBackgroundColor(backgroundColor());
00167 #endif
00168         Widget *widget = d->editor->createWidgetForProperty(d->property, false /*don't change Widget::property() */);
00169         if(widget) {
00170             QRect r(0, 0, d->editor->header()->sectionSize(1), height() - (widget->hasBorders() ? 1 : 2));
00171             p->setClipRect(r, QPainter::CoordPainter);
00172             p->setClipping(true);
00173             widget->drawViewer(p, icg, r, d->property->value());
00174             p->setClipping(false);
00175         }
00176     }
00177     p->setPen( KPROPEDITOR_ITEM_BORDER_COLOR ); 
00178     p->drawLine(0, height()-1, width, height()-1 );
00179 }
00180 
00181 void
00182 EditorItem::paintBranches(QPainter *p, const QColorGroup &cg, int w, int y, int h)
00183 {
00184     p->eraseRect(0,0,w,h);
00185 #ifdef QT_ONLY
00186     QListViewItem *item = firstChild();
00187 #else
00188     KListViewItem *item = static_cast<KListViewItem*>(firstChild());
00189 #endif
00190     if(!item)
00191         return;
00192 
00193     QColor backgroundColor;
00194     p->save();
00195     p->translate(0,y);
00196     QFont font = listView()->font();
00197     while(item)
00198     {
00199         if(item->isSelected())
00200             backgroundColor = cg.highlight();
00201         else {
00202 #ifdef QT_ONLY
00203             backgroundColor = cg.base();
00204 #else
00205             if (dynamic_cast<EditorGroupItem*>(item))
00206                 backgroundColor = cg.base();
00207             else
00208                 backgroundColor = item->backgroundColor();
00209 #endif
00210         }
00211 //      p->fillRect(-50,0,50, item->height(), QBrush(backgroundColor));
00212         p->save();
00213         p->setPen( KPROPEDITOR_ITEM_BORDER_COLOR );
00214         int delta = 0;
00215         int fillWidth = w;
00216         int x = 0;
00217         if (dynamic_cast<EditorGroupItem*>(item->parent())) {
00218             delta = 0;//-19;
00219             fillWidth += 19;
00220         }
00221         else {
00222             if (dynamic_cast<EditorGroupItem*>(item) || /*for flat mode*/ dynamic_cast<EditorDummyItem*>(item->parent()))
00223                 x = 19;
00224             else
00225                 x = -19;
00226             fillWidth += 19;
00227         }
00228         if (dynamic_cast<EditorDummyItem*>(item->parent())) {
00229             x = 19;
00230         }
00231         else if (item->parent() && dynamic_cast<EditorDummyItem*>(item->parent()->parent())) {
00232             x = 0;
00233         }
00234         p->fillRect(x+1, 0, fillWidth-1, item->height()-1, QBrush(backgroundColor));
00235         p->drawLine(x, item->height()-1, w, item->height()-1 );
00236         if (!dynamic_cast<EditorGroupItem*>(item))
00237             p->drawLine(x, 0, x, item->height()-1 );
00238         p->restore();
00239 
00240 //  for (int i=0; i<10000000; i++)
00241 //      ;
00242 //      if(item->isSelected())  {
00243 //          p->fillRect(parent() ? 0 : 50, 0, w, item->height()-1, QBrush(cg.highlight()));
00244 //          p->fillRect(-50,0,50, item->height(), QBrush(cg.highlight()));
00245 //      }
00246 
00247         //sorry, but we need to draw text here again
00248         font.setBold( dynamic_cast<EditorGroupItem*>(item) 
00249             || (static_cast<EditorItem*>(item)->property() && static_cast<EditorItem*>(item)->property()->isModified()) );
00250         p->setFont(font);
00251         p->setPen(item->isSelected() ? cg.highlightedText() : cg.text());
00252         if (item->firstChild() && dynamic_cast<EditorGroupItem*>(item->parent())) {
00253             delta = 19-KPROPEDITOR_ITEM_MARGIN-1;
00254         }
00255         else if (dynamic_cast<EditorDummyItem*>(item->parent())) {
00256             delta = 19;
00257         }
00258         if (item->parent() && dynamic_cast<EditorDummyItem*>(item->parent()->parent())) {
00259             if (dynamic_cast<EditorGroupItem*>(item->parent()))
00260                 delta += KPROPEDITOR_ITEM_MARGIN*2;
00261             else
00262                 delta += KPROPEDITOR_ITEM_MARGIN*5;
00263         }
00264 
00265         if (!dynamic_cast<EditorDummyItem*>(item->parent()))
00266             p->drawText(QRect(delta+1,0, w+listView()->columnWidth(1), item->height()), 
00267             Qt::AlignLeft | Qt::AlignVCenter /*| Qt::SingleLine*/, item->text(0)); 
00268 
00269         if(item->firstChild())  {
00271             KStyle* kstyle = dynamic_cast<KStyle*>(&listView()->style());
00272             const int lh = item->height(); 
00273             const int marg = (lh -2 - BRANCHBOX_SIZE) / 2;
00274             int xmarg = marg;
00275             if (dynamic_cast<EditorGroupItem*>(item))
00276                 xmarg = xmarg * 10 / 14 -1;
00277             if (kstyle) {
00278                 kstyle->drawKStylePrimitive( 
00279                     KStyle::KPE_ListViewExpander, p, listView(), 
00280                     QRect( xmarg, marg, BRANCHBOX_SIZE, BRANCHBOX_SIZE ), cg, item->isOpen() ? 0 : QStyle::Style_On,
00281                         QStyleOption::Default);
00282             }
00283             else {//draw by hand
00284                 p->setPen( KPROPEDITOR_ITEM_BORDER_COLOR );
00285                 p->drawRect(xmarg, marg, BRANCHBOX_SIZE, BRANCHBOX_SIZE);
00286                 p->fillRect(xmarg+1, marg + 1, BRANCHBOX_SIZE-2, BRANCHBOX_SIZE-2, 
00287                     item->listView()->paletteBackgroundColor());
00288                 p->setPen( item->listView()->paletteForegroundColor() );
00289                 p->drawLine(xmarg+2, marg+BRANCHBOX_SIZE/2, xmarg+BRANCHBOX_SIZE-3, marg+BRANCHBOX_SIZE/2);
00290                 if(!item->isOpen())
00291                     p->drawLine(xmarg+BRANCHBOX_SIZE/2, marg+2,
00292                         xmarg+BRANCHBOX_SIZE/2, marg+BRANCHBOX_SIZE-3);
00293             }
00294         }
00295 
00296         // draw icon (if there is one)
00297         EditorItem *editorItem = dynamic_cast<EditorItem*>(item);
00298         if (editorItem && editorItem->property() && !editorItem->property()->icon().isEmpty()) {
00299             //int margin = listView()->itemMargin();
00300             QPixmap pix = SmallIcon(editorItem->property()->icon());
00301             if (!pix.isNull())
00302                 p->drawPixmap(1, (item->height() - pix.height()) / 2, pix);
00303         }
00304 
00305         p->translate(0, item->totalHeight());
00306 #ifdef QT_ONLY
00307         item = item->nextSibling();
00308 #else
00309         item = (KListViewItem*)item->nextSibling();
00310 #endif
00311     }
00312     p->restore();
00313 }
00314 
00315 void
00316 EditorItem::paintFocus(QPainter *, const QColorGroup &, const QRect & )
00317 {}
00318 
00319 int
00320 EditorItem::compare( QListViewItem *i, int col, bool ascending ) const
00321 {
00322     if (!ascending)
00323         return -QListViewItem::key( col, ascending ).localeAwareCompare( i->key( col, ascending ) );
00324 
00325     if (d->property) {
00326 //      kopropertydbg << d->property->name() << " " << d->property->sortingKey() << " | "
00327 //          << static_cast<EditorItem*>(i)->property()->name() << " "
00328 //          << static_cast<EditorItem*>(i)->property()->sortingKey() << endl;
00329         return d->property->sortingKey() 
00330             - ((dynamic_cast<EditorItem*>(i) && dynamic_cast<EditorItem*>(i)->property()) ? dynamic_cast<EditorItem*>(i)->property()->sortingKey() : 0);
00331     }
00332 
00333     return 0;
00334 //  return d->order - static_cast<EditorItem*>(i)->d->order;
00335 }
00336 
00337 void
00338 EditorItem::setHeight( int height )
00339 {
00340     KListViewItem::setHeight(height);
00341 }
00342 
00344 
00345 EditorGroupItem::EditorGroupItem(EditorItem *parent, const QString &text)
00346  : EditorItem(parent, text)
00347 {
00348     setOpen(true);
00349     setSelectable(false);
00350 }
00351 
00352 EditorGroupItem::~EditorGroupItem()
00353 {}
00354 
00355 void
00356 EditorGroupItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int width, int /*align*/)
00357 {
00358     //if(column == 1)
00359     //  return;
00360     p->setPen( KPROPEDITOR_ITEM_BORDER_COLOR ); 
00361     p->drawLine(0, height()-1, width-1, height()-1);
00362     if (column==0) {
00363         p->eraseRect(QRect(0,0,width+listView()->columnWidth(1),height()-1));
00364     }
00365     else {
00366         return;
00367     }
00368 
00369     QFont font = listView()->font();
00370     font.setBold(true);
00371     p->setFont(font);
00372     p->setBrush(cg.highlight());
00373     p->setPen(cg.highlightedText());
00374 /*
00375 #ifdef QT_ONLY
00376         QListViewItem::paintCell(p, cg, column, width, align);
00377 #else
00378         KListViewItem::paintCell(p, cg, column, width, align);
00379 #endif*/
00380     p->setPen(cg.text());
00381     p->drawText(QRect(0,0, width+listView()->columnWidth(1), height()), 
00382         Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine, text(0)); 
00383 }
00384 
00385 void
00386 EditorGroupItem::setup()
00387 {
00388     KListViewItem::setup();
00389     setHeight( int(height()*14/10) );
00390 }
00391 
00393 
00394 EditorDummyItem::EditorDummyItem(KListView *listview)
00395  : EditorItem(listview)
00396 {
00397     setSelectable(false);
00398     setOpen(true);
00399 }
00400 
00401 EditorDummyItem::~EditorDummyItem()
00402 {}
00403 
00404 void
00405 EditorDummyItem::setup()
00406 {
00407     setHeight(0);
00408 }
KDE Home | KDE Accessibility Home | Description of Access Keys