Repository navigation
Expand file tree
/
Copy pathdictionarygui.cpp
More file actions
318 lines (274 loc) · 10.1 KB
/
Copy pathdictionarygui.cpp
File metadata and controls
318 lines (274 loc) · 10.1 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* James Mead Dagres
* jdagres@vt.edu
*
* 02-27-13
*
* Dictionary GUI Implementation File
*/
#include "dictionary.h"
#include "dictionarygui.h"
#include "ui_dictionarygui.h"
#include <QtGui>
dictionaryGUI::dictionaryGUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::dictionaryGUI)
{
ui->setupUi(this);
/* In order to globally change the value in the private object dictionaryOpened you have
* to first store the data you want to change it to in a temp obejct, and then set the
* private object = to the newly created temp object. You will see this type of object
* declaration throughout this class
*/
bool temp = false;
dictionaryOpened = temp;
// Clears the display file
ui->fileDisplayed->clear();
ui->fileDisplayed->setAcceptRichText(true);
}
dictionaryGUI::~dictionaryGUI()
{
delete ui;
}
void dictionaryGUI::openFile()
{
// Uses QDileDialog to browse the files
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
{
loadFile(fileName);
bool temp = true;
fileOpened = temp;
}
}
void dictionaryGUI::saveFile()
{
QString fileName = QFileDialog::getOpenFileName(this);
// Ensures the file isn't empty
if (!fileName.isEmpty())
{
loadFile(fileName);
bool temp = true;
fileOpened = temp;
}
}
void dictionaryGUI::loadFile(const QString &fileName)
{
// Sets the global flag as true, allowing spellcheck to be run
bool tempFileOpened = true;
fileOpened = tempFileOpened;
QFile file(fileName);
// Ensures that the file was successfully opened
if (!file.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::warning(this, tr("Simple Mian Window Example"),
tr("Cannot read file %1:\n%2.")
.arg(fileName).arg(file.errorString()));
return;
}
QString tempFileName = fileName;
fileNameStored = tempFileName;
// If a dictionary has already been chosen then run spellcheck
if(dictionaryOpened)
{
// Clears the display windows because a new file was chosen:
ui->fileDisplayed->clear();
ui->foundWordOccurances->clear();
ui->misspelledWordOccurances->clear();
// Calls functions to repopulate the windows with the new file
QString buffer = myDictionary.spellCheck(myDictionary, fileNameStored);
ui->fileDisplayed->append(buffer);
displayCommonWords();
}
else // Display the file as is
{
QTextStream text(&file);
// Open the test file and create a Text Stream from it
file.open(QIODevice::ReadOnly);
// This while loop will insert the entire textfile as is into the window
while ( !text.atEnd() )
{
ui->fileDisplayed->append(text.readAll());
}
file.close();
}
statusBar()->showMessage(tr("File Opened"), 2000);
}
void dictionaryGUI::saveFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(this, tr("Simple Mian Window Example"),
tr("Cannot read file %1:\n%2.")
.arg(fileName).arg(file.errorString()));
return;
}
// For debugging purposes
// qDebug() << "save fileName " << fileName;
// Sets the file as an output and takes the input from the window to save as text
QTextStream out(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
out << ui->fileDisplayed->toPlainText();
QApplication::restoreOverrideCursor();
statusBar()->showMessage(tr("File saved"), 2000);
setCurrentFile(fileName);
}
void dictionaryGUI::setCurrentFile(const QString &fileName)
{ // Gives you global control of the filename
m_FileName = fileName;
}
void dictionaryGUI::on_dictionarySelection_currentIndexChanged(const QString &arg1)
{
qDebug() << arg1;
// Goes through case statements for all 3 possible choices in the dropdown box
if(arg1 == "Choose Dictionary")
{
// The next 4 lines update global variables ensuring that spellcheck and word count donn't run
QString tempDictionaryPath = "None";
dictionaryPath = tempDictionaryPath;
bool tempFalse = false;
dictionaryOpened = tempFalse;
// Because the dictionary was unselected clear the common word windows
ui->foundWordOccurances->clear();
ui->misspelledWordOccurances->clear();
// Use the status bar to alert the user to select a dictionary, displays for 4 seconds
statusBar()->showMessage(tr("Please Select a Dictionary"), 4000);
return;
}
else if(arg1 == "British")
{
bool tempTrue = true;
dictionaryOpened = tempTrue;
QString tempDictionaryPath = "/usr/share/dict/british-english";
/* Calls the helper function to store the selected dictionary and update the
* three windows on the GUI
*/
setDictionaryHelper(tempDictionaryPath);
}
else if(arg1 == "American")
{
bool tempTrue = true;
dictionaryOpened = tempTrue;
QString tempDictionaryPath = "/usr/share/dict/american-english";
setDictionaryHelper(tempDictionaryPath);
}
else // Something in the GUI is buggy
{
dictionaryOpened = false;
qDebug() << "Drop Down Error!?";
return;
}
}
void dictionaryGUI::setDictionaryHelper(QString tempDictionaryPath)
{
// For debugging purposes
qDebug() << "Dictionary Initialized In function" << tempDictionaryPath;
if(!dictionaryOpened)
{ // Failsafe incase a dictionary has not been chosen
return;
}
// If the dictionary has been changed then:
if(dictionaryPath != tempDictionaryPath)
{
dictionaryPath = tempDictionaryPath;
Dictionary temp(dictionaryPath); // The class object
// Stores the temp map in the private global object
myDictionary = temp;
statusBar()->showMessage(tr("Dictionary Selected"), 2000);
if(fileOpened)
{ // Clears the windows display file
ui->fileDisplayed->clear();
qDebug() << "The dictionary is set, start the horses!";
// Calls the spellcheck function to check mispelled words
QString buffer = myDictionary.spellCheck(myDictionary, fileNameStored);
// Prints the results to the text window
ui->fileDisplayed->append(buffer);
// Calls a function to display the common words
displayCommonWords();
}
}
}
void dictionaryGUI::on_fileopen_triggered()
{ // If the open file button was hit then call the openfile function
openFile();
}
void dictionaryGUI::on_openButton_released()
{ // If the open file button was hit then call the openfile function
openFile();
}
void dictionaryGUI::on_numberOfPopularSpinBox_valueChanged(const int &arg1)
{
int temp = arg1;
numOfPopularWords = temp;
// For debugging purposes
// qDebug() << "Number of Popular Words" << numOfPopularWords;
// Ensures that a file was opened and a dictionary was selected
if (fileOpened & dictionaryOpened)
{
// Calls a function to display the common words
displayCommonWords();
}
}
void dictionaryGUI::displayCommonWords()
{
// Failsafe in case a file wasn't opened or a dictionary wasn't chosen
if (!fileOpened | !dictionaryOpened)
{ // If not then don't do anything:
return;
}
// Calls the necessary functions from the dictionary class to return two string lists of common words
Dictionary tempDictionary(dictionaryPath); // The class object
myDictionary = tempDictionary;
myDictionary.textBank(fileNameStored); // Creates a map of the stored text
QStringList popularInsideDictionaryList = myDictionary.containsWordFound(myDictionary, numOfPopularWords);
QStringList popularMisspelledDictionaryList = myDictionary.containsWordMisspelled(myDictionary, numOfPopularWords);
// Clears the word occurances windows, and displays the standard index
ui->foundWordOccurances->clear();
ui->misspelledWordOccurances->clear();
for(int j=0; (j < popularInsideDictionaryList.length() ) & (j<= numOfPopularWords); j++)
{ // Traverses through the popular inside dictionary list and displays each word
ui->foundWordOccurances->append(popularInsideDictionaryList[j]);
}
for(int j=0; (j < popularMisspelledDictionaryList.length() ) & (j<= numOfPopularWords); j++)
{ // Traverses through the popular misspelled dictionary list and displays each word
ui->misspelledWordOccurances->append(popularMisspelledDictionaryList[j]);
}
//Updates the status bar for two seconds
statusBar()->showMessage(tr("Popular List Updated"), 2000);
}
void dictionaryGUI::on_saveAsButton_released()
{
// The next line is critical for being able to save as a specified "fileName"
QString fileName = QFileDialog::getSaveFileName(this);
saveFile(fileName);
}
void dictionaryGUI::on_actionSave_As_triggered()
{
// The next line is critical for being able to save as a specified "fileName"
QString fileName = QFileDialog::getSaveFileName(this);
saveFile(fileName);
}
void dictionaryGUI::on_fileDisplayed_textChanged()
{ /*
* This function is for the extra credit realtime read in,
* due to lack of time it is currently incomplete
*
if (fileOpened & dictionaryOpened)
{
Dictionary temp(dictionaryPath); // The class object
// Stores the temp map in the private global object
myDictionary = temp;
QString changedWord;
// Reads the changed value added
// ui->fileDisplayed->changeEvent();
qDebug() << "File has been changed";
// Calls a single word spellcheck function to check the added words
QString buffer = myDictionary.singleWordSpellCheck(myDictionary, changedWord);
ui->fileDisplayed->append(buffer);
// ui->fileDisplayed->selectionChanged();
// Calls a function to display the common words
// displayCommonWords();
statusBar()->showMessage(tr("Dictionary Selected"), 2000);
} */
}