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