]>
Softwares of Agnibho - anagram.git/blob - anagramx.cpp
1 /**********************************************************************
3 * Description: Application for finding anagrams of a word
4 * Author: Agnibho Mondal
5 * Website: http://code.agnibho.com/anagram
6 **********************************************************************
7 Copyright (c) 2013-2015 Agnibho Mondal
9 **********************************************************************
10 This file is part of Anagram.
12 Anagram is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 Anagram is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with Anagram. If not, see <http://www.gnu.org/licenses/>.
24 **********************************************************************/
26 * The Graphical User Interface for Anagram
29 #include <string> //For string
30 #include <vector> //For string vector
31 #include <algorithm> //For find
34 #include <QApplication>
39 #include <QPushButton>
42 #include <QScrollArea>
43 #include <QGridLayout>
44 #include <QMessageBox>
46 #include "validity.hpp" //For is_valid
47 #include "find.hpp" //To find anagrams
51 //Defines the AnagramX class
52 class AnagramX
: public QWidget
{
54 AnagramX(QWidget
*parent
=0);
56 void findAnagrams(); //Launches the finder thread
57 void showOutput(); //Displays the output
58 void clearAll(); //Clears the text entry and output
59 void showAbout(); //Shows an About messagebox
61 QLineEdit
*entry
; //Text input
62 QCheckBox
*multi
; //Multi-word checkbox
63 QTextEdit
*result
; //Program output
66 //Implements the AnagramX class
67 AnagramX::AnagramX(QWidget
*parent
)
70 QLabel
*label
= new QLabel("Anagram Finder", this); //The top label with formatted and centered text
71 label
->setFont(QFont("FreeSerif", 20));
72 label
->setAlignment(Qt::AlignCenter
);
74 entry
= new QLineEdit(this); //The text input line
75 connect(entry
, &QLineEdit::returnPressed
, this, &AnagramX::findAnagrams
);
77 QPushButton
*find
= new QPushButton("Find", this); //Button to trigger anagram search
78 find
->setAutoDefault(true);
79 connect(find
, &QPushButton::clicked
, this, &AnagramX::findAnagrams
);
81 QPushButton
*clear
= new QPushButton("Clear", this); //Button to clear input and output
82 clear
->setAutoDefault(true);
83 connect(clear
, &QPushButton::clicked
, this, &AnagramX::clearAll
);
85 QPushButton
*about
= new QPushButton("About", this); //Button to show an About message box
86 about
->setAutoDefault(true);
87 connect(about
, &QPushButton::clicked
, this, &AnagramX::showAbout
);
89 multi
= new QCheckBox("Allow multiple words", this); //The multi-word checkbox
91 result
= new QTextEdit(this); //The anagram output area
92 result
->setReadOnly(true);
94 QScrollArea
*scroll
= new QScrollArea(this); //Wraps the output in a scrollable area
95 scroll
->setWidget(result
);
96 scroll
->setWidgetResizable(true);
98 QGridLayout
*grid
= new QGridLayout(this); //Organizes the other widgets in a grid
99 grid
->addWidget(label
, 0, 0, 1, 3);
100 grid
->addWidget(entry
, 1, 0, 1, 3);
101 grid
->addWidget(find
, 2, 0, 1, 1);
102 grid
->addWidget(clear
, 2, 1, 1, 1);
103 grid
->addWidget(about
, 2, 2, 1, 1);
104 grid
->addWidget(multi
, 3, 0, 1, 3);
105 grid
->addWidget(scroll
, 4, 0, 1, 3);
108 //Finds the anagrams and launches the anagram finder thread
109 void AnagramX::findAnagrams(){
111 bool flag
; //Holds multi-word checkbox state
112 string word
; //Holds the word
113 word
=AnagramX::entry
->text().toStdString(); //Collects the input
114 flag
=AnagramX::multi
->isChecked(); //Checks if multi-word allowed
115 vector
<string
> anagram_list
; //Recieves the found anagrams
116 string plain_list
=""; //Anagrams in a plain string form
117 if(is_valid(word
, flag
)){ //If the input is valid find anagrams
119 anagram_list
=find_anagram_multiword(word
); //Thread to find the multi-word anagrams of the word
122 anagram_list
=find_anagram(word
); //Thread to find the single-word anagrams of the word
124 if(!anagram_list
.empty()){ //If anagram_list is NOT empty
125 { //Remove the word itself from the anagram list if present
126 vector
<string
>::iterator i
=std::find(anagram_list
.begin(), anagram_list
.end(), word
);
127 if(i
!=anagram_list
.end()){
128 anagram_list
.erase(i
);
131 plain_list
+=word
+": The word is not present in the dictionary\n";
136 if(anagram_list
.size()>1){ //Initializes plain_list with plural
137 plain_list
+=word
+": The following "+to_string(anagram_list
.size())+" anagrams were found\n\n";
139 else if(anagram_list
.size()>0){ //Initializes plain_list with singular
140 plain_list
+=word
+": The following anagram was found\n\n";
142 else{ //No anagram found
143 plain_list
+=word
+": No anagram was found\n";
145 for(string i
: anagram_list
){ //Converts the vector to newline separated strings
148 AnagramX::result
->setText(QString::fromStdString(plain_list
)); //Outputs the anagram strings
150 else{ //If input invalid show error
151 QMessageBox::warning(this, "Error", "The entered word is not valid");
156 //Clears input and output
157 void AnagramX::clearAll(){
158 AnagramX::entry
->setText("");
159 AnagramX::result
->setText("");
162 //Shows an About message
163 void AnagramX::showAbout(){
164 QMessageBox::information(this, "About", "<h1>Anagram 2.0</h1>"
165 "<p>Copyright © 2013-2015 Agnibho Mondal</p>"
166 "<a href='http://code.agnibho.com/anagram'>http://code.agnibho.com/anagram</a>");
171 int main(int argc
, char *argv
[]){
173 QApplication
app(argc
, argv
); //Qt Application object
174 AnagramX window
; //Instance of AnagramX, creates the main window
176 window
.resize(450, 450); //Set window size
177 window
.setWindowTitle("Anagram"); //Set window title
178 window
.show(); //Displays the window
180 return app
.exec(); //Enters the main execution loop