forked from marina13594/NETB380
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
128 lines (117 loc) · 4.15 KB
/
mainwindow.cpp
File metadata and controls
128 lines (117 loc) · 4.15 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "secdialog.h"
#include "musicplayerdialog.h"
#include <QtSql>
#include <QDebug>
#include <QDesktopServices>
#include <QUrl>
#include <QStandardPaths>
#include <QFormLayout>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->SearchByBox->addItem("Song_name");
ui->SearchByBox->addItem("Artist_name");
ui->SearchByBox->addItem("Album_name");
ui->SearchByBox->addItem("Genre_type");
QDialog *inputPanel = new QDialog(this);
QFormLayout *form = new QFormLayout(inputPanel);
QLabel *ip = new QLabel("IP Address :",inputPanel);
QLineEdit *ipInput = new QLineEdit(inputPanel);
form->addRow(ip,ipInput);
QLabel *dbname = new QLabel("DataBase Name :",inputPanel);
QLineEdit *dbInput = new QLineEdit(inputPanel);
form->addRow(dbname,dbInput);
QLabel *userName = new QLabel("Username :",inputPanel);
QLineEdit *userNameInput = new QLineEdit(inputPanel);
form->addRow(userName,userNameInput);
QLabel *password = new QLabel("Password :",inputPanel);
QLineEdit *passInput = new QLineEdit(inputPanel);
form->addRow(password,passInput);
QPushButton *connect_btn = new QPushButton(inputPanel);
connect_btn->setText("Connect to Database");
connect_btn->connect(connect_btn,SIGNAL(clicked()),inputPanel,SLOT(close()));
form->addRow(connect_btn);
inputPanel->exec();
db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName(ipInput->text());
db.setDatabaseName(dbInput->text());
db.setUserName(userNameInput->text());
db.setPassword(passInput->text());
if(db.open())
{
qDebug() << "Opened!";
this->qmodel = new QSqlQueryModel();
qmodel->setQuery("select songs.song_name,artist.artist_name,album.album_name, songs.address,genre.genre_type, songs.year from songs \
join artist on songs.artist = artist.artist_id \
join album on songs.album = album_id \
join genre on songs.genre = genre_id");
ui->tableView->setModel(qmodel);
}
else
{
QMessageBox messageBox;
messageBox.critical(0,"Error","Database didn't open!Probabbly wrong input.");
messageBox.setFixedSize(500,200);
qDebug() << "Error = " << db.lastError().text();
}
}
/* Destructor
* closes database connecting when called */
MainWindow::~MainWindow()
{
db.close();
qDebug() << "DB CLOSED!"<< endl;
delete ui;
}
void MainWindow::on_Add_clicked()
{
SecDialog secdialog;
secdialog.setModal(true);
secdialog.exec();
}
void MainWindow::on_Play_clicked()
{
MusicPlayerDialog musicplayerdialog;
musicplayerdialog.get_string_to_play(selected);
musicplayerdialog.setModal(true);
musicplayerdialog.exec();
}
void MainWindow::on_GoTo_clicked()
{
QString GoToStr = selected;
int i = GoToStr.size();
while(GoToStr[i]!= '/')
{
i--;
}
i= GoToStr.size() - i;
GoToStr.chop(i-1);
QDesktopServices::openUrl(QUrl("file:///" + GoToStr, QUrl::TolerantMode));
}
void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
selected = ui->tableView->model()->data(index).toString();
}
void MainWindow::on_Search_Button_clicked()
{
qmodel->setQuery(QString("select songs.song_name, artist.artist_name, album.album_name,genre.genre_type, songs.address from songs \
join artist on songs.artist = artist.artist_id \
join album on songs.album = album_id \
join genre on songs.genre = genre_id \
where %2 = '%1'").arg(ui->lineEdit->text()).arg(ui->SearchByBox->currentText()));
qDebug() << ui->lineEdit->text() << endl;
ui->tableView->setModel(qmodel);
}
void MainWindow::on_pushButton_clicked()
{
qmodel->setQuery("select songs.song_name,artist.artist_name,album.album_name, songs.address,genre.genre_type, songs.year from songs \
join artist on songs.artist = artist.artist_id \
join album on songs.album = album_id \
join genre on songs.genre = genre_id");
ui->tableView->setModel(qmodel);
}