| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. | 
|---|
| 2 | // | 
|---|
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later | 
|---|
| 4 |  | 
|---|
| 5 | #include "findtoolbar.h" | 
|---|
| 6 | #include "common/common.h" | 
|---|
| 7 |  | 
|---|
| 8 | #include <QVBoxLayout> | 
|---|
| 9 | #include <QHBoxLayout> | 
|---|
| 10 | #include <QLabel> | 
|---|
| 11 | #include <QLineEdit> | 
|---|
| 12 | #include <QPushButton> | 
|---|
| 13 |  | 
|---|
| 14 | #define LABEL_WIDTH           (80) | 
|---|
| 15 | #define OPERATOR_WIDGET_WIDTH (400) | 
|---|
| 16 |  | 
|---|
| 17 | class FindToolBarPrivate | 
|---|
| 18 | { | 
|---|
| 19 | FindToolBarPrivate(){} | 
|---|
| 20 | QLineEdit *findLineEdit{nullptr}; | 
|---|
| 21 | QLineEdit *replaceLineEdit{nullptr}; | 
|---|
| 22 |  | 
|---|
| 23 | friend class FindToolBar; | 
|---|
| 24 | }; | 
|---|
| 25 |  | 
|---|
| 26 | FindToolBar::FindToolBar(QWidget *parent) | 
|---|
| 27 | : QWidget(parent) | 
|---|
| 28 | , d(new FindToolBarPrivate()) | 
|---|
| 29 | { | 
|---|
| 30 | setupUi(); | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | void FindToolBar::setupUi() | 
|---|
| 34 | { | 
|---|
| 35 | setMaximumHeight(85); | 
|---|
| 36 | setMinimumWidth(800); | 
|---|
| 37 |  | 
|---|
| 38 | QVBoxLayout *vLayout = new QVBoxLayout(); | 
|---|
| 39 | QHBoxLayout *findLayout = new QHBoxLayout(); | 
|---|
| 40 | vLayout->addLayout(findLayout); | 
|---|
| 41 | QLabel *findLabel = new QLabel(QLabel::tr( "Find:")); | 
|---|
| 42 | findLabel->setFixedWidth(LABEL_WIDTH); | 
|---|
| 43 | findLabel->setAlignment(Qt::AlignRight); | 
|---|
| 44 |  | 
|---|
| 45 | d->findLineEdit = new QLineEdit(); | 
|---|
| 46 |  | 
|---|
| 47 | QWidget *findWidget = new QWidget(); | 
|---|
| 48 | findWidget->setFixedWidth(OPERATOR_WIDGET_WIDTH); | 
|---|
| 49 | QHBoxLayout *findWidgetLayout = new QHBoxLayout(); | 
|---|
| 50 | findWidgetLayout->setMargin(0); | 
|---|
| 51 | findWidget->setLayout(findWidgetLayout); | 
|---|
| 52 |  | 
|---|
| 53 | QPushButton *findPreBtn = new QPushButton(QPushButton::tr( "Find Previous")); | 
|---|
| 54 | QPushButton *findNextBtn = new QPushButton(QPushButton::tr( "Find Next")); | 
|---|
| 55 | QPushButton *advancedBtn = new QPushButton(QPushButton::tr( "Advanced...")); | 
|---|
| 56 | findWidgetLayout->addWidget(findPreBtn); | 
|---|
| 57 | findWidgetLayout->addWidget(findNextBtn); | 
|---|
| 58 | findWidgetLayout->addWidget(advancedBtn); | 
|---|
| 59 |  | 
|---|
| 60 | findLayout->addWidget(findLabel); | 
|---|
| 61 | findLayout->addWidget(d->findLineEdit); | 
|---|
| 62 | findLayout->addWidget(findWidget); | 
|---|
| 63 |  | 
|---|
| 64 | QHBoxLayout *repalceLayout = new QHBoxLayout(); | 
|---|
| 65 | vLayout->addLayout(repalceLayout); | 
|---|
| 66 | QLabel *repalceLabel = new QLabel(QLabel::tr( "Repalce:")); | 
|---|
| 67 | repalceLabel->setFixedWidth(LABEL_WIDTH); | 
|---|
| 68 | repalceLabel->setAlignment(Qt::AlignRight); | 
|---|
| 69 |  | 
|---|
| 70 | d->replaceLineEdit = new QLineEdit(); | 
|---|
| 71 |  | 
|---|
| 72 | QWidget *replaceWidget = new QWidget(); | 
|---|
| 73 | replaceWidget->setFixedWidth(OPERATOR_WIDGET_WIDTH); | 
|---|
| 74 | QHBoxLayout *replaceWidgetLayout = new QHBoxLayout(); | 
|---|
| 75 | replaceWidgetLayout->setMargin(0); | 
|---|
| 76 | replaceWidget->setLayout(replaceWidgetLayout); | 
|---|
| 77 |  | 
|---|
| 78 | QPushButton *replaceBtn = new QPushButton(QPushButton::tr( "Replace")); | 
|---|
| 79 | QPushButton *replaceFindBtn = new QPushButton(QPushButton::tr( "Replace && Find")); | 
|---|
| 80 | QPushButton *replaceAllBtn = new QPushButton(QPushButton::tr( "Repalce All")); | 
|---|
| 81 | replaceWidgetLayout->addWidget(replaceBtn); | 
|---|
| 82 | replaceWidgetLayout->addWidget(replaceFindBtn); | 
|---|
| 83 | replaceWidgetLayout->addWidget(replaceAllBtn); | 
|---|
| 84 |  | 
|---|
| 85 | repalceLayout->addWidget(repalceLabel); | 
|---|
| 86 | repalceLayout->addWidget(d->replaceLineEdit); | 
|---|
| 87 | repalceLayout->addWidget(replaceWidget); | 
|---|
| 88 |  | 
|---|
| 89 | connect(findPreBtn, &QAbstractButton::clicked, this, &FindToolBar::findPrevious); | 
|---|
| 90 | connect(findNextBtn, &QAbstractButton::clicked, this, &FindToolBar::findNext); | 
|---|
| 91 | connect(advancedBtn, &QAbstractButton::clicked, this, &FindToolBar::advancedSearch); | 
|---|
| 92 | connect(replaceBtn, &QAbstractButton::clicked, this, &FindToolBar::replace); | 
|---|
| 93 | connect(replaceFindBtn, &QAbstractButton::clicked, this, &FindToolBar::replaceSearch); | 
|---|
| 94 | connect(replaceAllBtn, &QAbstractButton::clicked, this, &FindToolBar::replaceAll); | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | setLayout(vLayout); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | void FindToolBar::findPrevious() | 
|---|
| 101 | { | 
|---|
| 102 | QString text = d->findLineEdit->text(); | 
|---|
| 103 | if (text.isEmpty()) | 
|---|
| 104 | return; | 
|---|
| 105 | editor.searchText(text, FindType::Previous); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | void FindToolBar::findNext() | 
|---|
| 109 | { | 
|---|
| 110 | QString text = d->findLineEdit->text(); | 
|---|
| 111 | if (text.isEmpty()) | 
|---|
| 112 | return; | 
|---|
| 113 | editor.searchText(text, FindType::Next); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void FindToolBar::advancedSearch() | 
|---|
| 117 | { | 
|---|
| 118 | emit advanced(); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | void FindToolBar::replace() | 
|---|
| 122 | { | 
|---|
| 123 | QString srcText = d->findLineEdit->text(); | 
|---|
| 124 | if (srcText.isEmpty()) | 
|---|
| 125 | return; | 
|---|
| 126 | QString destText = d->replaceLineEdit->text(); | 
|---|
| 127 | editor.replaceText(srcText, destText, RepalceType::Repalce); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | void FindToolBar::replaceSearch() | 
|---|
| 131 | { | 
|---|
| 132 | QString srcText = d->findLineEdit->text(); | 
|---|
| 133 | if (srcText.isEmpty()) | 
|---|
| 134 | return; | 
|---|
| 135 | QString destText = d->replaceLineEdit->text(); | 
|---|
| 136 | editor.replaceText(srcText, destText, RepalceType::FindAndReplace); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | void FindToolBar::replaceAll() | 
|---|
| 140 | { | 
|---|
| 141 | QString srcText = d->findLineEdit->text(); | 
|---|
| 142 | if (srcText.isEmpty()) | 
|---|
| 143 | return; | 
|---|
| 144 | QString destText = d->replaceLineEdit->text(); | 
|---|
| 145 | editor.replaceText(srcText, destText, RepalceType::RepalceAll); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|