lib

widget.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 
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 as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "widget.h"
00022 #include "property.h"
00023 #include "editoritem.h"
00024 #include "editor.h"
00025 
00026 #include <qpainter.h>
00027 #include <qvariant.h>
00028 
00029 #ifdef QT_ONLY
00030 #include <qlistview.h>
00031 #else
00032 #include <klistview.h>
00033 #include <kdebug.h>
00034 #endif
00035 
00036 using namespace KoProperty;
00037 
00038 namespace KoProperty {
00039 class WidgetPrivate
00040 {
00041     public:
00042         WidgetPrivate()
00043         : property(0)
00044         , editor(0)
00045         , leaveTheSpaceForRevertButton(false)
00046         , hasBorders(true)
00047         , readOnly(false)
00048         , visibleFlag(true)
00049         {
00050         }
00051         ~WidgetPrivate() {}
00052 
00053         Property *property;
00054         QWidget *editor;
00055         bool leaveTheSpaceForRevertButton : 1;
00056         bool hasBorders : 1;
00057         bool readOnly : 1;
00058         bool visibleFlag : 1;
00059 };
00060 }
00061 
00062 Widget::Widget(Property *property, QWidget *parent, const char *name)
00063  : QWidget(parent, name)
00064 {
00065     d = new WidgetPrivate();
00066     d->property = property;
00067 }
00068 
00069 Widget::~Widget()
00070 {
00071     delete d;
00072     d = 0;
00073 }
00074 
00075 Property*
00076 Widget::property() const
00077 {
00078     return d ? d->property : 0; //for sanity
00079 }
00080 
00081 void
00082 Widget::setProperty(Property *property)
00083 {
00084     d->property = property;
00085     if(property)
00086         setValue(property->value(), false);
00087     //if(property->type() == ValueFromList)
00088     //  setValueList(property->valueList());
00089 }
00090 
00091 void
00092 Widget::drawViewer(QPainter *p, const QColorGroup &, const QRect &r, const QVariant &value)
00093 {
00094     p->eraseRect(r);
00095     QRect rect(r);
00096     rect.setLeft(rect.left()+KPROPEDITOR_ITEM_MARGIN);
00097 //  if (d->hasBorders)
00098 //      rect.setTop(rect.top()+1); //+1 to have the same vertical position as editor
00099 //  else
00100 //      rect.setHeight(rect.height()-1); //don't place over listviews's border
00101     p->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine, value.toString());
00102 }
00103 
00104 void
00105 Widget::undo()
00106 {
00107     if(d->property)
00108         d->property->resetValue();
00109 }
00110 
00111 bool
00112 Widget::eventFilter(QObject*, QEvent* e)
00113 {
00114     if(e->type() == QEvent::KeyPress)
00115     {
00116         QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00117         if(ev->key() == Key_Escape)
00118         {
00119             emit rejectInput(this);
00120             return true;
00121         }
00122         else if((ev->key() == Key_Return) || (ev->key() == Key_Enter))
00123         {
00124             // should apply when autosync == false
00125             emit acceptInput(this);
00126             return true;
00127         }
00128         else {
00129             Editor *list = static_cast<KoProperty::Editor*>(parentWidget()->parentWidget());
00130             if (!list)
00131                 return false; //for sanity
00132             return list->handleKeyPress(ev);
00133         }
00134 
00135         /* moved in Editor
00136         if (item) {
00137             if(ev->key() == Key_Up && ev->state() != ControlButton)
00138             {
00139                 if(item->itemAbove())
00140                     list->setCurrentItem(item->itemAbove());
00141                 return true;
00142             }
00143             else if(ev->key() == Key_Down && ev->state() != ControlButton)
00144             {
00145                 if(item->itemBelow())
00146                     list->setCurrentItem(item->itemBelow());
00147                 return true;
00148             }
00149         }*/
00150     }
00151 
00152     return false;
00153 }
00154 
00155 void
00156 Widget::setFocusWidget(QWidget*focusProxy)
00157 {
00158     if (focusProxy) {
00159         if (focusProxy->focusPolicy() != NoFocus)
00160             setFocusProxy(focusProxy);
00161         focusProxy->installEventFilter(this);
00162     }
00163     else if (this->focusProxy()) {
00164         this->focusProxy()->removeEventFilter(this);
00165         setFocusProxy(0);
00166     }
00167 }
00168 
00169 bool
00170 Widget::leavesTheSpaceForRevertButton() const
00171 {
00172     return d->leaveTheSpaceForRevertButton;
00173 }
00174 
00175 void
00176 Widget::setLeavesTheSpaceForRevertButton(bool set)
00177 {
00178     d->leaveTheSpaceForRevertButton = set;
00179 }
00180 
00181 void
00182 Widget::setHasBorders(bool set)
00183 {
00184     d->hasBorders = set;
00185 }
00186 
00187 bool
00188 Widget::hasBorders() const
00189 {
00190     return d->hasBorders;
00191 }
00192 
00193 void
00194 Widget::setEditor(QWidget* editor)
00195 {
00196     d->editor = editor;
00197     if (!d->editor)
00198         return;
00199     d->editor->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00200     d->editor->move(0,0);
00201 }
00202 
00203 void
00204 Widget::resizeEvent(QResizeEvent *e)
00205 {
00206     QWidget::resizeEvent(e);
00207     if (d->editor)
00208         d->editor->resize(size());
00209 }
00210 
00211 bool
00212 Widget::isReadOnly() const
00213 {
00214     return d->readOnly;
00215 }
00216 
00217 void
00218 Widget::setReadOnly(bool readOnly)
00219 {
00220     d->readOnly = readOnly;
00221     setReadOnlyInternal(readOnly);
00222 }
00223 
00224 bool 
00225 Widget::visibleFlag() const
00226 {
00227     return d->visibleFlag;
00228 }
00229 
00230 void
00231 Widget::setVisibleFlag(bool visible)
00232 {
00233     d->visibleFlag = visible;
00234 }
00235 
00236 #include "widget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys