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 }
00073 
00074 Property*
00075 Widget::property() const
00076 {
00077     return d->property;
00078 }
00079 
00080 void
00081 Widget::setProperty(Property *property)
00082 {
00083     d->property = property;
00084     if(property)
00085         setValue(property->value(), false);
00086     //if(property->type() == ValueFromList)
00087     //  setValueList(property->valueList());
00088 }
00089 
00090 void
00091 Widget::drawViewer(QPainter *p, const QColorGroup &, const QRect &r, const QVariant &value)
00092 {
00093     p->eraseRect(r);
00094     QRect rect(r);
00095     rect.setLeft(rect.left()+KPROPEDITOR_ITEM_MARGIN);
00096 //  if (d->hasBorders)
00097 //      rect.setTop(rect.top()+1); //+1 to have the same vertical position as editor
00098 //  else
00099 //      rect.setHeight(rect.height()-1); //don't place over listviews's border
00100     p->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine, value.toString());
00101 }
00102 
00103 void
00104 Widget::undo()
00105 {
00106     if(d->property)
00107         d->property->resetValue();
00108 }
00109 
00110 bool
00111 Widget::eventFilter(QObject*, QEvent* e)
00112 {
00113     if(e->type() == QEvent::KeyPress)
00114     {
00115         QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00116         if(ev->key() == Key_Escape)
00117         {
00118             emit rejectInput(this);
00119             return true;
00120         }
00121         else if((ev->key() == Key_Return) || (ev->key() == Key_Enter))
00122         {
00123             // should apply when autosync == false
00124             emit acceptInput(this);
00125             return true;
00126         }
00127         else {
00128             Editor *list = static_cast<KoProperty::Editor*>(parentWidget()->parentWidget());
00129             if (!list)
00130                 return false; //for sanity
00131             return list->handleKeyPress(ev);
00132         }
00133 
00134         /* moved in Editor
00135         if (item) {
00136             if(ev->key() == Key_Up && ev->state() != ControlButton)
00137             {
00138                 if(item->itemAbove())
00139                     list->setCurrentItem(item->itemAbove());
00140                 return true;
00141             }
00142             else if(ev->key() == Key_Down && ev->state() != ControlButton)
00143             {
00144                 if(item->itemBelow())
00145                     list->setCurrentItem(item->itemBelow());
00146                 return true;
00147             }
00148         }*/
00149     }
00150 
00151     return false;
00152 }
00153 
00154 void
00155 Widget::setFocusWidget(QWidget*focusProxy)
00156 {
00157     if (focusProxy) {
00158         if (focusProxy->focusPolicy() != NoFocus)
00159             setFocusProxy(focusProxy);
00160         focusProxy->installEventFilter(this);
00161     }
00162     else if (this->focusProxy()) {
00163         this->focusProxy()->removeEventFilter(this);
00164         setFocusProxy(0);
00165     }
00166 }
00167 
00168 bool
00169 Widget::leavesTheSpaceForRevertButton() const
00170 {
00171     return d->leaveTheSpaceForRevertButton;
00172 }
00173 
00174 void
00175 Widget::setLeavesTheSpaceForRevertButton(bool set)
00176 {
00177     d->leaveTheSpaceForRevertButton = set;
00178 }
00179 
00180 void
00181 Widget::setHasBorders(bool set)
00182 {
00183     d->hasBorders = set;
00184 }
00185 
00186 bool
00187 Widget::hasBorders() const
00188 {
00189     return d->hasBorders;
00190 }
00191 
00192 void
00193 Widget::setEditor(QWidget* editor)
00194 {
00195     d->editor = editor;
00196     if (!d->editor)
00197         return;
00198     d->editor->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00199     d->editor->move(0,0);
00200 }
00201 
00202 void
00203 Widget::resizeEvent(QResizeEvent *e)
00204 {
00205     QWidget::resizeEvent(e);
00206     if (d->editor)
00207         d->editor->resize(size());
00208 }
00209 
00210 bool
00211 Widget::isReadOnly() const
00212 {
00213     return d->readOnly;
00214 }
00215 
00216 void
00217 Widget::setReadOnly(bool readOnly)
00218 {
00219     d->readOnly = readOnly;
00220     setReadOnlyInternal(readOnly);
00221 }
00222 
00223 bool 
00224 Widget::visibleFlag() const
00225 {
00226     return d->visibleFlag;
00227 }
00228 
00229 void
00230 Widget::setVisibleFlag(bool visible)
00231 {
00232     d->visibleFlag = visible;
00233 }
00234 
00235 #include "widget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys