cache: better implemented changing disk cache folders

Not complete yet, but getting there.
This commit is contained in:
itsmattkc
2020-08-07 02:31:46 +10:00
parent 00d84cfda0
commit df14c2aaa7
33 changed files with 701 additions and 431 deletions
@@ -27,12 +27,15 @@
#include <QLabel>
#include <QMessageBox>
#include "render/diskmanager.h"
#include "common/filefunctions.h"
OLIVE_NAMESPACE_ENTER
PreferencesDiskTab::PreferencesDiskTab()
{
// Get default disk cache folder
default_disk_cache_folder_ = DiskManager::instance()->GetDefaultCacheFolder();
QVBoxLayout* outer_layout = new QVBoxLayout(this);
QGroupBox* disk_management_group = new QGroupBox(tr("Disk Management"));
@@ -44,15 +47,9 @@ PreferencesDiskTab::PreferencesDiskTab()
disk_management_layout->addWidget(new QLabel(tr("Disk Cache Location:")), row, 0);
disk_cache_location_ = new QLineEdit();
disk_cache_location_->setText(Config::Current()["DiskCachePath"].toString());
connect(disk_cache_location_, &QLineEdit::textChanged, this, &PreferencesDiskTab::DiskCacheLineEditChanged);
disk_cache_location_ = new PathWidget(default_disk_cache_folder_->GetPath());
disk_management_layout->addWidget(disk_cache_location_, row, 1);
QPushButton* browse_btn = new QPushButton(tr("Browse"));
connect(browse_btn, &QPushButton::clicked, this, &PreferencesDiskTab::BrowseDiskCachePath);
disk_management_layout->addWidget(browse_btn, row, 2);
row++;
disk_management_layout->addWidget(new QLabel(tr("Maximum Disk Cache:")), row, 0);
@@ -60,20 +57,20 @@ PreferencesDiskTab::PreferencesDiskTab()
maximum_cache_slider_ = new FloatSlider();
maximum_cache_slider_->SetFormat(tr("%1 GB"));
maximum_cache_slider_->SetMinimum(1.0);
maximum_cache_slider_->SetValue(Config::Current()["DiskCacheSize"].toDouble());
disk_management_layout->addWidget(maximum_cache_slider_, row, 1, 1, 2);
maximum_cache_slider_->SetValue(static_cast<double>(default_disk_cache_folder_->GetLimit()) / static_cast<double>(kBytesInGigabyte));
disk_management_layout->addWidget(maximum_cache_slider_, row, 1);
row++;
clear_cache_btn_ = new QPushButton(tr("Clear Disk Cache"));
connect(clear_cache_btn_, &QPushButton::clicked, this, &PreferencesDiskTab::ClearDiskCache);
disk_management_layout->addWidget(clear_cache_btn_, row, 1, 1, 2);
disk_management_layout->addWidget(clear_cache_btn_, row, 1);
row++;
clear_disk_cache_ = new QCheckBox(tr("Automatically clear disk cache on close"));
clear_disk_cache_->setChecked(Config::Current()["ClearDiskCacheOnClose"].toBool());
disk_management_layout->addWidget(clear_disk_cache_, row, 1, 1, 2);
clear_disk_cache_->setChecked(default_disk_cache_folder_->GetClearOnClose());
disk_management_layout->addWidget(clear_disk_cache_, row, 1);
QGroupBox* cache_behavior = new QGroupBox(tr("Cache Behavior"));
outer_layout->addWidget(cache_behavior);
@@ -100,35 +97,51 @@ PreferencesDiskTab::PreferencesDiskTab()
outer_layout->addStretch();
}
bool PreferencesDiskTab::Validate()
{
if (disk_cache_location_->text() != default_disk_cache_folder_->GetPath()) {
// Disk cache location is changing
// Check if the user wants to move the cache here
if (QMessageBox::question(this,
tr("Disk Cache"),
tr("You've chosen to change the default disk cache location. This "
"will invalidate your current cache. Would you like to continue?"),
QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Cancel) {
return false;
}
// Check validity of the new path
if (!FileFunctions::DirectoryIsValid(disk_cache_location_->text(), true)) {
QMessageBox::critical(this,
tr("Disk Cache"),
tr("Failed to set disk cache location. Access was denied."));
return false;
}
}
return true;
}
void PreferencesDiskTab::Accept()
{
Config::Current()["DiskCachePath"] = disk_cache_location_->text();
Config::Current()["DiskCacheSize"] = maximum_cache_slider_->GetValue();
Config::Current()["ClearDiskCacheOnClose"] = clear_disk_cache_->isChecked();
if (disk_cache_location_->text() != default_disk_cache_folder_->GetPath()) {
default_disk_cache_folder_->SetPath(disk_cache_location_->text());
}
qint64 new_disk_cache_limit = qRound64(maximum_cache_slider_->GetValue() * kBytesInGigabyte);
if (new_disk_cache_limit != default_disk_cache_folder_->GetLimit()) {
default_disk_cache_folder_->SetLimit(new_disk_cache_limit);
}
if (default_disk_cache_folder_->GetClearOnClose() != clear_disk_cache_->isChecked()) {
default_disk_cache_folder_->SetClearOnClose(clear_disk_cache_->isChecked());
}
Config::Current()["DiskCacheBehind"] = QVariant::fromValue(rational::fromDouble(cache_behind_slider_->GetValue()));
Config::Current()["DiskCacheAhead"] = QVariant::fromValue(rational::fromDouble(cache_ahead_slider_->GetValue()));
}
void PreferencesDiskTab::DiskCacheLineEditChanged()
{
QString entered_dir = disk_cache_location_->text();
if (!entered_dir.isEmpty() && !QDir(entered_dir).exists()) {
disk_cache_location_->setStyleSheet(QStringLiteral("color: red;"));
} else {
disk_cache_location_->setStyleSheet(QString());
}
}
void PreferencesDiskTab::BrowseDiskCachePath()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Browse for disk cache path"), disk_cache_location_->text());
if (!dir.isEmpty()) {
disk_cache_location_->setText(dir);
}
}
void PreferencesDiskTab::ClearDiskCache()
{
if (QMessageBox::question(this,
@@ -137,7 +150,7 @@ void PreferencesDiskTab::ClearDiskCache()
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
clear_cache_btn_->setEnabled(false);
if (DiskManager::instance()->ClearDiskCache(false)) {
if (DiskManager::instance()->ClearDiskCache(default_disk_cache_folder_->GetPath())) {
clear_cache_btn_->setText(tr("Disk Cache Cleared"));
} else {
QMessageBox::information(this,
@@ -26,7 +26,9 @@
#include <QPushButton>
#include "preferencestab.h"
#include "render/diskmanager.h"
#include "widget/slider/floatslider.h"
#include "widget/path/pathwidget.h"
OLIVE_NAMESPACE_ENTER
@@ -36,10 +38,12 @@ class PreferencesDiskTab : public PreferencesTab
public:
PreferencesDiskTab();
virtual bool Validate() override;
virtual void Accept() override;
private:
QLineEdit* disk_cache_location_;
PathWidget* disk_cache_location_;
FloatSlider* maximum_cache_slider_;
@@ -51,11 +55,9 @@ private:
QPushButton* clear_cache_btn_;
DiskCacheFolder* default_disk_cache_folder_;
private slots:
void DiskCacheLineEditChanged();
void BrowseDiskCachePath();
void ClearDiskCache();
};
@@ -20,6 +20,7 @@
#include "projectproperties.h"
#include <QButtonGroup>
#include <QDialogButtonBox>
#include <QFileDialog>
#include <QGroupBox>
@@ -29,6 +30,7 @@
#include <OpenColorIO/OpenColorIO.h>
namespace OCIO = OCIO_NAMESPACE::v1;
#include "common/filefunctions.h"
#include "config/config.h"
#include "core.h"
#include "render/colormanager.h"
@@ -51,7 +53,10 @@ ProjectPropertiesDialog::ProjectPropertiesDialog(Project* p, QWidget *parent) :
// Color management group
QWidget* color_group = new QWidget();
QGridLayout* color_layout = new QGridLayout(color_group);
QVBoxLayout* color_outer_layout = new QVBoxLayout(color_group);
QGridLayout* color_layout = new QGridLayout();
color_outer_layout->addLayout(color_layout);
int row = 0;
@@ -80,26 +85,47 @@ ProjectPropertiesDialog::ProjectPropertiesDialog(Project* p, QWidget *parent) :
OCIOFilenameUpdated();
tabs->addTab(color_group, tr("Color Management"));
color_outer_layout->addStretch();
}
{
// Paths group
QWidget* paths_group = new QWidget();
// Cache group
QWidget* cache_group = new QWidget();
QGridLayout* paths_layout = new QGridLayout(paths_group);
QVBoxLayout* cache_layout = new QVBoxLayout(cache_group);
cache_path_ = new PathWidget(working_project_->cache_path(), this);
QButtonGroup* disk_cache_btn_group = new QButtonGroup();
int row = 0;
disk_cache_use_default_btn_ = new QRadioButton(tr("Use Default Location"));
disk_cache_store_alongside_project_btn_ = new QRadioButton(tr("Store Alongside Project"));
disk_cache_use_custom_btn_ = new QRadioButton(tr("Use Custom Location:"));
paths_layout->addWidget(new QLabel(tr("Cache Path:")), row, 0);
paths_layout->addWidget(cache_path_->path_edit(), row, 1);
paths_layout->addWidget(cache_path_->browse_btn(), row, 2);
paths_layout->addWidget(cache_path_->default_box(), row, 3);
disk_cache_btn_group->addButton(disk_cache_use_default_btn_);
disk_cache_btn_group->addButton(disk_cache_store_alongside_project_btn_);
disk_cache_btn_group->addButton(disk_cache_use_custom_btn_);
tabs->addTab(paths_group, tr("Paths"));
cache_layout->addWidget(disk_cache_use_default_btn_);
cache_layout->addWidget(disk_cache_store_alongside_project_btn_);
cache_layout->addWidget(disk_cache_use_custom_btn_);
cache_path_ = new PathWidget(working_project_->cache_path(false), this);
cache_path_->setEnabled(false);
cache_layout->addWidget(cache_path_);
connect(disk_cache_use_custom_btn_, &QRadioButton::toggled, cache_path_, &PathWidget::setEnabled);
if (working_project_->cache_path(false).isEmpty()) {
disk_cache_use_default_btn_->setChecked(true);
} else {
disk_cache_use_custom_btn_->setChecked(true);
}
cache_layout->addWidget(cache_path_);
tabs->addTab(cache_group, tr("Disk Cache"));
}
QDialogButtonBox* dialog_btns = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
@@ -122,18 +148,26 @@ void ProjectPropertiesDialog::accept()
return;
}
if (!cache_path_->PathIsValid(true)) {
QMessageBox mb(this);
mb.setWindowModality(Qt::WindowModal);
mb.setIcon(QMessageBox::Critical);
mb.setWindowTitle(tr("Invalid path"));
mb.setText(tr("The cache path is invalid. Please check it and try again."));
mb.addButton(QMessageBox::Ok);
mb.exec();
if (disk_cache_use_default_btn_->isChecked()) {
// Empty cache path means default
working_project_->set_cache_path(QString());
} else if (disk_cache_store_alongside_project_btn_->isChecked()) {
QMessageBox::information(this, QString(), tr("\"Store alignside project\" functionality not implemented yet"));
return;
}
} else {
if (!FileFunctions::DirectoryIsValid(cache_path_->text(), true)) {
QMessageBox mb(this);
mb.setWindowModality(Qt::WindowModal);
mb.setIcon(QMessageBox::Critical);
mb.setWindowTitle(tr("Invalid path"));
mb.setText(tr("The cache path is invalid. Please check it and try again."));
mb.addButton(QMessageBox::Ok);
mb.exec();
return;
}
working_project_->set_cache_path(cache_path_->path_edit()->text());
working_project_->set_cache_path(cache_path_->text());
}
// This should ripple changes throughout the program that the color config has changed, therefore must be done last
working_project_->color_manager()->SetConfigAndDefaultInput(ocio_filename_->text(),
@@ -184,54 +218,4 @@ void ProjectPropertiesDialog::OCIOFilenameUpdated()
}
}
PathWidget::PathWidget(const QString &path, QWidget *parent) :
QObject(parent)
{
path_edit_ = new QLineEdit();
path_edit_->setText(path);
connect(path_edit_, &QLineEdit::textChanged, this, &PathWidget::LineEditChanged);
default_box_ = new QCheckBox(tr("Default"));
browse_btn_ = new QPushButton(tr("Browse"));
connect(default_box_, &QCheckBox::toggled, this, &PathWidget::DefaultToggled);
default_box_->setChecked(path.isEmpty());
connect(browse_btn_, &QPushButton::clicked, this, &PathWidget::BrowseClicked);
}
bool PathWidget::PathIsValid(bool try_to_create) const
{
return default_box_->isChecked()
|| QDir(path_edit_->text()).exists()
|| (try_to_create && QDir(path_edit_->text()).mkpath(QStringLiteral(".")));
}
void PathWidget::DefaultToggled(bool e)
{
path_edit_->setEnabled(!e);
}
void PathWidget::BrowseClicked()
{
QString dir = QFileDialog::getExistingDirectory(static_cast<QWidget*>(parent()),
tr("Browse for path"),
path_edit_->text());
if (!dir.isEmpty()) {
path_edit_->setText(dir);
}
}
void PathWidget::LineEditChanged()
{
if (PathIsValid(false)) {
path_edit_->setStyleSheet(QString());
} else {
path_edit_->setStyleSheet(QStringLiteral("QLineEdit {color: red;}"));
}
}
OLIVE_NAMESPACE_EXIT
@@ -26,48 +26,13 @@
#include <QDialog>
#include <QGridLayout>
#include <QLineEdit>
#include <QRadioButton>
#include "project/project.h"
#include "widget/path/pathwidget.h"
OLIVE_NAMESPACE_ENTER
class PathWidget : public QObject
{
Q_OBJECT
public:
PathWidget(const QString& path,
QWidget* parent = nullptr);
bool PathIsValid(bool try_to_create) const;
QLineEdit* path_edit() const {
return path_edit_;
}
QCheckBox* default_box() const {
return default_box_;
}
QPushButton* browse_btn() const {
return browse_btn_;
}
private slots:
void DefaultToggled(bool e);
void BrowseClicked();
void LineEditChanged();
private:
QLineEdit* path_edit_;
QCheckBox* default_box_;
QPushButton* browse_btn_;
};
class ProjectPropertiesDialog : public QDialog
{
Q_OBJECT
@@ -89,6 +54,12 @@ private:
QString ocio_config_error_;
PathWidget* cache_path_;
QRadioButton* disk_cache_use_default_btn_;
QRadioButton* disk_cache_store_alongside_project_btn_;
QRadioButton* disk_cache_use_custom_btn_;
private slots:
void BrowseForOCIOConfig();