-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialog.cpp
More file actions
99 lines (88 loc) · 1.95 KB
/
dialog.cpp
File metadata and controls
99 lines (88 loc) · 1.95 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
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::TodoList)
{
ui->setupUi(this);
setFocusPolicy(Qt::StrongFocus);
readFromFile();
}
Dialog::~Dialog()
{
ofile.close();
ifile.close();
delete ui;
}
void Dialog::writeToFile(QString data)
{
if(!ofile.is_open())
{
ofile.open("todo.txt", ios_base::app /*| ios_base::out*/);
}
ofile<<data.toStdString()<<endl;
}
void Dialog::readFromFile()
{
string data1;
ifile.open("todo.txt", ios::in);
while(getline(ifile,data1))
{
ui->listWidget->addItem(data1.data());
}
ifile.close();
}
//is called whenever Add is clicked
void Dialog::on_pushButton_clicked()
{
QString a;
a = ui->textEdit->toPlainText();
ui->listWidget->addItem(a);
writeToFile(a);
ofile.close();
ui->textEdit->clear();//clearing text box every time
}
void Dialog::on_pbRemove_clicked()
{
QListWidgetItem *item = ui->listWidget->currentItem();
char const *address = "todo.txt";
removeLine(address,ui->listWidget->currentRow());
delete item;
}
void Dialog::removeLine(const char *sourcefile, int line){
char tempPath[100]="temp.txt";
ifile.open(sourcefile,ios::in);
if(ifile){
int numLine=countLine(sourcefile);
if(numLine<line){
cout<<"\nNo line to delete\n.";
return;
}
ofile.open(tempPath,ios::out);
string data;
int i=0;
while(getline(ifile,data)){
if(i != line)
{
ofile<<data<<"\n";
}
i++;
}
ofile.close();
}
ifile.close();
remove(sourcefile);
rename(tempPath,sourcefile);
}
int Dialog::countLine(const char *sourcefile){
ifstream infile;
infile.open(sourcefile,ios::in);
string data;
int line=0;
while(getline(infile,data))
{
line++;
}
infile.close();
return line;
}