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