-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsong.cpp
More file actions
159 lines (155 loc) · 5.53 KB
/
song.cpp
File metadata and controls
159 lines (155 loc) · 5.53 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
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
#include "song.h"
#include "OpenFileException.h"
#include "SaveFileException.h"
void Song::readFromFile(QString fileName)
{
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly))
{
throw OpenFileException();
}
QTextStream in(&file);
in >> (*this);
isSaved = false;
file.close();
}
void Song::saveToFile(QString fileName)
{
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly)) {
throw SaveFileException();
}
QTextStream out(&file);
out << *(this);
isSaved = true;
file.close();
}
QTextStream &operator<<(QTextStream &out, Song &song)
{
out<<"Name:"<<song.name<<"\n";
out<<"Author:"<<song.author<<"\n";
for(int i = 0; i < song.getCountOfCouplets(); i++)
{
out<<"Couplet:"<<i+1<<"\n"<<song.songText[i].getTextOfCouplet();
}
return out;
}
QTextStream &operator>>(QTextStream &in, Song &song)
{
QString text = in.readAll();
QStringList coupletList = text.split(QRegExp("Couplet:[0-9]+"), QString::SkipEmptyParts);
for (int i = 0; i < coupletList.size();i++) {
if(i){
Couplet temp;
QString tmp = coupletList[i];
//QString deletestr = QString::number(i)+"\n";
//tmp.remove(deletestr);
temp.setTextOfCouplet(tmp);
temp.setnumberOfCouplet(i);
song.songText.push_back(temp);}
else{
QStringList list = coupletList[0].split(QRegExp("\n"), QString::SkipEmptyParts);
for (int j = 0; j < list.size();j++) {
if(j==1){
QString author = list[j];
author.remove("Author:");
song.setAuthor(author);
}
if(!j){
QString name = list[j];
name.remove("Name:");
song.setName(name);
}
}
}
}
song.setCountOfCouplets(coupletList.size()-1);
song.isSaved = true;
return in;
}
void operator<<(QListWidget *listWidget, Song &song){
QString output = "";
output+="Name:"+song.getName()+"\n";
output+="Author:"+song.getAuthor()+"\n";
for(int i = 0; i < song.getCountOfCouplets(); i++)
{
output+="Couplet:"+QString::number(i+1)+(song.getSongText())[i].getTextOfCouplet();
}
QListWidgetItem *newItem = new QListWidgetItem;
newItem->setText(output.toLocal8Bit());
listWidget->addItem(newItem);
}
QString Song::findWordInCouplet(QString word, int numOfCouplet, int* count)
{
QString output = "";
QStringList list = (this->getSongText())[numOfCouplet].getTextOfCouplet().split(QRegExp("\r\n"));
for(int i = 0; i < list.size(); i++)
if(list[i].contains(word, Qt::CaseInsensitive)){ output += list[i]+"\n"; (*count)++;}
if(output == ""){
output = "Not found input word\n";
}
return output;
}
QString Song::findWordInSong(QString word, int* count)
{
QString output = "";
QStringList list;
for(int i = 0; i<this->getCountOfCouplets();i++)
{
list = (this->getSongText())[i].getTextOfCouplet().split(QRegExp("\r\n"),QString::SkipEmptyParts);
for(int j = 0; j < list.size(); j++)
if(list[j].contains(word, Qt::CaseInsensitive))
{
output += (j == list.size()-1) ? list[j] : list[j]+"\n";
(*count)++;
}
}
if(output == ""){
output = "Not found input word\n";
}
return output;
}
void Song::changeLineInCouplet(QString text, int numOfCouplet, int numOfLineInCouplet)
{
QStringList list = (this->getSongText())[numOfCouplet].getTextOfCouplet().split(QRegExp("\r\n"));
list[numOfLineInCouplet] = (numOfLineInCouplet == list.size()-1)?text+"\n":text;
(this->getSongText())[numOfCouplet].setTextOfCouplet(list.join("\r\n"));
}
void Song::swapTwoCoupets(int numOfFirstCouplet, int numOfSecondCouplet){
QString temp1 = (this->getSongText())[numOfFirstCouplet].getTextOfCouplet();
QString temp2 = (this->getSongText())[numOfSecondCouplet].getTextOfCouplet();
(this->getSongText())[numOfFirstCouplet].setTextOfCouplet(temp2);
(this->getSongText())[numOfSecondCouplet].setTextOfCouplet(temp1);
}
QString Song::printLastLines(int num){
QString output = "";
QStringList list;
for(int i = 0; i<this->getCountOfCouplets();i++){
list = (this->getSongText())[i].getTextOfCouplet().split(QRegExp("\r\n"),QString::SkipEmptyParts);
output +=(QString)list.takeLast()+"\r\n";
}
return output;
}
QString Song::typeOfSonnet()
{
QVector<int> arr;
QStringList list;
int sum = 0;
QString result = "It's not sonnet!";
if(this->getCountOfCouplets()>1 && this->getCountOfCouplets()<5){
for(int i = 0; i<this->getCountOfCouplets();i++){
list = (this->getSongText())[i].getTextOfCouplet().split(QRegExp("\r\n"),QString::SkipEmptyParts);
arr.push_back(list.size());
}
for(int i = 0; i<arr.size(); i++)
sum += arr[i];
if(sum == 14 && arr.size() == 4 &&(arr[0] == 4 && arr[1] == 4 && arr[2] == 3 && arr[3] == 3))
result = "It's sonnet!";
else if(sum == 10 && arr.size() == 3 &&(arr[0] == 4 && arr[1] == 3 && arr[2] == 3))
result = "It's headless sonnet!";
else if(sum == 7 && arr.size() == 2 &&(arr[0] == 4 && arr[1] == 3))
result = "It's halfsonnet!";
else result = "It's not sonnet!";
}
return result;
}