KDevelop API Documentation

input.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 1997-2000 by Dimitri van Heesch                         *
00003  *   dimitri@stack.nl                                                      *
00004  *   Copyright (C) 2001 by Bernd Gehrmann                                  *
00005  *   bernd@kdevelop.org                                                    *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00014 #include <qlabel.h>
00015 #include <qlayout.h>
00016 #include <qcombobox.h>
00017 #include <qlineedit.h>
00018 #include <qpushbutton.h>
00019 #include <qspinbox.h>
00020 #include <qtooltip.h>
00021 #include <klocale.h>
00022 #include <kfiledialog.h>
00023 #include <kglobal.h>
00024 #include <kiconloader.h>
00025 
00026 #include "input.h"
00027 
00028 
00029 static const char * const add_xpm_data[] =
00030 {
00031   "16 16 5 1",
00032   ". c None",
00033   "* c #0328f9",
00034   "# c #354396",
00035   "a c #353740",
00036   "c c #999999",
00037   "................",
00038   "......###.......",
00039   "......#*ac......",
00040   "......#*ac......",
00041   "......#*ac......",
00042   "......#*ac......",
00043   ".######*a#####..",
00044   ".#***********ac.",
00045   ".#aaaaa*aaaaaac.",
00046   "..cccc#*acccccc.",
00047   "......#*ac......",
00048   "......#*ac......",
00049   "......#*ac......",
00050   "......#aac......",
00051   ".......ccc......",
00052   "................"
00053 };
00054 const char **add_xpm = (const char **)add_xpm_data;
00055 
00056 static const char * const del_xpm_data[] =
00057 {
00058   "16 16 5 1",
00059   ". c None",
00060   "* c #0328f9",
00061   "# c #354396",
00062   "a c #353740",
00063   "c c #999999",
00064   "................",
00065   "................",
00066   "................",
00067   "................",
00068   "................",
00069   "................",
00070   ".#############..",
00071   ".#***********ac.",
00072   ".aaaaaaaaaaaaac.",
00073   "..ccccccccccccc.",
00074   "................",
00075   "................",
00076   "................",
00077   "................",
00078   "................",
00079   "................"
00080 };
00081 const char **del_xpm = (const char **)del_xpm_data;
00082 
00083 static const char* const update_xpm_data[] =
00084 {
00085   "16 16 5 1",
00086   /* colors */
00087   ". c #0328f9",
00088   "# c #354396",
00089   "a c #353740",
00090   "b c None",
00091   "c c #999999",
00092   /* pixels */
00093   "bbbbbbbbbbbbbbbb",
00094   "bbbbbbbb#####acb",
00095   "bbbbbbbb#....abb",
00096   "bbc##cbb#...acbb",
00097   "bb#..abb#....abb",
00098   "bc#..abb#.a..acb",
00099   "b#..acbbaac#..ab",
00100   "b#..abbbcbb#..ab",
00101   "b#..abbbbbb#..ab",
00102   "b#..acbbbbc#..ab",
00103   "bc#..#cbbc#..acb",
00104   "bb#...####...acb",
00105   "bbca........acbb",
00106   "bbbbaa....aaccbb",
00107   "bbbbbcaaaaccbbbb",
00108   "bbbbbbbbbbbbbbbb"
00109 };
00110 const char **update_xpm = (const char **)update_xpm_data;
00111 
00112 
00113 InputBool::InputBool(const QCString &k, const QString &text, QWidget * parent, bool &flag)
00114     : QWidget(parent), state(flag), key(k)
00115 {
00116     QHBoxLayout *layout = new QHBoxLayout(this);
00117     cb = new QCheckBox(text,this);
00118 
00119     init();
00120 
00121     layout->addWidget(cb);
00122     layout->addStretch(1);
00123     
00124     connect( cb, SIGNAL(toggled(bool)), this, SLOT(valueChanged(bool)));
00125 }
00126 
00127 
00128 InputBool::~InputBool()
00129 {}
00130 
00131 
00132 void InputBool::init()
00133 {
00134     cb->setChecked(state);
00135 }
00136 
00137 
00138 void InputBool::valueChanged(bool s)
00139 {
00140     if (s != state) {
00141         emit changed();
00142         emit toggle(key, s);
00143     }
00144     state = s;
00145 }
00146 
00147 
00148 void InputBool::setEnabled(bool b)
00149 {
00150     cb->setEnabled(b);
00151 }
00152 
00153 
00154 InputInt::InputInt(const QString &label, QWidget *parent, int &val, int minVal, int maxVal)
00155     : QWidget(parent), m_val(val), m_minVal(minVal), m_maxVal(maxVal)
00156 {
00157     QHBoxLayout *layout = new QHBoxLayout(this, 5);
00158     
00159     sp = new QSpinBox(minVal, maxVal, 1, this);
00160     lab = new QLabel(sp, label+":", this);
00161     
00162     init();
00163   
00164     layout->addWidget(lab);
00165     layout->addWidget(sp);
00166     layout->addStretch(1);
00167 
00168     connect(sp, SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int)));
00169 }
00170 
00171 
00172 InputInt::~InputInt()
00173 {}
00174 
00175 
00176 void InputInt::init()
00177 {
00178     m_val = QMAX(m_minVal, m_val);
00179     m_val = QMIN(m_maxVal, m_val);
00180     sp->setValue(m_val);
00181 }
00182 
00183 
00184 void InputInt::valueChanged(int val)
00185 {
00186     if (val != m_val)
00187         emit changed(); 
00188     m_val = val;
00189 }
00190 
00191 
00192 void InputInt::setEnabled(bool state)
00193 {
00194     lab->setEnabled(state);
00195     sp->setEnabled(state);
00196 }
00197 
00198 
00199 InputString::InputString(const QString & label, 
00200                          QWidget *parent, QCString &s, StringMode m)
00201     : QWidget(parent), str(s), sm(m), m_values(0), m_index(0)
00202 {
00203     le = 0; br = 0; com = 0;
00204     
00205     if (m == StringFixed) {
00206         QHBoxLayout *layout = new QHBoxLayout(this, 5);
00207         com = new QComboBox(this); 
00208     lab = new QLabel(com,label+":", this);
00209     layout->addWidget(lab);
00210         layout->addWidget(com);
00211         layout->addStretch(1);
00212     } else {
00213         QGridLayout *layout = new QGridLayout(this, 1, m==StringFree? 1 : 3, 5);
00214         le = new QLineEdit(this);
00215     lab = new QLabel(le,label+":", this);
00216     layout->addWidget(lab, 0, 0);
00217         le->setText(s);
00218         layout->addWidget(le, 0, 1);
00219         
00220         if (m == StringFile || m == StringDir) {
00221             br = new QPushButton(this);
00222             br->setPixmap(SmallIcon(m==StringFile? "document" : "folder"));
00223             QToolTip::add(br, m==StringFile? i18n("Browse to a file") : i18n("Browse to a folder"));
00224             layout->addWidget(br, 0, 2);
00225         }
00226     }
00227     
00228     if (le)
00229         connect( le,   SIGNAL(textChanged(const QString&)), 
00230                  this, SLOT(textChanged(const QString&)) );
00231     if (br)
00232         connect( br,   SIGNAL(clicked()), this, SLOT(browse()) );
00233     if (com)
00234         connect( com,  SIGNAL(activated(const QString &)), 
00235                  this, SLOT(textChanged(const QString &)) );
00236 }
00237 
00238 InputString::~InputString()
00239 {
00240     if (m_values)
00241         delete m_values;
00242 }
00243 
00244 
00245 void InputString::init()
00246 {
00247     if (sm == StringFixed) {
00248         int *itemIndex = m_values->find(str);
00249         if (itemIndex) 
00250             com->setCurrentItem(*itemIndex);
00251         else
00252             com->setCurrentItem(0);
00253     } else
00254         le->setText(str);
00255 }
00256 
00257 
00258 void InputString::addValue(const char *s)
00259 {
00260     if (sm == StringFixed) {
00261         if (!m_values)
00262             m_values = new QDict<int>;
00263         m_values->setAutoDelete(true);
00264         m_values->insert(s, new int(m_index++));
00265         com->insertItem(s);
00266     }
00267 }
00268 
00269 
00270 void InputString::clear()
00271 {
00272     le->setText("");
00273     if (!str.isEmpty()) {
00274         emit changed();
00275         str = "";
00276     }
00277 }
00278 
00279 
00280 void InputString::textChanged(const QString &s)
00281 {
00282     if (str!=s.latin1()) {
00283         str = s.latin1();
00284         emit changed();
00285     }
00286 }
00287 
00288 void InputString::setEnabled(bool state)
00289 {
00290     lab->setEnabled(state);
00291     if (le)
00292         le->setEnabled(state);
00293     if (br)
00294         br->setEnabled(state);
00295     if (com)
00296         com->setEnabled(state);
00297 }
00298 
00299 
00300 void InputString::browse()
00301 {
00302     if (sm == StringFile) {
00303         QString fileName = KFileDialog::getOpenFileName();
00304     
00305         if (!fileName.isNull()) {
00306             le->setText(fileName);
00307             if (str != le->text().latin1()) {
00308                 str = le->text().latin1(); 
00309                 emit changed();
00310             }
00311         }
00312     } else { // sm==StringDir
00313         QString dirName = KFileDialog::getExistingDirectory();
00314 
00315         if (!dirName.isNull()) {
00316             le->setText( dirName );     
00317             if (str != le->text().latin1()) {
00318                 str = le->text().latin1();
00319                 emit changed();
00320             }
00321         }   
00322     }
00323 }
00324 
00325 
00326 InputStrList::InputStrList(const QString & label, 
00327                            QWidget *parent, QStrList &sl, ListMode lm)
00328     : QWidget(parent), strList(sl)
00329 {
00330     QGridLayout *layout = new QGridLayout(this, 2, 2, 5);
00331     
00332     QWidget *dw = new QWidget(this); /* dummy widget used for layouting */
00333     QHBoxLayout *boxlayout = new QHBoxLayout(dw, 0, 5);
00334     le  = new QLineEdit(dw);
00335     lab = new QLabel(le,label+":", this );
00336     layout->addWidget(lab, 0, 0);
00337     boxlayout->addWidget(le, 1);
00338 
00339     add = new QPushButton(dw);
00340     add->setPixmap(QPixmap( add_xpm ));
00341     QToolTip::add(add, i18n("Add item"));
00342     boxlayout->addWidget(add);
00343     
00344     del = new QPushButton(dw);
00345     del->setPixmap(QPixmap( del_xpm ));
00346     QToolTip::add(del, i18n("Delete selected item"));
00347     boxlayout->addWidget(del);
00348 
00349     upd = new QPushButton(dw); 
00350     upd->setPixmap(QPixmap( update_xpm ));
00351     QToolTip::add(upd, i18n("Update selected item"));
00352     boxlayout->addWidget(upd);
00353     
00354     lb  = new QListBox(this);
00355     lb->setMinimumSize(400, 100);
00356     init();
00357     lb->setVScrollBarMode(QScrollView::Auto);
00358     lb->setHScrollBarMode(QScrollView::Auto);
00359     
00360     brFile = 0;
00361     brDir = 0;
00362     if (lm != ListString) {
00363         if (lm & ListFile) {
00364             brFile = new QPushButton(dw);
00365             brFile->setPixmap(SmallIcon("document"));
00366             QToolTip::add(brFile, i18n("Browse to a file"));
00367             boxlayout->addWidget(brFile);
00368         } 
00369         if (lm & ListDir) {
00370             brDir = new QPushButton(dw);
00371             brDir->setPixmap(SmallIcon("folder"));
00372             QToolTip::add(brDir, i18n("Browse to a folder"));
00373             boxlayout->addWidget(brDir);
00374         }
00375     }
00376     layout->addWidget(dw, 0, 1);
00377     layout->addWidget(lb, 1, 1);
00378     
00379     connect( le,   SIGNAL(returnPressed()), 
00380              this, SLOT(addString()) );
00381     connect( add,  SIGNAL(clicked()), 
00382              this, SLOT(addString()) );
00383     connect( del,  SIGNAL(clicked()), 
00384              this, SLOT(delString()) );
00385     connect( upd,  SIGNAL(clicked()), 
00386              this, SLOT(updateString()) );
00387     if (brFile) 
00388         connect( brFile, SIGNAL(clicked()),
00389                  this, SLOT(browseFiles()) );
00390     if (brDir)
00391         connect( brDir, SIGNAL(clicked()),
00392                  this, SLOT(browseDir()) );
00393     connect( lb,   SIGNAL(selected(const QString &)), 
00394              this, SLOT(selectText(const QString &)) );
00395     
00396     strList = sl;
00397 }
00398 
00399 
00400 InputStrList::~InputStrList()
00401 {}
00402 
00403 
00404 void InputStrList::init()
00405 {
00406     le->clear();
00407     lb->clear();
00408     char *s = strList.first();
00409     while (s) {
00410         lb->insertItem(s);
00411         s = strList.next();
00412     }
00413 }
00414 
00415 
00416 void InputStrList::addString()
00417 {
00418     if (!le->text().isEmpty()) {
00419         lb->insertItem(le->text());
00420         strList.append(le->text().latin1());
00421         emit changed();
00422         le->clear();
00423     }
00424 }
00425 
00426 
00427 void InputStrList::delString()
00428 {
00429     if (lb->currentItem() != -1) {
00430         int itemIndex = lb->currentItem();
00431         lb->removeItem(itemIndex);
00432         strList.remove(itemIndex);
00433         emit changed();
00434     }
00435 }
00436 
00437 
00438 void InputStrList::updateString()
00439 {
00440     if (lb->currentItem() != -1 && !le->text().isEmpty()) {
00441         lb->changeItem(le->text(),lb->currentItem());
00442         strList.insert(lb->currentItem(),le->text().latin1());
00443         strList.remove(lb->currentItem()+1);
00444         emit changed();
00445     }
00446 }
00447 
00448 
00449 void InputStrList::selectText(const QString &s)
00450 {
00451     le->setText(s);
00452 }
00453 
00454 
00455 void InputStrList::setEnabled(bool state)
00456 {
00457     lab->setEnabled(state);
00458     le->setEnabled(state);
00459     add->setEnabled(state);
00460     del->setEnabled(state);
00461     upd->setEnabled(state);
00462     lb->setEnabled(state);
00463     if (brFile)
00464         brFile->setEnabled(state);
00465     if (brDir)
00466         brDir->setEnabled(state);
00467 }
00468 
00469 
00470 void InputStrList::browseFiles()
00471 {
00472     QStringList fileNames = KFileDialog::getOpenFileNames();    
00473     
00474     if (!fileNames.isEmpty()) {
00475         QStringList::Iterator it;
00476         for (it = fileNames.begin(); it != fileNames.end(); ++it) {
00477             lb->insertItem(*it);
00478             strList.append(( *it ).latin1());
00479             emit changed();
00480         }
00481         le->setText(*fileNames.begin());
00482     }
00483 }
00484 
00485 
00486 void InputStrList::browseDir()
00487 {   
00488     QString dirName = KFileDialog::getExistingDirectory();  
00489     
00490     if (!dirName.isNull()) {
00491         lb->insertItem(dirName);
00492         strList.append(dirName.latin1());
00493         emit changed();
00494         le->setText(dirName);
00495     }
00496 }
00497 
00498 
00499 #include "input.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 22 09:22:40 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003