F:/KPlato/koffice/libs/koproperty/editors/combobox.cpp

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

Généré le Wed Nov 22 23:41:06 2006 pour KPlato par  doxygen 1.5.1-p1