lib
combobox.cpp00001
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.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);
00047 m_edit->setAutoCompletion(true);
00048 m_edit->setContextMenuEnabled(false);
00049
00050 if (this->property()->listData()) {
00051 fillBox();
00052 }
00053
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
00075
00076
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
00112
00113
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);
00129
00130
00131 }
00132
00133 void
00134 ComboBox::fillBox()
00135 {
00136 m_edit->clear();
00137
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;
00157 Widget::setProperty(prop);
00158 m_setValueEnabled = true;
00159 if(!b)
00160 fillBox();
00161 if(prop)
00162 setValue(prop->value(), false);
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
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 #include "combobox.moc"
00199
|