Skip to content

Commit

Permalink
FIN! v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-mroku committed Jan 19, 2013
1 parent 454dc72 commit a3bbf78
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 60 deletions.
4 changes: 2 additions & 2 deletions album.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "album.hpp"

Album::Album(const int year, const QString& title):Year(year), Title(title)
Album::Album(const int year, const QString& title):Year(year), Title(title), TreeItem()
{
}

Album::Album(const Album& other)
Album::Album(const Album& other):TreeItem(other)
{
id=other.id;
Year=other.Year;
Expand Down
9 changes: 2 additions & 7 deletions artist.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
#include "artist.hpp"
#include "cd.hpp"

Artist::Artist(const QString& name)
Artist::Artist(const QString& name):TreeItem()
{
Name=name;
Albums=&Children;
}

Artist::Artist(const Artist& other)
Artist::Artist(const Artist& other):TreeItem(other)
{
id=other.id;
Name=other.Name;
Albums=&Children;
for(const TreeItem* item:other.Children)
{
Children.push_back(item->Copy());
Children.back()->Parent=this;
}
}

TreeItem* Artist::Copy() const
Expand Down
8 changes: 2 additions & 6 deletions cd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ CD::CD(const int year, const QString& title):Album(year,title)

CD::CD(const CD& other):Album(other)
{
for(const TreeItem* item:other.Children)
{
Children.push_back(item->Copy());
Children.back()->Parent=this;
}
Tracks=&Children;
Tracks=&Children;
}

TreeItem* CD::Copy() const
Expand All @@ -23,6 +18,7 @@ TreeItem* CD::Copy() const
void CD::AddTrack(const QString& title, const int duration, const int index)
{
Children.push_back(new Track(Children.size()+1, title, duration));
Children.back()->Parent=this;
}

Album::Types CD::AlbumType() const
Expand Down
89 changes: 78 additions & 11 deletions dialog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "dialog.hpp"
#include "ui_dialog.h"
#include <QMessageBox>
#include "artist.hpp"
#include "cd.hpp"
#include <QTime>

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
Expand All @@ -17,29 +20,93 @@ Dialog::Dialog(QWidget *parent) :
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 ui;
delete model;
//delete NewArtist; //Tym zajmuje się już model
}


void Dialog::Check()
{
if(ui->ArtistEdit->text().size()==0)
return;
if(ui->TitleEdit->text().size()==0)
return;
if(ui->YearEdit->text().size()==0)
return;
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)
return;
{
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);

}

QMessageBox check;
check.setText("check");
check.exec();
emit Ok();
}

Expand Down
3 changes: 3 additions & 0 deletions dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <QDialog>
#include <QStandardItemModel>

#include "artist.hpp"

namespace Ui {
class Dialog;
}
Expand All @@ -16,6 +18,7 @@ class Dialog : public QDialog
explicit Dialog(QWidget *parent = 0);
~Dialog();
QStandardItemModel *model;
Artist* NewArtist;

public slots:
void Check();
Expand Down
28 changes: 21 additions & 7 deletions library.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
#include "library.hpp"

Library::Library(std::string name)
Library::Library(std::string name):TreeItem()
{
Name=name;
Artists=&Children;
}

Library::Library(const Library& library)
Library::Library(const Library& library):TreeItem(library)
{
Artists=&Children;
id=library.id;
Name=library.Name;
for(const TreeItem* item:library.Children)
{
Children.push_back(item->Copy());
Children.back()->Parent=this;
}
}

TreeItem* Library::Copy() const
Expand All @@ -32,3 +27,22 @@ TreeItem::ItemTypes Library::ItemType() const
{
return ItemTypes::library;
}

TreeItem* Library::AddArtist(Artist* artist)
{
for(TreeItem* knownArtist:*Artists)
{
if(dynamic_cast<Artist*>(knownArtist)->Name==artist->Name)
{
knownArtist->Children.push_back(artist->Children[0]);
knownArtist->Children.back()->Parent=knownArtist;
artist->Children.pop_back();// żeby destruktor nie zniszczył mi albumu
delete artist;// Bo klasy wyżej nie będą wiedziały, czy skopiowaliśmy, czy dodaliśmy artystę
return knownArtist->Children.back();
}
}
//Jeśli tu doszliśmy, to jest to nowy artysta;
Children.push_back(artist);
artist->Parent=this;
return Children.back();
}
2 changes: 2 additions & 0 deletions library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Library:public TreeItem
virtual QString QData() const;
virtual ItemTypes ItemType() const;

