BibleTime
btfindwidget.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "btfindwidget.h"
14 
15 #include <QIcon>
16 #include <QCheckBox>
17 #include <QHBoxLayout>
18 #include <QLineEdit>
19 #include <QSize>
20 #include <QSizePolicy>
21 #include <QSpacerItem>
22 #include <Qt>
23 #include <QTimerEvent>
24 #include <QToolButton>
25 #include <utility>
26 #include "../../util/btconnect.h"
27 #include "../../util/cresmgr.h"
28 
29 
31  : QWidget(parent)
32 {
33  // Overall layout:
34  auto widgetLayout = new QHBoxLayout(this);
35  widgetLayout->setContentsMargins(0, 0, 0, 0);
36  widgetLayout->setSpacing(8);
37 
38  // Buttons and text editor:
39  auto const newButton =
40  [this, widgetLayout](QIcon const & icon) {
41  QToolButton * const button = new QToolButton(this);
42  button->setIcon(icon);
43  button->setIconSize(QSize(16, 16));
44  button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
45  button->setAutoRaise(true);
46  widgetLayout->addWidget(button);
47  return button;
48  };
49 
50  // Close button:
51  auto * const closeButton = newButton(CResMgr::findWidget::icon_close());
52  BT_CONNECT(closeButton, &QToolButton::released, this, &BtFindWidget::hide);
53 
54  // Text editor:
55  m_textEditor = new QLineEdit(this);
56  widgetLayout->addWidget(m_textEditor);
57  BT_CONNECT(m_textEditor, &QLineEdit::textChanged,
59  BT_CONNECT(m_textEditor, &QLineEdit::returnPressed,
61 
62  // Next and Previous buttons:
63  m_previousButton = newButton(CResMgr::findWidget::icon_previous());
64  BT_CONNECT(m_previousButton, &QToolButton::released,
66  BT_CONNECT(m_previousButton, &QToolButton::released,
68  m_nextButton = newButton(CResMgr::findWidget::icon_next());
69  BT_CONNECT(m_nextButton, &QToolButton::released,
71  BT_CONNECT(m_nextButton, &QToolButton::released,
72  this, &BtFindWidget::findNext);
73 
74  // Case checkbox:
75  m_caseCheckBox = new QCheckBox(this);
76  BT_CONNECT(m_caseCheckBox, &QCheckBox::stateChanged,
78  widgetLayout->addWidget(m_caseCheckBox);
79 
80  // Spacer:
81  widgetLayout->addItem(new QSpacerItem(0,
82  0,
83  QSizePolicy::Expanding,
84  QSizePolicy::Minimum));
85  setFocusProxy(m_textEditor);
86 
87  retranslateUi();
88 
91  == m_caseCheckBox->isChecked());
92 }
93 
95  setVisible(true);
96  m_textEditor->selectAll();
97  m_textEditor->setFocus(Qt::ShortcutFocusReason);
98 }
99 
100 void BtFindWidget::timerEvent(QTimerEvent * const event) {
101  auto const timerId = event->timerId();
102  BT_ASSERT(timerId);
103  if (timerId != m_throttleTimerId)
104  return QObject::timerEvent(event);
105 
106  event->accept();
108 }
109 
111  killTimer(m_throttleTimerId);
112  m_throttleTimerId = startTimer(900);
113 }
114 
116  killTimer(m_throttleTimerId);
117  m_throttleTimerId = 0;
118 
119  HighlightState newState{m_textEditor->text(), m_caseCheckBox->isChecked()};
120  if (m_lastHighlightState != newState) {
121  m_lastHighlightState = std::move(newState);
122  Q_EMIT highlightText(m_textEditor->text(), m_caseCheckBox->isChecked());
123  }
124 }
125 
127  m_textEditor->setToolTip(tr("The text you want to search for",
128  "findWidget"));
129  m_previousButton->setText(tr("Previous"));
130  m_nextButton->setText(tr("Next"));
131  m_caseCheckBox->setText(tr("Match case"));
132 }
#define BT_ASSERT(...)
Definition: btassert.h:17
#define BT_CONNECT(...)
Definition: btconnect.h:20
HighlightState m_lastHighlightState
Definition: btfindwidget.h:78
void findPrevious()
void retranslateUi()
BtFindWidget(QWidget *parent=nullptr)
void highlightImmediately()
void showAndSelect()
QLineEdit * m_textEditor
Definition: btfindwidget.h:74
void findNext()
QToolButton * m_previousButton
Definition: btfindwidget.h:76
QToolButton * m_nextButton
Definition: btfindwidget.h:75
int m_throttleTimerId
Definition: btfindwidget.h:79
void highlightText(QString const &text, bool caseSensitive)
void timerEvent(QTimerEvent *const event) final override
QCheckBox * m_caseCheckBox
Definition: btfindwidget.h:77
void queueHighlight()