lib

combobox.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 #include "combobox.h"
00021 
00022 #include <qlayout.h>
00023 #include <qmap.h>
00024 #include <qvariant.h>
00025 #include <qpainter.h>
00026 
00027 #ifdef QT_ONLY
00028 #iinclude <qcombobox.h>
00029 #else
00030 #include <kcombobox.h>
00031 #include <kdebug.h>
00032 #endif
00033 
00034 #include "property.h"
00035 
00036 using namespace KoProperty;
00037 
00038 ComboBox::ComboBox(Property *property, QWidget *parent, const char *name)
00039  : Widget(property, parent, name)
00040  , m_setValueEnabled(true)
00041 {
00042     QHBoxLayout *l = new QHBoxLayout(this, 0, 0);
00043 #ifdef QT_ONLY
00044     m_edit = new QComboBox(this);
00045 #else
00046     m_edit = new KComboBox(this);
00047 #endif
00048     m_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00049     m_edit->setMinimumHeight(5);
00050     l->addWidget(m_edit);
00051 
00052     m_edit->setEditable(false);
00053     m_edit->setInsertionPolicy(QComboBox::NoInsertion);
00054     m_edit->setMinimumSize(10, 0); // to allow the combo to be resized to a small size
00055     m_edit->setAutoCompletion(true);
00056 #ifndef QT_ONLY
00057     m_edit->setContextMenuEnabled(false);
00058 #endif
00059 
00060     if (this->property()->listData()) {
00061         fillBox();
00062     }
00063 //not needed for combo  setLeavesTheSpaceForRevertButton(true);
00064 
00065     setFocusWidget(m_edit);
00066     connect(m_edit, SIGNAL(activated(int)), this, SLOT(slotValueChanged(int)));
00067 }
00068 
00069 ComboBox::~ComboBox()
00070 {
00071 }
00072 
00073 QVariant
00074 ComboBox::value() const
00075 {
00076     if (!property()->listData()) {
00077         kopropertywarn << "ComboBox::value(): propery listData not available!" << endl;
00078         return QVariant();
00079     }
00080     const int idx = m_edit->currentItem();
00081     if (idx<0 || idx>=(int)property()->listData()->keys.count())
00082         return QVariant();
00083     return QVariant( property()->listData()->keys[idx] );
00084 //  if(property()->listData() && property()->listData()->contains(m_edit->currentText()))
00085 //      return (*(property()->valueList()))[m_edit->currentText()];
00086 //  return QVariant();
00087 }
00088 
00089 void
00090 ComboBox::setValue(const QVariant &value, bool emitChange)
00091 {
00092     if (!property()->listData()) {
00093         kopropertywarn << "ComboBox::value(): propery listData not available!" << endl;
00094         return;
00095     }
00096     if (!m_setValueEnabled)
00097         return;
00098     int idx = property()->listData()->keys.findIndex( value );
00099     if (idx>=0 && idx<m_edit->count()) {
00100         m_edit->setCurrentItem(idx);
00101     }
00102     else {
00103         if (idx<0) {
00104             kopropertywarn << "ComboBox::setValue(): NO SUCH KEY '" << value.toString() 
00105                 << "' (property '" << property()->name() << "')" << endl;
00106         } else {
00107             QStringList list;
00108             for (int i=0; i<m_edit->count(); i++)
00109                 list += m_edit->text(i);
00110             kopropertywarn << "ComboBox::setValue(): NO SUCH INDEX WITHIN COMBOBOX: " << idx 
00111                 << " count=" << m_edit->count() << " value='" << value.toString() 
00112                 << "' (property '" << property()->name() << "')\nActual combobox contents: "
00113                 << list << endl;
00114         }
00115         m_edit->setCurrentText(QString::null);
00116     }
00117 
00118     if(value.isNull())
00119         return;
00120 
00121 //  m_edit->blockSignals(true);
00122 //  m_edit->setCurrentText(keyForValue(value));
00123 //  m_edit->blockSignals(false);
00124     if (emitChange)
00125         emit valueChanged(this);
00126 }
00127 
00128 void
00129 ComboBox::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value)
00130 {
00131     QString txt;
00132     if (property()->listData()) {
00133         const int idx = property()->listData()->keys.findIndex( value );
00134         if (idx>=0)
00135             txt = property()->listData()->names[ idx ];
00136     }
00137 
00138     Widget::drawViewer(p, cg, r, txt); //keyForValue(value));
00139 //  p->eraseRect(r);
00140 //  p->drawText(r, Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine, keyForValue(value));
00141 }
00142 
00143 void
00144 ComboBox::fillBox()
00145 {
00146     m_edit->clear();
00147     //m_edit->clearContents();
00148 
00149     if(!property())
00150         return;
00151     if (!property()->listData()) {
00152         kopropertywarn << "ComboBox::fillBox(): propery listData not available!" << endl;
00153         return;
00154     }
00155 
00156     m_edit->insertStringList(property()->listData()->names);
00157 #ifndef QT_ONLY
00158     KCompletion *comp = m_edit->completionObject();
00159     comp->insertItems(property()->listData()->names);
00160     comp->setCompletionMode(KGlobalSettings::CompletionShell);
00161 #endif
00162 }
00163 
00164 void
00165 ComboBox::setProperty(Property *prop)
00166 {
00167     const bool b = (property() == prop);
00168     m_setValueEnabled = false; //setValue() couldn't be called before fillBox()
00169     Widget::setProperty(prop);
00170     m_setValueEnabled = true;
00171     if(!b)
00172         fillBox();
00173     if(prop)
00174         setValue(prop->value(), false); //now the value can be set
00175 }
00176 
00177 void
00178 ComboBox::slotValueChanged(int)
00179 {
00180     emit valueChanged(this);
00181 }
00182 
00183 void
00184 ComboBox::setReadOnlyInternal(bool readOnly)
00185 {
00186     setVisibleFlag(!readOnly);
00187 }
00188 
00189 
00190 /*QString
00191 ComboBox::keyForValue(const QVariant &value)
00192 {
00193     const QMap<QString, QVariant> *list = property()->valueList();
00194     Property::ListData *list = property()->listData();
00195 
00196     if (!list)
00197         return QString::null;
00198     int idx = listData->keys.findIndex( value );
00199 
00200 
00201     QMap<QString, QVariant>::ConstIterator endIt = list->constEnd();
00202     for(QMap<QString, QVariant>::ConstIterator it = list->constBegin(); it != endIt; ++it) {
00203         if(it.data() == value)
00204             return it.key();
00205     }
00206     return QString::null;
00207 }*/
00208 
00209 
00210 #include "combobox.moc"
00211 
KDE Home | KDE Accessibility Home | Description of Access Keys