-
Notifications
You must be signed in to change notification settings - Fork 1
/
news.cpp
286 lines (273 loc) · 9.76 KB
/
news.cpp
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
#include "news.h"
#include "homepage.h"
#include "ui_news.h"
#include"newsbasedon.h"
#include"adminx.h"
#include"qmessagebox.h"
#include"login.h"
#include"stdafx.h"
News::News(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::News)
{
ui->setupUi(this);
ui->pushButton_saveNewAdmin->hide();
ui->pushButton_editDesc->hide();
ui->pushButton_editTitle->hide();
ui->pushButton_editCategory->hide();
ui->lineEdit->show();
}
string RatefromUser;
bool checkEditTitle=false;
bool checkEditCategory=false;
bool flag=false;
News::~News()
{
delete ui;
}
void News::displayNew(){
ui->pushButton_2->hide();
ui->lineEdit_Category->setText(Adminx::news[Newsbasedon::currentNew].getCategory().c_str());
ui->plainTextEdit_Title->setPlainText(Adminx::news[Newsbasedon::currentNew].getTitle().c_str());
ui->plainTextEdit_Description->setPlainText(Adminx::news[Newsbasedon::currentNew].getDescription().c_str());
ui->lineEdit_Date->setText(Adminx::news[Newsbasedon::currentNew].getDate().c_str());
for(auto it:favNews){
if(it.first==Login::UserID){
for(int i=0;i<it.second.size();i++){
if(ui->plainTextEdit_Title->toPlainText()==it.second[i].c_str())
ui->checkBox_favNews->setChecked(true);
}
}
}
for(auto it:User::Rates)
{
if(it.first.first==Login::UserID)
{
if(it.first.second==Adminx::news[Newsbasedon::currentNew].getTitle())
{
string rate=to_string(it.second);
ui->lineEdit->show();
ui->lineEdit->setText(rate.c_str());
ui->lineEdit->setEnabled(false);
ui->pushButton_2->show();
ui->pushButton_saveRate->hide();
}
}
}
}
void News::on_pushButton_clicked()
{
if(Adminx::backbuttons[0]){
hide();
Newsbasedon *w=new Newsbasedon();
w->show();
}
else if(Adminx::backbuttons[1]){
HomePage*h=new HomePage(this);
hide();
h->show();
}
else if(Adminx::backbuttons[2]){
Adminx *a=new Adminx(this);
hide();
a->show();
}
else if(Adminx::backbuttons[3]){
Search *s=new Search(this);
hide();
s->show();
}
}
void News::AddNew(){
ui->pushButton_2->hide();
ui->pushButton_saveRate->hide();
ui->lineEdit->hide();
ui->checkBox_favNews->hide();
ui->label_4->hide();
NewsModel getCurrentdate;
string date=getCurrentdate.getCurrentDate();
ui->lineEdit_Date->setText(date.c_str());
ui->plainTextEdit_Title->setEnabled(true);
ui->lineEdit_Category->setText(Adminx::NewCat.c_str());
ui->plainTextEdit_Description->setEnabled(true);
ui->pushButton_saveNewAdmin->show();
qDebug()<<Adminx::NewCat;
}
bool News::checkValidateOfTitle()
{
for(int i=0;i<Adminx::news.size();i++){
if(ui->plainTextEdit_Title->toPlainText().toStdString()==Adminx::news[i].getTitle())
return true;
}
return false;
}
bool News::checkValidateOfCategory()
{
for(int i=0;i<Adminx::news.size();i++){
if(ui->lineEdit_Category->text().toStdString()==Adminx::news[i].getCategory())
return true;
}
return false;
}
void News::on_pushButton_saveNewAdmin_clicked()
{
if(Adminx::checkedAdmin[0]){
//addnew
if(ui->lineEdit_Category->text()=="\0"||ui->plainTextEdit_Title->toPlainText()=="\0"||ui->plainTextEdit_Description->toPlainText()=="\0"){
QMessageBox::warning(this,"Error","Please fill the data");
}
else if(ui->lineEdit_Category->text()==ui->plainTextEdit_Title->toPlainText()){
QMessageBox::warning(this,"Error","Category and Title can't have same name ");
}
else if(checkValidateOfTitle()){
QMessageBox::warning(this,"error","this Title Name is already Exist");
}
else{
QMessageBox::information(this,"Success","New Added");
string newCategory=ui->lineEdit_Category->text().toStdString();
string newTitle=ui->plainTextEdit_Title->toPlainText().toStdString();
string newDescription=ui->plainTextEdit_Description->toPlainText().toStdString();
string newDate=ui->lineEdit_Date->text().toStdString();
NewsModel::addnew(NewsModel(newTitle,newDescription,newDate,newCategory,0.0));
for(auto it:Adminx::news){
qDebug()<<it.getCategory()<<" "<<it.getTitle();
}
}
}
else if(Adminx::checkedAdmin[1]){
//update New
if(ui->plainTextEdit_Title->toPlainText()=="\0"||ui->plainTextEdit_Description->toPlainText()=="\0"||ui->lineEdit_Category->text()=="\0"){
QMessageBox::warning(this,"Error","Please fill the data");
}
else if(checkValidateOfTitle()&&checkEditTitle){
QMessageBox::warning(this,"error","this Title Name is already Exist");
qDebug ()<<"error";
}
else if(!checkValidateOfCategory()&&checkEditCategory){
QMessageBox::warning(this,"error","this Category Name doesn't Exist, Please Add it");
qDebug ()<<"error";
}
else{
checkEditTitle=false;
checkEditCategory=false;
QMessageBox::information(this,"success","New Updated !");
string NewTitle=ui->plainTextEdit_Title->toPlainText().toStdString();
Adminx::news[Newsbasedon::currentNew].setTitle(NewTitle);
string NewDesc=ui->plainTextEdit_Description->toPlainText().toStdString();
Adminx::news[Newsbasedon::currentNew].setDescription(NewDesc);
string NewCat=ui->lineEdit_Category->text().toStdString();
Adminx::news[Newsbasedon::currentNew].setCategory(NewCat);
qDebug()<<Adminx::news[Newsbasedon::currentNew].getTitle()<<Adminx::news[Newsbasedon::currentNew].getDescription();
qDebug()<<"==============";
for(auto it:Adminx::news){
qDebug()<<it.getTitle()<<it.getDescription();
}
hide();
Adminx *x=new Adminx(this);
x->show();
}
}
}
void News:: showEditButtons(){
ui->pushButton_editDesc->show();
ui->pushButton_editTitle->show();
ui->pushButton_editCategory->show();
ui->pushButton_saveNewAdmin->show();
ui->checkBox_favNews->hide();
ui->label_4->hide();
ui->lineEdit->hide();
ui->pushButton_2->hide();
ui->pushButton_saveRate->hide();
}
void News::on_pushButton_editTitle_clicked()
{
checkEditTitle=true;
ui->plainTextEdit_Title->setEnabled(true);
}
void News::on_pushButton_editDesc_clicked()
{
ui->plainTextEdit_Description->setEnabled(true);
}
void News::on_pushButton_editCategory_clicked()
{
checkEditCategory=true;
ui->lineEdit_Category->setEnabled(true);
}
void News::on_checkBox_favNews_clicked(bool checked)
{
bool checkFavNews=ui->checkBox_favNews->isChecked();
if(checkFavNews){
favNews[Login::UserID].push_back(Adminx::news[Newsbasedon::currentNew].getTitle());
QMessageBox::information(this,"Success","New Added to ur Favourite News");
}
else if(!checkFavNews)
{
for(auto it:favNews)
{
if(it.first==Login::UserID)
{
for(int i=0;i<it.second.size();i++)
{
if(ui->plainTextEdit_Title->toPlainText()==it.second[i].c_str())
{
favNews[it.first].erase(favNews[it.first].begin()+i);
qDebug()<<Adminx::news[Newsbasedon::currentNew].getTitle();
QMessageBox::information(this,"Success","New Removed from ur Favourite News");
}
}
}
}
}
}
void News::checkRate(){
int Rate;
RatefromUser=ui->lineEdit->text().toStdString();
if(RatefromUser>="1"&&RatefromUser<="5"){
Rate=stoi(RatefromUser);
User::Rates[make_pair(Login::UserID,Adminx::news[Newsbasedon::currentNew].getTitle())]=Rate;
calcRate(Rate, Adminx::news[Newsbasedon::currentNew]);
if(flag){
QMessageBox::information(this,"Success","Rate Added !");
ui->lineEdit->setEnabled(false);
}
}
else{
QMessageBox::warning(this,"Error","please Entre number between 1 and 5");
}
for(auto it:User::Rates){
qDebug()<<it.first.first<<it.first.second<<it.second;
}
qDebug()<<"========";
}
void News::on_pushButton_saveRate_clicked()
{
flag=true;
checkRate();
}
void News::on_pushButton_2_clicked()
{
flag=false;
checkRate();
ui->lineEdit->setEnabled(true);
ui->pushButton_saveRate->show();
ui->pushButton_2->hide();
}
void News::calcRate(int rate, NewsModel& newsModel)
{
int id = Login::UserID;
if(!newsModel.rates.count(id))
{
qDebug() << newsModel.avgRate << ' ' << newsModel.rates.size() << '\n';
newsModel.rates[id] = rate;
newsModel.avgRate = ((newsModel.avgRate * (newsModel.rates.size() - 1)) + rate) / newsModel.rates.size();
qDebug() << newsModel.avgRate << ' ' << newsModel.rates.size() << '\n';
}
else
{
qDebug() << newsModel.avgRate << ' ' << newsModel.rates.size() << '\n';
newsModel.avgRate += double( rate - newsModel.rates[id]) / double(newsModel.rates.size());
qDebug() << rate << ' ' << id << ' ' << newsModel.rates[id] << ' ' << newsModel.rates.size() << '\n';
newsModel.rates[id] = rate;
qDebug() << newsModel.avgRate << ' ' << newsModel.rates.size() << '\n';
}
}