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 #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);
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
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
00085
00086
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
00122
00123
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);
00139
00140
00141 }
00142
00143 void
00144 ComboBox::fillBox()
00145 {
00146 m_edit->clear();
00147
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;
00169 Widget::setProperty(prop);
00170 m_setValueEnabled = true;
00171 if(!b)
00172 fillBox();
00173 if(prop)
00174 setValue(prop->value(), false);
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
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 #include "combobox.moc"
00211
|