1
0
mirror of https://github.com/strawberrymusicplayer/strawberry synced 2024-12-16 10:38:53 +01:00

Minor fixes to collection pixmap cache

- Add variables for cache size defaults
- Increase default disk cache size
- Change the pixmap cache settings UI to look better
- Add current pixmap disk cache used to settings
This commit is contained in:
Jonas Kvinge 2020-04-29 00:33:38 +02:00
parent 9cc6a94353
commit a835a4a2f7
5 changed files with 227 additions and 123 deletions

View File

@ -74,7 +74,7 @@ using std::placeholders::_2;
const char *CollectionModel::kSavedGroupingsSettingsGroup = "SavedGroupings";
const int CollectionModel::kPrettyCoverSize = 32;
const char *CollectionModel::kPixmapDiskCacheDir = "/pixmapcache";
const char *CollectionModel::kPixmapDiskCacheDir = "pixmapcache";
QNetworkDiskCache *CollectionModel::sIconCache = nullptr;
@ -114,17 +114,18 @@ CollectionModel::CollectionModel(CollectionBackend *backend, Application *app, Q
cover_loader_options_.pad_output_image_ = true;
cover_loader_options_.scale_output_image_ = true;
if (app_)
if (app_) {
connect(app_->album_cover_loader(), SIGNAL(AlbumCoverLoaded(quint64, AlbumCoverLoaderResult)), SLOT(AlbumCoverLoaded(quint64, AlbumCoverLoaderResult)));
}
QIcon nocover = IconLoader::Load("cdcase");
no_cover_icon_ = nocover.pixmap(nocover.availableSizes().last()).scaled(kPrettyCoverSize, kPrettyCoverSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
//no_cover_icon_ = QPixmap(":/pictures/noalbumart.png").scaled(kPrettyCoverSize, kPrettyCoverSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
// When running under gdb, all calls to this constructor came from the same thread.
// If this ever changes, these two lines might need to be protected by a mutex.
if (sIconCache == nullptr)
if (sIconCache == nullptr) {
sIconCache = new QNetworkDiskCache(this);
sIconCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + kPixmapDiskCacheDir);
}
connect(backend_, SIGNAL(SongsDiscovered(SongList)), SLOT(SongsDiscovered(SongList)));
connect(backend_, SIGNAL(SongsDeleted(SongList)), SLOT(SongsDeleted(SongList)));
@ -186,16 +187,16 @@ void CollectionModel::ReloadSettings() {
use_disk_cache_ = s.value(CollectionSettingsPage::kSettingsDiskCacheEnable, false).toBool();
if (!use_disk_cache_) {
sIconCache->clear();
}
QPixmapCache::setCacheLimit(MaximumCacheSize(&s, CollectionSettingsPage::kSettingsCacheSize, CollectionSettingsPage::kSettingsCacheSizeUnit, CollectionSettingsPage::kSettingsCacheSizeDefault) / 1024);
sIconCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + kPixmapDiskCacheDir);
sIconCache->setMaximumCacheSize(MaximumCacheSize(&s, CollectionSettingsPage::kSettingsDiskCacheSize, CollectionSettingsPage::kSettingsDiskCacheSizeUnit));
QPixmapCache::setCacheLimit(MaximumCacheSize(&s, CollectionSettingsPage::kSettingsCacheSize, CollectionSettingsPage::kSettingsCacheSizeUnit) / 1024);
sIconCache->setMaximumCacheSize(MaximumCacheSize(&s, CollectionSettingsPage::kSettingsDiskCacheSize, CollectionSettingsPage::kSettingsDiskCacheSizeUnit, CollectionSettingsPage::kSettingsDiskCacheSizeDefault));
s.endGroup();
if (!use_disk_cache_) {
ClearDiskCache();
}
}
void CollectionModel::Init(bool async) {
@ -642,7 +643,7 @@ void CollectionModel::AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderR
QNetworkCacheMetaData item_metadata;
item_metadata.setSaveToDisk(true);
item_metadata.setUrl(QUrl(cache_key));
QIODevice* cache = sIconCache->prepare(item_metadata);
QIODevice *cache = sIconCache->prepare(item_metadata);
if (cache) {
result.image_scaled.save(cache, "XPM");
sIconCache->insert(cache);
@ -1538,9 +1539,9 @@ bool CollectionModel::CompareItems(const CollectionItem *a, const CollectionItem
}
int CollectionModel::MaximumCacheSize(QSettings *s, const char *size_id, const char *size_unit_id) const {
int CollectionModel::MaximumCacheSize(QSettings *s, const char *size_id, const char *size_unit_id, const int cache_size_default) const {
int size = s->value(size_id, 80).toInt();
int size = s->value(size_id, cache_size_default).toInt();
int unit = s->value(size_unit_id, CollectionSettingsPage::CacheSizeUnit::CacheSizeUnit_MB).toInt() + 1;
do {

View File

@ -41,6 +41,7 @@
#include <QImage>
#include <QIcon>
#include <QPixmap>
#include <QNetworkDiskCache>
#include "core/simpletreemodel.h"
#include "core/song.h"
@ -51,7 +52,6 @@
#include "covermanager/albumcoverloaderoptions.h"
class QSettings;
class QNetworkDiskCache;
class Application;
class CollectionBackend;
@ -177,7 +177,9 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
static QString SortTextForYear(int year);
static QString SortTextForBitrate(int bitrate);
signals:
quint64 icon_cache_disk_size() { return sIconCache->cacheSize(); }
signals:
void TotalSongCountUpdated(int count);
void TotalArtistCountUpdated(int count);
void TotalAlbumCountUpdated(int count);
@ -248,7 +250,7 @@ signals:
QVariant AlbumIcon(const QModelIndex &idx);
QVariant data(const CollectionItem *item, int role) const;
bool CompareItems(const CollectionItem *a, const CollectionItem *b) const;
int MaximumCacheSize(QSettings *s, const char *size_id, const char *size_unit_id) const;
int MaximumCacheSize(QSettings *s, const char *size_id, const char *size_unit_id, const int cache_size_default) const;
private:
CollectionBackend *backend_;

View File

@ -41,6 +41,8 @@
#include "core/application.h"
#include "core/iconloader.h"
#include "core/utilities.h"
#include "collection/collectionmodel.h"
#include "collection/collectiondirectorymodel.h"
#include "collectionsettingspage.h"
#include "playlist/playlistdelegates.h"
@ -54,6 +56,8 @@ const char *CollectionSettingsPage::kSettingsCacheSizeUnit = "cache_size_unit";
const char *CollectionSettingsPage::kSettingsDiskCacheEnable = "disk_cache_enable";
const char *CollectionSettingsPage::kSettingsDiskCacheSize = "disk_cache_size";
const char *CollectionSettingsPage::kSettingsDiskCacheSizeUnit = "disk_cache_size_unit";
const int CollectionSettingsPage::kSettingsCacheSizeDefault = 160;
const int CollectionSettingsPage::kSettingsDiskCacheSizeDefault = 360;
const QStringList CollectionSettingsPage::cacheUnitNames = { "KB", "MB", "GB", "TB" };
@ -70,6 +74,9 @@ CollectionSettingsPage::CollectionSettingsPage(SettingsDialog *dialog)
setWindowIcon(IconLoader::Load("library-music"));
ui_->add->setIcon(IconLoader::Load("document-open-folder"));
ui_->combobox_cache_size->addItems(cacheUnitNames);
ui_->combobox_disk_cache_size->addItems(cacheUnitNames);
connect(ui_->add, SIGNAL(clicked()), SLOT(Add()));
connect(ui_->remove, SIGNAL(clicked()), SLOT(Remove()));
@ -77,6 +84,10 @@ CollectionSettingsPage::CollectionSettingsPage(SettingsDialog *dialog)
connect(ui_->radiobutton_cover_hash, SIGNAL(toggled(bool)), SLOT(CoverSaveInAlbumDirChanged()));
connect(ui_->radiobutton_cover_pattern, SIGNAL(toggled(bool)), SLOT(CoverSaveInAlbumDirChanged()));
connect(ui_->checkbox_disk_cache, SIGNAL(stateChanged(int)), SLOT(DiskCacheEnable(int)));
connect(ui_->button_clear_disk_cache, SIGNAL(clicked()), dialog->app(), SIGNAL(ClearPixmapDiskCache()));
connect(ui_->button_clear_disk_cache, SIGNAL(clicked()), SLOT(ClearPixmapDiskCache()));
}
CollectionSettingsPage::~CollectionSettingsPage() { delete ui_; }
@ -104,12 +115,16 @@ void CollectionSettingsPage::CurrentRowChanged(const QModelIndex& index) {
ui_->remove->setEnabled(index.isValid());
}
void CollectionSettingsPage::DiskCacheEnable(int state) {
void CollectionSettingsPage::DiskCacheEnable(const int state) {
bool checked = state == Qt::Checked;
ui_->button_disk_cache->setEnabled(checked);
ui_->label_disk_cache_size->setEnabled(checked);
ui_->spinbox_disk_cache_size->setEnabled(checked);
ui_->combobox_disk_cache_size->setEnabled(checked);
ui_->label_disk_cache_in_use->setEnabled(checked);
ui_->disk_cache_in_use->setEnabled(checked);
ui_->button_clear_disk_cache->setEnabled(checked);
}
void CollectionSettingsPage::Load() {
@ -151,22 +166,18 @@ void CollectionSettingsPage::Load() {
ui_->checkbox_cover_lowercase->setChecked(s.value("cover_lowercase", true).toBool());
ui_->checkbox_cover_replace_spaces->setChecked(s.value("cover_replace_spaces", true).toBool());
ui_->spinbox_cache_size->setValue(s.value(kSettingsCacheSize, 80).toInt());
ui_->combobox_cache_size->addItems(cacheUnitNames);
ui_->spinbox_cache_size->setValue(s.value(kSettingsCacheSize, kSettingsCacheSizeDefault).toInt());
ui_->combobox_cache_size->setCurrentIndex(s.value(kSettingsCacheSizeUnit, (int) CacheSizeUnit_MB).toInt());
ui_->checkbox_disk_cache->setChecked(s.value(kSettingsDiskCacheEnable, false).toBool());
ui_->label_disk_cache_size->setEnabled(ui_->checkbox_disk_cache->isChecked());
ui_->spinbox_disk_cache_size->setEnabled(ui_->checkbox_disk_cache->isChecked());
ui_->spinbox_disk_cache_size->setValue(s.value(kSettingsDiskCacheSize, 80).toInt());
ui_->combobox_disk_cache_size->setEnabled(ui_->checkbox_disk_cache->isChecked());
ui_->combobox_disk_cache_size->addItems(cacheUnitNames);
ui_->spinbox_disk_cache_size->setValue(s.value(kSettingsDiskCacheSize, kSettingsDiskCacheSizeDefault).toInt());
ui_->combobox_disk_cache_size->setCurrentIndex(s.value(kSettingsDiskCacheSizeUnit, (int) CacheSizeUnit_MB).toInt());
connect(ui_->checkbox_disk_cache, SIGNAL(stateChanged(int)), SLOT(DiskCacheEnable(int)));
connect(ui_->button_disk_cache, SIGNAL(clicked()), dialog()->app(), SIGNAL(ClearPixmapDiskCache()));
s.endGroup();
DiskCacheEnable(ui_->checkbox_disk_cache->checkState());
ui_->disk_cache_in_use->setText((dialog()->app()->collection_model()->icon_cache_disk_size() == 0 ? "empty" : Utilities::PrettySize(dialog()->app()->collection_model()->icon_cache_disk_size())));
}
void CollectionSettingsPage::Save() {
@ -232,3 +243,9 @@ void CollectionSettingsPage::CoverSaveInAlbumDirChanged() {
}
}
void CollectionSettingsPage::ClearPixmapDiskCache() {
ui_->disk_cache_in_use->setText("empty");
}

View File

@ -47,6 +47,8 @@ class CollectionSettingsPage : public SettingsPage {
static const char *kSettingsDiskCacheEnable;
static const char *kSettingsDiskCacheSize;
static const char *kSettingsDiskCacheSizeUnit;
static const int kSettingsCacheSizeDefault;
static const int kSettingsDiskCacheSizeDefault;
enum CacheSizeUnit {
CacheSizeUnit_KB,
@ -68,8 +70,9 @@ class CollectionSettingsPage : public SettingsPage {
void Remove();
void CurrentRowChanged(const QModelIndex &index);
void DiskCacheEnable(int state);
void DiskCacheEnable(const int state);
void CoverSaveInAlbumDirChanged();
void ClearPixmapDiskCache();
private:
Ui_CollectionSettingsPage *ui_;

View File

@ -160,6 +160,9 @@ If there are no matches then it will use the largest image in the directory.</st
<string>Saving album covers</string>
</property>
<layout class="QVBoxLayout" name="layout_albumcovers">
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="checkbox_cover_album_dir">
<property name="text">
@ -249,98 +252,176 @@ If there are no matches then it will use the largest image in the directory.</st
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupbox_albumcovercache">
<property name="title">
<string>Album cover pixmap cache</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="albumArtGroupBox">
<property name="title">
<string>Album art cache</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Size</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinbox_cache_size">
<property name="maximum">
<number>1048576</number>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="combobox_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="checkbox_disk_cache">
<property name="text">
<string>Enable Disk Cache</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="button_disk_cache">
<property name="text">
<string>Clear Disk Cache</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_disk_cache_size">
<property name="text">
<string>Disk Cache Size</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinbox_disk_cache_size">
<property name="maximum">
<number>1048576</number>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="combobox_disk_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<layout class="QGridLayout" name="layout_cache_size">
<item row="0" column="2">
<widget class="QSpinBox" name="spinbox_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>80</width>
<height>16777215</height>
</size>
</property>
<property name="maximum">
<number>1048576</number>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Size</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QComboBox" name="combobox_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="4">
<spacer name="spacer_cache_size">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="layout_disk_cache">
<item>
<widget class="QCheckBox" name="checkbox_disk_cache">
<property name="text">
<string>Enable Disk Cache</string>
</property>
</widget>
</item>
<item>
<spacer name="spacer_disk_cache">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QGridLayout" name="layout_disk_cache_size">
<item row="0" column="0">
<widget class="QLabel" name="label_disk_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Disk Cache Size</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QComboBox" name="combobox_disk_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="spinbox_disk_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>80</width>
<height>16777215</height>
</size>
</property>
<property name="maximum">
<number>1048576</number>
</property>
</widget>
</item>
<item row="0" column="4">
<spacer name="spacer_disk_cache_size">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_disk_cache_in_use">
<property name="text">
<string>Current disk cache in use:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="disk_cache_in_use">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="button_clear_disk_cache">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Clear Disk Cache</string>
</property>
</widget>
</item>
<item>
<spacer name="spacer_disk_cache_in_use">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
@ -369,9 +450,9 @@ If there are no matches then it will use the largest image in the directory.</st
<tabstop>spinbox_cache_size</tabstop>
<tabstop>combobox_cache_size</tabstop>
<tabstop>checkbox_disk_cache</tabstop>
<tabstop>button_disk_cache</tabstop>
<tabstop>spinbox_disk_cache_size</tabstop>
<tabstop>combobox_disk_cache_size</tabstop>
<tabstop>button_clear_disk_cache</tabstop>
</tabstops>
<resources/>
<connections/>