#include "library/basetracktablemodel.h"
#include <QGuiApplication>
#include <QScreen>
#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
constexpr int BaseTrackTableModel::kBpmColumnPrecisionDefault;
constexpr int BaseTrackTableModel::kBpmColumnPrecisionMinimum;
constexpr int BaseTrackTableModel::kBpmColumnPrecisionMaximum;
int BaseTrackTableModel::s_bpmColumnPrecision =
kBpmColumnPrecisionDefault;
// static
void BaseTrackTableModel::setBpmColumnPrecision(int precision) {
VERIFY_OR_DEBUG_ASSERT(precision >= BaseTrackTableModel::kBpmColumnPrecisionMinimum) {
precision = BaseTrackTableModel::kBpmColumnPrecisionMinimum;
}
VERIFY_OR_DEBUG_ASSERT(precision <= BaseTrackTableModel::kBpmColumnPrecisionMaximum) {
precision = BaseTrackTableModel::kBpmColumnPrecisionMaximum;
}
s_bpmColumnPrecision = precision;
}
//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(