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