diff options
author | Arno <arno@disconnect.de> | 2017-10-15 09:17:50 +0200 |
---|---|---|
committer | Arno <arno@disconnect.de> | 2017-10-15 09:17:50 +0200 |
commit | 1bcbd9e93eadcffec21d32100b70310abec9a58f (patch) | |
tree | 674920d3f07992fcf0e77a5bdbf43c98aaa6f58c /webradiodialog.cpp | |
parent | 7e1ebd90789ae1d9d9fdfc54988aad722667df4c (diff) | |
download | BeetPlayer-1bcbd9e93eadcffec21d32100b70310abec9a58f.tar.gz BeetPlayer-1bcbd9e93eadcffec21d32100b70310abec9a58f.tar.bz2 BeetPlayer-1bcbd9e93eadcffec21d32100b70310abec9a58f.zip |
Fix WebRadioDialog
Revamp the WebRadio Dialog: Make it possible to delete WebRadios.
Diffstat (limited to 'webradiodialog.cpp')
-rw-r--r-- | webradiodialog.cpp | 110 |
1 files changed, 96 insertions, 14 deletions
diff --git a/webradiodialog.cpp b/webradiodialog.cpp index 4e08627..25578cc 100644 --- a/webradiodialog.cpp +++ b/webradiodialog.cpp @@ -2,28 +2,110 @@ #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 *topL = new QFormLayout; - topL->addRow(tr("&Description:"), mDescription); - topL->addRow(tr("&Url:"), mUrl); - QPushButton *okB = new QPushButton(tr("OK")); - connect(okB, &QPushButton::clicked, this, &QDialog::accept); - QPushButton *cancelB = new QPushButton(tr("Cancel")); - connect(cancelB, &QPushButton::clicked, this, &QDialog::reject); - QHBoxLayout *buttonL = new QHBoxLayout; - buttonL->addStretch(); - buttonL->addWidget(okB); - buttonL->addWidget(cancelB); + 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->addLayout(topL); - mainL->addLayout(buttonL); + mainL->addWidget(availGB); + mainL->addWidget(addGB); setLayout(mainL); - setWindowTitle(QString(tr("%1 - Add WebRadio")).arg(qApp->applicationName())); + 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.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->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->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(); } |