| 1 | /* |
| 2 | Copyright (C) 2005-2014 Mario Stephan <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published |
| 6 | by the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "djbrowser.h" |
| 19 | #include "collectiondb.h" |
| 20 | #include "dj.h" |
| 21 | #include "djfilterwidget.h" |
| 22 | #include "djwidget.h" |
| 23 | |
| 24 | #include <QListWidget> |
| 25 | #include <QPushButton> |
| 26 | #include <QSplitter> |
| 27 | #include <QVBoxLayout> |
| 28 | |
| 29 | class DjBrowserPrivate { |
| 30 | public: |
| 31 | QSplitter* splitter; |
| 32 | QListWidget* listDjs; |
| 33 | QListWidget* listDjFilters; |
| 34 | Dj* currentDj; |
| 35 | DjWidget* currentDjw; |
| 36 | QStringList allGenres; |
| 37 | QStringList allArtists; |
| 38 | CollectionDB* database; |
| 39 | }; |
| 40 | |
| 41 | DjBrowser::DjBrowser(QWidget* parent) |
| 42 | : QWidget(parent) |
| 43 | { |
| 44 | p = new DjBrowserPrivate; |
| 45 | p->database = new CollectionDB(); |
| 46 | |
| 47 | QList<QStringList> tags = p->database->selectArtists(); |
| 48 | p->allArtists.append(QString::null); |
| 49 | foreach (QStringList tag, tags) |
| 50 | p->allArtists.append(tag[0]); |
| 51 | |
| 52 | tags = p->database->selectGenres(); |
| 53 | p->allGenres.append(QString::null); |
| 54 | foreach (QStringList tag, tags) |
| 55 | p->allGenres.append(tag[0]); |
| 56 | |
| 57 | QPushButton* pushAddDj = new QPushButton(); |
| 58 | pushAddDj->setGeometry(QRect(1, 1, 60, 25)); |
| 59 | pushAddDj->setMaximumWidth(60); |
| 60 | pushAddDj->setMinimumWidth(60); |
| 61 | pushAddDj->setText("+"); |
| 62 | QFont pushFont = pushAddDj->font(); |
| 63 | pushFont.setBold(true); |
| 64 | pushFont.setPointSize(pushFont.pointSize() + 4); |
| 65 | pushAddDj->setFont(pushFont); |
| 66 | |
| 67 | pushAddDj->setStyleSheet("QPushButton { border: none; padding-top: -3px; margin-left:8px;max-height: 20px; margin-right: 28px;}"); |
| 68 | pushAddDj->setToolTip(tr("Add a new AutoDj")); |
| 69 | connect(pushAddDj, SIGNAL(clicked()), this, SLOT(addDj())); |
| 70 | |
| 71 | QPushButton* pushAddFilter = new QPushButton(); |
| 72 | pushAddFilter->setGeometry(QRect(1, 1, 60, 25)); |
| 73 | pushAddFilter->setMaximumWidth(60); |
| 74 | pushAddFilter->setMinimumWidth(60); |
| 75 | pushAddFilter->setText("+"); |
| 76 | pushAddFilter->setFont(pushFont); |
| 77 | |
| 78 | pushAddFilter->setStyleSheet("QPushButton { border: none; padding-top: -3px; margin-left: 8px;max-height: 20px; margin-right: 28px;}"); |
| 79 | pushAddFilter->setToolTip(tr("Add a new record case for current AutoDj")); |
| 80 | connect(pushAddFilter, SIGNAL(clicked()), this, SLOT(addFilter())); |
| 81 | |
| 82 | QPushButton* pushSave = new QPushButton(); |
| 83 | pushSave->setGeometry(QRect(1, 1, 60, 25)); |
| 84 | pushSave->setMaximumWidth(60); |
| 85 | pushSave->setMinimumWidth(60); |
| 86 | pushSave->setText(QString::fromUtf8("\U0001f4be")); |
| 87 | pushSave->setFont(pushFont); |
| 88 | |
| 89 | pushSave->setStyleSheet("QPushButton { border: none; padding-top: -3px; margin-left: 8px;max-height: 20px; margin-right: 28px;}"); |
| 90 | pushSave->setToolTip(tr("Save all settings")); |
| 91 | connect(pushSave, SIGNAL(clicked()), this, SLOT(saveSettings())); |
| 92 | |
| 93 | p->listDjFilters = new QListWidget(); |
| 94 | p->listDjs = new QListWidget(); |
| 95 | p->listDjFilters->setAttribute(Qt::WA_MacShowFocusRect, false); |
| 96 | p->listDjs->setAttribute(Qt::WA_MacShowFocusRect, false); |
| 97 | p->listDjs->setItemSelected(p->listDjs->currentItem(), false); |
| 98 | |
| 99 | QVBoxLayout* mainLayout = new QVBoxLayout; |
| 100 | |
| 101 | QVBoxLayout* headWidgetLeftLayout = new QVBoxLayout; |
| 102 | headWidgetLeftLayout->setMargin(0); |
| 103 | headWidgetLeftLayout->setSpacing(1); |
| 104 | headWidgetLeftLayout->setAlignment(Qt::AlignRight); |
| 105 | headWidgetLeftLayout->addWidget(pushAddDj); |
| 106 | |
| 107 | QWidget* headWidgetLeft = new QWidget(this); |
| 108 | headWidgetLeft->setMaximumHeight(20); |
| 109 | headWidgetLeft->setMinimumHeight(20); |
| 110 | headWidgetLeft->setLayout(headWidgetLeftLayout); |
| 111 | |
| 112 | QHBoxLayout* headWidgetRightLayout = new QHBoxLayout; |
| 113 | headWidgetRightLayout->setMargin(0); |
| 114 | headWidgetRightLayout->setSpacing(1); |
| 115 | headWidgetRightLayout->setAlignment(Qt::AlignRight); |
| 116 | headWidgetRightLayout->addWidget(pushAddFilter); |
| 117 | headWidgetRightLayout->addWidget(pushSave); |
| 118 | |
| 119 | QWidget* headWidgetRight = new QWidget(this); |
| 120 | headWidgetRight->setMaximumHeight(20); |
| 121 | headWidgetRight->setMinimumHeight(20); |
| 122 | headWidgetRight->setLayout(headWidgetRightLayout); |
| 123 | |
| 124 | QVBoxLayout* widgetLeftLayout = new QVBoxLayout; |
| 125 | widgetLeftLayout->setMargin(0); |
| 126 | widgetLeftLayout->setSpacing(1); |
| 127 | widgetLeftLayout->setAlignment(Qt::AlignRight); |
| 128 | widgetLeftLayout->addWidget(headWidgetLeft); |
| 129 | widgetLeftLayout->addWidget(p->listDjs); |
| 130 | |
| 131 | QVBoxLayout* widgetRightLayout = new QVBoxLayout; |
| 132 | widgetRightLayout->setMargin(0); |
| 133 | widgetRightLayout->setSpacing(1); |
| 134 | widgetRightLayout->setAlignment(Qt::AlignRight); |
| 135 | widgetRightLayout->addWidget(headWidgetRight); |
| 136 | widgetRightLayout->addWidget(p->listDjFilters); |
| 137 | |
| 138 | QWidget* widgetLeft = new QWidget(this); |
| 139 | widgetLeft->setLayout(widgetLeftLayout); |
| 140 | |
| 141 | QWidget* widgetRight = new QWidget(this); |
| 142 | widgetRight->setLayout(widgetRightLayout); |
| 143 | |
| 144 | p->splitter = new QSplitter(); |
| 145 | p->splitter->addWidget(widgetLeft); |
| 146 | p->splitter->addWidget(widgetRight); |
| 147 | |
| 148 | mainLayout->addWidget(p->splitter); |
| 149 | |
| 150 | mainLayout->setMargin(0); |
| 151 | mainLayout->setSpacing(0); |
| 152 | |
| 153 | setLayout(mainLayout); |
| 154 | |
| 155 | QSettings settings; |
| 156 | p->splitter->setStretchFactor(0, 5); |
| 157 | p->splitter->setStretchFactor(1, 9); |
| 158 | p->splitter->restoreState(settings.value("SplitterDjBrowser").toByteArray()); |
| 159 | } |
| 160 | |
| 161 | DjBrowser::~DjBrowser() |
| 162 | { |
| 163 | saveSettings(); |
| 164 | |
| 165 | delete p; |
| 166 | } |
| 167 | |
| 168 | void DjBrowser::saveSettings() |
| 169 | { |
| 170 | QSettings settings; |
| 171 | settings.setValue("SplitterDjBrowser", p->splitter->saveState()); |
| 172 | |
| 173 | settings.setValue("countDJ", p->listDjs->count()); |
| 174 | settings.remove("AutoDJ"); |
| 175 | settings.beginGroup("AutoDJ"); |
| 176 | for (int d = 0; d < p->listDjs->count(); d++) { |
| 177 | settings.beginGroup(QString::number(d)); |
| 178 | Dj* dj = ((DjWidget*)p->listDjs->itemWidget(p->listDjs->item(d)))->dj(); |
| 179 | if (dj != nullptr) { |
| 180 | settings.setValue("Name", dj->name); |
| 181 | settings.setValue("FilterCount", dj->filters().count()); |
| 182 | |
| 183 | QList<Filter*> f = dj->filters(); |
| 184 | |
| 185 | settings.beginGroup("Filter"); |
| 186 | for (int i = 0; i < f.count(); i++) { |
| 187 | settings.beginGroup(QString::number(i)); |
| 188 | settings.setValue("Path", f.at(i)->path()); |
| 189 | settings.setValue("Genre", f.at(i)->genre()); |
| 190 | settings.setValue("Artist", f.at(i)->artist()); |
| 191 | settings.setValue("Value", QString::number(f.at(i)->maxUsage())); |
| 192 | settings.endGroup(); |
| 193 | } |
| 194 | settings.endGroup(); |
| 195 | settings.endGroup(); |
| 196 | } |
| 197 | } |
| 198 | settings.endGroup(); |
| 199 | } |
| 200 | |
| 201 | void DjBrowser::updateList() |
| 202 | { |
| 203 | QSettings settings; |
| 204 | |
| 205 | //Dj Widget List |
| 206 | p->listDjs->clear(); |
| 207 | p->listDjs->setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 208 | |
| 209 | DjWidget* djw; |
| 210 | QListWidgetItem* itm; |
| 211 | Dj* dj; |
| 212 | int maxDj = settings.value("countDJ", "3").toInt(); |
| 213 | if (maxDj == 0) |
| 214 | maxDj = 1; |
| 215 | |
| 216 | settings.beginGroup("AutoDJ"); |
| 217 | |
| 218 | // DJs |
| 219 | for (int d = 0; d < maxDj; d++) { |
| 220 | settings.beginGroup(QString::number(d)); |
| 221 | dj = new Dj(); |
| 222 | QString djID = QString("Dj%1").arg(d + 1); |
| 223 | dj->name = settings.value("Name", djID).toString(); |
| 224 | |
| 225 | djw = new DjWidget(p->listDjs); |
| 226 | |
| 227 | connect(djw, SIGNAL(activated()), this, SLOT(loadDj())); |
| 228 | connect(djw, SIGNAL(deleted()), this, SLOT(removeDj())); |
| 229 | connect(djw, SIGNAL(started()), this, SLOT(startDj())); |
| 230 | |
| 231 | itm = new QListWidgetItem(p->listDjs); |
| 232 | itm->setSizeHint(QSize(0, 75)); |
| 233 | p->listDjs->addItem(itm); |
| 234 | p->listDjs->setItemWidget(itm, djw); |
| 235 | |
| 236 | // Filters |
| 237 | int countFilter = settings.value("FilterCount", "2").toInt(); |
| 238 | if (countFilter == 0) |
| 239 | countFilter = 1; |
| 240 | settings.beginGroup("Filter"); |
| 241 | for (int i = 0; i < countFilter; i++) { |
| 242 | settings.beginGroup(QString::number(i)); |
| 243 | Filter* f = new Filter(); |
| 244 | dj->addFilter(f); |
| 245 | |
| 246 | f->setPath(settings.value("Path", "").toString()); |
| 247 | f->setGenre(settings.value("Genre", "").toString()); |
| 248 | f->setArtist(settings.value("Artist", "").toString()); |
| 249 | f->setMaxUsage(settings.value("Value", "4").toInt()); |
| 250 | settings.endGroup(); |
| 251 | |
| 252 | f->setUsage(0); |
| 253 | } |
| 254 | |
| 255 | djw->setDj(dj); |
| 256 | // force sum update |
| 257 | djw->clicked(); |
| 258 | |
| 259 | settings.endGroup(); |
| 260 | dj->setActiveFilterIdx(settings.value("currentDjActiveFilter", "0").toInt()); |
| 261 | settings.endGroup(); |
| 262 | } |
| 263 | settings.endGroup(); |
| 264 | p->listDjs->setCurrentRow(0); |
| 265 | |
| 266 | DjWidget* djWidget = static_cast<DjWidget*>(p->listDjs->itemWidget(p->listDjs->currentItem())); |
| 267 | djWidget->activateDJ(); |
| 268 | djWidget->clicked(); |
| 269 | p->listDjs->setItemSelected(p->listDjs->currentItem(), false); |
| 270 | p->listDjs->setSelectionMode(QAbstractItemView::NoSelection); |
| 271 | } |
| 272 | |
| 273 | void DjBrowser::addDj() |
| 274 | { |
| 275 | saveSettings(); |
| 276 | QSettings settings; |
| 277 | settings.setValue("countDJ", p->listDjs->count() + 1); |
| 278 | updateList(); |
| 279 | } |
| 280 | |
| 281 | void DjBrowser::removeDj() |
| 282 | { |
| 283 | if (DjWidget* djWidget = qobject_cast<DjWidget*>(QObject::sender())) { |
| 284 | |
| 285 | // search the Dj which to remove |
| 286 | for (int d = 0; d < p->listDjs->count(); d++) { |
| 287 | if ((DjWidget*)p->listDjs->itemWidget(p->listDjs->item(d)) == djWidget) { |
| 288 | p->listDjs->removeItemWidget(p->listDjs->item(d)); |
| 289 | delete p->listDjs->item(d); |
| 290 | //delete djWidget; |
| 291 | } |
| 292 | } |
| 293 | saveSettings(); |
| 294 | updateList(); |
| 295 | Dj* dj = ((DjWidget*)p->listDjs->itemWidget(p->listDjs->item(0)))->dj(); |
| 296 | Q_EMIT selectionChanged(dj); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void DjBrowser::startDj() |
| 301 | { |
| 302 | qDebug() << Q_FUNC_INFO; |
| 303 | loadDj(); |
| 304 | Q_EMIT selectionStarted(); |
| 305 | } |
| 306 | |
| 307 | void DjBrowser::loadDj() |
| 308 | { |
| 309 | //Fill Filter Widget List |
| 310 | qDebug() << Q_FUNC_INFO; |
| 311 | |
| 312 | p->listDjFilters->clear(); |
| 313 | |
| 314 | DjFilterWidget* djfw; |
| 315 | QListWidgetItem* itm; |
| 316 | int maxFilters = 0; |
| 317 | |
| 318 | // deactivate all Djs |
| 319 | for (int d = 0; d < p->listDjs->count(); d++) |
| 320 | static_cast<DjWidget*>(p->listDjs->itemWidget(p->listDjs->item(d)))->deactivateDJ(); |
| 321 | |
| 322 | // Activate current selected DJ |
| 323 | if (DjWidget* djWidget = qobject_cast<DjWidget*>(QObject::sender())) { |
| 324 | |
| 325 | djWidget->activateDJ(); |
| 326 | Dj* dj = djWidget->dj(); |
| 327 | |
| 328 | // Filters |
| 329 | if (dj->filters().count() == 0) { |
| 330 | //last filter has been removed |
| 331 | Filter* f = new Filter(); |
| 332 | f->setMaxUsage(4); |
| 333 | dj->addFilter(f); |
| 334 | } |
| 335 | |
| 336 | Q_EMIT selectionChanged(dj); |
| 337 | qDebug() << Q_FUNC_INFO << "name=" << dj->name << "filters=" << dj->filters().count(); |
| 338 | |
| 339 | maxFilters = dj->filters().count(); |
| 340 | for (int i = 0; i < maxFilters; i++) { |
| 341 | // |
| 342 | |
| 343 | djfw = new DjFilterWidget(p->listDjFilters); |
| 344 | djfw->setID(QString::number(i + 1)); |
| 345 | djfw->setAllGenres(p->allGenres); |
| 346 | djfw->setAllArtists(p->allArtists); |
| 347 | djfw->setFilter(dj->filters().at(i)); |
| 348 | itm = new QListWidgetItem(p->listDjFilters); |
| 349 | itm->setSizeHint(QSize(0, 75)); |
| 350 | p->listDjFilters->addItem(itm); |
| 351 | p->listDjFilters->setItemWidget(itm, djfw); |
| 352 | connect(djfw, SIGNAL(deleted()), this, SLOT(removeFilter())); |
| 353 | } |
| 354 | |
| 355 | dj->setActiveFilterIdx(0); |
| 356 | p->currentDj = dj; |
| 357 | p->currentDjw = djWidget; |
| 358 | } |
| 359 | |
| 360 | for (int i = 0; i < maxFilters; i++) { |
| 361 | p->currentDj->filters().at(i)->update(); |
| 362 | } |
| 363 | // give a chance to update the labels |
| 364 | qApp->processEvents(); |
| 365 | } |
| 366 | |
| 367 | void DjBrowser::addFilter() |
| 368 | { |
| 369 | Filter* f = new Filter(); |
| 370 | f->setMaxUsage(4); |
| 371 | p->currentDj->addFilter(f); |
| 372 | saveSettings(); |
| 373 | p->currentDjw->clicked(); |
| 374 | } |
| 375 | |
| 376 | void DjBrowser::removeFilter() |
| 377 | { |
| 378 | if (DjFilterWidget* fw = qobject_cast<DjFilterWidget*>(QObject::sender())) { |
| 379 | |
| 380 | // search the Filter which to remove |
| 381 | for (int d = 0; d < p->listDjFilters->count(); d++) { |
| 382 | if ((DjFilterWidget*)p->listDjFilters->itemWidget(p->listDjFilters->item(d)) == fw) { |
| 383 | |
| 384 | p->currentDj->removeFilter(fw->filter()); |
| 385 | |
| 386 | p->listDjFilters->removeItemWidget(p->listDjFilters->item(d)); |
| 387 | delete p->listDjFilters->item(d); |
| 388 | } |
| 389 | } |
| 390 | saveSettings(); |
| 391 | p->currentDjw->clicked(); |
| 392 | } |
| 393 | } |