TreeItem* AddArtist(Artist* artist);

protected:
friend class odb::access;

Expand Down
7 changes: 2 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ void populate(Model& model)
a->Albums->push_back(al);
alb->AddTrack("Track 1", 76);

//model.Root=lib->Copy();
//std::cout<<*lib<<std::endl;

odb::core::transaction t (db.begin ());
odb::schema_catalog::create_schema (db);
lib->Persist(db);
Expand All @@ -51,8 +48,8 @@ int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
create(*w.model);
populate(*w.model);
//create(*w.model);
//populate(*w.model);

w.showMaximized();
return a.exec();
Expand Down
11 changes: 5 additions & 6 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ void MainWindow::Reload()
ui->treeView->setModel(model);
}

void MainWindow::Save()
{
//model->
}

bool MainWindow::Add()
{
Dialog dialog(this);
return dialog.exec();
if(dialog.exec())
model->AddArtist(dialog.NewArtist);
ui->treeView->setModel(NULL);
ui->treeView->setModel(model);

}

void MainWindow::Delete()
Expand Down
1 change: 0 additions & 1 deletion mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class MainWindow : public QMainWindow
Model* model;
public slots:
void Reload();
void Save();
bool Add();
void Delete();

Expand Down
7 changes: 0 additions & 7 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="SaveButton">
<property name="text">
<string>Save database</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="NewButton">
<property name="text">
Expand Down
13 changes: 13 additions & 0 deletions model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void Model::LoadDatabase(const QString& filename)
//ograniczam się do jednej bazy
Root=r.begin()->Copy();


t.commit();
//std::cout<<*Root<<std::endl;
}
Expand Down Expand Up @@ -129,6 +130,18 @@ void Model::Erase(const QModelIndex& index)
delete item;
}

void Model::AddArtist(Artist* artist)
{
TreeItem* newNode=dynamic_cast<Library*>(Root)->AddArtist(artist);

odb::sqlite::database db(DatabaseFilename.toStdString(), SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
odb::core::transaction t (db.begin ());

newNode->Persist(db);
db.update(*(newNode->Parent));
t.commit();
}

std::ostream& operator<<(std::ostream& out, const Model& model)
{
out<<*model.Root<<std::endl;
Expand Down
7 changes: 1 addition & 6 deletions model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
#include "library-odb.hxx"
#include "treeitem.hpp"
#include "treeitem-odb.hxx"
//#include "artist.hpp"
//#include "artist-odb.hxx"
//#include "album.hpp"
//#include "album-odb.hxx"

//#include "rootitem.hpp"

class Model:public QAbstractItemModel
{
Expand All @@ -38,6 +32,7 @@ class Model:public QAbstractItemModel
int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
void Erase(const QModelIndex& index);
void AddArtist(Artist* artist);


friend std::ostream& operator<<(std::ostream& out, const Model& model);
Expand Down
2 changes: 1 addition & 1 deletion track.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "track.hpp"
#include <QTime>

Track::Track(const int number, const QString& title, const int duration):Number(number), Title(title), DurationSeconds(duration)
Track::Track(const int number, const QString& title, const int duration):Number(number), Title(title), DurationSeconds(duration), TreeItem()
{

}
Expand Down
11 changes: 10 additions & 1 deletion treeitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ void TreeItem::Erase(odb::database& db)
void TreeItem::Update(odb::database& db)
{
for(TreeItem* item:Children)
item->Update(db);
{
try
{
item->Update(db);
}
catch(odb::object_not_persistent e)
{
item->Persist(db);
}
}

db.update(const_cast<TreeItem&>(*this));
}
Expand Down
1 change: 1 addition & 0 deletions treeitem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TreeItem
QList<TreeItem*> Children;

//Wyświetlanie w QT zależy od parenta
#pragma db transient
TreeItem* Parent;

TreeItem(TreeItem* parent=NULL);
Expand Down
Binary file modified yarl.db
Binary file not shown.

0 comments on commit a3bbf78

Please sign in to comment.