00001 #include <kdeveditlistbox.h>
00002
00003 #if KDE_VERSION <= 305
00004
00005 #include <qlayout.h>
00006 #include <qpushbutton.h>
00007 #include <kdialog.h>
00008 #include <klocale.h>
00009
00010 namespace KDevCompat {
00011
00012 KEditListBox::KEditListBox(QWidget *parent, const char *name,
00013 bool checkAtEntering, int buttons )
00014 :QGroupBox(parent, name )
00015 {
00016 init( checkAtEntering, buttons );
00017 }
00018
00019 KEditListBox::KEditListBox(const QString& title, QWidget *parent,
00020 const char *name, bool checkAtEntering, int buttons)
00021 :QGroupBox(title, parent, name )
00022 {
00023 init( checkAtEntering, buttons );
00024 }
00025
00026 KEditListBox::KEditListBox(const QString& title, const CustomEditor& custom,
00027 QWidget *parent, const char *name,
00028 bool checkAtEntering, int buttons)
00029 :QGroupBox(title, parent, name )
00030 {
00031 m_lineEdit = custom.lineEdit();
00032 init( checkAtEntering, buttons, custom.representationWidget() );
00033 }
00034
00035 KEditListBox::~KEditListBox()
00036 {
00037 delete d;
00038 d=0;
00039 }
00040
00041 void KEditListBox::init( bool checkAtEntering, int buttons,
00042 QWidget *representationWidget )
00043 {
00044 d=new KEditListBoxPrivate;
00045 d->m_checkAtEntering=checkAtEntering;
00046 d->buttons = buttons;
00047
00048 int lostButtons = 0;
00049 if ( (buttons & Add) == 0 )
00050 lostButtons++;
00051 if ( (buttons & Remove) == 0 )
00052 lostButtons++;
00053 if ( (buttons & UpDown) == 0 )
00054 lostButtons += 2;
00055
00056
00057 servNewButton = servRemoveButton = servUpButton = servDownButton = 0L;
00058 setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,
00059 QSizePolicy::MinimumExpanding));
00060
00061 QWidget * gb = this;
00062 QGridLayout * grid = new QGridLayout(gb, 7 - lostButtons, 2,
00063 KDialog::marginHint(),
00064 KDialog::spacingHint());
00065 grid->addRowSpacing(0, fontMetrics().lineSpacing());
00066 for ( int i = 1; i < 7 - lostButtons; i++ )
00067 grid->setRowStretch(i, 1);
00068
00069 grid->setMargin(15);
00070
00071 if ( representationWidget )
00072 representationWidget->reparent( gb, QPoint(0,0) );
00073 else
00074 m_lineEdit=new KLineEdit(gb);
00075
00076 m_listBox = new QListBox(gb);
00077
00078 QWidget *editingWidget = representationWidget ?
00079 representationWidget : m_lineEdit;
00080 grid->addMultiCellWidget(editingWidget,1,1,0,1);
00081 grid->addMultiCellWidget(m_listBox, 2, 6 - lostButtons, 0, 0);
00082 int row = 2;
00083 if ( buttons & Add ) {
00084 servNewButton = new QPushButton(i18n("&Add"), gb);
00085 servNewButton->setEnabled(false);
00086 connect(servNewButton, SIGNAL(clicked()), SLOT(addItem()));
00087
00088 grid->addWidget(servNewButton, row++, 1);
00089 }
00090
00091 if ( buttons & Remove ) {
00092 servRemoveButton = new QPushButton(i18n("&Remove"), gb);
00093 servRemoveButton->setEnabled(false);
00094 connect(servRemoveButton, SIGNAL(clicked()), SLOT(removeItem()));
00095
00096 grid->addWidget(servRemoveButton, row++, 1);
00097 }
00098
00099 if ( buttons & UpDown ) {
00100 servUpButton = new QPushButton(i18n("Move &Up"), gb);
00101 servUpButton->setEnabled(false);
00102 connect(servUpButton, SIGNAL(clicked()), SLOT(moveItemUp()));
00103
00104 servDownButton = new QPushButton(i18n("Move &Down"), gb);
00105 servDownButton->setEnabled(false);
00106 connect(servDownButton, SIGNAL(clicked()), SLOT(moveItemDown()));
00107
00108 grid->addWidget(servUpButton, row++, 1);
00109 grid->addWidget(servDownButton, row++, 1);
00110 }
00111
00112 connect(m_lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(typedSomething(const QString&)));
00113 m_lineEdit->setTrapReturnKey(true);
00114 connect(m_lineEdit,SIGNAL(returnPressed()),this,SLOT(addItem()));
00115 connect(m_listBox, SIGNAL(highlighted(int)), SLOT(enableMoveButtons(int)));
00116
00117
00118 typedSomething( m_lineEdit->text() );
00119 }
00120
00121 void KEditListBox::typedSomething(const QString& text)
00122 {
00123 if(currentItem() >= 0) {
00124 if(currentText() != m_lineEdit->text())
00125 {
00126
00127
00128
00129 bool block = m_listBox->signalsBlocked();
00130 m_listBox->blockSignals( true );
00131 m_listBox->changeItem(text, currentItem());
00132 m_listBox->blockSignals( block );
00133 emit changed();
00134 }
00135 }
00136
00137 if ( !servNewButton )
00138 return;
00139
00140 if (!d->m_checkAtEntering)
00141 servNewButton->setEnabled(!text.isEmpty());
00142 else
00143 {
00144 if (text.isEmpty())
00145 {
00146 servNewButton->setEnabled(false);
00147 }
00148 else
00149 {
00150 StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
00151 bool enable = (m_listBox->findItem( text, mode ) == 0L);
00152 servNewButton->setEnabled( enable );
00153 }
00154 }
00155 }
00156
00157 void KEditListBox::moveItemUp()
00158 {
00159 if (!m_listBox->isEnabled())
00160 {
00161 KNotifyClient::beep();
00162 return;
00163 }
00164
00165 unsigned int selIndex = m_listBox->currentItem();
00166 if (selIndex == 0)
00167 {
00168 KNotifyClient::beep();
00169 return;
00170 }
00171
00172 QListBoxItem *selItem = m_listBox->item(selIndex);
00173 m_listBox->takeItem(selItem);
00174 m_listBox->insertItem(selItem, selIndex-1);
00175 m_listBox->setCurrentItem(selIndex - 1);
00176
00177 emit changed();
00178 }
00179
00180 void KEditListBox::moveItemDown()
00181 {
00182 if (!m_listBox->isEnabled())
00183 {
00184 KNotifyClient::beep();
00185 return;
00186 }
00187
00188 unsigned int selIndex = m_listBox->currentItem();
00189 if (selIndex == m_listBox->count() - 1)
00190 {
00191 KNotifyClient::beep();
00192 return;
00193 }
00194
00195 QListBoxItem *selItem = m_listBox->item(selIndex);
00196 m_listBox->takeItem(selItem);
00197 m_listBox->insertItem(selItem, selIndex+1);
00198 m_listBox->setCurrentItem(selIndex + 1);
00199
00200 emit changed();
00201 }
00202
00203 void KEditListBox::addItem()
00204 {
00205
00206
00207
00208 if ( !servNewButton || !servNewButton->isEnabled() )
00209 return;
00210
00211 const QString& currentTextLE=m_lineEdit->text();
00212 bool alreadyInList(false);
00213
00214 if (!d->m_checkAtEntering)
00215 {
00216
00217 if ( m_listBox->currentText() == currentTextLE )
00218 alreadyInList = true;
00219 else
00220 {
00221 StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
00222 alreadyInList =(m_listBox->findItem(currentTextLE, mode) != 0);
00223 }
00224 }
00225
00226 if ( servNewButton )
00227 servNewButton->setEnabled(false);
00228
00229 bool block = m_lineEdit->signalsBlocked();
00230 m_lineEdit->blockSignals(true);
00231 m_lineEdit->clear();
00232 m_lineEdit->blockSignals(block);
00233
00234 m_listBox->setSelected(currentItem(), false);
00235
00236 if (!alreadyInList)
00237 {
00238 block = m_listBox->signalsBlocked();
00239 m_listBox->blockSignals( true );
00240 m_listBox->insertItem(currentTextLE);
00241 m_listBox->blockSignals( block );
00242 emit changed();
00243 }
00244 }
00245
00246 int KEditListBox::currentItem() const
00247 {
00248 int nr = m_listBox->currentItem();
00249 if(nr >= 0 && !m_listBox->item(nr)->isSelected()) return -1;
00250 return nr;
00251 }
00252
00253 void KEditListBox::removeItem()
00254 {
00255 int selected = m_listBox->currentItem();
00256
00257 if ( selected >= 0 )
00258 {
00259 m_listBox->removeItem( selected );
00260 if ( count() > 0 )
00261 m_listBox->setSelected( QMIN( selected, count() - 1 ), true );
00262
00263 emit changed();
00264 }
00265
00266 if ( servRemoveButton && m_listBox->currentItem() == -1 )
00267 servRemoveButton->setEnabled(false);
00268 }
00269
00270 void KEditListBox::enableMoveButtons(int index)
00271 {
00272
00273 if(currentText() != m_lineEdit->text())
00274 m_lineEdit->setText(currentText());
00275
00276 bool moveEnabled = servUpButton && servDownButton;
00277
00278 if (moveEnabled )
00279 {
00280 if (m_listBox->count() <= 1)
00281 {
00282 servUpButton->setEnabled(false);
00283 servDownButton->setEnabled(false);
00284 }
00285 else if ((uint) index == (m_listBox->count() - 1))
00286 {
00287 servUpButton->setEnabled(true);
00288 servDownButton->setEnabled(false);
00289 }
00290 else if (index == 0)
00291 {
00292 servUpButton->setEnabled(false);
00293 servDownButton->setEnabled(true);
00294 }
00295 else
00296 {
00297 servUpButton->setEnabled(true);
00298 servDownButton->setEnabled(true);
00299 }
00300 }
00301
00302 if ( servRemoveButton )
00303 servRemoveButton->setEnabled(true);
00304 }
00305
00306 void KEditListBox::clear()
00307 {
00308 m_lineEdit->clear();
00309 m_listBox->clear();
00310 emit changed();
00311 }
00312
00313 void KEditListBox::insertStringList(const QStringList& list, int index)
00314 {
00315 m_listBox->insertStringList(list,index);
00316 }
00317
00318 void KEditListBox::insertStrList(const QStrList* list, int index)
00319 {
00320 m_listBox->insertStrList(list,index);
00321 }
00322
00323 void KEditListBox::insertStrList(const QStrList& list, int index)
00324 {
00325 m_listBox->insertStrList(list,index);
00326 }
00327
00328 void KEditListBox::insertStrList(const char ** list, int numStrings, int index)
00329 {
00330 m_listBox->insertStrList(list,numStrings,index);
00331 }
00332
00333 QStringList KEditListBox::items() const
00334 {
00335 QStringList list;
00336 for ( uint i = 0; i < m_listBox->count(); i++ )
00337 list.append( m_listBox->text( i ));
00338
00339 return list;
00340 }
00341
00342 void KEditListBox::virtual_hook( int, void* )
00343 { }
00344
00345 KEditListBox::CustomEditor::CustomEditor( KComboBox *combo )
00346 {
00347 m_representationWidget = combo;
00348 m_lineEdit = dynamic_cast<KLineEdit*>( combo->lineEdit() );
00349 assert( m_lineEdit );
00350 }
00351
00352 };
00353
00354 #include "kdeveditlistbox.moc"
00355
00356 #endif
00357
00358