#include "library/basetracktablemodel.h"
#include <QScreen>
// for hack to get primary screen instead of view widget's screen
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
#include <QGuiApplication>
#endif
#include "library/bpmdelegate.h"
#include "library/colordelegate.h"
#include "library/coverartcache.h"
#include "library/coverartdelegate.h"
#include "library/dao/trackschema.h"
#include "library/locationdelegate.h"
#include "library/previewbuttondelegate.h"
#include "library/stardelegate.h"
#include "library/starrating.h"
#include "library/trackcollection.h"
#include "library/trackcollectionmanager.h"
#include "mixer/playerinfo.h"
#include "mixer/playermanager.h"
#include "moc_basetracktablemodel.cpp"
#include "track/track.h"
#include "util/assert.h"
#include "util/datetime.h"
#include "util/db/sqlite.h"
#include "util/logger.h"
#include "widget/wlibrary.h"
#include "widget/wtracktableview.h"
namespace {
const mixxx::Logger kLogger("BaseTrackTableModel");
constexpr double kRelativeHeightOfCoverartToolTip =
0.165; // Height of the image for the cover art tooltip (Relative to the available screen size)
const QStringList kDefaultTableColumns = {
LIBRARYTABLE_ALBUM,
LIBRARYTABLE_ALBUMARTIST,
LIBRARYTABLE_ARTIST,
LIBRARYTABLE_BPM,
LIBRARYTABLE_BPM_LOCK,
LIBRARYTABLE_BITRATE,
LIBRARYTABLE_CHANNELS,
LIBRARYTABLE_COLOR,
LIBRARYTABLE_COMMENT,
LIBRARYTABLE_COMPOSER,
LIBRARYTABLE_COVERART,
LIBRARYTABLE_DATETIMEADDED,
LIBRARYTABLE_DURATION,
LIBRARYTABLE_FILETYPE,
LIBRARYTABLE_GENRE,
LIBRARYTABLE_GROUPING,
LIBRARYTABLE_KEY,
TRACKLOCATIONSTABLE_LOCATION,
LIBRARYTABLE_PLAYED,
LIBRARYTABLE_PREVIEW,
LIBRARYTABLE_RATING,
LIBRARYTABLE_REPLAYGAIN,
LIBRARYTABLE_SAMPLERATE,
LIBRARYTABLE_TIMESPLAYED,
LIBRARYTABLE_LAST_PLAYED_AT,
LIBRARYTABLE_TITLE,
LIBRARYTABLE_TRACKNUMBER,
LIBRARYTABLE_YEAR,
};
inline QSqlDatabase cloneDatabase(
const QSqlDatabase& prototype) {
const auto connectionName =
QUuid::createUuid().toString(QUuid::WithoutBraces);
auto cloned = QSqlDatabase::cloneDatabase(
prototype,
connectionName);
DEBUG_ASSERT(cloned.isValid());
if (prototype.isOpen() && !cloned.open()) {
kLogger.warning()
<< "Failed to open cloned database connection"
<< cloned
<< cloned.lastError();
}
return cloned;
}
QSqlDatabase cloneDatabase(
TrackCollectionManager* pTrackCollectionManager) {
VERIFY_OR_DEBUG_ASSERT(pTrackCollectionManager &&
pTrackCollectionManager->internalCollection()) {
return QSqlDatabase();
}
return cloneDatabase(
pTrackCollectionManager->internalCollection()->database());
}
} // anonymous namespace
//static
QStringList BaseTrackTableModel::defaultTableColumns() {
return kDefaultTableColumns;
}
BaseTrackTableModel::BaseTrackTableModel(
QObject* parent,
TrackCollectionManager* pTrackCollectionManager,
const char* settingsNamespace)
: QAbstractTableModel(parent),
TrackModel(cloneDatabase(pTrackCollectionManager), settingsNamespace),
m_pTrackCollectionManager(pTrackCollectionManager),
m_previewDeckGroup(PlayerManager::groupForPreviewDeck(0)),
m_backgroundColorOpacity(WLibrary::kDefaultTrackTableBackgroundColorOpacity) {
connect(&pTrackCollectionManager->internalCollection()->getTrackDAO(),
&TrackDAO::forceModelUpdate,
this,
&BaseTrackTableModel::slotRefreshAllRows);
connect(&PlayerInfo::instance(),
&PlayerInfo::trackChanged,
this,
&BaseTrackTableModel::slotTrackChanged);
}
void BaseTrackTableModel::initTableColumnsAndHeaderProperties(
const QStringList& tableColumns) {
m_columnCache.setColumns(tableColumns);
if (m_columnHeaders.size() < tableColumns.size()) {
m_columnHeaders.resize(tableColumns.size());
}
initHeaderProperties();
}
void BaseTrackTableModel::initHeaderProperties() {
setHeaderProperties(
ColumnCache::COLUMN_LIBRARYTABLE_ALBUM,
tr("Album"),
defaultColumnWidth() * 4);
setHeaderProperties(
ColumnCache::COLUMN_LIBRARYTABLE_ALBUMARTIST,
tr("Album Artist"),
defaultColumnWidth() * 4);
setHeaderProperties(
ColumnCache::COLUMN_LIBRARYTABLE_ARTIST,
tr("Artist"),
defaultColumnWidth() * 4);