| 1 | /* |
| 2 | Copyright (C) 2005-2019 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 "collectionwidget.h" |
| 19 | #include "collectiontree.h" |
| 20 | #include "collectionupdater.h" |
| 21 | #include "modeselector.h" |
| 22 | #include "progressbar.h" |
| 23 | #include "track.h" |
| 24 | |
| 25 | #include <QDropEvent> |
| 26 | #include <QLabel> |
| 27 | #include <QMenu> |
| 28 | #include <QPixmap> |
| 29 | #include <QPushButton> |
| 30 | #include <QTimerEvent> |
| 31 | #include <QVBoxLayout> |
| 32 | #include <QtGui> |
| 33 | |
| 34 | class CollectionWidgetPrivate { |
| 35 | public: |
| 36 | QMenu* actionsMenu; |
| 37 | QAction* actionScan; |
| 38 | CollectionTree* collectiontree; |
| 39 | ProgressBar* progress; |
| 40 | CollectionUpdater* updater; |
| 41 | QTimer* timer; |
| 42 | QToolButton* button; |
| 43 | SearchEdit* searchEdit; |
| 44 | ModeSelector* modeSelect; |
| 45 | }; |
| 46 | |
| 47 | CollectionWidget::CollectionWidget(QWidget* parent) |
| 48 | : QWidget(parent) |
| 49 | { |
| 50 | p = new CollectionWidgetPrivate; |
| 51 | |
| 52 | QPushButton* pushRandom = new QPushButton(); |
| 53 | QPixmap pixmap2(":shuffle.png"); |
| 54 | pushRandom->setIcon(QIcon(pixmap2)); |
| 55 | pushRandom->setIconSize(QSize(27, 27)); |
| 56 | pushRandom->setMaximumWidth(40); |
| 57 | pushRandom->setStyleSheet( |
| 58 | "QPushButton { border: none; padding: 0px; margin-left: 5px;max-height: " |
| 59 | "20px; margin-right: 5px;}"); |
| 60 | pushRandom->setToolTip(tr("Random Tracks")); |
| 61 | |
| 62 | p->searchEdit = new SearchEdit(); |
| 63 | p->searchEdit->setToolTip( |
| 64 | tr("Enter space-separated terms to filter collection")); |
| 65 | |
| 66 | QVBoxLayout* mainLayout = new QVBoxLayout; |
| 67 | QWidget* headWidget = new QWidget(this); |
| 68 | headWidget->setMaximumHeight(38); |
| 69 | |
| 70 | QHBoxLayout* headWidgetLayout = new QHBoxLayout; |
| 71 | headWidgetLayout->setMargin(0); |
| 72 | headWidgetLayout->setSpacing(1); |
| 73 | |
| 74 | headWidgetLayout->addSpacerItem( |
| 75 | new QSpacerItem(10, 0, QSizePolicy::Fixed, QSizePolicy::Expanding)); |
| 76 | headWidgetLayout->addWidget(p->searchEdit); |
| 77 | headWidgetLayout->addWidget(pushRandom); |
| 78 | headWidget->setLayout(headWidgetLayout); |
| 79 | |
| 80 | p->updater = new CollectionUpdater(); |
| 81 | |
| 82 | p->timer = new QTimer(this); |
| 83 | p->timer->setInterval(300); |
| 84 | p->timer->setSingleShot(true); |
| 85 | |
| 86 | p->collectiontree = new CollectionTree(this); |
| 87 | p->modeSelect = new ModeSelector(p->collectiontree); |
| 88 | |
| 89 | headWidget->raise(); |
| 90 | mainLayout->addWidget(headWidget); |
| 91 | |
| 92 | mainLayout->setMargin(0); |
| 93 | mainLayout->setSpacing(0); |
| 94 | |
| 95 | connect(pushRandom, SIGNAL(clicked()), p->collectiontree, |
| 96 | SLOT(triggerRandomSelection())); |
| 97 | |
| 98 | connect(p->modeSelect, SIGNAL(modeChanged(ModeSelector::modeType)), this, |
| 99 | SLOT(onModeSelected(ModeSelector::modeType))); |
| 100 | |
| 101 | connect(p->searchEdit, SIGNAL(textChanged(const QString&)), this, |
| 102 | SLOT(onSetFilterTimeout())); |
| 103 | |
| 104 | connect(p->searchEdit, SIGNAL(trackDropped(QString)), this, |
| 105 | SIGNAL(trackDropped(QString))); |
| 106 | |
| 107 | connect(p->collectiontree, SIGNAL(selectionChanged(QList<Track*>)), this, |
| 108 | SIGNAL(selectionChanged(QList<Track*>))); |
| 109 | |
| 110 | connect(p->collectiontree, SIGNAL(wantLoad(QList<Track*>, QString)), this, |
| 111 | SIGNAL(wantLoad(QList<Track*>, QString))); |
| 112 | |
| 113 | connect(p->updater, SIGNAL(changesDone()), p->collectiontree, |
| 114 | SLOT(createTrunk())); |
| 115 | connect(p->collectiontree, SIGNAL(rescan()), p->updater, SLOT(scan())); |
| 116 | |
| 117 | connect(p->timer, SIGNAL(timeout()), SLOT(onSetFilter())); |
| 118 | |
| 119 | setFocusProxy(p->collectiontree); // default object to get focus |
| 120 | setMaximumWidth(400); |
| 121 | |
| 122 | // Pogressbar for re-read collection |
| 123 | p->progress = new ProgressBar(this); |
| 124 | p->progress->setValue(0); |
| 125 | p->progress->setStyleSheet("* { margin-bottom: 3px; }"); |
| 126 | |
| 127 | mainLayout->addWidget(p->collectiontree); |
| 128 | |
| 129 | // Read config values |
| 130 | QSettings settings; |
| 131 | p->modeSelect->setMode(static_cast<ModeSelector::modeType>( |
| 132 | settings.value("TreeMode", ModeSelector::MODENONE).toUInt())); |
| 133 | |
| 134 | p->collectiontree->createTrunk(); |
| 135 | setLayout(mainLayout); |
| 136 | |
| 137 | connect(p->updater, SIGNAL(progressChanged(int)), p->progress, |
| 138 | SLOT(setValue(int))); |
| 139 | connect(p->progress, SIGNAL(stopped()), p->updater, SLOT(stop())); |
| 140 | p->modeSelect->show(); |
| 141 | } |
| 142 | |
| 143 | CollectionWidget::~CollectionWidget() |
| 144 | { |
| 145 | QSettings settings; |
| 146 | settings.setValue("TreeMode", p->collectiontree->treeMode); |
| 147 | delete p; |
| 148 | } |
| 149 | |
| 150 | void CollectionWidget::scan() |
| 151 | { |
| 152 | if (p->progress->isHidden()) { |
| 153 | QSettings settings; |
| 154 | p->updater->setDirectoryList(settings.value("Dirs").toStringList(), true); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void CollectionWidget::onModeSelected(ModeSelector::modeType value) |
| 159 | { |
| 160 | p->collectiontree->treeMode = static_cast<CollectionTree::modeType>(value); |
| 161 | p->collectiontree->createTrunk(); |
| 162 | } |
| 163 | |
| 164 | void CollectionWidget::onSetFilterTimeout() |
| 165 | { |
| 166 | if (p->timer->isActive()) |
| 167 | p->timer->stop(); |
| 168 | p->timer->start(); |
| 169 | } |
| 170 | |
| 171 | void CollectionWidget::onSetFilter() |
| 172 | { |
| 173 | p->collectiontree->setFilter(p->searchEdit->text()); |
| 174 | p->collectiontree->createTrunk(); |
| 175 | } |
| 176 | |
| 177 | void CollectionWidget::onSetClicked() |
| 178 | { |
| 179 | p->actionsMenu->popup(QCursor::pos(), nullptr); |
| 180 | } |
| 181 | |
| 182 | bool CollectionWidget::hasItems() |
| 183 | { |
| 184 | return (p->collectiontree->topLevelItemCount() > 0); |
| 185 | } |
| 186 | |
| 187 | void CollectionWidget::loadSettings() |
| 188 | { |
| 189 | QSettings settings; |
| 190 | p->updater->setDoMonitor(settings.value("Monitor").toBool()); |
| 191 | p->updater->setDirectoryList(settings.value("Dirs").toStringList()); |
| 192 | } |
| 193 | |
| 194 | void CollectionWidget::setFilterText(QString strFilter) |
| 195 | { |
| 196 | p->searchEdit->setText(strFilter); |
| 197 | onSetFilter(); |
| 198 | } |
| 199 | |
| 200 | QString CollectionWidget::filterText() { return p->searchEdit->text(); } |
| 201 | |
| 202 | void CollectionWidget::resizeEvent(QResizeEvent*) |
| 203 | { |
| 204 | QRect rec = p->collectiontree->geometry(); |
| 205 | #if defined(Q_OS_DARWIN) |
| 206 | int y = 39; |
| 207 | #else |
| 208 | int y = 41; |
| 209 | #endif |
| 210 | p->progress->setGeometry(0, y, rec.width() - 20, 20); |
| 211 | p->modeSelect->setGeometry(rec.width() - p->modeSelect->width(), 0, |
| 212 | p->modeSelect->width(), 20); |
| 213 | } |
| 214 | |
| 215 | SearchEdit::SearchEdit(QWidget* parent) |
| 216 | : QLineEdit(parent) |
| 217 | { |
| 218 | QPixmap searchIcon(":search.png"); |
| 219 | QLabel* lbl = new QLabel(this); |
| 220 | lbl->setScaledContents(true); |
| 221 | lbl->setPixmap(searchIcon); |
| 222 | lbl->setFixedSize(QSize(23, 23)); |
| 223 | lbl->setStyleSheet("QLabel { border: none; padding: 0px; margin-left: 9px; " |
| 224 | "margin-top: 11px; margin-right: 3px}"); |
| 225 | clearButton = new QToolButton(this); |
| 226 | QPixmap pixmap(":clear_left.png"); |
| 227 | clearButton->setIcon(QIcon(pixmap)); |
| 228 | clearButton->setIconSize(QSize(18, 18)); |
| 229 | clearButton->setCursor(Qt::ArrowCursor); |
| 230 | clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; " |
| 231 | "margin-right: 2px; margin-top: 3px;}"); |
| 232 | clearButton->hide(); |
| 233 | setAttribute(Qt::WA_MacShowFocusRect, false); |
| 234 | connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); |
| 235 | connect(this, SIGNAL(textChanged(const QString&)), this, |
| 236 | SLOT(updateCloseButton(const QString&))); |
| 237 | int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); |
| 238 | // setStyleSheet(QString("QLineEdit { padding-right: %1px; } |
| 239 | // ").arg(clearButton->sizeHint().width() + frameWidth + 1)); |
| 240 | QSize msz = minimumSizeHint(); |
| 241 | setMinimumSize( |
| 242 | qMax(msz.width(), clearButton->sizeHint().height() + frameWidth * 2 + 2), |
| 243 | qMax(msz.height(), |
| 244 | clearButton->sizeHint().height() + frameWidth * 2 + 2)); |
| 245 | } |
| 246 | SearchEdit::~SearchEdit() {} |
| 247 | |
| 248 | void SearchEdit::dragEnterEvent(QDragEnterEvent* event) |
| 249 | { |
| 250 | if (event->mimeData()->hasFormat("text/playlistitem")) |
| 251 | event->accept(); |
| 252 | } |
| 253 | |
| 254 | void SearchEdit::dropEvent(QDropEvent* event) |
| 255 | { |
| 256 | if (event->mimeData()->hasFormat("text/playlistitem")) { |
| 257 | |
| 258 | // decode playlistitem |
| 259 | QByteArray itemData = event->mimeData()->data("text/playlistitem"); |
| 260 | QDataStream stream(&itemData, QIODevice::ReadOnly); |
| 261 | QVector<QStringList> tags; |
| 262 | |
| 263 | stream >> tags; |
| 264 | event->setDropAction(Qt::CopyAction); |
| 265 | event->accept(); |
| 266 | |
| 267 | if (tags.count() > 0) { |
| 268 | Track* track = new Track(tags[0]); |
| 269 | this->setText(track->artist()); |
| 270 | } |
| 271 | |
| 272 | } else |
| 273 | event->ignore(); |
| 274 | } |
| 275 | |
| 276 | void SearchEdit::resizeEvent(QResizeEvent*) |
| 277 | { |
| 278 | QSize sz = clearButton->sizeHint(); |
| 279 | int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); |
| 280 | clearButton->move(rect().right() - frameWidth - sz.width(), |
| 281 | (rect().bottom() + 2 - sz.height()) / 2); |
| 282 | } |
| 283 | |
| 284 | // esc key for clean up |
| 285 | void SearchEdit::keyPressEvent(QKeyEvent* e) |
| 286 | { |
| 287 | |
| 288 | if (e->key() == Qt::Key_Escape) |
| 289 | this->setText(""); |
| 290 | else |
| 291 | QLineEdit::keyPressEvent(e); |
| 292 | } |
| 293 | |
| 294 | void SearchEdit::updateCloseButton(const QString& text) |
| 295 | { |
| 296 | clearButton->setVisible(!text.isEmpty()); |
| 297 | } |