Allow renaming currently opened file in GUI
This commit is contained in:
parent
77c07d89fa
commit
507d1ae6d8
|
@ -126,6 +126,7 @@ set(REQUIRED_ICONS
|
||||||
document-open
|
document-open
|
||||||
edit-clear
|
edit-clear
|
||||||
edit-copy
|
edit-copy
|
||||||
|
edit-rename
|
||||||
emblem-error
|
emblem-error
|
||||||
emblem-warning
|
emblem-warning
|
||||||
emblem-information
|
emblem-information
|
||||||
|
|
|
@ -42,6 +42,7 @@
|
||||||
#include <QFileSystemWatcher>
|
#include <QFileSystemWatcher>
|
||||||
#include <QGuiApplication>
|
#include <QGuiApplication>
|
||||||
#include <QHeaderView>
|
#include <QHeaderView>
|
||||||
|
#include <QInputDialog>
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
@ -147,6 +148,7 @@ TagEditorWidget::TagEditorWidget(QWidget *parent)
|
||||||
connect(m_ui->deleteTagsButton, &QPushButton::clicked, this, &TagEditorWidget::deleteAllTagsAndSave);
|
connect(m_ui->deleteTagsButton, &QPushButton::clicked, this, &TagEditorWidget::deleteAllTagsAndSave);
|
||||||
connect(m_ui->nextButton, &QPushButton::clicked, this, &TagEditorWidget::saveAndShowNextFile);
|
connect(m_ui->nextButton, &QPushButton::clicked, this, &TagEditorWidget::saveAndShowNextFile);
|
||||||
connect(m_ui->closeButton, &QPushButton::clicked, this, &TagEditorWidget::closeFile);
|
connect(m_ui->closeButton, &QPushButton::clicked, this, &TagEditorWidget::closeFile);
|
||||||
|
connect(m_ui->renamePushButton, &QPushButton::clicked, this, &TagEditorWidget::renameFile);
|
||||||
|
|
||||||
// misc
|
// misc
|
||||||
connect(m_ui->abortButton, &QPushButton::clicked, [this] {
|
connect(m_ui->abortButton, &QPushButton::clicked, [this] {
|
||||||
|
@ -487,11 +489,12 @@ void TagEditorWidget::updateFileStatusStatus()
|
||||||
const bool showDocumentTitle = opened && m_fileInfo.container() && m_fileInfo.container()->supportsTitle();
|
const bool showDocumentTitle = opened && m_fileInfo.container() && m_fileInfo.container()->supportsTitle();
|
||||||
m_ui->docTitleLabel->setVisible(showDocumentTitle);
|
m_ui->docTitleLabel->setVisible(showDocumentTitle);
|
||||||
m_ui->docTitleWidget->setVisible(showDocumentTitle);
|
m_ui->docTitleWidget->setVisible(showDocumentTitle);
|
||||||
// buttons and actions to save, delete, close
|
// buttons and actions to save, delete, close and rename
|
||||||
m_ui->saveButton->setEnabled(opened);
|
m_ui->saveButton->setEnabled(opened);
|
||||||
m_ui->nextButton->setEnabled(opened);
|
m_ui->nextButton->setEnabled(opened);
|
||||||
m_ui->deleteTagsButton->setEnabled(hasTag);
|
m_ui->deleteTagsButton->setEnabled(hasTag);
|
||||||
m_ui->buttonsWidget->setEnabled(opened);
|
m_ui->buttonsWidget->setEnabled(opened);
|
||||||
|
m_ui->renamePushButton->setEnabled(opened);
|
||||||
// clear and restore buttons
|
// clear and restore buttons
|
||||||
m_ui->clearEntriesPushButton->setEnabled(hasTag);
|
m_ui->clearEntriesPushButton->setEnabled(hasTag);
|
||||||
m_ui->restoreEntriesPushButton->setEnabled(hasTag);
|
m_ui->restoreEntriesPushButton->setEnabled(hasTag);
|
||||||
|
@ -1377,6 +1380,63 @@ void TagEditorWidget::closeFile()
|
||||||
updateFileStatusStatus();
|
updateFileStatusStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QtGui::TagEditorWidget::renameFile()
|
||||||
|
{
|
||||||
|
if (isFileOperationOngoing()) {
|
||||||
|
emit statusMessage(tr("Unable to rename the file because the current process hasn't been finished yet."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (m_currentPath.isEmpty() || m_fileName.isEmpty() || !m_fileInfo.isOpen()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
static const auto windowTitle = tr("Renaming file - ") + QCoreApplication::applicationName();
|
||||||
|
const auto newFileName = QInputDialog::getText(this, windowTitle, tr("New file name:"), QLineEdit::Normal, m_fileName);
|
||||||
|
if (newFileName.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto errorMsg = QString();
|
||||||
|
try {
|
||||||
|
// remove watcher, close file
|
||||||
|
m_fileWatcher->removePath(m_currentPath);
|
||||||
|
m_fileInfo.stream().close();
|
||||||
|
|
||||||
|
// rename file
|
||||||
|
auto oldPath = std::filesystem::path(makeNativePath(m_currentPath.toStdString()));
|
||||||
|
auto newPath = oldPath.parent_path();
|
||||||
|
newPath.append(makeNativePath(newFileName.toStdString()));
|
||||||
|
std::filesystem::rename(oldPath, newPath);
|
||||||
|
|
||||||
|
// open again with write access
|
||||||
|
m_fileInfo.reportPathChanged(newPath.u8string());
|
||||||
|
try {
|
||||||
|
m_fileInfo.stream().open(m_fileInfo.path().data(), ios_base::in | ios_base::out | ios_base::binary);
|
||||||
|
} catch (const std::ios_base::failure &) {
|
||||||
|
// try to open read-only if opening with write access failed
|
||||||
|
m_fileInfo.stream().open(m_fileInfo.path().data(), ios_base::in | ios_base::binary);
|
||||||
|
}
|
||||||
|
m_currentPath = QString::fromStdString(m_fileInfo.path());
|
||||||
|
m_fileName = QString::fromStdString(m_fileInfo.fileName());
|
||||||
|
m_ui->fileNameLabel->setText(m_fileName);
|
||||||
|
emit currentPathChanged(m_currentPath);
|
||||||
|
|
||||||
|
} catch (const std::runtime_error &e) {
|
||||||
|
auto msgBox = new QMessageBox(this);
|
||||||
|
msgBox->setIcon(QMessageBox::Critical);
|
||||||
|
msgBox->setAttribute(Qt::WA_DeleteOnClose, true);
|
||||||
|
msgBox->setWindowTitle(windowTitle);
|
||||||
|
msgBox->setText(errorMsg = tr("Unable to rename file: %1").arg(e.what()));
|
||||||
|
msgBox->setInformativeText(tr("Tried to rename file: ") + m_currentPath);
|
||||||
|
msgBox->show();
|
||||||
|
}
|
||||||
|
|
||||||
|
// add new current path to file watcher
|
||||||
|
m_fileWatcher->addPath(m_currentPath);
|
||||||
|
// update ui
|
||||||
|
emit statusMessage(errorMsg.isEmpty() ? tr("The file has been renamed.") : errorMsg);
|
||||||
|
updateFileStatusStatus();
|
||||||
|
}
|
||||||
|
|
||||||
bool TagEditorWidget::handleFileInfoUnavailable()
|
bool TagEditorWidget::handleFileInfoUnavailable()
|
||||||
{
|
{
|
||||||
if (isFileOperationOngoing()) {
|
if (isFileOperationOngoing()) {
|
||||||
|
|
|
@ -73,6 +73,7 @@ public Q_SLOTS:
|
||||||
bool applyEntriesAndSaveChangings();
|
bool applyEntriesAndSaveChangings();
|
||||||
bool deleteAllTagsAndSave();
|
bool deleteAllTagsAndSave();
|
||||||
void closeFile();
|
void closeFile();
|
||||||
|
void renameFile();
|
||||||
void saveFileInfo();
|
void saveFileInfo();
|
||||||
void openFileInfoInBrowser();
|
void openFileInfoInBrowser();
|
||||||
// misc
|
// misc
|
||||||
|
|
|
@ -16,14 +16,42 @@
|
||||||
<number>4</number>
|
<number>4</number>
|
||||||
</property>
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLabel" name="fileNameLabel">
|
<layout class="QHBoxLayout" name="fileNameHorizontalLayout">
|
||||||
<property name="acceptDrops">
|
<item>
|
||||||
<bool>true</bool>
|
<widget class="QLabel" name="fileNameLabel">
|
||||||
</property>
|
<property name="acceptDrops">
|
||||||
<property name="text">
|
<bool>true</bool>
|
||||||
<string>No file selected</string>
|
</property>
|
||||||
</property>
|
<property name="text">
|
||||||
</widget>
|
<string>No file selected</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<spacer name="horizontalSpacer">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeHint" stdset="0">
|
||||||
|
<size>
|
||||||
|
<width>40</width>
|
||||||
|
<height>20</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</spacer>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="renamePushButton">
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>Rename file</string>
|
||||||
|
</property>
|
||||||
|
<property name="icon">
|
||||||
|
<iconset theme="edit-rename">
|
||||||
|
<normaloff>.</normaloff>.</iconset>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="NotificationLabel" name="parsingNotificationWidget" native="true">
|
<widget class="NotificationLabel" name="parsingNotificationWidget" native="true">
|
||||||
|
|
Loading…
Reference in New Issue