Center organise and transcoder dialog on same screen as mainwindow

This commit is contained in:
Jonas Kvinge
2020-04-07 01:26:17 +02:00
parent 3074377b55
commit 307961cc7e
12 changed files with 260 additions and 152 deletions

View File

@@ -28,6 +28,8 @@
#include <QtConcurrentRun>
#include <QAbstractItemModel>
#include <QDialog>
#include <QScreen>
#include <QWindow>
#include <QHash>
#include <QMap>
#include <QDir>
@@ -49,6 +51,7 @@
#include <QToolButton>
#include <QFlags>
#include <QShowEvent>
#include <QCloseEvent>
#include <QSettings>
#include "core/closure.h"
@@ -65,21 +68,22 @@
#include "organiseerrordialog.h"
#include "ui_organisedialog.h"
using std::shared_ptr;
using std::stable_sort;
const char *OrganiseDialog::kDefaultFormat = "%albumartist/%album{ (Disc %disc)}/{%track - }{%albumartist - }%album{ (Disc %disc)} - %title.%extension";
const char *OrganiseDialog::kSettingsGroup = "OrganiseDialog";
OrganiseDialog::OrganiseDialog(TaskManager *task_manager, CollectionBackend *backend, QWidget *parent)
OrganiseDialog::OrganiseDialog(TaskManager *task_manager, CollectionBackend *backend, QWidget *parentwindow, QWidget *parent)
: QDialog(parent),
parentwindow_(parentwindow),
ui_(new Ui_OrganiseDialog),
task_manager_(task_manager),
backend_(backend),
total_size_(0) {
ui_->setupUi(this);
setWindowFlags(windowFlags()|Qt::WindowMaximizeButtonHint);
connect(ui_->button_box->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(Reset()));
ui_->aftercopying->setItemIcon(1, IconLoader::Load("edit-delete"));
@@ -131,13 +135,6 @@ OrganiseDialog::OrganiseDialog(TaskManager *task_manager, CollectionBackend *bac
ui_->insert->setMenu(tag_menu);
QSettings s;
s.beginGroup(kSettingsGroup);
if (s.contains("geometry")) {
restoreGeometry(s.value("geometry").toByteArray());
}
s.endGroup();
}
OrganiseDialog::~OrganiseDialog() {
@@ -149,6 +146,134 @@ void OrganiseDialog::SetDestinationModel(QAbstractItemModel *model, bool devices
ui_->destination->setModel(model);
ui_->eject_after->setVisible(devices);
}
void OrganiseDialog::showEvent(QShowEvent*) {
LoadGeometry();
LoadSettings();
}
void OrganiseDialog::closeEvent(QCloseEvent*) {
SaveGeometry();
}
void OrganiseDialog::accept() {
SaveSettings();
const QModelIndex destination = ui_->destination->model()->index(ui_->destination->currentIndex(), 0);
std::shared_ptr<MusicStorage> storage = destination.data(MusicStorage::Role_StorageForceConnect).value<std::shared_ptr<MusicStorage>>();
if (!storage) return;
// It deletes itself when it's finished.
const bool copy = ui_->aftercopying->currentIndex() == 0;
Organise *organise = new Organise(task_manager_, storage, format_, copy, ui_->overwrite->isChecked(), ui_->mark_as_listened->isChecked(), ui_->albumcover->isChecked(), new_songs_info_, ui_->eject_after->isChecked(), playlist_);
connect(organise, SIGNAL(Finished(QStringList, QStringList)), SLOT(OrganiseFinished(QStringList, QStringList)));
connect(organise, SIGNAL(FileCopied(int)), this, SIGNAL(FileCopied(int)));
if (backend_)
connect(organise, SIGNAL(SongPathChanged(const Song&, const QFileInfo&)), backend_, SLOT(SongPathChanged(const Song&, const QFileInfo&)));
organise->Start();
SaveGeometry();
QDialog::accept();
}
void OrganiseDialog::reject() {
SaveGeometry();
QDialog::reject();
}
void OrganiseDialog::LoadGeometry() {
if (parentwindow_) {
QSettings s;
s.beginGroup(kSettingsGroup);
if (s.contains("geometry")) {
restoreGeometry(s.value("geometry").toByteArray());
}
s.endGroup();
// Center the window on the same screen as the parentwindow.
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QScreen *screen = parentwindow_->screen();
#else
QScreen *screen = (parentwindow_->window() && parentwindow_->window()->windowHandle() ? parentwindow_->window()->windowHandle()->screen() : nullptr);
#endif
if (screen) {
const QRect sr = screen->availableGeometry();
const QRect wr({}, size().boundedTo(sr.size()));
resize(wr.size());
move(sr.center() - wr.center());
}
}
}
void OrganiseDialog::SaveGeometry() {
if (parentwindow_) {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("geometry", saveGeometry());
s.endGroup();
}
}
void OrganiseDialog::LoadSettings() {
QSettings s;
s.beginGroup(kSettingsGroup);
ui_->naming->setPlainText(s.value("format", kDefaultFormat).toString());
ui_->remove_non_fat->setChecked(s.value("remove_non_fat", false).toBool());
ui_->remove_non_ascii->setChecked(s.value("remove_non_ascii", false).toBool());
ui_->allow_ascii_ext->setChecked(s.value("allow_ascii_ext", false).toBool());
ui_->replace_spaces->setChecked(s.value("replace_spaces", true).toBool());
ui_->overwrite->setChecked(s.value("overwrite", false).toBool());
ui_->albumcover->setChecked(s.value("albumcover", true).toBool());
ui_->mark_as_listened->setChecked(s.value("mark_as_listened", false).toBool());
ui_->eject_after->setChecked(s.value("eject_after", false).toBool());
QString destination = s.value("destination").toString();
int index = ui_->destination->findText(destination);
if (index != -1 && !destination.isEmpty()) {
ui_->destination->setCurrentIndex(index);
}
s.endGroup();
AllowExtASCII(ui_->remove_non_ascii->isChecked());
}
void OrganiseDialog::SaveSettings() {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("format", ui_->naming->toPlainText());
s.setValue("remove_non_fat", ui_->remove_non_fat->isChecked());
s.setValue("remove_non_ascii", ui_->remove_non_ascii->isChecked());
s.setValue("allow_ascii_ext", ui_->allow_ascii_ext->isChecked());
s.setValue("replace_spaces", ui_->replace_spaces->isChecked());
s.setValue("overwrite", ui_->overwrite->isChecked());
s.setValue("mark_as_listened", ui_->overwrite->isChecked());
s.setValue("albumcover", ui_->albumcover->isChecked());
s.setValue("destination", ui_->destination->currentText());
s.setValue("eject_after", ui_->eject_after->isChecked());
s.endGroup();
}
bool OrganiseDialog::SetSongs(const SongList &songs) {
@@ -354,86 +479,6 @@ void OrganiseDialog::Reset() {
}
void OrganiseDialog::showEvent(QShowEvent*) {
QSettings s;
s.beginGroup(kSettingsGroup);
ui_->naming->setPlainText(s.value("format", kDefaultFormat).toString());
ui_->remove_non_fat->setChecked(s.value("remove_non_fat", false).toBool());
ui_->remove_non_ascii->setChecked(s.value("remove_non_ascii", false).toBool());
ui_->allow_ascii_ext->setChecked(s.value("allow_ascii_ext", false).toBool());
ui_->replace_spaces->setChecked(s.value("replace_spaces", true).toBool());
ui_->overwrite->setChecked(s.value("overwrite", false).toBool());
ui_->albumcover->setChecked(s.value("albumcover", true).toBool());
ui_->mark_as_listened->setChecked(s.value("mark_as_listened", false).toBool());
ui_->eject_after->setChecked(s.value("eject_after", false).toBool());
QString destination = s.value("destination").toString();
int index = ui_->destination->findText(destination);
if (index != -1 && !destination.isEmpty()) {
ui_->destination->setCurrentIndex(index);
}
s.endGroup();
AllowExtASCII(ui_->remove_non_ascii->isChecked());
}
void OrganiseDialog::accept() {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("format", ui_->naming->toPlainText());
s.setValue("remove_non_fat", ui_->remove_non_fat->isChecked());
s.setValue("remove_non_ascii", ui_->remove_non_ascii->isChecked());
s.setValue("allow_ascii_ext", ui_->allow_ascii_ext->isChecked());
s.setValue("replace_spaces", ui_->replace_spaces->isChecked());
s.setValue("overwrite", ui_->overwrite->isChecked());
s.setValue("mark_as_listened", ui_->overwrite->isChecked());
s.setValue("albumcover", ui_->albumcover->isChecked());
s.setValue("destination", ui_->destination->currentText());
s.setValue("eject_after", ui_->eject_after->isChecked());
s.endGroup();
const QModelIndex destination = ui_->destination->model()->index(ui_->destination->currentIndex(), 0);
std::shared_ptr<MusicStorage> storage = destination.data(MusicStorage::Role_StorageForceConnect).value<std::shared_ptr<MusicStorage>>();
if (!storage) return;
// It deletes itself when it's finished.
const bool copy = ui_->aftercopying->currentIndex() == 0;
Organise *organise = new Organise(task_manager_, storage, format_, copy, ui_->overwrite->isChecked(), ui_->mark_as_listened->isChecked(), ui_->albumcover->isChecked(), new_songs_info_, ui_->eject_after->isChecked(), playlist_);
connect(organise, SIGNAL(Finished(QStringList, QStringList)), SLOT(OrganiseFinished(QStringList, QStringList)));
connect(organise, SIGNAL(FileCopied(int)), this, SIGNAL(FileCopied(int)));
if (backend_)
connect(organise, SIGNAL(SongPathChanged(const Song&, const QFileInfo&)), backend_, SLOT(SongPathChanged(const Song&, const QFileInfo&)));
organise->Start();
SaveGeometry();
QDialog::accept();
}
void OrganiseDialog::reject() {
SaveGeometry();
QDialog::reject();
}
void OrganiseDialog::SaveGeometry() {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("geometry", saveGeometry());
s.endGroup();
}
void OrganiseDialog::OrganiseFinished(const QStringList files_with_errors, const QStringList log) {
if (files_with_errors.isEmpty()) return;

View File

@@ -45,6 +45,7 @@ class QAbstractItemModel;
class QWidget;
class QResizeEvent;
class QShowEvent;
class QCloseEvent;
class TaskManager;
class CollectionBackend;
@@ -55,7 +56,7 @@ class OrganiseDialog : public QDialog {
Q_OBJECT
public:
OrganiseDialog(TaskManager *task_manager, CollectionBackend *backend = nullptr, QWidget *parent = nullptr);
OrganiseDialog(TaskManager *task_manager, CollectionBackend *backend = nullptr, QWidget *parentwindow = nullptr, QWidget *parent = nullptr);
~OrganiseDialog();
static const char *kDefaultFormat;
@@ -76,6 +77,19 @@ class OrganiseDialog : public QDialog {
void SetPlaylist(const QString &playlist);
protected:
void showEvent(QShowEvent*);
void closeEvent(QCloseEvent*);
private:
void LoadGeometry();
void SaveGeometry();
void LoadSettings();
void SaveSettings();
SongList LoadSongsBlocking(const QStringList &filenames);
void SetLoadingSongs(bool loading);
signals:
void FileCopied(int);
@@ -83,9 +97,6 @@ class OrganiseDialog : public QDialog {
void accept();
void reject();
protected:
void showEvent(QShowEvent *);
private slots:
void Reset();
@@ -96,15 +107,10 @@ class OrganiseDialog : public QDialog {
void AllowExtASCII(bool checked);
void SaveGeometry();
private:
SongList LoadSongsBlocking(const QStringList &filenames);
void SetLoadingSongs(bool loading);
private:
static const char *kSettingsGroup;
QWidget *parentwindow_;
Ui_OrganiseDialog *ui_;
TaskManager *task_manager_;
CollectionBackend *backend_;

View File

@@ -17,7 +17,7 @@
<iconset resource="../../data/icons.qrc">
<normaloff>:/icons/64x64/strawberry.png</normaloff>:/icons/64x64/strawberry.png</iconset>
</property>
<layout class="QVBoxLayout" name="layout_dialog">
<layout class="QVBoxLayout" name="layout_organisedialog">
<item>
<layout class="QFormLayout" name="layout_copying">
<item row="0" column="0">