advancedsearchdialog.cpp 6 KB
Newer Older
1 2
/**
 * Copyright (C) 2003-2004 Scott Wheeler <wheeler@kde.org>
3
 * Copyright (C) 2017 Michael Pyne <mpyne@kde.org>
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17

18 19
#include "advancedsearchdialog.h"

20
#include <kcombobox.h>
Michael Pyne's avatar
Michael Pyne committed
21
#include <KLocalizedString>
22
#include <KStandardGuiItem>
23

24
#include <QDialogButtonBox>
25
#include <QRadioButton>
26
#include <QLabel>
Laurent Montel's avatar
Laurent Montel committed
27
#include <QHBoxLayout>
Tim Beaulen's avatar
Tim Beaulen committed
28
#include <QVBoxLayout>
29
#include <QGroupBox>
30
#include <QPushButton>
31
#include <QLineEdit>
32

33
#include "collectionlist.h"
34
#include "searchwidget.h"
35

36 37 38 39
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

40
AdvancedSearchDialog::AdvancedSearchDialog(const QString &defaultName,
41
                                           const PlaylistSearch &defaultSearch,
42 43
                                           QWidget *parent) :
    QDialog(parent)
44
{
45 46
    setWindowTitle(i18n("Create Search Playlist"));
    setObjectName(QStringLiteral("juk_advSrchDlg"));
47

48 49
    auto mw = new QVBoxLayout(this);
    setLayout(mw);
Tim Beaulen's avatar
Tim Beaulen committed
50

51 52
    auto box = new QHBoxLayout;
    mw->addLayout(box);
53

54 55 56
    box->addWidget(new QLabel(i18n("Playlist name:")));
    m_playlistNameLineEdit = new QLineEdit(defaultName);
    box->addWidget(m_playlistNameLineEdit);
57

58 59 60
    auto criteriaGroupBox = new QGroupBox(i18n("Search Criteria"));
    mw->addWidget(criteriaGroupBox, 1);
    m_criteriaLayout = new QVBoxLayout(criteriaGroupBox);
61

62
    auto group = new QGroupBox;
63 64 65
    m_matchAnyButton = new QRadioButton(i18n("Match any of the following"));
    m_matchAllButton = new QRadioButton(i18n("Match all of the following"));

66
    QHBoxLayout *hgroupbox = new QHBoxLayout(group);
67 68 69
    hgroupbox->addWidget(m_matchAnyButton);
    hgroupbox->addWidget(m_matchAllButton);

70
    m_criteriaLayout->addWidget(group);
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
    m_criteriaLayout->addStretch(1); // more()/fewer() assume this is here

    QWidget *buttons = new QWidget;
    mw->addWidget(buttons);

    QHBoxLayout *l = new QHBoxLayout(buttons);
    l->setSpacing(5);
    l->setMargin(0);

    const auto &clearGuiItem = KStandardGuiItem::clear();
    QPushButton *clearButton = new QPushButton(clearGuiItem.icon(), clearGuiItem.text());
    connect(clearButton, &QPushButton::clicked,
            this, &AdvancedSearchDialog::clearSearches);
    l->addWidget(clearButton);

    l->addStretch(1);

    m_moreButton = new QPushButton(i18nc("additional search options", "More"));
    connect(m_moreButton, &QPushButton::clicked,
            this, &AdvancedSearchDialog::more);
    l->addWidget(m_moreButton);

    m_fewerButton = new QPushButton(i18n("Fewer"));
    connect(m_fewerButton, &QPushButton::clicked,
            this, &AdvancedSearchDialog::fewer);
    l->addWidget(m_fewerButton);

    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
    mw->addWidget(buttonBox);
102

103
    if(defaultSearch.isNull()) {
104 105
        this->more();
        this->more(); // Create first 2 searches
106 107 108 109
        m_matchAnyButton->setChecked(true);
    }
    else {
        PlaylistSearch::ComponentList components = defaultSearch.components();
110

Laurent Montel's avatar
Laurent Montel committed
111 112
        for(PlaylistSearch::ComponentList::ConstIterator it = components.constBegin();
            it != components.constEnd();
113 114
            ++it)
        {
115
            SearchLine *s = new SearchLine(this);
116 117
            s->setSearchComponent(*it);
            m_searchLines.append(s);
118
            m_criteriaLayout->insertWidget(m_criteriaLayout->count() - 1, s);
119
        }
120

121 122 123 124 125
        if(defaultSearch.searchMode() == PlaylistSearch::MatchAny)
            m_matchAnyButton->setChecked(true);
        else
            m_matchAllButton->setChecked(true);
    }
126

127
    m_playlistNameLineEdit->setFocus();
128 129
}

130 131 132 133 134 135 136 137 138 139 140
////////////////////////////////////////////////////////////////////////////////
// protected slots
////////////////////////////////////////////////////////////////////////////////

void AdvancedSearchDialog::accept()
{
    m_search.clearPlaylists();
    m_search.clearComponents();

    m_search.addPlaylist(CollectionList::instance());

141 142
    for(const auto &searchLine : m_searchLines)
        m_search.addComponent(searchLine->searchComponent());
143 144 145 146

    PlaylistSearch::SearchMode m = PlaylistSearch::SearchMode(!m_matchAnyButton->isChecked());
    m_search.setSearchMode(m);

147 148
    m_playlistName = m_playlistNameLineEdit->text();

149
    QDialog::accept();
150 151
}

152
void AdvancedSearchDialog::clearSearches()
153
{
154 155
    for(auto &searchLine : m_searchLines)
        searchLine->clear();
156 157
}

158 159
void AdvancedSearchDialog::more()
{
160
    SearchLine *searchLine = new SearchLine(this);
161 162
    // inserting it to keep the trailing stretch item at end
    m_criteriaLayout->insertWidget(m_criteriaLayout->count() - 1, searchLine);
163 164 165 166 167 168 169 170
    m_searchLines.append(searchLine);
    searchLine->show();
    updateButtons();
}

void AdvancedSearchDialog::fewer()
{
    SearchLine *searchLine = m_searchLines.last();
171
    m_searchLines.removeAll(searchLine);
172 173 174 175 176 177 178 179 180 181 182 183 184 185
    delete searchLine;
    updateButtons();
}

////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

void AdvancedSearchDialog::updateButtons()
{
    m_moreButton->setEnabled(m_searchLines.count() < 16);
    m_fewerButton->setEnabled(m_searchLines.count() > 1);
}

186
// vim: set et sw=4 tw=0 sta: