File indexing completed on 2023-05-30 11:30:44

0001 /**
0002  * Copyright (C) 2003-2004 Scott Wheeler <wheeler@kde.org>
0003  * Copyright (C) 2017 Michael Pyne <mpyne@kde.org>
0004  *
0005  * This program is free software; you can redistribute it and/or modify it under
0006  * the terms of the GNU General Public License as published by the Free Software
0007  * Foundation; either version 2 of the License, or (at your option) any later
0008  * version.
0009  *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License along with
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "advancedsearchdialog.h"
0019 
0020 #include <kcombobox.h>
0021 #include <KLocalizedString>
0022 #include <KStandardGuiItem>
0023 
0024 #include <QDialogButtonBox>
0025 #include <QRadioButton>
0026 #include <QLabel>
0027 #include <QHBoxLayout>
0028 #include <QVBoxLayout>
0029 #include <QGroupBox>
0030 #include <QPushButton>
0031 #include <QLineEdit>
0032 
0033 #include "collectionlist.h"
0034 #include "searchwidget.h"
0035 
0036 ////////////////////////////////////////////////////////////////////////////////
0037 // public methods
0038 ////////////////////////////////////////////////////////////////////////////////
0039 
0040 AdvancedSearchDialog::AdvancedSearchDialog(const QString &defaultName,
0041                                            PlaylistSearch &defaultSearch,
0042                                            QWidget *parent) :
0043     QDialog(parent),
0044     m_search(&defaultSearch)
0045 {
0046     setWindowTitle(i18n("Create Search Playlist"));
0047     setObjectName(QStringLiteral("juk_advSrchDlg"));
0048 
0049     auto mw = new QVBoxLayout(this);
0050     setLayout(mw);
0051 
0052     auto box = new QHBoxLayout;
0053     mw->addLayout(box);
0054 
0055     box->addWidget(new QLabel(i18n("Playlist name:")));
0056     m_playlistNameLineEdit = new QLineEdit(defaultName);
0057     box->addWidget(m_playlistNameLineEdit);
0058 
0059     auto criteriaGroupBox = new QGroupBox(i18n("Search Criteria"));
0060     mw->addWidget(criteriaGroupBox, 1);
0061     m_criteriaLayout = new QVBoxLayout(criteriaGroupBox);
0062 
0063     auto group = new QGroupBox;
0064     m_matchAnyButton = new QRadioButton(i18n("Match any of the following"));
0065     m_matchAllButton = new QRadioButton(i18n("Match all of the following"));
0066 
0067     QHBoxLayout *hgroupbox = new QHBoxLayout(group);
0068     hgroupbox->addWidget(m_matchAnyButton);
0069     hgroupbox->addWidget(m_matchAllButton);
0070 
0071     m_criteriaLayout->addWidget(group);
0072     m_criteriaLayout->addStretch(1); // more()/fewer() assume this is here
0073 
0074     QWidget *buttons = new QWidget;
0075     mw->addWidget(buttons);
0076 
0077     QHBoxLayout *l = new QHBoxLayout(buttons);
0078     l->setSpacing(5);
0079     l->setContentsMargins(0, 0, 0, 0);
0080 
0081     const auto &clearGuiItem = KStandardGuiItem::clear();
0082     QPushButton *clearButton = new QPushButton(clearGuiItem.icon(), clearGuiItem.text());
0083     connect(clearButton, &QPushButton::clicked,
0084             this, &AdvancedSearchDialog::clearSearches);
0085     l->addWidget(clearButton);
0086 
0087     l->addStretch(1);
0088 
0089     m_moreButton = new QPushButton(i18nc("additional search options", "More"));
0090     connect(m_moreButton, &QPushButton::clicked,
0091             this, &AdvancedSearchDialog::more);
0092     l->addWidget(m_moreButton);
0093 
0094     m_fewerButton = new QPushButton(i18n("Fewer"));
0095     connect(m_fewerButton, &QPushButton::clicked,
0096             this, &AdvancedSearchDialog::fewer);
0097     l->addWidget(m_fewerButton);
0098 
0099     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0100     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0101     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0102     mw->addWidget(buttonBox);
0103 
0104     if(defaultSearch.isNull()) {
0105         this->more();
0106         this->more(); // Create first 2 searches
0107         m_matchAnyButton->setChecked(true);
0108     }
0109     else {
0110         PlaylistSearch::ComponentList components = defaultSearch.components();
0111 
0112         for(PlaylistSearch::ComponentList::ConstIterator it = components.constBegin();
0113             it != components.constEnd();
0114             ++it)
0115         {
0116             SearchLine *s = new SearchLine(this);
0117             s->setSearchComponent(*it);
0118             m_searchLines.append(s);
0119             m_criteriaLayout->insertWidget(m_criteriaLayout->count() - 1, s);
0120         }
0121 
0122         if(defaultSearch.searchMode() == PlaylistSearch::MatchAny)
0123             m_matchAnyButton->setChecked(true);
0124         else
0125             m_matchAllButton->setChecked(true);
0126     }
0127 
0128     m_playlistNameLineEdit->setFocus();
0129 }
0130 
0131 ////////////////////////////////////////////////////////////////////////////////
0132 // protected slots
0133 ////////////////////////////////////////////////////////////////////////////////
0134 
0135 void AdvancedSearchDialog::accept()
0136 {
0137     m_search->clearPlaylists();
0138     m_search->clearComponents();
0139 
0140     m_search->addPlaylist(CollectionList::instance());
0141 
0142     for(const auto &searchLine : qAsConst(m_searchLines))
0143         m_search->addComponent(searchLine->searchComponent());
0144 
0145     PlaylistSearch::SearchMode m = PlaylistSearch::SearchMode(!m_matchAnyButton->isChecked());
0146     m_search->setSearchMode(m);
0147 
0148     m_playlistName = m_playlistNameLineEdit->text();
0149 
0150     QDialog::accept();
0151 }
0152 
0153 void AdvancedSearchDialog::clearSearches()
0154 {
0155     for(auto &searchLine : m_searchLines)
0156         searchLine->clear();
0157 }
0158 
0159 void AdvancedSearchDialog::more()
0160 {
0161     SearchLine *searchLine = new SearchLine(this);
0162     // inserting it to keep the trailing stretch item at end
0163     m_criteriaLayout->insertWidget(m_criteriaLayout->count() - 1, searchLine);
0164     m_searchLines.append(searchLine);
0165     searchLine->show();
0166     updateButtons();
0167 }
0168 
0169 void AdvancedSearchDialog::fewer()
0170 {
0171     SearchLine *searchLine = m_searchLines.last();
0172     m_searchLines.removeAll(searchLine);
0173     delete searchLine;
0174     updateButtons();
0175 }
0176 
0177 ////////////////////////////////////////////////////////////////////////////////
0178 // private methods
0179 ////////////////////////////////////////////////////////////////////////////////
0180 
0181 void AdvancedSearchDialog::updateButtons()
0182 {
0183     m_moreButton->setEnabled(m_searchLines.count() < 16);
0184     m_fewerButton->setEnabled(m_searchLines.count() > 1);
0185 }
0186 
0187 // vim: set et sw=4 tw=0 sta: