2014-11-12 15:01:08 +01:00
|
|
|
//=============================================================================
|
|
|
|
// MuseScore
|
|
|
|
// Music Composition & Notation
|
|
|
|
//
|
|
|
|
// Copyright (C) 2014 Werner Schweer
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License version 2
|
|
|
|
// as published by the Free Software Foundation and appearing in
|
|
|
|
// the file LICENCE.GPL
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
#include "scoreBrowser.h"
|
2014-11-12 16:36:40 +01:00
|
|
|
#include "musescore.h"
|
2014-11-14 11:44:48 +01:00
|
|
|
#include "icons.h"
|
2014-11-12 15:01:08 +01:00
|
|
|
#include "libmscore/score.h"
|
|
|
|
|
|
|
|
namespace Ms {
|
|
|
|
|
2014-11-19 14:26:20 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// sizeHint
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
QSize ScoreListWidget::sizeHint() const
|
|
|
|
{
|
|
|
|
int cols = (width()-SPACE) / (CELLW + SPACE);
|
2017-11-10 00:22:42 +01:00
|
|
|
int n = 0;
|
|
|
|
for (int i = 0; i < count(); ++i) {
|
|
|
|
if (!item(i)->isHidden())
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:02:41 +01:00
|
|
|
int rows = 1;
|
|
|
|
if (cols > 0)
|
|
|
|
rows = (n+cols-1) / cols;
|
2014-11-19 14:26:20 +01:00
|
|
|
if (rows <= 0)
|
|
|
|
rows = 1;
|
|
|
|
return QSize(cols * CELLW, rows * (CELLH + SPACE) + SPACE);
|
|
|
|
}
|
|
|
|
|
2014-11-12 15:01:08 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// ScoreItem
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
class ScoreItem : public QListWidgetItem
|
|
|
|
{
|
|
|
|
ScoreInfo _info;
|
|
|
|
|
|
|
|
public:
|
2014-12-11 15:11:54 +01:00
|
|
|
ScoreItem(const ScoreInfo& i) : QListWidgetItem(), _info(i) {}
|
2014-11-12 15:01:08 +01:00
|
|
|
const ScoreInfo& info() const { return _info; }
|
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// ScoreBrowser
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
ScoreBrowser::ScoreBrowser(QWidget* parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
setupUi(this);
|
2014-11-19 14:26:20 +01:00
|
|
|
scoreList->setLayout(new QVBoxLayout);
|
|
|
|
scoreList->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
2014-12-16 18:02:41 +01:00
|
|
|
scoreList->layout()->setMargin(0);
|
2017-11-10 00:22:42 +01:00
|
|
|
_noMatchedScoresLabel = new QLabel(tr("There are no templates matching the current search."));
|
|
|
|
_noMatchedScoresLabel->setHidden(true);
|
2018-06-20 21:48:43 +02:00
|
|
|
_noMatchedScoresLabel->setObjectName("noMatchedScoresLabel");
|
2017-11-10 00:22:42 +01:00
|
|
|
scoreList->layout()->addWidget(_noMatchedScoresLabel);
|
2014-11-21 12:52:07 +01:00
|
|
|
connect(preview, SIGNAL(doubleClicked(QString)), SIGNAL(scoreActivated(QString)));
|
2014-12-11 15:11:54 +01:00
|
|
|
if (!_showPreview)
|
|
|
|
preview->setVisible(false);
|
2014-11-19 14:26:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// createScoreList
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2014-12-11 15:11:54 +01:00
|
|
|
ScoreListWidget* ScoreBrowser::createScoreList()
|
2014-11-19 14:26:20 +01:00
|
|
|
{
|
|
|
|
ScoreListWidget* sl = new ScoreListWidget;
|
|
|
|
sl->setWrapping(true);
|
|
|
|
sl->setViewMode(QListView::IconMode);
|
2015-02-20 18:48:58 +01:00
|
|
|
sl->setIconSize(QSize(sl->cellWidth(), sl->cellHeight() - 30));
|
2015-03-05 19:00:18 +01:00
|
|
|
sl->setSpacing(sl->space());
|
2014-11-19 14:26:20 +01:00
|
|
|
sl->setResizeMode(QListView::Adjust);
|
|
|
|
sl->setFlow(QListView::LeftToRight);
|
|
|
|
sl->setMovement(QListView::Static);
|
|
|
|
sl->setTextElideMode(Qt::ElideRight);
|
2014-11-22 18:02:49 +01:00
|
|
|
sl->setWordWrap(true);
|
2014-11-19 14:26:20 +01:00
|
|
|
sl->setUniformItemSizes(true);
|
|
|
|
sl->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
|
|
|
|
sl->setLineWidth(0);
|
|
|
|
sl->setFrameStyle(QFrame::NoFrame | QFrame::Plain);
|
|
|
|
sl->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
sl->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
sl->setLayoutMode(QListView::SinglePass);
|
|
|
|
sl->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
|
2014-12-11 15:11:54 +01:00
|
|
|
if (!_showPreview)
|
|
|
|
sl->setSelectionMode(QAbstractItemView::NoSelection);
|
2014-11-19 14:26:20 +01:00
|
|
|
|
2018-10-29 11:48:13 +01:00
|
|
|
if (!style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick)) {
|
|
|
|
// Set our handler for item clicks only if Qt
|
|
|
|
// doesn't treat a click as an item activation.
|
|
|
|
connect(sl, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(scoreClicked(QListWidgetItem*)), Qt::QueuedConnection);
|
|
|
|
}
|
2014-11-19 14:26:20 +01:00
|
|
|
connect(sl, SIGNAL(itemActivated(QListWidgetItem*)), SLOT(setScoreActivated(QListWidgetItem*)));
|
|
|
|
scoreLists.append(sl);
|
|
|
|
return sl;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// genScoreItem
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2014-12-11 15:11:54 +01:00
|
|
|
ScoreItem* ScoreBrowser::genScoreItem(const QFileInfo& fi, ScoreListWidget* l)
|
2014-11-19 14:26:20 +01:00
|
|
|
{
|
|
|
|
ScoreInfo si(fi);
|
2015-02-06 18:39:13 +01:00
|
|
|
|
2015-02-20 18:48:58 +01:00
|
|
|
QPixmap pm(l->iconSize() * qApp->devicePixelRatio());
|
2015-02-06 18:39:13 +01:00
|
|
|
if (!QPixmapCache::find(fi.filePath(), &pm)) {
|
2015-02-20 18:48:58 +01:00
|
|
|
//load and scale pixmap
|
|
|
|
QPixmap pixmap = mscore->extractThumbnail(fi.filePath());
|
|
|
|
if (pixmap.isNull())
|
|
|
|
pixmap = icons[int(Icons::file_ICON)]->pixmap(QSize(50,60));
|
|
|
|
pixmap = pixmap.scaled(pm.width() - 2, pm.height() - 2, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
|
// draw pixmap and add border
|
|
|
|
pm.fill(Qt::transparent);
|
|
|
|
QPainter painter( &pm );
|
2015-02-25 20:38:48 +01:00
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setRenderHint(QPainter::TextAntialiasing);
|
2015-02-20 18:48:58 +01:00
|
|
|
painter.drawPixmap(0, 0, pixmap);
|
2015-03-03 16:56:52 +01:00
|
|
|
painter.setPen(QPen(QColor(0, 0, 0, 128), 1));
|
2015-02-20 18:48:58 +01:00
|
|
|
painter.setBrush(Qt::white);
|
2015-12-16 10:24:34 +01:00
|
|
|
if (fi.completeBaseName() == "00-Blank" || fi.completeBaseName() == "Create_New_Score") {
|
2015-02-25 20:38:48 +01:00
|
|
|
qreal round = 8.0 * qApp->devicePixelRatio();
|
|
|
|
painter.drawRoundedRect(QRectF(0, 0, pm.width() - 1 , pm.height() - 1), round, round);
|
|
|
|
}
|
2015-02-20 18:48:58 +01:00
|
|
|
else
|
|
|
|
painter.drawRect(0, 0, pm.width() - 1, pm.height() - 1);
|
2015-12-16 10:24:34 +01:00
|
|
|
if (fi.completeBaseName() != "00-Blank")
|
2015-02-20 18:48:58 +01:00
|
|
|
painter.drawPixmap(1, 1, pixmap);
|
|
|
|
painter.end();
|
2015-02-06 18:39:13 +01:00
|
|
|
QPixmapCache::insert(fi.filePath(), pm);
|
|
|
|
}
|
2015-02-20 18:48:58 +01:00
|
|
|
|
2014-11-19 14:26:20 +01:00
|
|
|
si.setPixmap(pm);
|
|
|
|
ScoreItem* item = new ScoreItem(si);
|
|
|
|
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignBottom);
|
2014-11-19 15:25:03 +01:00
|
|
|
|
2015-03-03 16:56:52 +01:00
|
|
|
QFont f = item->font();
|
2017-03-25 12:37:24 +01:00
|
|
|
f.setPixelSize(11);
|
2015-03-03 16:56:52 +01:00
|
|
|
f.setBold(_boldTitle);
|
2015-12-16 10:24:34 +01:00
|
|
|
if (fi.completeBaseName() == "00-Blank") {
|
2015-02-20 18:48:58 +01:00
|
|
|
item->setText(tr("Choose Instruments"));
|
2015-03-03 16:56:52 +01:00
|
|
|
f.setBold(true);
|
|
|
|
}
|
2015-12-16 10:24:34 +01:00
|
|
|
else if (fi.completeBaseName() == "Create_New_Score") {
|
2016-12-31 19:26:25 +01:00
|
|
|
item->setText(tr("Create New Score..."));
|
2015-03-03 16:56:52 +01:00
|
|
|
f.setBold(true);
|
|
|
|
}
|
2015-02-20 18:48:58 +01:00
|
|
|
else {
|
|
|
|
QString s(si.completeBaseName());
|
|
|
|
if (!s.isEmpty() && s[0].isNumber() && _stripNumbers)
|
|
|
|
s = s.mid(3);
|
|
|
|
s = s.replace('_', ' ');
|
|
|
|
item->setText(s);
|
|
|
|
}
|
2014-11-22 18:02:49 +01:00
|
|
|
item->setFont(f);
|
|
|
|
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignTop);
|
2015-02-20 18:48:58 +01:00
|
|
|
item->setIcon(QIcon(pm));
|
2014-12-11 15:11:54 +01:00
|
|
|
item->setSizeHint(l->cellSize());
|
2014-11-19 14:26:20 +01:00
|
|
|
return item;
|
2014-11-12 15:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// setScores
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2015-04-29 11:34:10 +02:00
|
|
|
void ScoreBrowser::setScores(QFileInfoList& s)
|
2014-11-12 15:01:08 +01:00
|
|
|
{
|
2014-11-19 14:26:20 +01:00
|
|
|
qDeleteAll(scoreLists);
|
|
|
|
scoreLists.clear();
|
|
|
|
|
2015-04-29 11:34:10 +02:00
|
|
|
QVBoxLayout* l = static_cast<QVBoxLayout*>(scoreList->layout());
|
2018-06-05 20:17:21 +02:00
|
|
|
QLayoutItem* child;
|
|
|
|
while (l->count()) {
|
|
|
|
child = l->takeAt(0);
|
2018-06-20 21:48:43 +02:00
|
|
|
if (child->widget() != 0) {
|
|
|
|
if (child->widget()->objectName() == "noMatchedScoresLabel") // do not delete
|
|
|
|
continue;
|
2018-06-05 20:17:21 +02:00
|
|
|
delete child->widget();
|
2018-06-20 21:48:43 +02:00
|
|
|
}
|
2018-06-05 20:17:21 +02:00
|
|
|
delete child;
|
|
|
|
}
|
2014-11-19 14:26:20 +01:00
|
|
|
|
2014-12-11 15:11:54 +01:00
|
|
|
ScoreListWidget* sl = 0;
|
2014-11-19 14:26:20 +01:00
|
|
|
|
2017-03-13 09:27:08 +01:00
|
|
|
QStringList filter = { "*.mscz", "*.mscx" };
|
2015-01-12 02:11:47 +01:00
|
|
|
|
2015-04-29 11:34:10 +02:00
|
|
|
if (_showCustomCategory)
|
|
|
|
std::sort(s.begin(), s.end(), [](QFileInfo a, QFileInfo b)->bool { return a.fileName() < b.fileName(); });
|
|
|
|
|
2015-01-12 02:11:47 +01:00
|
|
|
QSet<QString> entries; //to avoid duplicates
|
2018-08-17 15:06:15 +02:00
|
|
|
for (const QFileInfo& fil : s) {
|
|
|
|
if (fil.isDir()) {
|
|
|
|
QString st(fil.fileName());
|
2018-08-14 10:03:49 +02:00
|
|
|
if (!st.isEmpty() && st[0].isNumber() && _stripNumbers)
|
|
|
|
st = st.mid(3);
|
|
|
|
st = st.replace('_', ' ');
|
|
|
|
QLabel* label = new QLabel(st);
|
2014-11-22 18:02:49 +01:00
|
|
|
QFont f = label->font();
|
|
|
|
f.setBold(true);
|
|
|
|
label->setFont(f);
|
|
|
|
static_cast<QVBoxLayout*>(l)->addWidget(label);
|
2018-08-17 15:06:15 +02:00
|
|
|
QDir dir(fil.filePath());
|
2014-11-19 14:26:20 +01:00
|
|
|
sl = createScoreList();
|
2015-04-29 11:34:10 +02:00
|
|
|
l->addWidget(sl);
|
2015-01-12 02:13:06 +01:00
|
|
|
unsigned count = 0; //nbr of entries added
|
2018-08-17 15:06:15 +02:00
|
|
|
for (const QFileInfo& fi : dir.entryInfoList(filter, QDir::Files, QDir::Name)) {
|
2015-02-06 18:39:13 +01:00
|
|
|
if (entries.contains(fi.filePath()))
|
2015-01-12 02:13:06 +01:00
|
|
|
continue;
|
2014-12-11 15:11:54 +01:00
|
|
|
sl->addItem(genScoreItem(fi, sl));
|
2015-01-12 02:13:06 +01:00
|
|
|
count++;
|
2015-01-12 02:11:47 +01:00
|
|
|
entries.insert(fi.filePath());
|
|
|
|
}
|
2015-02-06 18:39:13 +01:00
|
|
|
if (count == 0) {
|
2015-01-12 02:13:06 +01:00
|
|
|
delete label;
|
|
|
|
delete sl;
|
|
|
|
}
|
2014-11-19 14:26:20 +01:00
|
|
|
sl = 0;
|
|
|
|
}
|
2015-02-20 22:28:34 +01:00
|
|
|
}
|
|
|
|
for (const QFileInfo& fi : s) {
|
|
|
|
if (fi.isFile()) {
|
2018-08-14 10:03:49 +02:00
|
|
|
QString st = fi.filePath();
|
|
|
|
if (entries.contains(st))
|
2015-02-20 22:28:34 +01:00
|
|
|
continue;
|
2018-08-14 10:03:49 +02:00
|
|
|
if (st.endsWith(".mscz") || st.endsWith(".mscx")) {
|
2015-02-20 22:28:34 +01:00
|
|
|
if (!sl) {
|
|
|
|
if (_showCustomCategory) {
|
|
|
|
QLabel* label = new QLabel(tr("Custom Templates"));
|
|
|
|
QFont f = label->font();
|
|
|
|
f.setBold(true);
|
|
|
|
label->setFont(f);
|
2015-04-29 11:34:10 +02:00
|
|
|
l->insertWidget(2,label);
|
2015-02-20 22:28:34 +01:00
|
|
|
}
|
|
|
|
sl = createScoreList();
|
2015-04-29 11:34:10 +02:00
|
|
|
l->insertWidget(3,sl);
|
2015-02-20 22:28:34 +01:00
|
|
|
}
|
|
|
|
sl->addItem(genScoreItem(fi, sl));
|
2018-08-14 10:03:49 +02:00
|
|
|
entries.insert(st);
|
2015-02-20 22:28:34 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-12 15:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 10:03:51 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// selectFirst
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void ScoreBrowser::selectFirst()
|
|
|
|
{
|
|
|
|
if (scoreLists.isEmpty())
|
|
|
|
return;
|
|
|
|
ScoreListWidget* w = scoreLists.front();
|
|
|
|
if (w->count() == 0)
|
|
|
|
return;
|
2014-12-11 10:54:16 +01:00
|
|
|
ScoreItem* item = static_cast<ScoreItem*>(w->item(0));
|
|
|
|
w->setCurrentItem(item);
|
|
|
|
preview->setScore(item->info());
|
2014-11-21 10:03:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
2014-12-11 10:54:16 +01:00
|
|
|
// selectLast
|
2014-11-21 10:03:51 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void ScoreBrowser::selectLast()
|
|
|
|
{
|
|
|
|
if (scoreLists.isEmpty())
|
|
|
|
return;
|
|
|
|
ScoreListWidget* w = scoreLists.front();
|
|
|
|
if (w->count() == 0)
|
|
|
|
return;
|
2014-12-11 10:54:16 +01:00
|
|
|
ScoreItem* item = static_cast<ScoreItem*>(w->item(w->count()-1));
|
|
|
|
w->setCurrentItem(item);
|
|
|
|
preview->setScore(item->info());
|
2018-06-20 21:48:43 +02:00
|
|
|
}
|
2017-11-10 00:22:42 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// filter
|
|
|
|
// filter which scores are visible based on searchString
|
|
|
|
//---------------------------------------------------------
|
|
|
|
void ScoreBrowser::filter(const QString &searchString)
|
2018-06-20 21:48:43 +02:00
|
|
|
{
|
2017-11-10 00:22:42 +01:00
|
|
|
int numCategoriesWithMathingScores = 0;
|
|
|
|
|
|
|
|
for (ScoreListWidget* list : scoreLists) {
|
|
|
|
int numMatchedScores = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < list->count(); ++i) {
|
|
|
|
ScoreItem* score = static_cast<ScoreItem*>(list->item(i));
|
|
|
|
QString title = score->text();
|
|
|
|
bool isMatch = title.contains(searchString, Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
score->setHidden(!isMatch);
|
|
|
|
|
|
|
|
if (isMatch)
|
|
|
|
numMatchedScores++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int listindex = scoreList->layout()->indexOf(list);
|
|
|
|
QLayoutItem *label = scoreList->layout()->itemAt(listindex - 1);
|
|
|
|
if (label && label->widget()) {
|
|
|
|
label->widget()->setHidden(numMatchedScores == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
list->setHidden(numMatchedScores == 0);
|
|
|
|
if (numMatchedScores > 0) {
|
|
|
|
numCategoriesWithMathingScores++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_noMatchedScoresLabel->setHidden(numCategoriesWithMathingScores > 0);
|
2018-06-20 21:48:43 +02:00
|
|
|
}
|
2014-11-21 10:03:51 +01:00
|
|
|
|
2014-11-12 15:01:08 +01:00
|
|
|
//---------------------------------------------------------
|
2018-03-15 02:18:21 +01:00
|
|
|
// scoreClicked
|
2014-11-12 15:01:08 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2018-03-15 02:18:21 +01:00
|
|
|
void ScoreBrowser::scoreClicked(QListWidgetItem* current)
|
2014-11-12 15:01:08 +01:00
|
|
|
{
|
|
|
|
if (!current)
|
|
|
|
return;
|
2018-03-15 02:18:21 +01:00
|
|
|
|
2014-11-12 15:01:08 +01:00
|
|
|
ScoreItem* item = static_cast<ScoreItem*>(current);
|
2014-12-11 15:11:54 +01:00
|
|
|
if (!_showPreview)
|
|
|
|
emit scoreActivated(item->info().filePath());
|
|
|
|
else {
|
|
|
|
preview->setScore(item->info());
|
|
|
|
emit scoreSelected(item->info().filePath());
|
|
|
|
|
|
|
|
for (ScoreListWidget* sl : scoreLists) {
|
|
|
|
if (static_cast<QListWidget*>(sl) != item->listWidget()) {
|
|
|
|
sl->clearSelection();
|
|
|
|
}
|
2014-12-11 10:54:16 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-12 15:01:08 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 16:36:40 +01:00
|
|
|
//---------------------------------------------------------
|
2014-11-19 14:26:20 +01:00
|
|
|
// setScoreActivated
|
2014-11-12 16:36:40 +01:00
|
|
|
//---------------------------------------------------------
|
2014-11-12 15:01:08 +01:00
|
|
|
|
2014-11-19 14:26:20 +01:00
|
|
|
void ScoreBrowser::setScoreActivated(QListWidgetItem* val)
|
2014-11-12 15:01:08 +01:00
|
|
|
{
|
2014-11-12 16:36:40 +01:00
|
|
|
ScoreItem* item = static_cast<ScoreItem*>(val);
|
2014-11-19 14:26:20 +01:00
|
|
|
emit scoreActivated(item->info().filePath());
|
2014-11-12 15:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|