Debian Codegraph @

addon/doxywizard/inputstring.cpp

raw
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 "inputstring.h"
14 #include "helplabel.h"
15 #include "doxywizard.h"
16 #include "config_msg.h"
17 #include "config.h"
18
19 #include <QComboBox>
20 #include <QLineEdit>
21 #include <QGridLayout>
22 #include <QWheelEvent>
23 #include <QToolBar>
24 #include <QFileInfo>
25 #include <QFileDialog>
26
27 class NoWheelComboBox : public QComboBox
28 {
29 protected:
30 void wheelEvent(QWheelEvent *e)
31 {
32 e->ignore();
33 }
34 };
35
36
37 InputString::InputString( QGridLayout *layout,int &row,
38 const QString & id, const QString &s,
39 StringMode m, const QString &docs,
40 const QString &absPath )
41 : m_default(s), m_sm(m), m_index(0), m_docs(docs), m_id(id),
42 m_absPath(absPath==QString::fromLatin1("1"))
43 {
44 m_lab = new HelpLabel(id);
45 m_brFile = nullptr;
46 m_brDir = nullptr;
47 if (m==StringFixed)
48 {
49 layout->addWidget( m_lab, row, 0 );
50 m_com = new NoWheelComboBox;
51 layout->addWidget( m_com, row, 1, 1, 3, Qt::AlignLeft );
52 m_le=nullptr;
53 m_br=nullptr;
54 m_im=nullptr;
55 row++;
56 }
57 else
58 {
59 layout->addWidget( m_lab, row, 0 );
60 m_le = new QLineEdit;
61 m_le->setText( s );
62 m_im = nullptr;
63 //layout->setColumnMinimumWidth(2,150);
64 if (m==StringFile || m==StringDir || m==StringImage || m==StringFileDir)
65 {
66 QHBoxLayout *rowLayout = new QHBoxLayout;
67 rowLayout->addWidget( m_le);
68 m_br = new QToolBar;
69 m_br->setIconSize(QSize(24,24));
70 if (m==StringFile || m==StringImage || m==StringFileDir)
71 {
72 m_brFile = m_br->addAction(QIcon(QString::fromLatin1(":/images/file.svg")),QString(),this,SLOT(browseFile()));
73 m_brFile->setToolTip(tr("Browse to a file"));
74 if (m==StringImage)
75 {
76 m_im = new QLabel;
77 m_im->setMinimumSize(1,55);
78 m_im->setAlignment(Qt::AlignLeft|Qt::AlignTop);
79 row++;
80 layout->addWidget( m_im,row,1 );
81 }
82 }
83 if (m==StringDir || m==StringFileDir)
84 {
85 m_brDir = m_br->addAction(QIcon(QString::fromLatin1(":/images/folder.svg")),QString(),this,SLOT(browseDir()));
86 m_brDir->setToolTip(tr("Browse to a folder"));
87 }
88 rowLayout->addWidget( m_br);
89 layout->addLayout( rowLayout, m==StringImage?row-1:row, 1, 1, 2 );
90 }
91 else
92 {
93 layout->addWidget( m_le, row, 1, 1, 2 );
94 m_br=nullptr;
95 m_im=nullptr;
96 }
97 m_com=nullptr;
98 row++;
99 }
100
101 if (m_le) connect( m_le, SIGNAL(textChanged(const QString&)),
102 this, SLOT(setValue(const QString&)) );
103 if (m_com) connect( m_com, SIGNAL(textActivated(const QString &)),
104 this, SLOT(setValue(const QString &)) );
105 m_str = s+QChar::fromLatin1('!'); // force update
106 setValue(s);
107 connect( m_lab, SIGNAL(enter()), SLOT(help()) );
108 connect( m_lab, SIGNAL(reset()), SLOT(reset()) );
109 }
110
111 void InputString::help()
112 {
113 showHelp(this);
114 }
115
116
117 InputString::~InputString()
118 {
119 }
120
121
122 void InputString::setValue(const QString &s)
123 {
124 if (m_str!=s)
125 {
126 m_str = s;
127 m_value = m_str;
128 updateDefault();
129 }
130 }
131 void InputString::updateDefault()
132 {
133 {
134 if (m_str==m_default || !m_lab->isEnabled())
135 {
136 m_lab->setText(QString::fromLatin1("<qt>")+m_id+QString::fromLatin1("</qt>"));
137 }
138 else
139 {
140 m_lab->setText(QString::fromLatin1("<qt><font color='red'>")+m_id+QString::fromLatin1("</font></qt>"));
141 }
142 if (m_im)
143 {
144 if (m_str.isEmpty())
145 {
146 m_im->setText(tr("No Project logo selected."));
147 }
148 else
149 {
150 QFile Fout(m_str);
151 if(!Fout.exists())
152 {
153 m_im->setText(tr("Sorry, cannot find file(")+m_str+QString::fromLatin1(");"));
154 }
155 else
156 {
157 QPixmap pm(m_str);
158 if (!pm.isNull())
159 {
160 m_im->setPixmap(pm.scaledToHeight(55,Qt::SmoothTransformation));
161 }
162 else
163 {
164 m_im->setText(tr("Sorry, no preview available (")+m_str+QString::fromLatin1(");"));
165 }
166 }
167 }
168 }
169 if (m_le && m_le->text()!=m_str) m_le->setText( m_str );
170 emit changed();
171 }
172 }
173
174 void InputString::setEnabled(bool state)
175 {
176 m_lab->setEnabled(state);
177 if (m_le) m_le->setEnabled(state);
178 if (m_im) m_im->setEnabled(state);
179 if (m_br) m_br->setEnabled(state);
180 if (m_brFile) m_brFile->setEnabled(state);
181 if (m_brDir) m_brDir->setEnabled(state);
182 if (m_com) m_com->setEnabled(state);
183 updateDefault();
184 }
185
186 void InputString::browseFile()
187 {
188 QString path = QFileInfo(MainWindow::instance().configFileName()).path();
189 QString fileName = QFileDialog::getOpenFileName(&MainWindow::instance(),
190 tr("Select file"),path);
191 if (!fileName.isNull())
192 {
193 QDir dir(path);
194 if (!MainWindow::instance().configFileName().isEmpty() && dir.exists())
195 {
196 fileName = m_absPath ? fileName : dir.relativeFilePath(fileName);
197 }
198 setValue(fileName);
199 }
200 }
201 void InputString::browseDir()
202 {
203 QString path = QFileInfo(MainWindow::instance().configFileName()).path();
204 {
205 QString dirName = QFileDialog::getExistingDirectory(&MainWindow::instance(),
206 tr("Select directory"),path);
207 if (!dirName.isNull())
208 {
209 QDir dir(path);
210 if (!MainWindow::instance().configFileName().isEmpty() && dir.exists())
211 {
212 dirName = m_absPath ? dirName : dir.relativeFilePath(dirName);
213 }
214 setValue(dirName);
215 }
216 }
217 }
218
219 void InputString::clear()
220 {
221 setValue(QString());
222 }
223
224 void InputString::addValue(QString s)
225 {
226 if (m_sm==StringFixed)
227 {
228 m_values.append(s);
229 m_com->addItem(s);
230 }
231 }
232
233 void InputString::setDefault()
234 {
235 int index = m_values.indexOf(m_str);
236 if (index!=-1 && m_com) m_com->setCurrentIndex(index);
237 }
238
239 QVariant &InputString::value()
240 {
241 return m_value;
242 }
243
244 void InputString::update()
245 {
246 setValue(m_value.toString().trimmed());
247 setDefault();
248 }
249
250 void InputString::reset()
251 {
252 setValue(m_default);
253 setDefault();
254 }
255
256 void InputString::writeValue(QTextStream &t,TextCodecAdapter *codec,bool convert)
257 {
258 writeStringValue(t,codec,m_str,convert);
259 }
260
261 bool InputString::isDefault()
262 {
263 return m_str == m_default;
264 }
265
266 QString InputString::checkEnumVal(const QString &value)
267 {
268 QString val = value.trimmed().toLower();
269 QStringList::Iterator it;
270 for ( it= m_values.begin(); it != m_values.end(); ++it )
271 {
272 QString enumVal = *it;
273 if (enumVal.toLower() == val) return enumVal;
274 }
275
276 config_warn("argument '%s' for option %s is not a valid enum value."
277 " Using the default: %s!",qPrintable(value),qPrintable(m_id),qPrintable(m_default));
278 return m_default;
279 }