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
|
||||
edit-clear
|
||||
edit-copy
|
||||
edit-rename
|
||||
emblem-error
|
||||
emblem-warning
|
||||
emblem-information
|
||||
|
|
|
@ -42,6 +42,7 @@
|
|||
#include <QFileSystemWatcher>
|
||||
#include <QGuiApplication>
|
||||
#include <QHeaderView>
|
||||
#include <QInputDialog>
|
||||
#include <QKeyEvent>
|
||||
#include <QMenu>
|
||||
#include <QMessageBox>
|
||||
|
@ -147,6 +148,7 @@ TagEditorWidget::TagEditorWidget(QWidget *parent)
|
|||
connect(m_ui->deleteTagsButton, &QPushButton::clicked, this, &TagEditorWidget::deleteAllTagsAndSave);
|
||||
connect(m_ui->nextButton, &QPushButton::clicked, this, &TagEditorWidget::saveAndShowNextFile);
|
||||
connect(m_ui->closeButton, &QPushButton::clicked, this, &TagEditorWidget::closeFile);
|
||||
connect(m_ui->renamePushButton, &QPushButton::clicked, this, &TagEditorWidget::renameFile);
|
||||
|
||||
// misc
|
||||
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();
|
||||
m_ui->docTitleLabel->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->nextButton->setEnabled(opened);
|
||||
m_ui->deleteTagsButton->setEnabled(hasTag);
|
||||
m_ui->buttonsWidget->setEnabled(opened);
|
||||
m_ui->renamePushButton->setEnabled(opened);
|
||||
// clear and restore buttons
|
||||
m_ui->clearEntriesPushButton->setEnabled(hasTag);
|
||||
m_ui->restoreEntriesPushButton->setEnabled(hasTag);
|
||||
|
@ -1377,6 +1380,63 @@ void TagEditorWidget::closeFile()
|
|||
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()
|
||||
{
|
||||
if (isFileOperationOngoing()) {
|
||||
|
|
|
@ -73,6 +73,7 @@ public Q_SLOTS:
|
|||
bool applyEntriesAndSaveChangings();
|
||||
bool deleteAllTagsAndSave();
|
||||
void closeFile();
|
||||
void renameFile();
|
||||
void saveFileInfo();
|
||||
void openFileInfoInBrowser();
|
||||
// misc
|
||||
|
|
|
@ -16,14 +16,42 @@
|
|||
<number>4</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLabel" name="fileNameLabel">
|
||||
<property name="acceptDrops">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>No file selected</string>
|
||||
</property>
|
||||
</widget>
|
||||
<layout class="QHBoxLayout" name="fileNameHorizontalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="fileNameLabel">
|
||||
<property name="acceptDrops">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<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>
|
||||
<widget class="NotificationLabel" name="parsingNotificationWidget" native="true">
|
||||
|
|
Loading…
Reference in New Issue