| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Copyright (C) 1997-2019 by Dimitri van Heesch. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, and distribute this software and its |
| 6 |
* documentation under the terms of the GNU General Public License is hereby |
| 7 |
* granted. No representations are made about the suitability of this software |
| 8 |
* for any purpose. It is provided "as is" without express or implied warranty. |
| 9 |
* See the GNU General Public License for more details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "inputstrlist.h" |
| 14 |
#include "helplabel.h" |
| 15 |
#include "doxywizard.h" |
| 16 |
#include "config.h" |
| 17 |
|
| 18 |
#include <QToolBar> |
| 19 |
#include <QGridLayout> |
| 20 |
#include <QLineEdit> |
| 21 |
#include <QListWidget> |
| 22 |
#include <QFileInfo> |
| 23 |
#include <QFileDialog> |
| 24 |
#include <QTextStream> |
| 25 |
|
| 26 |
InputStrList::InputStrList( QGridLayout *layout,int &row, |
| 27 |
const QString & id, |
| 28 |
const QStringList &sl, ListMode lm, |
| 29 |
const QString & docs) |
| 30 |
: m_default(sl), m_strList(sl), m_docs(docs), m_id(id) |
| 31 |
{ |
| 32 |
m_lab = new HelpLabel( id ); |
| 33 |
|
| 34 |
m_le = new QLineEdit; |
| 35 |
m_le->clear(); |
| 36 |
|
| 37 |
QToolBar *toolBar = new QToolBar; |
| 38 |
toolBar->setIconSize(QSize(24,24)); |
| 39 |
m_add = toolBar->addAction(QIcon(QString::fromLatin1(":/images/add.svg")),QString(), |
| 40 |
this,SLOT(addString())); |
| 41 |
m_add->setToolTip(tr("Add item")); |
| 42 |
m_del = toolBar->addAction(QIcon(QString::fromLatin1(":/images/del.svg")),QString(), |
| 43 |
this,SLOT(delString())); |
| 44 |
m_del->setToolTip(tr("Delete selected item")); |
| 45 |
m_upd = toolBar->addAction(QIcon(QString::fromLatin1(":/images/refresh.svg")),QString(), |
| 46 |
this,SLOT(updateString())); |
| 47 |
m_upd->setToolTip(tr("Update selected item")); |
| 48 |
|
| 49 |
m_lb = new QListWidget; |
| 50 |
//m_lb->setMinimumSize(400,100); |
| 51 |
foreach (QString s, m_strList) m_lb->addItem(s); |
| 52 |
|
| 53 |
m_brFile=nullptr; |
| 54 |
m_brDir=nullptr; |
| 55 |
if (lm!=ListString) |
| 56 |
{ |
| 57 |
if (lm&ListFile) |
| 58 |
{ |
| 59 |
m_brFile = toolBar->addAction(QIcon(QString::fromLatin1(":/images/file.svg")),QString(), |
| 60 |
this,SLOT(browseFiles())); |
| 61 |
m_brFile->setToolTip(tr("Browse to a file")); |
| 62 |
} |
| 63 |
if (lm&ListDir) |
| 64 |
{ |
| 65 |
m_brDir = toolBar->addAction(QIcon(QString::fromLatin1(":/images/folder.svg")),QString(), |
| 66 |
this,SLOT(browseDir())); |
| 67 |
m_brDir->setToolTip(tr("Browse to a folder")); |
| 68 |
} |
| 69 |
} |
| 70 |
QHBoxLayout *rowLayout = new QHBoxLayout; |
| 71 |
rowLayout->addWidget( m_le ); |
| 72 |
rowLayout->addWidget( toolBar ); |
| 73 |
layout->addWidget( m_lab, row,0 ); |
| 74 |
layout->addLayout( rowLayout, row,1,1,2 ); |
| 75 |
layout->addWidget( m_lb, row+1,1,1,2 ); |
| 76 |
row+=2; |
| 77 |
|
| 78 |
m_value = m_strList; |
| 79 |
|
| 80 |
connect(m_le, SIGNAL(returnPressed()), |
| 81 |
this, SLOT(addString()) ); |
| 82 |
connect(m_lb, SIGNAL(currentTextChanged(const QString &)), |
| 83 |
this, SLOT(selectText(const QString &))); |
| 84 |
connect( m_lab, SIGNAL(enter()), SLOT(help()) ); |
| 85 |
connect( m_lab, SIGNAL(reset()), SLOT(reset()) ); |
| 86 |
} |
| 87 |
|
| 88 |
void InputStrList::help() |
| 89 |
{ |
| 90 |
showHelp(this); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
void InputStrList::addString() |
| 95 |
{ |
| 96 |
if (!m_le->text().isEmpty()) |
| 97 |
{ |
| 98 |
m_lb->addItem(m_le->text()); |
| 99 |
m_strList.append(m_le->text()); |
| 100 |
m_value = m_strList; |
| 101 |
updateDefault(); |
| 102 |
emit changed(); |
| 103 |
m_le->clear(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void InputStrList::delString() |
| 108 |
{ |
| 109 |
if (m_lb->currentRow()!=-1) |
| 110 |
{ |
| 111 |
int itemIndex = m_lb->currentRow(); |
| 112 |
delete m_lb->currentItem(); |
| 113 |
m_strList.removeAt(itemIndex); |
| 114 |
m_value = m_strList; |
| 115 |
updateDefault(); |
| 116 |
emit changed(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void InputStrList::updateString() |
| 121 |
{ |
| 122 |
if (m_lb->currentRow()!=-1 && !m_le->text().isEmpty()) |
| 123 |
{ |
| 124 |
m_lb->currentItem()->setText(m_le->text()); |
| 125 |
m_strList.insert(m_lb->currentRow(),m_le->text()); |
| 126 |
m_strList.removeAt(m_lb->currentRow()+1); |
| 127 |
m_value = m_strList; |
| 128 |
updateDefault(); |
| 129 |
emit changed(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
void InputStrList::selectText(const QString &s) |
| 134 |
{ |
| 135 |
m_le->setText(s); |
| 136 |
} |
| 137 |
|
| 138 |
void InputStrList::setEnabled(bool state) |
| 139 |
{ |
| 140 |
m_lab->setEnabled(state); |
| 141 |
m_le->setEnabled(state); |
| 142 |
m_add->setEnabled(state); |
| 143 |
m_del->setEnabled(state); |
| 144 |
m_upd->setEnabled(state); |
| 145 |
m_lb->setEnabled(state); |
| 146 |
if (m_brFile) m_brFile->setEnabled(state); |
| 147 |
if (m_brDir) m_brDir->setEnabled(state); |
| 148 |
updateDefault(); |
| 149 |
} |
| 150 |
|
| 151 |
void InputStrList::browseFiles() |
| 152 |
{ |
| 153 |
QString path = QFileInfo(MainWindow::instance().configFileName()).path(); |
| 154 |
QStringList fileNames = QFileDialog::getOpenFileNames(); |
| 155 |
|
| 156 |
if (!fileNames.isEmpty()) |
| 157 |
{ |
| 158 |
QStringList::Iterator it; |
| 159 |
for ( it= fileNames.begin(); it != fileNames.end(); ++it ) |
| 160 |
{ |
| 161 |
QString fileName; |
| 162 |
QDir dir(path); |
| 163 |
if (!MainWindow::instance().configFileName().isEmpty() && dir.exists()) |
| 164 |
{ |
| 165 |
fileName = dir.relativeFilePath(*it); |
| 166 |
} |
| 167 |
if (fileName.isEmpty()) |
| 168 |
{ |
| 169 |
fileName = *it; |
| 170 |
} |
| 171 |
m_lb->addItem(fileName); |
| 172 |
m_strList.append(fileName); |
| 173 |
m_value = m_strList; |
| 174 |
updateDefault(); |
| 175 |
emit changed(); |
| 176 |
} |
| 177 |
m_le->setText(m_strList[0]); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
void InputStrList::browseDir() |
| 182 |
{ |
| 183 |
QString path = QFileInfo(MainWindow::instance().configFileName()).path(); |
| 184 |
QString dirName = QFileDialog::getExistingDirectory(); |
| 185 |
|
| 186 |
if (!dirName.isNull()) |
| 187 |
{ |
| 188 |
QDir dir(path); |
| 189 |
if (!MainWindow::instance().configFileName().isEmpty() && dir.exists()) |
| 190 |
{ |
| 191 |
dirName = dir.relativeFilePath(dirName); |
| 192 |
} |
| 193 |
if (dirName.isEmpty()) |
| 194 |
{ |
| 195 |
dirName=QString::fromLatin1("."); |
| 196 |
} |
| 197 |
m_lb->addItem(dirName); |
| 198 |
m_strList.append(dirName); |
| 199 |
m_value = m_strList; |
| 200 |
updateDefault(); |
| 201 |
emit changed(); |
| 202 |
m_le->setText(dirName); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void InputStrList::setValue(const QStringList &sl) |
| 207 |
{ |
| 208 |
m_le->clear(); |
| 209 |
m_lb->clear(); |
| 210 |
m_strList = sl; |
| 211 |
for (int i=0;i<m_strList.size();i++) |
| 212 |
{ |
| 213 |
m_lb->addItem(m_strList[i].trimmed()); |
| 214 |
} |
| 215 |
updateDefault(); |
| 216 |
} |
| 217 |
|
| 218 |
QVariant &InputStrList::value() |
| 219 |
{ |
| 220 |
return m_value; |
| 221 |
} |
| 222 |
|
| 223 |
void InputStrList::update() |
| 224 |
{ |
| 225 |
setValue(m_value.toStringList()); |
| 226 |
} |
| 227 |
|
| 228 |
void InputStrList::updateDefault() |
| 229 |
{ |
| 230 |
if (isDefault() || !m_lab->isEnabled()) |
| 231 |
{ |
| 232 |
m_lab->setText(QString::fromLatin1("<qt>")+m_id+QString::fromLatin1("</qt>")); |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
m_lab->setText(QString::fromLatin1("<qt><font color='red'>")+m_id+QString::fromLatin1("</font></qt>")); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void InputStrList::reset() |
| 241 |
{ |
| 242 |
setValue(m_default); |
| 243 |
} |
| 244 |
|
| 245 |
void InputStrList::writeValue(QTextStream &t,TextCodecAdapter *codec,bool convert) |
| 246 |
{ |
| 247 |
bool first=true; |
| 248 |
foreach (QString s, m_strList) |
| 249 |
{ |
| 250 |
if (!first) |
| 251 |
{ |
| 252 |
t << " \\\n"; |
| 253 |
t << " "; |
| 254 |
} |
| 255 |
first=false; |
| 256 |
writeStringValue(t,codec,s,convert); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
bool InputStrList::isDefault() |
| 261 |
{ |
| 262 |
if (m_strList==m_default) return true; |
| 263 |
|
| 264 |
auto it1 = m_strList.begin(); |
| 265 |
auto it2 = m_default.begin(); |
| 266 |
while (it1!=m_strList.end() && (*it1).isEmpty()) |
| 267 |
{ |
| 268 |
++it1; |
| 269 |
} |
| 270 |
while (it2!=m_default.end() && (*it2).isEmpty()) |
| 271 |
{ |
| 272 |
++it2; |
| 273 |
} |
| 274 |
// both lists are empty |
| 275 |
if (it1==m_strList.end() && it2==m_default.end()) return true; |
| 276 |
|
| 277 |
// one list is empty but the other is not |
| 278 |
if (it1==m_strList.end()) return false; |
| 279 |
if (it2==m_default.end()) return false; |
| 280 |
|
| 281 |
it1 = m_strList.begin(); |
| 282 |
it2 = m_default.begin(); |
| 283 |
while (it1!=m_strList.end() && it2!=m_default.end()) |
| 284 |
{ |
| 285 |
// skip over empty values |
| 286 |
while (it1!=m_strList.end() && (*it1).isEmpty()) |
| 287 |
{ |
| 288 |
++it1; |
| 289 |
} |
| 290 |
while (it2!=m_default.end() && (*it2).isEmpty()) |
| 291 |
{ |
| 292 |
++it2; |
| 293 |
} |
| 294 |
if ((it1!=m_strList.end()) && (it2!=m_default.end())) |
| 295 |
{ |
| 296 |
if ((*it1).trimmed()!= (*it2).trimmed()) // difference so not the default |
| 297 |
{ |
| 298 |
return false; |
| 299 |
} |
| 300 |
++it1; |
| 301 |
++it2; |
| 302 |
} |
| 303 |
else if ((it1!=m_strList.end()) || (it2!=m_default.end())) |
| 304 |
{ |
| 305 |
// one list empty so cannot be the default |
| 306 |
return false; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
return true; |
| 311 |
} |
| 312 |
|
| 313 |
bool InputStrList::isEmpty() |
| 314 |
{ |
| 315 |
foreach (QString s, m_strList) |
| 316 |
{ |
| 317 |
if (!s.isEmpty()) return false; |
| 318 |
} |
| 319 |
return true; |
| 320 |
} |