-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.cpp
151 lines (127 loc) · 3.73 KB
/
dialog.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
#include "dialog.hpp"
#include "ui_dialog.h"
#include <QMessageBox>
#include "artist.hpp"
#include "cd.hpp"
#include <QTime>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->setLayout(ui->verticalLayout);
model=new QStandardItemModel();
model->setHorizontalHeaderLabels(QStringList({"Title", "Duration"}));
ui->tableView->setModel(model);
ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
connect(ui->AddButton, SIGNAL(clicked()), this, SLOT(AddTrack()));
connect(ui->RemoveButton, SIGNAL(clicked()), this, SLOT(DelTrack()));
connect(ui->UpButton, SIGNAL(clicked()), this, SLOT(PopTrack()));
connect(ui->DownButton, SIGNAL(clicked()), this, SLOT(PushTrack()));
NewArtist=NULL;
}
Dialog::~Dialog()
{
delete ui;
delete model;
//delete NewArtist; //Tym zajmuje się już model
}
void Dialog::Check()
{
QString artistName, albumTitle;
int albumYear;
if(NewArtist!=NULL)
{
delete NewArtist;
NewArtist=NULL;
}
artistName=ui->ArtistEdit->text();
if(artistName.isEmpty())
{
QMessageBox check;
check.setText("There were errors in the name of the artist.\nCorrect your data and try again.");
check.exec();
return;
}
NewArtist=new Artist(artistName);
albumTitle=ui->TitleEdit->text();
if(albumTitle.isEmpty())
{
QMessageBox check;
check.setText("There were errors in title of the album.\nCorrect your data and try again.");
check.exec();
return;
}
bool yearOk;
albumYear=ui->YearEdit->text().toInt(&yearOk);
if(ui->YearEdit->text().size()==0||!yearOk)
{
QMessageBox check;
check.setText("There were errors in year of production of the album.\nCorrect your data and try again.");
check.exec();
return;
}
Album* NewAlbum=new CD(albumYear, albumTitle);
NewArtist->Children.push_back(NewAlbum);
NewAlbum->Parent=NewArtist;
if(model->rowCount()==0)
{
QMessageBox check;
check.setText("There were errors in track list.\nCorrect your data and try again.");
check.exec();
return;
}
for(int trackIndex=0;trackIndex<model->rowCount();trackIndex++)
{
QString trackTitle=model->item(trackIndex,0)->text();
if(trackTitle.isEmpty())
{
QMessageBox check;
check.setText("There were errors in track list.\nCorrect your data and try again.");
check.exec();
return;
}
QTime duration=QTime::fromString(model->item(trackIndex,1)->text(), "m:s");
if(!duration.isValid())
{
QMessageBox check;
check.setText("There were errors in track list.\nCorrect your data and try again.");
check.exec();
return;
}
NewAlbum->AddTrack(trackTitle, duration.elapsed()*1000);
}
emit Ok();
}
void Dialog::AddTrack()
{
QStandardItem track(2,1);
model->appendRow(&track);
}
void Dialog::DelTrack()
{
QModelIndexList indexList=ui->tableView->selectionModel()->selectedIndexes();
if(indexList.size()==0)
return;
model->removeRows(indexList[0].row(),1);
}
void Dialog::PopTrack()
{
QModelIndexList indexList=ui->tableView->selectionModel()->selectedIndexes();
if(indexList.size()==0)
return;
if(indexList[0].row()==0)
return;
QList<QStandardItem*> pushed=model->takeRow(indexList[0].row());
model->insertRow(indexList[0].row()-1, pushed);
}
void Dialog::PushTrack()
{
QModelIndexList indexList=ui->tableView->selectionModel()->selectedIndexes();
if(indexList.size()==0)
return;
if(indexList[0].row()==model->rowCount()-1)
return;
QList<QStandardItem*> pushed=model->takeRow(indexList[0].row());
model->insertRow(indexList[0].row()+1, pushed);
}