1 /* find_line_edit.cpp
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9 
10 #include <ui/qt/widgets/find_line_edit.h>
11 #include <ui/qt/utils/color_utils.h>
12 #include "epan/prefs.h"
13 
14 #include <QAction>
15 #include <QKeyEvent>
16 #include <QMenu>
17 
contextMenuEvent(QContextMenuEvent * event)18 void FindLineEdit::contextMenuEvent(QContextMenuEvent *event)
19 {
20     QMenu *menu = createStandardContextMenu();
21     QAction *action;
22 
23     menu->addSeparator();
24 
25     action = menu->addAction(tr("Textual Find"));
26     action->setCheckable(true);
27     action->setChecked(!use_regex_);
28     connect(action, &QAction::triggered, this, &FindLineEdit::setUseTextual);
29 
30     action = menu->addAction(tr("Regular Expression Find"));
31     action->setCheckable(true);
32     action->setChecked(use_regex_);
33     connect(action, &QAction::triggered, this, &FindLineEdit::setUseRegex);
34 
35     menu->exec(event->globalPos());
36     delete menu;
37 }
38 
keyPressEvent(QKeyEvent * event)39 void FindLineEdit::keyPressEvent(QKeyEvent *event)
40 {
41     QLineEdit::keyPressEvent(event);
42 
43     if (use_regex_) {
44         validateText();
45     }
46 }
47 
validateText()48 void FindLineEdit::validateText()
49 {
50     QString style("QLineEdit { background-color: %1; }");
51 
52     if (!use_regex_ || text().isEmpty()) {
53         setStyleSheet(style.arg(QString("")));
54     } else {
55         QRegExp regexp(text());
56         if (regexp.isValid()) {
57             setStyleSheet(style.arg(ColorUtils::fromColorT(prefs.gui_text_valid).name()));
58         } else {
59             setStyleSheet(style.arg(ColorUtils::fromColorT(prefs.gui_text_invalid).name()));
60         }
61     }
62 }
63 
setUseTextual()64 void FindLineEdit::setUseTextual()
65 {
66     use_regex_ = false;
67     validateText();
68     emit useRegexFind(use_regex_);
69 }
70 
setUseRegex()71 void FindLineEdit::setUseRegex()
72 {
73     use_regex_ = true;
74     validateText();
75     emit useRegexFind(use_regex_);
76 }
77