replace SPDX identifier GPL-2.0+ with GPL-2.0-or-later.
[metze/wireshark/wip.git] / ui / qt / widgets / find_line_edit.cpp
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 #include <ui/qt/widgets/find_line_edit.h>
10 #include <ui/qt/utils/color_utils.h>
11 #include "epan/prefs.h"
12
13 #include <QAction>
14 #include <QKeyEvent>
15 #include <QMenu>
16
17 void FindLineEdit::contextMenuEvent(QContextMenuEvent *event)
18 {
19     QMenu *menu = createStandardContextMenu();
20
21 #if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
22     QAction *action;
23
24     menu->addSeparator();
25
26     action = menu->addAction(tr("Textual Find"));
27     action->setCheckable(true);
28     action->setChecked(!use_regex_);
29     connect(action, SIGNAL(triggered()), this, SLOT(setUseTextual()));
30
31     action = menu->addAction(tr("Regular Expression Find"));
32     action->setCheckable(true);
33     action->setChecked(use_regex_);
34     connect(action, SIGNAL(triggered()), this, SLOT(setUseRegex()));
35 #endif
36
37     menu->exec(event->globalPos());
38     delete menu;
39 }
40
41 void FindLineEdit::keyPressEvent(QKeyEvent *event)
42 {
43     QLineEdit::keyPressEvent(event);
44
45     if (use_regex_) {
46         validateText();
47     }
48 }
49
50 void FindLineEdit::validateText()
51 {
52     QString style("QLineEdit { background-color: %1; }");
53
54     if (!use_regex_ || text().isEmpty()) {
55         setStyleSheet(style.arg(QString("")));
56     } else {
57         QRegExp regexp(text());
58         if (regexp.isValid()) {
59             setStyleSheet(style.arg(ColorUtils::fromColorT(prefs.gui_text_valid).name()));
60         } else {
61             setStyleSheet(style.arg(ColorUtils::fromColorT(prefs.gui_text_invalid).name()));
62         }
63     }
64 }
65
66 void FindLineEdit::setUseTextual()
67 {
68     use_regex_ = false;
69     validateText();
70     emit useRegexFind(use_regex_);
71 }
72
73 void FindLineEdit::setUseRegex()
74 {
75     use_regex_ = true;
76     validateText();
77     emit useRegexFind(use_regex_);
78 }
79
80 /*
81  * Editor modelines
82  *
83  * Local Variables:
84  * c-basic-offset: 4
85  * tab-width: 8
86  * indent-tabs-mode: nil
87  * End:
88  *
89  * ex: set shiftwidth=4 tabstop=8 expandtab:
90  * :indentSize=4:tabSize=8:noTabs=true:
91  */