-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
182 lines (148 loc) · 4.94 KB
/
mainwindow.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//---------------------------------------------------------->
// MainWindow.cpp file.
// Author : Jayakrishnan P.
// Last Edited: 14/10/2022
//---------------------------------------------------------->
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "highlightcolor.h"
#include <QMessageBox>
#include <QList>
#include <QVector>
#include <QObject>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("Text Comparison Tool");
QSettings settings("JK", "Text Comparison Tool");
settings.beginGroup("Highlight Color");
m_txtBox1Color = settings.value("color_one", "green").value<QColor>();
m_txtBox2Color = settings.value("color_two", "yellow").value<QColor>();
settings.endGroup();
}
MainWindow::~MainWindow()
{
delete ui;
}
//---------------------------------------------------------->
// Function Name: MainWindow::OnClickBtnCompare.
// Return Type : void.
// Parameters : No parameters.
// Remarks : Event handler for compare button click.
//---------------------------------------------------------->
void MainWindow::OnClickBtnCompare()
{
//Taking strings from text boxes.
QByteArray text1 = (ui->textEdit1->toPlainText()).toUtf8();
QByteArray text2 = (ui->textEdit2->toPlainText()).toUtf8();
if(text1 == "" || text2 == "")
{
QMessageBox::information(this," "," Enter two strings");
return;
}
QList<int> differenceList;
int larger = 0;
bool oneIsBig = false;
bool twoIsBig = false;
if(text1.length() > text2.length())
{
twoIsBig = false;
oneIsBig = true;
larger = text1.length();
}
if(text1.length() < text2.length())
{
twoIsBig = true;
oneIsBig = false;
larger = text2.length();
}
if(text1.length() == text2.length())
{
twoIsBig = false;
oneIsBig = false;
larger = text1.length();
}
//Filling rest of smaller array with ' '.
if(oneIsBig == true)
{
for(int i = text2.length(); i < text1.length();i++)
{
text2.append(' ');
}
}
if(twoIsBig == true)
{
for(int i = text1.length(); i < text2.length();i++)
{
text1.append(' ');
}
}
ui->textEdit1->setText(text1);
ui->textEdit2->setText(text2);
//Finding position indeces of difference between strings.
for(int i = 0; i < larger; i++)
{
if(text1[i] != text2[i])
{
differenceList.append(i);
}
}
QTextCursor cursorText1(ui->textEdit1->document());
QTextCursor cursorText2(ui->textEdit2->document());
QSettings settings("JK", "Text Comparison Tool");
settings.beginGroup("Highlight Color");
m_txtBox1Color = settings.value("color_one", "Green").value<QColor>();
m_txtBox2Color = settings.value("color_two", "Yellow").value<QColor>();
settings.endGroup();
QTextCharFormat backgroundClear, background1, background2;
backgroundClear.clearBackground();
background1.setBackground(m_txtBox1Color);
background2.setBackground(m_txtBox2Color);
//Text Edit reset.
cursorText1.setPosition(QTextCursor::Start,QTextCursor::MoveAnchor);
cursorText1.setPosition(QTextCursor::End,QTextCursor::KeepAnchor);
cursorText1.setCharFormat(backgroundClear);
cursorText2.setPosition(QTextCursor::Start,QTextCursor::MoveAnchor);
cursorText2.setPosition(QTextCursor::End,QTextCursor::KeepAnchor);
cursorText2.setCharFormat(backgroundClear);
//If strings are equal.
if(text1 == text2)
{
QMessageBox::information(this,""," Both strings are exactly the same!!");
return;
}
//Highlighting the difference.
for(int i = 0;i < differenceList.size();i++)
{
cursorText1.setPosition(differenceList[i],QTextCursor::MoveAnchor);
cursorText1.setPosition(differenceList[i] + 1,QTextCursor::KeepAnchor);
cursorText1.setCharFormat(background1);
cursorText2.setPosition(differenceList[i],QTextCursor::MoveAnchor);
cursorText2.setPosition(differenceList[i] + 1,QTextCursor::KeepAnchor);
cursorText2.setCharFormat(background2);
}
}
//---------------------------------------------------------->
// Function Name: MainWindow::ShowHighlighterDlg.
// Return Type : void.
// Parameters : None.
// Remarks : Creates dialog for highlighting color settings.
//---------------------------------------------------------->
void MainWindow::ShowHighlighterDlg()
{
HighlightColor* highlighter = new HighlightColor(this);
highlighter->show();
}
//---------------------------------------------------------->
// Function Name: MainWindow::ClearAll.
// Return Type : VOID.
// Parameters : None.
// Remarks : Clears the text boxes.
//---------------------------------------------------------->
void MainWindow::ClearAll()
{
ui->textEdit1->clear();
ui->textEdit2->clear();
}