1 /* module_preferences_scroll_area.cpp
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>
36 #include <QFileDialog>
37 #include <QHBoxLayout>
40 #include <QPushButton>
41 #include <QRadioButton>
43 #include <QSpacerItem>
47 Q_DECLARE_METATYPE(pref_t
*)
49 const char *pref_prop_
= "pref_ptr";
52 // Callbacks prefs routines
54 /* show a single preference on the GtkGrid of a preference page */
56 pref_show(pref_t
*pref
, gpointer layout_ptr
)
58 QVBoxLayout
*vb
= static_cast<QVBoxLayout
*>(layout_ptr
);
60 if (!pref
|| !vb
) return 0;
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
));
77 QCheckBox
*bool_cb
= new QCheckBox(pref
->title
);
78 bool_cb
->setProperty(pref_prop_
, qVariantFromValue(pref
));
79 vb
->addWidget(bool_cb
);
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(
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
);
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
));
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
));
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
));
141 case PREF_STATIC_TEXT
:
143 vb
->addWidget(new QLabel(pref
->title
));
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
));
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(
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
));
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.
194 ModulePreferencesScrollArea::ModulePreferencesScrollArea(module_t
*module
, QWidget
*parent
) :
196 ui(new Ui::ModulePreferencesScrollArea
),
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
*>();
210 switch (pref
->type
) {
212 connect(le
, SIGNAL(textEdited(QString
)), this, SLOT(uintLineEditTextEdited(QString
)));
217 connect(le
, SIGNAL(textEdited(QString
)), this, SLOT(stringLineEditTextEdited(QString
)));
220 connect(le
, SIGNAL(textEdited(QString
)), this, SLOT(rangeSyntaxLineEditTextEdited(QString
)));
227 foreach (QCheckBox
*cb
, findChildren
<QCheckBox
*>()) {
228 pref_t
*pref
= cb
->property(pref_prop_
).value
<pref_t
*>();
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
*>();
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
*>();
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
*>();
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()
275 void ModulePreferencesScrollArea::showEvent(QShowEvent
*evt
)
281 void ModulePreferencesScrollArea::resizeEvent(QResizeEvent
*evt
)
283 QScrollArea::resizeEvent(evt
);
285 if (verticalScrollBar()->isVisible()) {
286 setFrameStyle(QFrame::StyledPanel
);
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
*>();
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
*>();
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
*>();
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
*>();
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
*>();
347 uint new_uint
= new_str
.toUInt(&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
*>();
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
*>();
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
*>();
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
*>();
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
*>();
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
);
422 range_se
->setSyntaxState(SyntaxLineEdit::Valid
);
425 range_se
->setSyntaxState(SyntaxLineEdit::Invalid
);
429 void ModulePreferencesScrollArea::uatPushButtonPressed()
431 QPushButton
*uat_pb
= qobject_cast
<QPushButton
*>(sender());
434 pref_t
*pref
= uat_pb
->property(pref_prop_
).value
<pref_t
*>();
437 UatDialog
uat_dlg(this, pref
->varp
.uat
);
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
*>();
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
);
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
*>();
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
);
485 * indent-tabs-mode: nil
488 * ex: set shiftwidth=4 tabstop=8 expandtab:
489 * :indentSize=4:tabSize=8:noTabs=true: