summaryrefslogtreecommitdiffstats
path: root/webradiodialog.cpp
blob: 53ff3fc1dc361ca56cb69a5ed02001403d3f9855 (plain)
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
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QPushButton>
#include <QSqlQuery>
#include <QGroupBox>
#include <QApplication>

#include "webradiodialog.h"

WebRadioDialog::WebRadioDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
    //available
    mModel = new QStandardItemModel;
    mView = new QTreeView;
    mView->setModel(mModel);
    mView->setSelectionBehavior(QAbstractItemView::SelectRows);
    mView->setSelectionMode(QAbstractItemView::SingleSelection);
    QGroupBox *availGB = new QGroupBox(tr("Available"));
    QVBoxLayout *availL = new QVBoxLayout;
    availL->addWidget(mView);
    QHBoxLayout *availBL = new QHBoxLayout;
    QPushButton *deleteBtn = new QPushButton(tr("Delete selected..."));
    connect(deleteBtn, &QPushButton::clicked, this, &WebRadioDialog::deleteSelected);
    availBL->addStretch();
    availBL->addWidget(deleteBtn);
    availL->addLayout(availBL);
    availGB->setLayout(availL);

    //add
    QGroupBox *addGB = new QGroupBox(tr("Add"));
    mDescription = new QLineEdit;
    mUrl = new QLineEdit;
    QFormLayout *addFormL = new QFormLayout;
    addFormL->addRow(tr("&Description:"), mDescription);
    addFormL->addRow(tr("&Url:"), mUrl);
    QPushButton *addBtn = new QPushButton(tr("Add"));
    QPushButton *doneBtn = new QPushButton(tr("Done"));
    connect(addBtn, &QPushButton::clicked, this, &WebRadioDialog::add);
    connect(doneBtn, &QPushButton::clicked, this, &QDialog::accept);
    QHBoxLayout *addBtnL = new QHBoxLayout;
    addBtnL->addWidget(doneBtn);
    addBtnL->addStretch();
    addBtnL->addWidget(addBtn);
    QVBoxLayout *addL = new QVBoxLayout;
    addL->addLayout(addFormL);
    addL->addLayout(addBtnL);
    addGB->setLayout(addL);

    QVBoxLayout *mainL = new QVBoxLayout;
    mainL->addWidget(availGB);
    mainL->addWidget(addGB);
    setLayout(mainL);
    setWindowTitle(QString(tr("%1 - Edit WebRadio")).arg(qApp->applicationName()));
    setMinimumWidth(768);
    populate();
}

void WebRadioDialog::populate(){
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    mModel->clear();
    mModel->setHorizontalHeaderLabels(QStringList() << tr("Description") << tr("URL"));
    QStandardItem *root = mModel->invisibleRootItem();
    QIcon wrIcon(":/dog_hood_light.png");
    QSqlQuery wrQ = QSqlQuery(db);
    wrQ.prepare("SELECT tdescription, turl FROM webradio ORDER BY tdescription DESC");
    wrQ.exec();
    while(wrQ.next()){
        QStandardItem *curDescr = new QStandardItem;
        curDescr->setEditable(false);
        curDescr->setIcon(wrIcon);
        curDescr->setText(wrQ.value(0).toString());
        curDescr->setData(wrQ.value(0), DescriptionRole);
        curDescr->setData(wrQ.value(1), UrlRole);
        curDescr->setFont(QFont("courier"));
        QStandardItem *curUrl = new QStandardItem;
        curUrl->setEditable(false);
        curUrl->setText(wrQ.value(1).toString());
        curUrl->setFont(QFont("courier"));
        root->appendRow(QList<QStandardItem*>() << curDescr << curUrl);
    }
    mView->resizeColumnToContents(1);
    mView->resizeColumnToContents(0);
}

void WebRadioDialog::deleteSelected(){
    QModelIndexList sel = mView->selectionModel()->selectedRows();
    if(sel.isEmpty()){
        return;
    }
    QVariant descr = sel.at(0).data(DescriptionRole);
    QVariant url = sel.at(0).data(UrlRole);
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    QSqlQuery delQ(db);
    delQ.prepare("DELETE FROM webradio WHERE tdescription = :descr AND turl = :url");
    delQ.bindValue(":descr", descr);
    delQ.bindValue(":url", url);
    delQ.exec();
    populate();
}

void WebRadioDialog::add(){
    QString descr = mDescription->text();
    QString url = mUrl->text();
    if(!descr.isEmpty() && !url.isEmpty()){
        QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
        QSqlQuery addQ(db);
        addQ.prepare("INSERT INTO webradio (tdescription, turl) VALUES(:d, :u)");
        addQ.bindValue(":d", descr);
        addQ.bindValue(":u", url);
        addQ.exec();
    }
    populate();
}