KDevelop API Documentation

lib/widgets/flagboxes.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 Copyright (C) 2000-2001 Bernd Gehrmann <bernd@kdevelop.org> 00003 Copyright (C) 2003 Alexander Dymo <cloudtemple@mksat.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., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 #include <klocale.h> 00021 #include <kdebug.h> 00022 #include <kurlrequester.h> 00023 #include <klineedit.h> 00024 #include <kdialogbase.h> 00025 #include <kdeversion.h> 00026 00027 #include <qapplication.h> 00028 #include <qtooltip.h> 00029 #include <qheader.h> 00030 #include <qstringlist.h> 00031 #include <qlayout.h> 00032 #include <qlabel.h> 00033 #include <qregexp.h> 00034 #include <qspinbox.h> 00035 00036 // only for KDE < 3.1 00037 #if KDE_VERSION <= 305 00038 #include "../compat/kdeveditlistbox.h" 00039 using namespace KDevCompat; 00040 #endif 00041 00042 #include "flagboxes.h" 00043 00044 // partial copy of Qt-3.1 for back-compatibility to KDE-3.0 00045 QString QRegExp_escape( const QString& str ) 00046 { 00047 static const char meta[] = "$()*+.?[\\]^{|}"; 00048 QString quoted = str; 00049 int i = 0; 00050 00051 while ( i < (int) quoted.length() ) { 00052 if ( strchr(meta, quoted[i].latin1()) != 0 ) 00053 quoted.insert( i++, "\\" ); 00054 i++; 00055 } 00056 return quoted; 00057 } 00058 00059 00060 class FlagListToolTip : public QToolTip 00061 { 00062 public: 00063 FlagListToolTip(QWidget *parent); 00064 protected: 00065 void maybeTip(const QPoint &p); 00066 }; 00067 00068 00069 FlagListToolTip::FlagListToolTip(QWidget *parent) 00070 : QToolTip(parent) 00071 {} 00072 00073 00074 void FlagListToolTip::maybeTip(const QPoint &pos) 00075 { 00076 FlagListBox *listbox = static_cast<FlagListBox*>(parentWidget()); 00077 QListViewItem *item = listbox->itemAt(pos); 00078 FlagListItem *flitem = static_cast<FlagListItem*>(item); 00079 00080 if (item) 00081 tip(listbox->itemRect(item), flitem->desc); 00082 } 00083 00084 00085 FlagListItem::FlagListItem(FlagListBox *parent, const QString &flagstr, 00086 const QString &description) 00087 : QCheckListItem(parent, flagstr, QCheckListItem::CheckBox), 00088 flag(flagstr), desc(description) 00089 {} 00090 00091 00092 FlagListItem::FlagListItem(FlagListBox *parent, const QString &flagstr, 00093 const QString &description, const QString &offstr) 00094 : QCheckListItem(parent, flagstr, QCheckListItem::CheckBox), 00095 flag(flagstr), off(offstr), desc(description) 00096 {} 00097 00098 00099 FlagListBox::FlagListBox(QWidget *parent, const char *name) 00100 : QListView(parent, name) 00101 { 00102 setResizeMode(LastColumn); 00103 header()->hide(); 00104 addColumn(i18n("Flags")); 00105 (void) new FlagListToolTip(this); 00106 } 00107 00108 00109 void FlagListBox::readFlags(QStringList *list) 00110 { 00111 QListViewItem *item = firstChild(); 00112 for (; item; item = item->nextSibling()) { 00113 FlagListItem *flitem = static_cast<FlagListItem*>(item); 00114 QStringList::Iterator sli = list->find(flitem->flag); 00115 if (sli != list->end()) { 00116 flitem->setOn(true); 00117 list->remove(sli); 00118 } 00119 sli = list->find(flitem->off); 00120 if (sli != list->end()) { 00121 flitem->setOn(false); 00122 list->remove(sli); 00123 } 00124 } 00125 } 00126 00127 00128 void FlagListBox::writeFlags(QStringList *list) 00129 { 00130 QListViewItem *item = firstChild(); 00131 for (; item; item = item->nextSibling()) { 00132 FlagListItem *flitem = static_cast<FlagListItem*>(item); 00133 if (flitem->isOn()) 00134 (*list) << flitem->flag; 00135 } 00136 } 00137 00138 00139 FlagCheckBox::FlagCheckBox(QWidget *parent, FlagCheckBoxController *controller, 00140 const QString &flagstr, const QString &description) 00141 : QCheckBox(description, parent), flag(flagstr), includeOff(false), useDef(false), defSet(false) 00142 { 00143 QToolTip::add(this, flagstr); 00144 controller->addCheckBox(this); 00145 } 00146 00147 00148 FlagCheckBox::FlagCheckBox(QWidget *parent, FlagCheckBoxController *controller, 00149 const QString &flagstr, const QString &description, 00150 const QString &offstr) 00151 : QCheckBox(description, parent), flag(flagstr), off(offstr), includeOff(false), useDef(false), defSet(false) 00152 { 00153 QToolTip::add(this, flagstr); 00154 controller->addCheckBox(this); 00155 } 00156 00157 FlagCheckBox::FlagCheckBox(QWidget *parent, FlagCheckBoxController *controller, 00158 const QString &flagstr, const QString &description, 00159 const QString &offstr, const QString &defstr) 00160 : QCheckBox(description, parent), flag(flagstr), off(offstr), def(defstr), includeOff(false), useDef(true), defSet(false) 00161 { 00162 QToolTip::add(this, flagstr); 00163 controller->addCheckBox(this); 00164 } 00165 00166 FlagCheckBoxController::FlagCheckBoxController(QStringList multiKeys) 00167 :m_multiKeys(multiKeys) 00168 { 00169 } 00170 00171 00172 void FlagCheckBoxController::addCheckBox(FlagCheckBox *item) 00173 { 00174 cblist.append(item); 00175 } 00176 00177 00178 void FlagCheckBoxController::readFlags(QStringList *list) 00179 { 00180 //handle keys like -vxyz -> transform they into -vx -vy -vz 00181 //very "effective" algo :( 00182 /* QStringList addons; 00183 for (QStringList::Iterator mk = m_multiKeys.begin(); mk != m_multiKeys.end(); ++ mk) 00184 { 00185 kdDebug() << "multikey " << *mk << endl; 00186 for (QStringList::Iterator sli = list->begin(); sli != list->end(); ++sli) 00187 { 00188 QString key = *sli; 00189 kdDebug() << "current key: " << key << endl; 00190 if ( (key.length() > 3) && (key.startsWith(*mk)) ) 00191 { 00192 list->remove(sli); 00193 key = key.remove(*mk); 00194 kdDebug() << "refined key " << key << endl; 00195 for (int i = 0; i < key.length(); ++i) 00196 { 00197 kdDebug() << "adding key " << *mk + key[i] << endl; 00198 addons << *mk + key[i]; 00199 } 00200 } 00201 } 00202 } 00203 kdDebug() << "good" << endl; 00204 *list += addons; 00205 00206 for (QStringList::Iterator sli = list->begin(); sli != list->end(); ++sli) 00207 { 00208 kdDebug() << "KEYS: " << *sli << endl; 00209 } 00210 */ 00211 QPtrListIterator<FlagCheckBox> it(cblist); 00212 for (; it.current(); ++it) { 00213 FlagCheckBox *fitem = it.current(); 00214 QStringList::Iterator sli = list->find(fitem->flag); 00215 if (sli != list->end()) { 00216 fitem->setChecked(true); 00217 fitem->useDef = false; 00218 list->remove(sli); 00219 } 00220 sli = list->find(fitem->off); 00221 if (sli != list->end()) { 00222 fitem->setChecked(false); 00223 fitem->includeOff = true; 00224 fitem->useDef = false; 00225 list->remove(sli); 00226 } 00227 if (!fitem->def.isEmpty()) 00228 if (fitem->useDef && (fitem->def == fitem->flag)) 00229 { 00230 fitem->setChecked(true); 00231 fitem->defSet = true; 00232 } 00233 else 00234 fitem->useDef = false; 00235 } 00236 } 00237 00238 00239 void FlagCheckBoxController::writeFlags(QStringList *list) 00240 { 00241 QPtrListIterator<FlagCheckBox> it(cblist); 00242 for (; it.current(); ++it) { 00243 FlagCheckBox *fitem = it.current(); 00244 if (fitem->isChecked() && (!fitem->useDef)) 00245 { 00246 (*list) << fitem->flag; 00247 } 00248 else if ((!fitem->off.isEmpty()) && fitem->includeOff) 00249 (*list) << fitem->off; 00250 else if ((fitem->def == fitem->flag) && (!fitem->isChecked())) 00251 (*list) << fitem->off; 00252 else if ((fitem->def == fitem->off) && (fitem->isChecked())) 00253 (*list) << fitem->flag; 00254 } 00255 } 00256 00257 FlagPathEditController::FlagPathEditController( ) 00258 { 00259 } 00260 00261 FlagPathEditController::~ FlagPathEditController( ) 00262 { 00263 } 00264 00265 void FlagPathEditController::readFlags( QStringList * list ) 00266 { 00267 // kdDebug() << "read path flags" << endl; 00268 QPtrListIterator<FlagPathEdit> it(plist); 00269 for (; it.current(); ++it) { 00270 FlagPathEdit *peitem = it.current(); 00271 00272 QStringList::Iterator sli = list->begin(); 00273 while ( sli != list->end() ) 00274 { 00275 // kdDebug() << "option: " << (*sli) << " flag is: " << peitem->flag << endl; 00276 if ((*sli).startsWith(peitem->flag)) 00277 { 00278 // kdDebug() << "Processing.." << endl; 00279 peitem->setText((*sli).replace(QRegExp(QRegExp_escape(peitem->flag)),"")); 00280 sli = list->remove(sli); 00281 continue; 00282 } 00283 ++sli; 00284 } 00285 /* QStringList::Iterator sli = list->find(peitem->flag); 00286 if (sli != list->end()) { 00287 peitem->setText((*sli).remove(peitem->flag)); 00288 list->remove(sli); 00289 }*/ 00290 } 00291 } 00292 00293 void FlagPathEditController::writeFlags( QStringList * list ) 00294 { 00295 QPtrListIterator<FlagPathEdit> it(plist); 00296 for (; it.current(); ++it) { 00297 FlagPathEdit *pitem = it.current(); 00298 if (!pitem->isEmpty()) 00299 (*list) << pitem->flag + pitem->text(); 00300 } 00301 } 00302 00303 void FlagPathEditController::addPathEdit( FlagPathEdit * item ) 00304 { 00305 plist.append(item); 00306 } 00307 00308 FlagPathEdit::FlagPathEdit( QWidget * parent, QString pathDelimiter, 00309 FlagPathEditController * controller, const QString & flagstr, const QString & description, 00310 KFile::Mode mode ) 00311 : QWidget(parent), delimiter(pathDelimiter), flag(flagstr), m_description(description) 00312 { 00313 QBoxLayout *topLayout = new QVBoxLayout(this, 0, 1); 00314 topLayout->addWidget(new QLabel(description, this)); 00315 QBoxLayout *layout = new QHBoxLayout(topLayout, KDialog::spacingHint()); 00316 00317 if (delimiter.isEmpty()) 00318 { 00319 url = new KURLRequester(this); 00320 url->setMode(mode); 00321 layout->addWidget(url); 00322 } 00323 else 00324 { 00325 edit = new KLineEdit(this); 00326 layout->addWidget(edit); 00327 details = new QPushButton("...", this); 00328 details->setMaximumWidth(30); 00329 connect(details, SIGNAL(clicked()), this, SLOT(showPathDetails())); 00330 layout->addWidget(details); 00331 } 00332 00333 QApplication::sendPostedEvents(this, QEvent::ChildInserted); 00334 00335 QToolTip::add(this, flagstr); 00336 controller->addPathEdit(this); 00337 } 00338 00339 void FlagPathEdit::showPathDetails( ) 00340 { 00341 KDialogBase *dia = new KDialogBase(0, "flag_path_edit_dia", true, m_description, 00342 KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true); 00343 00344 QBoxLayout *diaLayout = new QVBoxLayout(dia, KDialog::marginHint(), KDialog::spacingHint()); 00345 diaLayout->setAutoAdd(true); 00346 00347 KURLRequester *req = new KURLRequester( dia ); 00348 req->setMode(KFile::Directory); 00349 KEditListBox::CustomEditor pCustomEditor; 00350 #if KDE_VERSION > 305 00351 pCustomEditor = req->customEditor(); 00352 #else 00353 QObjectList* pOL = req->queryList("KLineEdit"); // dirty hack :) 00354 KLineEdit* pEdit = dynamic_cast<KLineEdit*>(pOL->first()); 00355 assert(pEdit); 00356 KEditListBox::CustomEditor editor(req, pEdit); 00357 pCustomEditor = editor; 00358 #endif 00359 KEditListBox *elb = new KEditListBox( "", pCustomEditor, dia ); 00360 dia->setMainWidget(elb); 00361 00362 elb->insertStringList(QStringList::split(delimiter, text())); 00363 00364 if (dia->exec() == QDialog::Accepted) 00365 { 00366 setText(elb->items().join(delimiter)); 00367 } 00368 00369 delete dia; 00370 } 00371 00372 void FlagPathEdit::setText( const QString text ) 00373 { 00374 if (delimiter.isEmpty()) 00375 url->setURL(text); 00376 else 00377 edit->setText(text); 00378 } 00379 00380 QString FlagPathEdit::text( ) 00381 { 00382 if (delimiter.isEmpty()) 00383 return url->url(); 00384 else 00385 return edit->text(); 00386 } 00387 00388 bool FlagPathEdit::isEmpty( ) 00389 { 00390 if (delimiter.isEmpty()) 00391 return url->url().isEmpty(); 00392 else 00393 return edit->text().isEmpty(); 00394 } 00395 00396 FlagRadioButton::FlagRadioButton( QWidget * parent, FlagRadioButtonController * controller, const QString & flagstr, const QString & description ) 00397 : QRadioButton(description, parent), flag(flagstr) 00398 { 00399 QToolTip::add(this, flagstr); 00400 controller->addRadioButton(this); 00401 } 00402 00403 FlagRadioButtonController::FlagRadioButtonController(QStringList multiKeys) 00404 :m_multiKeys(multiKeys) 00405 { 00406 } 00407 00408 void FlagRadioButtonController::addRadioButton(FlagRadioButton *item) 00409 { 00410 cblist.append(item); 00411 } 00412 00413 00414 void FlagRadioButtonController::readFlags(QStringList *list) 00415 { 00416 //handle keys like -vxyz -> transform they into -vx -vy -vz 00417 //very "effective" algo :( 00418 /* QStringList addons; 00419 for (QStringList::Iterator mk = m_multiKeys.begin(); mk != m_multiKeys.end(); ++ mk) 00420 { 00421 kdDebug() << "multikey " << *mk << endl; 00422 for (QStringList::Iterator sli = list->begin(); sli != list->end(); ++sli) 00423 { 00424 QString key = *sli; 00425 kdDebug() << "current key: " << key << endl; 00426 if ( (key.length() > 3) && (key.startsWith(*mk)) ) 00427 { 00428 list->remove(sli); 00429 key = key.remove(*mk); 00430 kdDebug() << "refined key " << key << endl; 00431 for (int i = 0; i < key.length(); ++i) 00432 { 00433 kdDebug() << "adding key " << *mk + key[i] << endl; 00434 addons << *mk + key[i]; 00435 } 00436 } 00437 } 00438 } 00439 kdDebug() << "good" << endl; 00440 *list += addons; 00441 00442 for (QStringList::Iterator sli = list->begin(); sli != list->end(); ++sli) 00443 { 00444 kdDebug() << "KEYS: " << *sli << endl; 00445 } 00446 */ 00447 QPtrListIterator<FlagRadioButton> it(cblist); 00448 for (; it.current(); ++it) { 00449 FlagRadioButton *fitem = it.current(); 00450 QStringList::Iterator sli = list->find(fitem->flag); 00451 if (sli != list->end()) { 00452 fitem->setChecked(true); 00453 list->remove(sli); 00454 } 00455 } 00456 } 00457 00458 00459 void FlagRadioButtonController::writeFlags(QStringList *list) 00460 { 00461 QPtrListIterator<FlagRadioButton> it(cblist); 00462 for (; it.current(); ++it) { 00463 FlagRadioButton *fitem = it.current(); 00464 if (fitem->isChecked()) 00465 (*list) << fitem->flag; 00466 } 00467 } 00468 00469 FlagEditController::FlagEditController( ) 00470 { 00471 } 00472 00473 FlagEditController::~ FlagEditController( ) 00474 { 00475 } 00476 00477 void FlagEditController::readFlags( QStringList * list ) 00478 { 00479 QPtrListIterator<FlagListEdit> it(plist); 00480 for (; it.current(); ++it) { 00481 FlagListEdit *peitem = it.current(); 00482 00483 QStringList::Iterator sli = list->begin(); 00484 while (sli != list->end()) 00485 { 00486 if ((*sli).startsWith(peitem->flag)) 00487 { 00488 peitem->appendText((*sli).replace(QRegExp(QRegExp_escape(peitem->flag)),"")); 00489 sli = list->remove(sli); 00490 continue; 00491 } 00492 ++sli; 00493 } 00494 } 00495 00496 00497 QPtrListIterator<FlagSpinEdit> it2(slist); 00498 for (; it2.current(); ++it2) { 00499 FlagSpinEdit *sitem = it2.current(); 00500 00501 QStringList::Iterator sli = list->begin(); 00502 while ( sli != list->end() ) 00503 { 00504 if ((*sli).startsWith(sitem->flag)) 00505 { 00506 sitem->setText((*sli).replace(QRegExp(QRegExp_escape(sitem->flag)),"")); 00507 sli = list->remove(sli); 00508 continue; 00509 } 00510 ++sli; 00511 } 00512 } 00513 } 00514 00515 void FlagEditController::writeFlags( QStringList * list ) 00516 { 00517 QPtrListIterator<FlagListEdit> it(plist); 00518 for (; it.current(); ++it) { 00519 FlagListEdit *pitem = it.current(); 00520 if (!pitem->isEmpty()) 00521 (*list) += pitem->flags(); 00522 } 00523 00524 QPtrListIterator<FlagSpinEdit> it2(slist); 00525 for (; it2.current(); ++it2) { 00526 FlagSpinEdit *sitem = it2.current(); 00527 if (!sitem->isDefault()) 00528 (*list) << sitem->flags(); 00529 } 00530 } 00531 00532 void FlagEditController::addListEdit( FlagListEdit * item ) 00533 { 00534 plist.append(item); 00535 } 00536 00537 void FlagEditController::addSpinBox(FlagSpinEdit *item) 00538 { 00539 slist.append(item); 00540 } 00541 00542 00543 FlagListEdit::FlagListEdit( QWidget * parent, QString listDelimiter, FlagEditController * controller, 00544 const QString & flagstr, const QString & description) 00545 : QWidget(parent), delimiter(listDelimiter), flag(flagstr), m_description(description) 00546 { 00547 QBoxLayout *topLayout = new QVBoxLayout(this, 0, 1); 00548 topLayout->addWidget(new QLabel(description, this)); 00549 QBoxLayout *layout = new QHBoxLayout(topLayout, KDialog::spacingHint()); 00550 00551 edit = new KLineEdit(this); 00552 layout->addWidget(edit); 00553 if (! listDelimiter.isEmpty()) 00554 { 00555 details = new QPushButton("...", this); 00556 details->setMaximumWidth(30); 00557 connect(details, SIGNAL(clicked()), this, SLOT(showListDetails())); 00558 layout->addWidget(details); 00559 } 00560 00561 QApplication::sendPostedEvents(this, QEvent::ChildInserted); 00562 00563 QToolTip::add(this, flagstr); 00564 controller->addListEdit(this); 00565 } 00566 00567 void FlagListEdit::setText( const QString text ) 00568 { 00569 edit->setText(text); 00570 } 00571 00572 bool FlagListEdit::isEmpty( ) 00573 { 00574 return edit->text().isEmpty(); 00575 } 00576 00577 QString FlagListEdit::text( ) 00578 { 00579 return edit->text(); 00580 } 00581 00582 void FlagListEdit::showListDetails( ) 00583 { 00584 KDialogBase *dia = new KDialogBase(0, "flag_list_edit_dia", true, m_description, 00585 KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true); 00586 00587 QBoxLayout *diaLayout = new QVBoxLayout(dia, KDialog::marginHint(), KDialog::spacingHint()); 00588 diaLayout->setAutoAdd(true); 00589 00590 KEditListBox *elb = new KEditListBox( "", dia ); 00591 dia->setMainWidget(elb); 00592 00593 elb->insertStringList(QStringList::split(delimiter, text())); 00594 00595 if (dia->exec() == QDialog::Accepted) 00596 { 00597 setText(elb->items().join(delimiter)); 00598 } 00599 00600 delete dia; 00601 } 00602 00603 void FlagListEdit::appendText( const QString text ) 00604 { 00605 edit->setText(edit->text() + (edit->text().isEmpty()?QString(""):delimiter) + text); 00606 } 00607 00608 QStringList FlagListEdit::flags( ) 00609 { 00610 QStringList fl = QStringList::split(delimiter, text()); 00611 for (QStringList::iterator it = fl.begin(); it != fl.end(); ++it) 00612 { 00613 (*it).prepend(flag); 00614 } 00615 return fl; 00616 } 00617 00618 FlagSpinEdit::FlagSpinEdit( QWidget * parent, int minVal, int maxVal, int incr, int defaultVal, FlagEditController * controller, const QString & flagstr, const QString & description ) 00619 :QWidget(parent), m_defaultVal(defaultVal), flag(flagstr) 00620 { 00621 QBoxLayout *topLayout = new QVBoxLayout(this, 0, 1); 00622 topLayout->addWidget(new QLabel(description, this)); 00623 00624 spb = new QSpinBox(minVal, maxVal, incr, this); 00625 spb->setValue(defaultVal); 00626 topLayout->addWidget(spb); 00627 00628 QApplication::sendPostedEvents(this, QEvent::ChildInserted); 00629 00630 QToolTip::add(this, flagstr); 00631 controller->addSpinBox(this); 00632 } 00633 00634 void FlagSpinEdit::setText( const QString text ) 00635 { 00636 spb->setValue(text.toInt()); 00637 } 00638 00639 QString FlagSpinEdit::text( ) 00640 { 00641 return QString("%1").arg(spb->value()); 00642 } 00643 00644 QString FlagSpinEdit::flags( ) 00645 { 00646 return flag + text(); 00647 } 00648 00649 bool FlagSpinEdit::isDefault( ) 00650 { 00651 if (spb->value() == m_defaultVal) 00652 return true; 00653 return false; 00654 } 00655 00656 00657 #include "flagboxes.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Oct 19 08:01:48 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003