MSWSP: add parse_CNatLanguageRestriction()
[wireshark-wip.git] / ui / qt / module_preferences_scroll_area.cpp
blob7752cf4f7429874d6746be1e834eff47fc63cda4
1 /* module_preferences_scroll_area.cpp
3 * $Id$
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "module_preferences_scroll_area.h"
25 #include "ui_module_preferences_scroll_area.h"
26 #include "syntax_line_edit.h"
27 #include "qt_ui_utils.h"
28 #include "uat_dialog.h"
30 #include <epan/prefs-int.h>
32 #include <QAbstractButton>
33 #include <QButtonGroup>
34 #include <QCheckBox>
35 #include <QComboBox>
36 #include <QFileDialog>
37 #include <QHBoxLayout>
38 #include <QLabel>
39 #include <QLineEdit>
40 #include <QPushButton>
41 #include <QRadioButton>
42 #include <QScrollBar>
43 #include <QSpacerItem>
45 #include <QDebug>
47 Q_DECLARE_METATYPE(pref_t *)
49 const char *pref_prop_ = "pref_ptr";
51 extern "C" {
52 // Callbacks prefs routines
54 /* show a single preference on the GtkGrid of a preference page */
55 static guint
56 pref_show(pref_t *pref, gpointer layout_ptr)
58 QVBoxLayout *vb = static_cast<QVBoxLayout *>(layout_ptr);
60 if (!pref || !vb) return 0;
62 switch (pref->type) {
63 case PREF_UINT:
65 QHBoxLayout *hb = new QHBoxLayout();
66 hb->addWidget(new QLabel(pref->title));
67 QLineEdit *uint_le = new QLineEdit();
68 uint_le->setProperty(pref_prop_, qVariantFromValue(pref));
69 uint_le->setMinimumWidth(uint_le->fontMetrics().height() * 8);
70 hb->addWidget(uint_le);
71 hb->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum));
72 vb->addLayout(hb);
73 break;
75 case PREF_BOOL:
77 QCheckBox *bool_cb = new QCheckBox(pref->title);
78 bool_cb->setProperty(pref_prop_, qVariantFromValue(pref));
79 vb->addWidget(bool_cb);
80 break;
82 case PREF_ENUM:
84 const enum_val_t *ev;
85 if (!pref->info.enum_info.enumvals) return 0;
87 if (pref->info.enum_info.radio_buttons) {
88 vb->addWidget(new QLabel(pref->title));
89 QButtonGroup *enum_bg = new QButtonGroup();
90 for (ev = pref->info.enum_info.enumvals; ev && ev->description; ev++) {
91 QRadioButton *enum_rb = new QRadioButton(ev->description);
92 QStyleOption style_opt;
93 enum_rb->setProperty(pref_prop_, qVariantFromValue(pref));
94 enum_rb->setStyleSheet(QString(
95 "QRadioButton {"
96 " margin-left: %1px;"
97 "}"
99 .arg(enum_rb->style()->subElementRect(QStyle::SE_CheckBoxContents, &style_opt).left()));
100 enum_bg->addButton(enum_rb, ev->value);
101 vb->addWidget(enum_rb);
103 } else {
104 QHBoxLayout *hb = new QHBoxLayout();
105 QComboBox *enum_cb = new QComboBox();
106 enum_cb->setProperty(pref_prop_, qVariantFromValue(pref));
107 for (ev = pref->info.enum_info.enumvals; ev && ev->description; ev++) {
108 enum_cb->addItem(ev->description, QVariant(ev->value));
110 hb->addWidget(new QLabel(pref->title));
111 hb->addWidget(enum_cb);
112 hb->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum));
113 vb->addLayout(hb);
115 break;
117 case PREF_STRING:
119 QHBoxLayout *hb = new QHBoxLayout();
120 hb->addWidget(new QLabel(pref->title));
121 QLineEdit *string_le = new QLineEdit();
122 string_le->setProperty(pref_prop_, qVariantFromValue(pref));
123 string_le->setMinimumWidth(string_le->fontMetrics().height() * 20);
124 hb->addWidget(string_le);
125 hb->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum));
126 vb->addLayout(hb);
127 break;
129 case PREF_RANGE:
131 QHBoxLayout *hb = new QHBoxLayout();
132 hb->addWidget(new QLabel(pref->title));
133 SyntaxLineEdit *range_se = new SyntaxLineEdit();
134 range_se->setProperty(pref_prop_, qVariantFromValue(pref));
135 range_se->setMinimumWidth(range_se->fontMetrics().height() * 20);
136 hb->addWidget(range_se);
137 hb->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum));
138 vb->addLayout(hb);
139 break;
141 case PREF_STATIC_TEXT:
143 vb->addWidget(new QLabel(pref->title));
144 break;
146 case PREF_UAT:
148 QHBoxLayout *hb = new QHBoxLayout();
149 hb->addWidget(new QLabel(pref->title));
150 QPushButton *uat_pb = new QPushButton(QObject::tr("Edit..."));
151 uat_pb->setProperty(pref_prop_, qVariantFromValue(pref));
152 hb->addWidget(uat_pb);
153 hb->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum));
154 vb->addLayout(hb);
155 break;
157 case PREF_FILENAME:
158 case PREF_DIRNAME:
160 vb->addWidget(new QLabel(pref->title));
161 QHBoxLayout *hb = new QHBoxLayout();
162 QLineEdit *path_le = new QLineEdit();
163 QStyleOption style_opt;
164 path_le->setProperty(pref_prop_, qVariantFromValue(pref));
165 path_le->setMinimumWidth(path_le->fontMetrics().height() * 20);
166 path_le->setStyleSheet(QString(
167 "QLineEdit {"
168 " margin-left: %1px;"
171 .arg(path_le->style()->subElementRect(QStyle::SE_CheckBoxContents, &style_opt).left()));
172 hb->addWidget(path_le);
173 QPushButton *path_pb = new QPushButton(QObject::tr("Browse..."));
174 path_pb->setProperty(pref_prop_, qVariantFromValue(pref));
175 hb->addWidget(path_pb);
176 hb->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum));
177 vb->addLayout(hb);
178 break;
180 case PREF_COLOR:
182 // XXX - Not needed yet. When it is needed we can add a label + QFrame which pops up a
183 // color picker similar to the Font and Colors prefs.
184 break;
186 default:
187 break;
189 return 0;
192 } // extern "C"
194 ModulePreferencesScrollArea::ModulePreferencesScrollArea(module_t *module, QWidget *parent) :
195 QScrollArea(parent),
196 ui(new Ui::ModulePreferencesScrollArea),
197 module_(module)
199 ui->setupUi(this);
201 if (!module) return;
203 /* Add items for each of the preferences */
204 prefs_pref_foreach(module, pref_show, (gpointer) ui->verticalLayout);
206 foreach (QLineEdit *le, findChildren<QLineEdit *>()) {
207 pref_t *pref = le->property(pref_prop_).value<pref_t *>();
208 if (!pref) continue;
210 switch (pref->type) {
211 case PREF_UINT:
212 connect(le, SIGNAL(textEdited(QString)), this, SLOT(uintLineEditTextEdited(QString)));
213 break;
214 case PREF_STRING:
215 case PREF_FILENAME:
216 case PREF_DIRNAME:
217 connect(le, SIGNAL(textEdited(QString)), this, SLOT(stringLineEditTextEdited(QString)));
218 break;
219 case PREF_RANGE:
220 connect(le, SIGNAL(textEdited(QString)), this, SLOT(rangeSyntaxLineEditTextEdited(QString)));
221 break;
222 default:
223 break;
227 foreach (QCheckBox *cb, findChildren<QCheckBox *>()) {
228 pref_t *pref = cb->property(pref_prop_).value<pref_t *>();
229 if (!pref) continue;
231 if (pref->type == PREF_BOOL) {
232 connect(cb, SIGNAL(toggled(bool)), this, SLOT(boolCheckBoxToggled(bool)));
236 foreach (QRadioButton *rb, findChildren<QRadioButton *>()) {
237 pref_t *pref = rb->property(pref_prop_).value<pref_t *>();
238 if (!pref) continue;
240 if (pref->type == PREF_ENUM && pref->info.enum_info.radio_buttons) {
241 connect(rb, SIGNAL(toggled(bool)), this, SLOT(enumRadioButtonToggled(bool)));
245 foreach (QComboBox *combo, findChildren<QComboBox *>()) {
246 pref_t *pref = combo->property(pref_prop_).value<pref_t *>();
247 if (!pref) continue;
249 if (pref->type == PREF_ENUM && !pref->info.enum_info.radio_buttons) {
250 connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(enumComboBoxCurrentIndexChanged(int)));
254 foreach (QPushButton *pb, findChildren<QPushButton *>()) {
255 pref_t *pref = pb->property(pref_prop_).value<pref_t *>();
256 if (!pref) continue;
258 if (pref->type == PREF_UAT) {
259 connect(pb, SIGNAL(pressed()), this, SLOT(uatPushButtonPressed()));
260 } else if (pref->type == PREF_FILENAME) {
261 connect(pb, SIGNAL(pressed()), this, SLOT(filenamePushButtonPressed()));
262 } else if (pref->type == PREF_DIRNAME) {
263 connect(pb, SIGNAL(pressed()), this, SLOT(dirnamePushButtonPressed()));
267 ui->verticalLayout->addSpacerItem(new QSpacerItem(10, 1, QSizePolicy::Minimum, QSizePolicy::Expanding));
270 ModulePreferencesScrollArea::~ModulePreferencesScrollArea()
272 delete ui;
275 void ModulePreferencesScrollArea::showEvent(QShowEvent *evt)
277 Q_UNUSED(evt)
278 updateWidgets();
281 void ModulePreferencesScrollArea::resizeEvent(QResizeEvent *evt)
283 QScrollArea::resizeEvent(evt);
285 if (verticalScrollBar()->isVisible()) {
286 setFrameStyle(QFrame::StyledPanel);
287 } else {
288 setFrameStyle(QFrame::NoFrame);
292 void ModulePreferencesScrollArea::updateWidgets()
294 foreach (QLineEdit *le, findChildren<QLineEdit *>()) {
295 pref_t *pref = le->property(pref_prop_).value<pref_t *>();
296 if (!pref) continue;
298 le->setText(gchar_free_to_qstring(prefs_pref_to_str(pref, pref_stashed)).remove(QRegExp("\n\t")));
301 foreach (QCheckBox *cb, findChildren<QCheckBox *>()) {
302 pref_t *pref = cb->property(pref_prop_).value<pref_t *>();
303 if (!pref) continue;
305 if (pref->type == PREF_BOOL) {
306 cb->setChecked(pref->stashed_val.boolval);
310 foreach (QRadioButton *enum_rb, findChildren<QRadioButton *>()) {
311 pref_t *pref = enum_rb->property(pref_prop_).value<pref_t *>();
312 if (!pref) continue;
314 QButtonGroup *enum_bg = enum_rb->group();
315 if (!enum_bg) continue;
317 if (pref->type == PREF_ENUM && pref->info.enum_info.radio_buttons) {
318 if (pref->stashed_val.enumval == enum_bg->id(enum_rb)) {
319 enum_rb->setChecked(true);
324 foreach (QComboBox *enum_cb, findChildren<QComboBox *>()) {
325 pref_t *pref = enum_cb->property(pref_prop_).value<pref_t *>();
326 if (!pref) continue;
328 if (pref->type == PREF_ENUM && !pref->info.enum_info.radio_buttons) {
329 for (int i = 0; i < enum_cb->count(); i++) {
330 if (pref->stashed_val.enumval == enum_cb->itemData(i).toInt()) {
331 enum_cb->setCurrentIndex(i);
338 void ModulePreferencesScrollArea::uintLineEditTextEdited(const QString &new_str)
340 QLineEdit *uint_le = qobject_cast<QLineEdit*>(sender());
341 if (!uint_le) return;
343 pref_t *pref = uint_le->property(pref_prop_).value<pref_t *>();
344 if (!pref) return;
346 bool ok;
347 uint new_uint = new_str.toUInt(&ok);
348 if (ok) {
349 pref->stashed_val.uint = new_uint;
353 void ModulePreferencesScrollArea::boolCheckBoxToggled(bool checked)
355 QCheckBox *bool_cb = qobject_cast<QCheckBox*>(sender());
356 if (!bool_cb) return;
358 pref_t *pref = bool_cb->property(pref_prop_).value<pref_t *>();
359 if (!pref) return;
361 pref->stashed_val.boolval = checked;
364 void ModulePreferencesScrollArea::enumRadioButtonToggled(bool checked)
366 if (!checked) return;
367 QRadioButton *enum_rb = qobject_cast<QRadioButton*>(sender());
368 if (!enum_rb) return;
370 QButtonGroup *enum_bg = enum_rb->group();
371 if (!enum_bg) return;
373 pref_t *pref = enum_rb->property(pref_prop_).value<pref_t *>();
374 if (!pref) return;
376 if (enum_bg->checkedId() >= 0) {
377 pref->stashed_val.enumval = enum_bg->checkedId();
381 void ModulePreferencesScrollArea::enumComboBoxCurrentIndexChanged(int index)
383 QComboBox *enum_cb = qobject_cast<QComboBox*>(sender());
384 if (!enum_cb) return;
386 pref_t *pref = enum_cb->property(pref_prop_).value<pref_t *>();
387 if (!pref) return;
389 pref->stashed_val.enumval = enum_cb->itemData(index).toInt();
392 void ModulePreferencesScrollArea::stringLineEditTextEdited(const QString &new_str)
394 QLineEdit *string_le = qobject_cast<QLineEdit*>(sender());
395 if (!string_le) return;
397 pref_t *pref = string_le->property(pref_prop_).value<pref_t *>();
398 if (!pref) return;
400 g_free((void *)pref->stashed_val.string);
401 pref->stashed_val.string = qstring_strdup(new_str);
404 void ModulePreferencesScrollArea::rangeSyntaxLineEditTextEdited(const QString &new_str)
406 SyntaxLineEdit *range_se = qobject_cast<SyntaxLineEdit*>(sender());
407 if (!range_se) return;
409 pref_t *pref = range_se->property(pref_prop_).value<pref_t *>();
410 if (!pref) return;
412 range_t *newrange;
413 convert_ret_t ret = range_convert_str(&newrange, new_str.toUtf8().constData(), pref->info.max_value);
415 if (ret == CVT_NO_ERROR) {
416 g_free(pref->stashed_val.range);
417 pref->stashed_val.range = newrange;
419 if (new_str.isEmpty()) {
420 range_se->setSyntaxState(SyntaxLineEdit::Empty);
421 } else {
422 range_se->setSyntaxState(SyntaxLineEdit::Valid);
424 } else {
425 range_se->setSyntaxState(SyntaxLineEdit::Invalid);
429 void ModulePreferencesScrollArea::uatPushButtonPressed()
431 QPushButton *uat_pb = qobject_cast<QPushButton*>(sender());
432 if (!uat_pb) return;
434 pref_t *pref = uat_pb->property(pref_prop_).value<pref_t *>();
435 if (!pref) return;
437 UatDialog uat_dlg(this, pref->varp.uat);
438 uat_dlg.exec();
441 void ModulePreferencesScrollArea::filenamePushButtonPressed()
443 QPushButton *filename_pb = qobject_cast<QPushButton*>(sender());
444 if (!filename_pb) return;
446 pref_t *pref = filename_pb->property(pref_prop_).value<pref_t *>();
447 if (!pref) return;
449 QString filename = QFileDialog::getSaveFileName(this,
450 QString(tr("Wireshark: ")) + pref->description,
451 pref->stashed_val.string);
453 if (!filename.isEmpty()) {
454 g_free((void *)pref->stashed_val.string);
455 pref->stashed_val.string = qstring_strdup(filename);
456 updateWidgets();
460 void ModulePreferencesScrollArea::dirnamePushButtonPressed()
462 QPushButton *dirname_pb = qobject_cast<QPushButton*>(sender());
463 if (!dirname_pb) return;
465 pref_t *pref = dirname_pb->property(pref_prop_).value<pref_t *>();
466 if (!pref) return;
468 QString dirname = QFileDialog::getExistingDirectory(this,
469 QString(tr("Wireshark: ")) + pref->description,
470 pref->stashed_val.string);
472 if (!dirname.isEmpty()) {
473 g_free((void *)pref->stashed_val.string);
474 pref->stashed_val.string = qstring_strdup(dirname);
475 updateWidgets();
480 * Editor modelines
482 * Local Variables:
483 * c-basic-offset: 4
484 * tab-width: 8
485 * indent-tabs-mode: nil
486 * End:
488 * ex: set shiftwidth=4 tabstop=8 expandtab:
489 * :indentSize=4:tabSize=8:noTabs=true: