inputstrlist.cpp 5.02 KB
Newer Older
1 2
/******************************************************************************
 *
3
 * 
4
 *
5
 * Copyright (C) 1997-2004 by Dimitri van Heesch.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 */

#include "inputstrlist.h"
#include "pagewidget.h"
#include "pixmaps.h"

#include <qlabel.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlistbox.h>
#include <qstrlist.h>
#include <qstringlist.h>
#include <qfiledialog.h>
#include <qtooltip.h>

InputStrList::InputStrList( const QString & label, 
                            PageWidget *parent, QStrList &sl, ListMode lm)
  : QWidget(parent->getLayout()), strList(sl)
{
  QGridLayout *layout = new QGridLayout( this, 2, 2, 5 );
  lab = new QLabel( label, this );
  lab->setMinimumSize( lab->sizeHint() );
  layout->addWidget( lab,0,0 );

  QWidget *dw = new QWidget(this); /* dummy widget used for layouting */
  QHBoxLayout *boxlayout = new QHBoxLayout( dw, 0, 5 );
  le  = new QLineEdit( dw );
  le->setMinimumSize( le->sizeHint() );
  boxlayout->addWidget( le, 1 );

  add = new QPushButton( dw );
  add->setPixmap( QPixmap( add_xpm ));
  add->setMinimumSize( add->sizeHint() );
  QToolTip::add(add,"Add item");
  boxlayout->addWidget( add );

  del = new QPushButton( dw );
  del->setPixmap( QPixmap( del_xpm ));
  del->setMinimumSize( del->sizeHint() );
  QToolTip::add(del,"Delete selected item");
  boxlayout->addWidget( del );

  upd = new QPushButton( dw ); 
  upd->setPixmap( QPixmap( update_xpm ));
  upd->setMinimumSize( upd->sizeHint() );
  QToolTip::add(upd,"Update selected item");
  boxlayout->addWidget( upd );

  lb  = new QListBox( this );
  lb->setMinimumSize(400,100);
  init();
  lb->setVScrollBarMode(QScrollView::Auto);
  lb->setHScrollBarMode(QScrollView::Auto);

  brFile=0;
  brDir=0;
  if (lm!=ListString)
  {
    if (lm&ListFile)
    {
      brFile = new QPushButton(dw);
      brFile->setPixmap( QPixmap(file_xpm) );
      brFile->setMinimumSize(brFile->sizeHint());
      QToolTip::add(brFile,"Browse to a file");
      boxlayout->addWidget( brFile );
    } 
    if (lm&ListDir)
    {
      brDir = new QPushButton(dw);
      brDir->setPixmap( QPixmap(folder_xpm) );
      brDir->setMinimumSize(brDir->sizeHint());
      QToolTip::add(brDir,"Browse to a folder");
      boxlayout->addWidget( brDir );
    }
  }
  layout->addWidget( dw, 0,1 );
  layout->addWidget( lb,1,1 );
  layout->activate();
  setMinimumSize( sizeHint() );

  connect(le,   SIGNAL(returnPressed()), 
          this, SLOT(addString()) );
  connect(add,  SIGNAL(clicked()), 
          this, SLOT(addString()) );
  connect(del,  SIGNAL(clicked()), 
          this, SLOT(delString()) );
  connect(upd,  SIGNAL(clicked()), 
          this, SLOT(updateString()) );
  if (brFile)
  {
    connect(brFile, SIGNAL(clicked()),
            this, SLOT(browseFiles()));
  }
  if (brDir)
  {
    connect(brDir, SIGNAL(clicked()),
            this, SLOT(browseDir()));
  }
  connect(lb,   SIGNAL(selected(const QString &)), 
          this, SLOT(selectText(const QString &)));

  parent->addWidget(this);

  strList=sl;
}

void InputStrList::addString()
{
  if (!le->text().isEmpty())
  {
    lb->insertItem(le->text());
    strList.append(le->text());
    emit changed();
    le->clear();
  }
}

void InputStrList::delString()
{
  if (lb->currentItem()!=-1)
  {
    int itemIndex = lb->currentItem();
    lb->removeItem(itemIndex);
    strList.remove(itemIndex);
    emit changed();
  }
}

void InputStrList::updateString()
{
  if (lb->currentItem()!=-1 && !le->text().isEmpty())
  {
    lb->changeItem(le->text(),lb->currentItem());
    strList.insert(lb->currentItem(),le->text());
    strList.remove(lb->currentItem()+1);
    emit changed();
  }
}

void InputStrList::selectText(const QString &s)
{
  le->setText(s);
}

void InputStrList::setEnabled(bool state)
{
  lab->setEnabled(state);
  le->setEnabled(state);
  add->setEnabled(state);
  del->setEnabled(state);
  upd->setEnabled(state);
  lb->setEnabled(state);
  if (brFile) brFile->setEnabled(state);
  if (brDir)  brDir->setEnabled(state);
}

void InputStrList::browseFiles()
{
  QStringList fileNames = QFileDialog::getOpenFileNames();	

  if (!fileNames.isEmpty()) 
  {
    QStringList::Iterator it;
    for ( it= fileNames.begin(); it != fileNames.end(); ++it )
    {
      lb->insertItem(*it);
      strList.append(*it);
      emit changed();
    }
    le->setText(*fileNames.begin());
  }
}

void InputStrList::browseDir()
{	
  QString dirName = QFileDialog::getExistingDirectory();	

  if (!dirName.isNull()) 
  {
    lb->insertItem(dirName);
    strList.append(dirName);
    emit changed();
    le->setText(dirName);
  }
}

void InputStrList::init()
{
  le->clear();
  lb->clear();
  char *s = strList.first();
  while (s)
  {
    lb->insertItem(s);
    s = strList.next();
  }
}