inputstrlist.cpp 6.26 KB
Newer Older
1 2
/******************************************************************************
 *
3
 * 
4
 *
Dimitri van Heesch's avatar
Dimitri van Heesch committed
5
 * Copyright (C) 1997-2014 by Dimitri van Heesch.
6 7 8 9 10 11 12 13 14 15
 *
 * 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"
16 17 18 19 20 21 22 23 24 25 26
#include "helplabel.h"
#include "doxywizard.h"
#include "config.h"

#include <QtGui>

InputStrList::InputStrList( QGridLayout *layout,int &row,
                            const QString & id, 
                            const QStringList &sl, ListMode lm,
                            const QString & docs)
  : m_default(sl), m_strList(sl), m_docs(docs), m_id(id)
27
{
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  m_lab = new HelpLabel( id );

  m_le  = new QLineEdit;
  m_le->clear();

  QToolBar *toolBar = new QToolBar;
  toolBar->setIconSize(QSize(24,24));
  m_add = toolBar->addAction(QIcon(QString::fromAscii(":/images/add.png")),QString(),
                             this,SLOT(addString()));
  m_add->setToolTip(tr("Add item"));
  m_del = toolBar->addAction(QIcon(QString::fromAscii(":/images/del.png")),QString(),
                             this,SLOT(delString()));
  m_del->setToolTip(tr("Delete selected item"));
  m_upd = toolBar->addAction(QIcon(QString::fromAscii(":/images/refresh.png")),QString(),
                             this,SLOT(updateString()));
  m_upd->setToolTip(tr("Update selected item"));

  m_lb  = new QListWidget;
  //m_lb->setMinimumSize(400,100);
  foreach (QString s, m_strList) m_lb->addItem(s);
  
  m_brFile=0;
  m_brDir=0;
51 52 53 54
  if (lm!=ListString)
  {
    if (lm&ListFile)
    {
55 56 57
      m_brFile = toolBar->addAction(QIcon(QString::fromAscii(":/images/file.png")),QString(),
                                    this,SLOT(browseFiles()));
      m_brFile->setToolTip(tr("Browse to a file"));
58 59 60
    } 
    if (lm&ListDir)
    {
61 62 63
      m_brDir = toolBar->addAction(QIcon(QString::fromAscii(":/images/folder.png")),QString(),
                                   this,SLOT(browseDir()));
      m_brDir->setToolTip(tr("Browse to a folder"));
64 65
    }
  }
66 67 68 69 70 71 72
  QHBoxLayout *rowLayout = new QHBoxLayout;
  rowLayout->addWidget( m_le );
  rowLayout->addWidget( toolBar );
  layout->addWidget( m_lab,      row,0 );
  layout->addLayout( rowLayout,  row,1,1,2 );
  layout->addWidget( m_lb,       row+1,1,1,2 );
  row+=2;
73

74 75 76
  m_value = m_strList;

  connect(m_le,   SIGNAL(returnPressed()), 
77
          this, SLOT(addString()) );
78
  connect(m_lb,   SIGNAL(currentTextChanged(const QString &)), 
79
          this, SLOT(selectText(const QString &)));
80 81 82 83 84 85 86
  connect( m_lab, SIGNAL(enter()), SLOT(help()) );
  connect( m_lab, SIGNAL(reset()), SLOT(reset()) );
}

void InputStrList::help()
{
  showHelp(this);
87 88
}

89

90 91
void InputStrList::addString()
{
92
  if (!m_le->text().isEmpty())
93
  {
94 95 96 97
    m_lb->addItem(m_le->text());
    m_strList.append(m_le->text());
    m_value = m_strList;
    updateDefault();
98
    emit changed();
99
    m_le->clear();
100 101 102 103 104
  }
}

void InputStrList::delString()
{
105
  if (m_lb->currentRow()!=-1)
106
  {
107 108 109 110 111
    int itemIndex = m_lb->currentRow();
    delete m_lb->currentItem();
    m_strList.removeAt(itemIndex);
    m_value = m_strList;
    updateDefault();
112 113 114 115 116 117
    emit changed();
  }
}

void InputStrList::updateString()
{
118
  if (m_lb->currentRow()!=-1 && !m_le->text().isEmpty())
119
  {
120 121 122 123 124
    m_lb->currentItem()->setText(m_le->text());
    m_strList.insert(m_lb->currentRow(),m_le->text());
    m_strList.removeAt(m_lb->currentRow()+1);
    m_value = m_strList;
    updateDefault();
125 126 127 128 129 130
    emit changed();
  }
}

void InputStrList::selectText(const QString &s)
{
131
  m_le->setText(s);
132 133 134 135
}

void InputStrList::setEnabled(bool state)
{
136 137 138 139 140 141 142 143
  m_lab->setEnabled(state);
  m_le->setEnabled(state);
  m_add->setEnabled(state);
  m_del->setEnabled(state);
  m_upd->setEnabled(state);
  m_lb->setEnabled(state);
  if (m_brFile) m_brFile->setEnabled(state);
  if (m_brDir)  m_brDir->setEnabled(state);
144
  updateDefault();
145 146 147 148
}

void InputStrList::browseFiles()
{
149
  QString path = QFileInfo(MainWindow::instance().configFileName()).path();
150 151 152 153 154 155 156
  QStringList fileNames = QFileDialog::getOpenFileNames();	

  if (!fileNames.isEmpty()) 
  {
    QStringList::Iterator it;
    for ( it= fileNames.begin(); it != fileNames.end(); ++it )
    {
157 158 159 160 161 162 163 164 165 166 167 168 169 170
      QString fileName;
      QDir dir(path);
      if (!MainWindow::instance().configFileName().isEmpty() && dir.exists())
      {
        fileName = dir.relativeFilePath(*it);
      }
      if (fileName.isEmpty())
      {
        fileName = *it;
      }
      m_lb->addItem(fileName);
      m_strList.append(fileName);
      m_value = m_strList;
      updateDefault();
171 172
      emit changed();
    }
173
    m_le->setText(m_strList[0]);
174 175 176 177 178
  }
}

void InputStrList::browseDir()
{	
179
  QString path = QFileInfo(MainWindow::instance().configFileName()).path();
180 181 182 183
  QString dirName = QFileDialog::getExistingDirectory();	

  if (!dirName.isNull()) 
  {
184 185 186 187 188 189 190 191 192 193 194 195 196
    QDir dir(path);
    if (!MainWindow::instance().configFileName().isEmpty() && dir.exists())
    {
      dirName = dir.relativeFilePath(dirName);
    }
    if (dirName.isEmpty())
    {
      dirName=QString::fromAscii(".");
    }
    m_lb->addItem(dirName);
    m_strList.append(dirName);
    m_value = m_strList;
    updateDefault();
197
    emit changed();
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    m_le->setText(dirName);
  }
}

void InputStrList::setValue(const QStringList &sl)
{
  m_le->clear();
  m_lb->clear();
  m_strList = sl;
  for (int i=0;i<m_strList.size();i++)
  {
    m_lb->addItem(m_strList[i].trimmed());
  }
  updateDefault();
}

QVariant &InputStrList::value()
{
  return m_value;
}

void InputStrList::update()
{
  setValue(m_value.toStringList());
}

void InputStrList::updateDefault()
{
226
  if (m_strList==m_default || !m_lab->isEnabled())
227 228
  {
    m_lab->setText(QString::fromAscii("<qt>")+m_id+QString::fromAscii("</qt"));
229
  }
230 231 232 233 234 235 236 237 238
  else
  {
    m_lab->setText(QString::fromAscii("<qt><font color='red'>")+m_id+QString::fromAscii("</font></qt>"));
  }
}

void InputStrList::reset()
{
  setValue(m_default);
239 240
}

241
void InputStrList::writeValue(QTextStream &t,QTextCodec *codec)
242
{
243 244
  bool first=TRUE;
  foreach (QString s, m_strList) 
245
  {
246 247 248 249 250 251 252
    if (!first) 
    {
      t << " \\" << endl;
      t << "                         ";
    }
    first=FALSE;
    writeStringValue(t,codec,s);
253 254
  }
}
255