source: anagramx.cpp @ f6c467d

Last change on this file since f6c467d was f6c467d, checked in by Agnibho Mondal <mail@…>, 4 years ago

Rewritten in C++

  • Property mode set to 100644
File size: 6.3 KB
Line 
1/**********************************************************************
2 * Title: Anagram
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
8   All rights reserved
9 **********************************************************************
10   This file is part of Anagram.
11   
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.
16   
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.
21   
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 **********************************************************************/
25/*
26 * The Graphical User Interface for Anagram
27 */
28
29#include <string> //For string
30#include <vector> //For string vector
31#include <algorithm> //For find
32
33//For Qt Objects
34#include <QApplication>
35#include <QWidget>
36#include <QLabel>
37#include <QFont>
38#include <QLineEdit>
39#include <QPushButton>
40#include <QCheckBox>
41#include <QTextEdit>
42#include <QScrollArea>
43#include <QGridLayout>
44#include <QMessageBox>
45
46#include "validity.hpp" //For is_valid
47#include "find.hpp" //To find anagrams
48
49using namespace std;
50
51//Defines the AnagramX class
52class AnagramX : public QWidget{
53public:
54  AnagramX(QWidget *parent=0);
55private slots:
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
60private:
61  QLineEdit *entry; //Text input
62  QCheckBox *multi; //Multi-word checkbox
63  QTextEdit *result; //Program output
64};
65
66//Implements the AnagramX class
67AnagramX::AnagramX(QWidget *parent)
68  :QWidget(parent){
69
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);
73
74  entry = new QLineEdit(this); //The text input line
75  connect(entry, &QLineEdit::returnPressed, this, &AnagramX::findAnagrams);
76
77  QPushButton *find = new QPushButton("Find", this); //Button to trigger anagram search
78  find->setAutoDefault(true);
79  connect(find, &QPushButton::clicked, this, &AnagramX::findAnagrams);
80 
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);
84 
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);
88
89  multi = new QCheckBox("Allow multiple words", this); //The multi-word checkbox
90 
91  result = new QTextEdit(this); //The anagram output area
92  result->setReadOnly(true);
93 
94  QScrollArea *scroll = new QScrollArea(this); //Wraps the output in a scrollable area
95  scroll->setWidget(result);
96  scroll->setWidgetResizable(true);
97 
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);
106}
107
108//Finds the anagrams and launches the anagram finder thread
109void AnagramX::findAnagrams(){
110  //Define variables
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
118    if(flag){
119      anagram_list=find_anagram_multiword(word); //Thread to find the multi-word anagrams of the word
120    }
121    else{
122      anagram_list=find_anagram(word); //Thread to find the single-word anagrams of the word
123    }
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);
129        }
130        else{
131          plain_list+=word+": The word is not present in the dictionary\n";
132        }
133      }
134    }
135    //Display Output
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";
138    }
139    else if(anagram_list.size()>0){ //Initializes plain_list with singular
140      plain_list+=word+": The following anagram was found\n\n";
141    }
142    else{ //No anagram found
143      plain_list+=word+": No anagram was found\n";
144    }
145    for(string i : anagram_list){ //Converts the vector to newline separated strings
146      plain_list+=i+"\n";
147    }
148    AnagramX::result->setText(QString::fromStdString(plain_list)); //Outputs the anagram strings
149  }
150  else{ //If input invalid show error
151    QMessageBox::warning(this, "Error", "The entered word is not valid");
152    return;
153  }
154}
155
156//Clears input and output
157void AnagramX::clearAll(){
158  AnagramX::entry->setText("");
159  AnagramX::result->setText("");
160}
161
162//Shows an About message
163void AnagramX::showAbout(){
164  QMessageBox::information(this, "About", "<h1>Anagram 2.0</h1>"
165                           "<p>Copyright &copy; 2013-2015 Agnibho Mondal</p>"
166                           "<a href='http://code.agnibho.com/anagram'>http://code.agnibho.com/anagram</a>");
167}
168
169
170//Start execution
171int main(int argc, char *argv[]){
172 
173  QApplication app(argc, argv); //Qt Application object
174  AnagramX window; //Instance of AnagramX, creates the main window
175 
176  window.resize(450, 450); //Set window size
177  window.setWindowTitle("Anagram"); //Set window title
178  window.show(); //Displays the window
179
180  return app.exec(); //Enters the main execution loop
181}
Note: See TracBrowser for help on using the repository browser.