Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use checkbox menu in crate selection. #1341

Merged
merged 7 commits into from
Dec 23, 2017
Merged
36 changes: 36 additions & 0 deletions src/library/crate/cratestorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,42 @@ CrateTrackSelectResult CrateStorage::selectTrackCratesSorted(TrackId trackId) co
}
}

CrateSummarySelectResult CrateStorage::selectCratesWithTrackCount(const QList<TrackId>& trackIds) const {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: extra blank line here

// unfortunatelly we can't bind a QList to a SqlQuery therefor we have to construct a String first
// see: https://stackoverflow.com/questions/3220357/qt-how-to-bind-a-qlist-to-a-qsqlquery-with-a-where-in-clause
// Using bindValue did not work, only constructing the SQL string befor.
// As TrackId is a int, we are safe here
QStringList idstrings;
foreach(TrackId id, trackIds) {
Copy link
Contributor

@Be-ing Be-ing Nov 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use standard C++11 for loop instead of old Qt foreach macro: for (TrackId id : trackIds) {

idstrings << id.toString();
}
QString numberlist = idstrings.join(",");
QString ids = QString("%1").arg(numberlist);

FwdSqlQuery query(m_database, QString(
"SELECT *, ("
" SELECT COUNT(*) FROM %1 WHERE %2.%3 = %1.%4 and %1.%5 in (%9)"
" ) AS %6, 0 as %7 FROM %2 ORDER BY %8").arg(
CRATE_TRACKS_TABLE,
CRATE_TABLE,
CRATETABLE_ID,
CRATETRACKSTABLE_CRATEID,
CRATETRACKSTABLE_TRACKID,
CRATESUMMARY_TRACK_COUNT,
CRATESUMMARY_TRACK_DURATION,
CRATETABLE_NAME,
ids));

if (query.execPrepared()) {
return CrateSummarySelectResult(std::move(query));
} else {
return CrateSummarySelectResult();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra blank line

}



CrateTrackSelectResult CrateStorage::selectTracksSortedByCrateNameLike(const QString& crateNameLike) const {
FwdSqlQuery query(m_database, QString(
Expand Down
3 changes: 2 additions & 1 deletion src/library/crate/cratestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ class CrateStorage: public virtual /*implements*/ SqlStorage {
CrateId crateId) const;
CrateTrackSelectResult selectTrackCratesSorted(
TrackId trackId) const;
CrateSummarySelectResult selectCratesWithTrackCount(
const QList<TrackId>& trackIds) const;
CrateTrackSelectResult selectTracksSortedByCrateNameLike(
const QString& crateNameLike) const;

Expand All @@ -284,7 +286,6 @@ class CrateStorage: public virtual /*implements*/ SqlStorage {
QSet<CrateId> collectCrateIdsOfTracks(
const QList<TrackId>& trackIds) const;


/////////////////////////////////////////////////////////////////////////
// CrateSummary view operations (read-only, const)
/////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions src/util/parented_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ inline bool operator== (const parented_ptr<T>& lhs, const U* rhs) {
}

template<typename T, typename U>
inline bool operator== (const parented_ptr<T>& lhs, const parented_ptr<U>& rhs) const {
inline bool operator== (const parented_ptr<T>& lhs, const parented_ptr<U>& rhs) {
return lhs.get() == rhs.get();
}

Expand All @@ -115,7 +115,7 @@ inline bool operator!= (const parented_ptr<T>& lhs, const U* rhs) {
}

template<typename T, typename U>
inline bool operator!= (const parented_ptr<T>& lhs, const parented_ptr<U>& rhs) const {
inline bool operator!= (const parented_ptr<T>& lhs, const parented_ptr<U>& rhs) {
return !(lhs.get() == rhs.get());
}

Expand Down
92 changes: 76 additions & 16 deletions src/widget/wtracktableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <QUrl>
#include <QDrag>
#include <QShortcut>
#include <QWidgetAction>
#include <QCheckBox>

#include "widget/wtracktableview.h"

Expand All @@ -26,6 +28,7 @@
#include "util/dnd.h"
#include "util/time.h"
#include "util/assert.h"
#include "util/parented_ptr.h"

WTrackTableView::WTrackTableView(QWidget * parent,
UserSettingsPointer pConfig,
Expand Down Expand Up @@ -69,7 +72,7 @@ WTrackTableView::WTrackTableView(QWidget * parent,
m_pPlaylistMenu = new QMenu(this);
m_pPlaylistMenu->setTitle(tr("Add to Playlist"));
m_pCrateMenu = new QMenu(this);
m_pCrateMenu->setTitle(tr("Add to Crate"));
m_pCrateMenu->setTitle(tr("Crates"));
m_pBPMMenu = new QMenu(this);
m_pBPMMenu->setTitle(tr("BPM Options"));
m_pCoverMenu = new WCoverArtMenu(this);
Expand All @@ -93,8 +96,9 @@ WTrackTableView::WTrackTableView(QWidget * parent,

connect(&m_playlistMapper, SIGNAL(mapped(int)),
this, SLOT(addSelectionToPlaylist(int)));
connect(&m_crateMapper, SIGNAL(mapped(int)),
this, SLOT(addSelectionToCrate(int)));

connect(&m_crateMapper, SIGNAL(mapped(QWidget *)),
this, SLOT(addRemoveSelectionInCrate(QWidget *)));

m_pCOTGuiTick = new ControlProxy("[Master]", "guiTick50ms", this);
m_pCOTGuiTick->connectValueChanged(SLOT(slotGuiTick50ms(double)));
Expand Down Expand Up @@ -827,21 +831,44 @@ void WTrackTableView::contextMenuEvent(QContextMenuEvent* event) {

if (modelHasCapabilities(TrackModel::TRACKMODELCAPS_ADDTOCRATE)) {
m_pCrateMenu->clear();
CrateSelectResult allCrates(m_pTrackCollection->crates().selectCrates());
Crate crate;
const QList<TrackId> trackIds = getSelectedTrackIds();

CrateSummarySelectResult allCrates(m_pTrackCollection->crates().selectCratesWithTrackCount(trackIds));

CrateSummary crate;
while (allCrates.populateNext(&crate)) {
auto pAction = std::make_unique<QAction>(crate.getName(), m_pCrateMenu);
auto pAction = make_parented<QWidgetAction>(m_pCrateMenu);
auto pCheckBox = make_parented<QCheckBox>(m_pCrateMenu);

pCheckBox->setText(crate.getName());
pCheckBox->setProperty("crateId",
QVariant::fromValue(crate.getId()));
pCheckBox->setEnabled(!crate.isLocked());
pAction->setEnabled(!crate.isLocked());
m_crateMapper.setMapping(pAction.get(), crate.getId().toInt());
connect(pAction.get(), SIGNAL(triggered()), &m_crateMapper, SLOT(map()));
pAction->setDefaultWidget(pCheckBox.get());

if(crate.getTrackCount() == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (

pCheckBox->setChecked(false);
} else if(crate.getTrackCount() == (uint)trackIds.length()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else if (

pCheckBox->setChecked(true);
} else {
pCheckBox->setTristate(true);
pCheckBox->setCheckState(Qt::PartiallyChecked);
}

m_crateMapper.setMapping(pAction.get(), pCheckBox.get());
m_crateMapper.setMapping(pCheckBox.get(), pCheckBox.get());
m_pCrateMenu->addAction(pAction.get());
pAction.release();
connect(pAction.get(), SIGNAL(triggered()),
&m_crateMapper, SLOT(map()));
connect(pCheckBox.get(), SIGNAL(stateChanged(int)),
&m_crateMapper, SLOT(map()));

}
m_pCrateMenu->addSeparator();
QAction* newCrateAction = new QAction(tr("Create New Crate"), m_pCrateMenu);
m_pCrateMenu->addAction(newCrateAction);
m_crateMapper.setMapping(newCrateAction, CrateId().toInt());// invalid crate id for new crate
connect(newCrateAction, SIGNAL(triggered()), &m_crateMapper, SLOT(map()));
connect(newCrateAction, SIGNAL(triggered()), this, SLOT(addSelectionToNewCrate()));

m_pMenu->addMenu(m_pCrateMenu);
}
Expand Down Expand Up @@ -1466,22 +1493,55 @@ void WTrackTableView::addSelectionToPlaylist(int iPlaylistId) {
playlistDao.appendTracksToPlaylist(trackIds, iPlaylistId);
}

void WTrackTableView::addSelectionToCrate(int iCrateId) {
void WTrackTableView::addRemoveSelectionInCrate(QWidget* pWidget) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function name is ambiguous. Does it add or does it remove?

auto pCheckBox = qobject_cast<QCheckBox*>(pWidget);
VERIFY_OR_DEBUG_ASSERT(pCheckBox) {
qWarning() << "crateId is not ef CrateId type";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"not ef"? Did you mean "not of"?

return;
}
CrateId crateId = pCheckBox->property("crateId").value<CrateId>();

const QList<TrackId> trackIds = getSelectedTrackIds();

if (trackIds.isEmpty()) {
qWarning() << "No tracks selected for crate";
return;
}

CrateId crateId(iCrateId);
if (!crateId.isValid()) { // i.e. a new crate is suppose to be created
crateId = CrateFeatureHelper(
m_pTrackCollection, m_pConfig).createEmptyCrate();
// we need to disable tristate again as the mixed state will now be gone and can't be brought back
pCheckBox->setTristate(false);
if(!pCheckBox->isChecked()) {
if (crateId.isValid()) {
m_pTrackCollection->removeCrateTracks(crateId, trackIds);
}
} else {
if (!crateId.isValid()) { // i.e. a new crate is suppose to be created
crateId = CrateFeatureHelper(
m_pTrackCollection, m_pConfig).createEmptyCrate();
}
if (crateId.isValid()) {
m_pTrackCollection->unhideTracks(trackIds);
m_pTrackCollection->addCrateTracks(crateId, trackIds);
}
}
}

void WTrackTableView::addSelectionToNewCrate() {
const QList<TrackId> trackIds = getSelectedTrackIds();

if (trackIds.isEmpty()) {
qWarning() << "No tracks selected for crate";
return;
}

CrateId crateId = CrateFeatureHelper(
m_pTrackCollection, m_pConfig).createEmptyCrate();

if (crateId.isValid()) {
m_pTrackCollection->unhideTracks(trackIds);
m_pTrackCollection->addCrateTracks(crateId, trackIds);
}

}

void WTrackTableView::doSortByColumn(int headerSection) {
Expand Down
3 changes: 2 additions & 1 deletion src/widget/wtracktableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class WTrackTableView : public WLibraryTableView {
void slotReloadTrackMetadata();
void slotResetPlayed();
void addSelectionToPlaylist(int iPlaylistId);
void addSelectionToCrate(int iCrateId);
void addRemoveSelectionInCrate(QWidget* qc);
void addSelectionToNewCrate();
void loadSelectionToGroup(QString group, bool play = false);
void doSortByColumn(int headerSection);
void slotLockBpm();
Expand Down