1 //=============================================================================
2 //  MuseScore
3 //  Linux Music Score Editor
4 //
5 //  Copyright (C) 2010 Werner Schweer and others
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #include "selinstrument.h"
21 #include "instrdialog.h"
22 #include "musescore.h"
23 
24 #include "libmscore/instrument.h"
25 #include "libmscore/instrtemplate.h"
26 
27 namespace Ms {
28 
29 extern void filterInstruments(QTreeWidget *instrumentList, const QString &searchPhrase = QString(""));
30 
31 //---------------------------------------------------------
32 //   SelectInstrument
33 //---------------------------------------------------------
34 
SelectInstrument(const Instrument * instrument,QWidget * parent)35 SelectInstrument::SelectInstrument(const Instrument* instrument, QWidget* parent)
36    : QDialog(parent)
37       {
38       setObjectName("SelectInstrument");
39       setupUi(this);
40       setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
41       currentInstrument->setText(instrument->trackName());
42       buildTemplateList();
43       buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
44       instrumentSearch->setFilterableView(instrumentList);
45 
46       // get last saved, user-selected instrument genre and set filter to it
47       QSettings settings;
48       settings.beginGroup("selectInstrument");
49       if (!settings.value("selectedGenre").isNull()){
50             QString selectedGenre = settings.value("selectedGenre").value<QString>();
51             instrumentGenreFilter->setCurrentText(selectedGenre);
52             }
53       settings.endGroup();
54 
55       MuseScore::restoreGeometry(this);
56       }
57 
58 //---------------------------------------------------------
59 //   buildTemplateList
60 //---------------------------------------------------------
61 
buildTemplateList()62 void SelectInstrument::buildTemplateList()
63       {
64       // clear search if instrument list is updated
65       instrumentSearch->clear();
66 
67       populateInstrumentList(instrumentList);
68       populateGenreCombo(instrumentGenreFilter);
69       }
70 
71 //---------------------------------------------------------
72 //   expandOrCollapse
73 //---------------------------------------------------------
74 
expandOrCollapse(const QModelIndex & model)75 void SelectInstrument::expandOrCollapse(const QModelIndex &model)
76       {
77       if(instrumentList->isExpanded(model))
78             instrumentList->collapse(model);
79       else
80             instrumentList->expand(model);
81       }
82 
83 //---------------------------------------------------------
84 //   on_instrumentList_itemSelectionChanged
85 //---------------------------------------------------------
86 
on_instrumentList_itemSelectionChanged()87 void SelectInstrument::on_instrumentList_itemSelectionChanged()
88       {
89       QList<QTreeWidgetItem*> wi = instrumentList->selectedItems();
90       bool flag = !wi.isEmpty();
91       buttonBox->button(QDialogButtonBox::Ok)->setEnabled(flag);
92       }
93 
94 //---------------------------------------------------------
95 //   on_instrumentList
96 //---------------------------------------------------------
97 
on_instrumentList_itemDoubleClicked(QTreeWidgetItem *,int)98 void SelectInstrument::on_instrumentList_itemDoubleClicked(QTreeWidgetItem*, int)
99       {
100       QList<QTreeWidgetItem*> wi = instrumentList->selectedItems();
101       if(!wi.isEmpty())
102           done(true);
103       }
104 
105 //---------------------------------------------------------
106 //   instrTemplate
107 //---------------------------------------------------------
108 
instrTemplate() const109 const InstrumentTemplate* SelectInstrument::instrTemplate() const
110       {
111       QList<QTreeWidgetItem*> wi = instrumentList->selectedItems();
112       if (wi.isEmpty())
113             return 0;
114       InstrumentTemplateListItem* item = (InstrumentTemplateListItem*)wi.front();
115       return item->instrumentTemplate();
116       }
117 
118 //---------------------------------------------------------
119 //   on_search_textChanged
120 //---------------------------------------------------------
121 
on_search_textChanged(const QString &)122 void SelectInstrument::on_search_textChanged(const QString&)
123       {
124       // searching is done in Ms::SearchBox so here we just reset the
125       // genre dropdown to ensure that the search includes all genres
126       const int idxAllGenres = 0;
127       if (instrumentGenreFilter->currentIndex() != idxAllGenres) {
128             instrumentGenreFilter->blockSignals(true);
129             instrumentGenreFilter->setCurrentIndex(idxAllGenres);
130             instrumentGenreFilter->blockSignals(false);
131             }
132       }
133 
134 
135 //---------------------------------------------------------
136 //   on_instrumentGenreFilter_currentTextChanged
137 //---------------------------------------------------------
138 
on_instrumentGenreFilter_currentIndexChanged(int index)139 void SelectInstrument::on_instrumentGenreFilter_currentIndexChanged(int index)
140       {
141       QSettings settings;
142       settings.beginGroup("selectInstrument");  // hard coded, since this is also used in instrwidget
143       settings.setValue("selectedGenre", instrumentGenreFilter->currentText());
144       settings.endGroup();
145 
146       QString id = instrumentGenreFilter->itemData(index).toString();
147 
148       // Redisplay tree, only showing items from the selected genre
149       filterInstrumentsByGenre(instrumentList, id);
150       }
151 
152 
153 //---------------------------------------------------------
154 //   filterInstrumentsByGenre
155 //---------------------------------------------------------
156 
filterInstrumentsByGenre(QTreeWidget * instrList,QString genre)157 void SelectInstrument::filterInstrumentsByGenre(QTreeWidget *instrList, QString genre)
158       {
159       QTreeWidgetItemIterator iList(instrList);
160       while (*iList) {
161             (*iList)->setHidden(true);
162             InstrumentTemplateListItem* itli = static_cast<InstrumentTemplateListItem*>(*iList);
163             InstrumentTemplate *it=itli->instrumentTemplate();
164 
165             if(it) {
166                   if (genre == "all" || it->genreMember(genre)) {
167                         (*iList)->setHidden(false);
168 
169                         QTreeWidgetItem *iParent = (*iList)->parent();
170                         while(iParent) {
171                               if(!iParent->isHidden())
172                                     break;
173 
174                               iParent->setHidden(false);
175                               iParent = iParent->parent();
176                               }
177                         }
178                   }
179             ++iList;
180             }
181       }
182 
183 //---------------------------------------------------------
184 //   hideEvent
185 //---------------------------------------------------------
186 
hideEvent(QHideEvent * event)187 void SelectInstrument::hideEvent(QHideEvent* event)
188       {
189       MuseScore::saveGeometry(this);
190       QWidget::hideEvent(event);
191       }
192 
193 }
194 
195