forked from QNnovation/ZBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wgtfindreplacetext.cpp
161 lines (137 loc) · 5.27 KB
/
wgtfindreplacetext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "wgtfindreplacetext.h"
#include <QLineEdit>
#include <QLineEdit>
#include <QLabel>
#include <QCheckBox>
#include <QRadioButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QGridLayout>
#include <QDebug>
FindReplaceText::FindReplaceText(QWidget *parent)
: QDialog(parent)
{
//line edit block
m_textFindEdit = new QLineEdit();
m_textReplaceEdit = new QLineEdit();
m_textFindLbl = new QLabel(tr("Find:"));
m_textReplaceLbl = new QLabel(tr("Replace with:"));
m_textReplaceLbl->setVisible(false);
m_textReplaceEdit->setVisible(false);
//radiobutton group
m_directionLbl = new QLabel(tr("Direction"));
m_downBtn = new QRadioButton(tr("Down"));
m_upBtn = new QRadioButton(tr("Up"));
m_downBtn->setChecked(true);
//checkbox group
m_optionsLbl = new QLabel(tr("Options"));
m_caseSensitiveBox = new QCheckBox(tr("&Case sensitive"));
m_wholeWordsBox = new QCheckBox(tr("&Whole words only"));
//buttons block
m_findBtn = new QPushButton(tr("&Find"));
m_closeBtn = new QPushButton(tr("&Close"));
m_replaceBtn = new QPushButton(tr("&Replace"));
m_replaceAllBtn = new QPushButton(tr("Replace &All"));
m_replaceBtn->setVisible(false);
m_replaceAllBtn->setVisible(false);
m_statusLbl = new QLabel();
QGridLayout *lineBlockLayout = new QGridLayout;
lineBlockLayout->addWidget(m_textFindLbl, 0, 0);
lineBlockLayout->addWidget(m_textFindEdit, 0, 1);
lineBlockLayout->addWidget(m_findBtn, 0, 2);
lineBlockLayout->addWidget(m_textReplaceLbl, 1, 0);
lineBlockLayout->addWidget(m_textReplaceEdit, 1, 1);
lineBlockLayout->addWidget(m_closeBtn, 1, 2);
lineBlockLayout->addWidget(m_statusLbl, 2, 0);
lineBlockLayout->addWidget(m_replaceBtn, 2, 2);
lineBlockLayout->addWidget(m_directionLbl, 3, 0);
lineBlockLayout->addWidget(m_optionsLbl, 3, 1);
lineBlockLayout->addWidget(m_replaceAllBtn, 3, 2);
lineBlockLayout->addWidget(m_downBtn, 4, 0);
lineBlockLayout->addWidget(m_caseSensitiveBox, 4, 1);
lineBlockLayout->addWidget(m_upBtn, 5, 0);
lineBlockLayout->addWidget(m_wholeWordsBox, 5, 1);
m_textReplaceLbl->setVisible(false);
m_textReplaceEdit->setVisible(false);
m_replaceBtn->setVisible(false);
m_replaceAllBtn->setVisible(false);
QHBoxLayout *mainLayout = new QHBoxLayout();
mainLayout->addLayout(lineBlockLayout);
this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
setWindowTitle(tr("Find/Replace"));
setFixedSize(380, 180);
setLayout(mainLayout);
connect(m_closeBtn, &QPushButton::clicked, this, &FindReplaceText::close);
connect(m_findBtn, &QPushButton::clicked, this, &FindReplaceText::findSlot);
connect(m_replaceBtn, &QPushButton::clicked, this, &FindReplaceText::replaceSlot);
connect(m_replaceAllBtn, &QPushButton::clicked, this, &FindReplaceText::replaceAllSlot);
connect(m_textFindEdit, &QLineEdit::textChanged, this, &FindReplaceText::isEmptyText);
}
void FindReplaceText::isReplace(bool mode)
{
if (mode) {
m_textReplaceLbl->setVisible(true);
m_textReplaceEdit->setVisible(true);
m_replaceBtn->setVisible(true);
m_replaceAllBtn->setVisible(true);
}
}
//find text slot
void FindReplaceText::findSlot()
{
flags = options();
m_newWord = m_textReplaceEdit->text();
emit findSignal(m_textFindEdit->text(), flags);
}
//replace button slot
void FindReplaceText::replaceSlot()
{
flags = options();
m_newWord = m_textReplaceEdit->text();
emit replaceSignal(m_textFindEdit->text(), flags, m_newWord);
}
//replaceAll button slot
void FindReplaceText::replaceAllSlot()
{
flags = options();
m_newWord = m_textReplaceEdit->text();
emit replaceAllSignal(m_textFindEdit->text(), flags, m_newWord);
}
//block buttons if text is empty
void FindReplaceText::isEmptyText(const QString &text)
{
QList<QPushButton*> list = this->findChildren<QPushButton*>();
if (text.isEmpty()) {
for (int i = 0; i < list.size(); ++i) {
list.at(i)->setEnabled(false);
}
} else {
for (int i = 0; i < list.size(); ++i) {
list.at(i)->setEnabled(true);
}
}
}
QTextDocument::FindFlags FindReplaceText::options() const
{
QTextDocument::FindFlags options;
if (m_upBtn->isChecked())
options = QTextDocument::FindBackward;
else if (m_caseSensitiveBox->isChecked())
options = QTextDocument::FindCaseSensitively;
else if (m_caseSensitiveBox->isChecked() && m_upBtn->isChecked())
options = QTextDocument::FindCaseSensitively | QTextDocument::FindBackward;
else if (m_wholeWordsBox->isChecked())
options = QTextDocument::FindWholeWords;
else if (m_wholeWordsBox->isChecked() && m_upBtn->isChecked())
options = QTextDocument::FindWholeWords | QTextDocument::FindBackward;
else if (m_caseSensitiveBox->isChecked() && m_wholeWordsBox->isChecked() && m_upBtn->isChecked())
options = QTextDocument::FindWholeWords | QTextDocument::FindCaseSensitively | QTextDocument::FindBackward;
else if (m_caseSensitiveBox->isChecked() && m_wholeWordsBox->isChecked())
options = QTextDocument::FindWholeWords | QTextDocument::FindCaseSensitively;
return options;
}
//destructor
FindReplaceText::~FindReplaceText()
{
}