00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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);
00050 m_edit->setAutoCompletion(true);
00051 m_edit->setContextMenuEnabled(false);
00052
00053 if (this->property()->listData()) {
00054 fillBox();
00055 }
00056
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
00078
00079
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
00116
00117
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);
00133
00134
00135 }
00136
00137 void
00138 ComboBox::fillBox()
00139 {
00140 m_edit->clear();
00141
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;
00161 Widget::setProperty(prop);
00162 m_setValueEnabled = true;
00163 if(!b)
00164 fillBox();
00165 if(prop)
00166 setValue(prop->value(), false);
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
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 #include "combobox.moc"
00203