· 7 years ago · Sep 18, 2018, 02:46 PM
1#include "notepad.h"
2#include "ui_notepad.h"
3#include "search.h"
4#include "ui_search.h"
5#include <QFile>
6#include <QFileDialog>
7#include <QMessageBox>
8#include <QTextStream>
9#include <QFontDialog>
10#include <QTextCursor>
11Notepad::Notepad(QWidget *parent) :QMainWindow(parent),
12 ui(new Ui::Notepad)
13{
14 //Graphic Part
15 ui->setupUi(this);
16 this->setCentralWidget(ui->tabWidget); //Make sure that tabWidget occupies the entire parent space.
17 ui->tabWidget->setTabShape(QTabWidget::Triangular); //Make the tab look triangular.
18
19 //Create the required Components
20 QTextEdit *tempPad = new QTextEdit(); //Insert a textEdit when you open a new tab.
21 QString *openedFile = new QString; //For the saveFlag. The saveFlag checks if a file was saved in the current session.
22
23 //Add the tab
24 ui->tabWidget->addTab(tempPad, "New Text"); //Add a tab with the textEdit.
25 ui->tabWidget->setCurrentWidget(tempPad); //Change focus to the new tab.
26
27 //tab related ops and storing
28 currentTab = ui->tabWidget->currentIndex(); //Get the current tab index for operations on the tab.
29 tabIndex = ui->tabWidget->currentIndex(); //Get the current index for saving on the QHash table (pad).
30 pad.insert(tabIndex,tempPad); //Save the index with the corresponding QTextEdit.
31 saveFlag.insert(tabIndex, openedFile); //Save the index with the saveFlag.
32 ui->tabWidget->setTabToolTip(currentTab, "Not saved yet");
33 setWindowTitle("PseudoPad");
34}
35Notepad::~Notepad()
36{
37 delete ui;
38}
39void Notepad::on_actionNew_triggered()
40{
41 //Create required components
42 QTextEdit *tempPad = new QTextEdit();
43 QString *openedFile = new QString;
44
45 //add the tabs
46 ui->tabWidget->addTab(tempPad, "New Text");
47 ui->tabWidget->setCurrentWidget(tempPad);
48
49 currentTab = ui->tabWidget->currentIndex();
50 tabIndex = ui->tabWidget->currentIndex();
51
52 //storing the values in the QHash table.
53 pad.insert(tabIndex,tempPad);
54 ui->tabWidget->setTabToolTip(currentTab, "Not saved Yet");
55 saveFlag.insert(tabIndex, openedFile);
56}
57void Notepad::on_actionOpen_triggered()
58{
59 currentTab = ui->tabWidget->currentIndex(); // get the current index
60 QString filename = QFileDialog::getOpenFileName(this, "Open the file"); //open the file
61 QFile file(filename); //QFile to read all contents of files.
62 QFileInfo information(file); //QFileInfo to get the file reated information.
63 QString name = information.fileName(); // get the name of the file.
64
65 if(!file.open(QIODevice::ReadOnly | QFile::Text))
66 {
67 QMessageBox::warning(this, "Warning", "Cannot Open file\n Reason: " + file.errorString());
68 return;
69 }
70
71 *saveFlag[currentTab] = filename; //save the name in the saveFlag so that it doesnt ask you to save if you choose to save the file because the file already exists. Choose save as for saving again with different name and/or extensions.
72
73 ui->tabWidget->setTabText(currentTab, name);
74 ui->tabWidget->setTabToolTip(currentTab, "Location: "+filename);
75
76 QTextStream in(&file); //QTextStream object to reall all contents.
77 QString text = in.readAll(); //store the contents in a QString
78 QTextEdit *documentArea = pad[currentTab]; //get the current QTextEdit we are working on using the tabIndex for openning the contents in that textedit.
79 documentArea->setText(text);
80 file.close();
81}
82void Notepad::on_actionSave_triggered()
83{
84 currentTab = ui->tabWidget->currentIndex();
85 QString filename;
86 QString *chkSave = saveFlag[currentTab]; // Get the QString of the current tab.
87 if (chkSave->isEmpty()) //checks if some name was assigned to it.
88 { //If not assigned and it was empty:
89 filename = QFileDialog::getSaveFileName(this, "Save");
90 *chkSave = filename;
91 }
92 else//If assigned, the just save.
93 filename = *chkSave;
94
95 QFile file(filename);
96 QFileInfo information(file);
97
98 QString name = information.fileName();
99 ui->tabWidget->setTabText(currentTab, name);
100 if (!file.open(QIODevice::WriteOnly | QFile::Text))
101 {
102 QMessageBox::warning(this,"Warning!", "No file selected\n" + file.errorString());
103 return;
104 }
105
106 QTextStream out(&file); //Create the QTextStream to save all contents from the QTextEdit to the file.
107
108 QTextEdit *documentArea = pad[currentTab]; //get the QTextEdit associated to the tab we are working on.
109 QString text = documentArea->toPlainText();
110
111 out << text; // Copy the contents.
112 ui->tabWidget->setTabToolTip(currentTab, "Location: "+filename);
113 file.close();
114}
115void Notepad::on_actionSave_As_triggered()
116{
117 //Save_As functions the same way as Save but doesnt have the saveFlag check.
118 QString filename;
119 filename = QFileDialog::getSaveFileName(this, "Save");
120 //openedFile = filename;
121 QFile file(filename);
122 if (!file.open(QIODevice::WriteOnly))
123 {
124 QMessageBox::warning(this, "Warning", "Cannot Save" + file.errorString());
125 return;
126 }
127 setWindowTitle(filename);
128 QTextStream out(&file);
129 QTextEdit *documentArea = pad[currentTab];
130 QString text = documentArea->toPlainText();
131 out << text;
132 ui->tabWidget->setTabToolTip(currentTab, "Location: "+filename);
133 file.close();
134}
135
136void Notepad::on_actionCopy_triggered()
137{
138 currentTab = ui->tabWidget->currentIndex();
139 QTextEdit *documentArea = pad[currentTab];
140 documentArea->copy();
141}
142
143void Notepad::on_actionPaste_triggered()
144{
145 currentTab = ui->tabWidget->currentIndex();
146 QTextEdit *documentArea = pad[currentTab];
147 documentArea->paste();
148}
149
150void Notepad::on_actionUndo_triggered()
151{
152 currentTab = ui->tabWidget->currentIndex();
153 QTextEdit *documentArea = pad[currentTab];
154 documentArea->undo();
155}
156
157void Notepad::on_actionRedo_triggered()
158{
159 currentTab = ui->tabWidget->currentIndex();
160 QTextEdit *documentArea = pad[currentTab];
161 documentArea->redo();
162}
163
164void Notepad::on_actionFont_triggered()
165{ //CHange font of the selected text.
166 currentTab = ui->tabWidget->currentIndex();
167 bool fontselect;
168 QTextEdit *documentArea = pad[currentTab];
169 QFont font = QFontDialog::getFont(&fontselect, this);
170 if (fontselect)
171 {
172 documentArea->setFont(font);
173 }
174}
175
176void Notepad::on_actionUPPER_triggered() //CHange the Case of a string
177{
178 currentTab = ui->tabWidget->currentIndex();
179 QTextEdit *documentArea = pad[currentTab];
180 QTextCursor cursor = documentArea->textCursor();
181 if (cursor.hasSelection())
182 {
183 cursor.insertText(cursor.selectedText().toUpper());
184 }
185}
186
187void Notepad::on_actionlower_triggered() //CHange the Case of a string
188{
189
190 currentTab = ui->tabWidget->currentIndex();
191 QTextEdit *documentArea = pad[currentTab];
192 QTextCursor cursor = documentArea->textCursor();
193 if (cursor.hasSelection())
194 {
195 cursor.insertText(cursor.selectedText().toLower());
196 }
197}
198
199void Notepad::on_actionSearch_Word_triggered()
200{
201
202 currentTab = ui->tabWidget->currentIndex();
203 QFile file(*saveFlag[currentTab]);
204
205 QTextStream out(&file);
206 QTextEdit *documentArea = pad[currentTab];
207 QString worda = documentArea->toPlainText(); //store contents in worda.
208
209 out << worda; //copy the contents.
210 search.word = worda; //pass the contents of the file to the search file
211 search.edit_area = documentArea; //pas the current QTextEdit to the search
212 search.show();
213}
214
215void Notepad::on_actionCut_triggered()
216{
217 currentTab = ui->tabWidget->currentIndex();
218 QTextEdit *documentArea = pad[currentTab];
219 documentArea->cut();
220}