|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +// Copyright (C) 2025 The MMapper Authors |
| 3 | + |
| 4 | +#include "findreplacewidget.h" |
| 5 | + |
| 6 | +#include <QCheckBox> |
| 7 | +#include <QGridLayout> |
| 8 | +#include <QIcon> |
| 9 | +#include <QKeyEvent> |
| 10 | +#include <QLabel> |
| 11 | +#include <QLineEdit> |
| 12 | +#include <QMetaObject> |
| 13 | +#include <QSizePolicy> |
| 14 | +#include <QToolButton> |
| 15 | + |
| 16 | +FindReplaceWidget::FindReplaceWidget(bool allowReplace, QWidget *const parent) |
| 17 | + : QWidget(parent) |
| 18 | +{ |
| 19 | + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
| 20 | + |
| 21 | + QGridLayout *layout = new QGridLayout(this); |
| 22 | + layout->setContentsMargins(2, 2, 2, 2); |
| 23 | + layout->setSpacing(4); |
| 24 | + |
| 25 | + m_findLineEdit = new QLineEdit(this); |
| 26 | + m_findLineEdit->setPlaceholderText("Find"); |
| 27 | + connect(m_findLineEdit, |
| 28 | + &QLineEdit::textChanged, |
| 29 | + this, |
| 30 | + &FindReplaceWidget::slot_updateButtonStates); |
| 31 | + layout->addWidget(m_findLineEdit, 0, 0); |
| 32 | + |
| 33 | + m_findPreviousButton = createActionButton("go-previous", |
| 34 | + ":/icons/layerup.png", |
| 35 | + "", |
| 36 | + "Find Previous", |
| 37 | + true, |
| 38 | + Qt::ToolButtonIconOnly); |
| 39 | + layout->addWidget(m_findPreviousButton, 0, 1, Qt::AlignVCenter); |
| 40 | + |
| 41 | + m_findNextButton = createActionButton("go-next", |
| 42 | + ":/icons/layerdown.png", |
| 43 | + "", |
| 44 | + "Find Next (Enter)", |
| 45 | + true, |
| 46 | + Qt::ToolButtonIconOnly); |
| 47 | + layout->addWidget(m_findNextButton, 0, 2, Qt::AlignVCenter); |
| 48 | + |
| 49 | + m_replaceToggleCheckBox = new QCheckBox("Replace", this); |
| 50 | + m_replaceToggleCheckBox->setToolTip("Show/Hide Replace Options"); |
| 51 | + connect(m_replaceToggleCheckBox, &QCheckBox::toggled, this, [this](bool checked) { |
| 52 | + m_replaceLineEdit->setHidden(!checked); |
| 53 | + m_replaceCurrentButton->setHidden(!checked); |
| 54 | + m_replaceAllButton->setHidden(!checked); |
| 55 | + slot_updateButtonStates(); |
| 56 | + }); |
| 57 | + m_replaceToggleCheckBox->setEnabled(allowReplace); |
| 58 | + layout->addWidget(m_replaceToggleCheckBox, 0, 3, Qt::AlignVCenter | Qt::AlignRight); |
| 59 | + |
| 60 | + QToolButton *closeButton = createActionButton("window-close", |
| 61 | + ":/icons/cancel.png", |
| 62 | + "", |
| 63 | + "Close (Esc)", |
| 64 | + true, |
| 65 | + Qt::ToolButtonIconOnly); |
| 66 | + layout->addWidget(closeButton, 0, 4, Qt::AlignVCenter | Qt::AlignRight); |
| 67 | + |
| 68 | + m_replaceLineEdit = new QLineEdit(this); |
| 69 | + m_replaceLineEdit->setPlaceholderText("Replace"); |
| 70 | + layout->addWidget(m_replaceLineEdit, 1, 0); |
| 71 | + |
| 72 | + m_replaceCurrentButton = createActionButton("edit-find-replace", |
| 73 | + ":/icons/findreplace.png", |
| 74 | + "Replace", |
| 75 | + "Replace Current"); |
| 76 | + layout->addWidget(m_replaceCurrentButton, 1, 1, 1, 2, Qt::AlignVCenter); |
| 77 | + |
| 78 | + m_replaceAllButton = createActionButton("dialog-ok-apply", |
| 79 | + ":/icons/apply.png", |
| 80 | + "All", |
| 81 | + "Replace All"); |
| 82 | + layout->addWidget(m_replaceAllButton, 1, 3, 1, 2, Qt::AlignVCenter); |
| 83 | + |
| 84 | + layout->setColumnStretch(0, 10); |
| 85 | + layout->setColumnStretch(1, 0); |
| 86 | + layout->setColumnStretch(2, 0); |
| 87 | + layout->setColumnStretch(3, 0); |
| 88 | + layout->setColumnStretch(4, 0); |
| 89 | + |
| 90 | + setLayout(layout); |
| 91 | + |
| 92 | + m_replaceLineEdit->hide(); |
| 93 | + m_replaceCurrentButton->hide(); |
| 94 | + m_replaceAllButton->hide(); |
| 95 | + |
| 96 | + auto findRequested = [this](QTextDocument::FindFlags flags) { |
| 97 | + if (!m_findLineEdit->text().isEmpty()) { |
| 98 | + emit sig_findRequested(m_findLineEdit->text(), flags); |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + auto replaceCurrent = [this]() { |
| 103 | + if (!m_findLineEdit->text().isEmpty() && m_replaceToggleCheckBox->isChecked()) { |
| 104 | + emit sig_replaceCurrentRequested(m_findLineEdit->text(), |
| 105 | + m_replaceLineEdit->text(), |
| 106 | + QTextDocument::FindFlags()); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + connect(m_findLineEdit, &QLineEdit::returnPressed, this, [findRequested]() { |
| 111 | + findRequested(QTextDocument::FindFlags()); |
| 112 | + }); |
| 113 | + connect(m_findNextButton, &QToolButton::clicked, this, [findRequested]() { |
| 114 | + findRequested(QTextDocument::FindFlags()); |
| 115 | + }); |
| 116 | + connect(m_findPreviousButton, &QToolButton::clicked, this, [findRequested]() { |
| 117 | + findRequested(QTextDocument::FindBackward); |
| 118 | + }); |
| 119 | + connect(m_replaceLineEdit, &QLineEdit::returnPressed, this, replaceCurrent); |
| 120 | + connect(m_replaceCurrentButton, &QToolButton::clicked, this, replaceCurrent); |
| 121 | + connect(m_replaceAllButton, &QToolButton::clicked, this, [this]() { |
| 122 | + if (!m_findLineEdit->text().isEmpty() && m_replaceToggleCheckBox->isChecked()) { |
| 123 | + emit sig_replaceAllRequested(m_findLineEdit->text(), |
| 124 | + m_replaceLineEdit->text(), |
| 125 | + QTextDocument::FindFlags()); |
| 126 | + } |
| 127 | + }); |
| 128 | + connect(closeButton, &QToolButton::clicked, this, [this]() { emit sig_closeRequested(); }); |
| 129 | +} |
| 130 | + |
| 131 | +FindReplaceWidget::~FindReplaceWidget() = default; |
| 132 | + |
| 133 | +QToolButton *FindReplaceWidget::createActionButton(const QString &themeIcon, |
| 134 | + const QString &qrcFallbackIcon, |
| 135 | + const QString &text, |
| 136 | + const QString &tooltip, |
| 137 | + bool autoRaise, |
| 138 | + Qt::ToolButtonStyle buttonStyle, |
| 139 | + Qt::FocusPolicy focusPolicy) |
| 140 | +{ |
| 141 | + QToolButton *button = new QToolButton(this); |
| 142 | + button->setIcon(QIcon::fromTheme(themeIcon, QIcon(qrcFallbackIcon))); |
| 143 | + if (!text.isEmpty()) { |
| 144 | + button->setText(text); |
| 145 | + } |
| 146 | + button->setToolTip(tooltip); |
| 147 | + button->setAutoRaise(autoRaise); |
| 148 | + button->setToolButtonStyle(buttonStyle); |
| 149 | + button->setFocusPolicy(focusPolicy); |
| 150 | + return button; |
| 151 | +} |
| 152 | + |
| 153 | +void FindReplaceWidget::setFocusToFindInput() |
| 154 | +{ |
| 155 | + m_findLineEdit->setFocus(Qt::OtherFocusReason); |
| 156 | + m_findLineEdit->selectAll(); |
| 157 | +} |
| 158 | + |
| 159 | +void FindReplaceWidget::slot_updateButtonStates() |
| 160 | +{ |
| 161 | + const bool findTextNotEmpty = !m_findLineEdit->text().isEmpty(); |
| 162 | + m_findNextButton->setEnabled(findTextNotEmpty); |
| 163 | + m_findPreviousButton->setEnabled(findTextNotEmpty); |
| 164 | + |
| 165 | + const bool replaceChecked = m_replaceToggleCheckBox->isChecked(); |
| 166 | + const bool enableReplaceButtons = findTextNotEmpty && replaceChecked; |
| 167 | + |
| 168 | + m_replaceCurrentButton->setEnabled(enableReplaceButtons); |
| 169 | + m_replaceAllButton->setEnabled(enableReplaceButtons); |
| 170 | +} |
| 171 | + |
| 172 | +void FindReplaceWidget::keyPressEvent(QKeyEvent *const event) |
| 173 | +{ |
| 174 | + if (event->key() == Qt::Key_Escape) { |
| 175 | + emit sig_closeRequested(); |
| 176 | + event->accept(); |
| 177 | + return; |
| 178 | + } |
| 179 | + QWidget::keyPressEvent(event); |
| 180 | +} |
0 commit comments