Handle read only state.

This commit is contained in:
Roman Telezhynskyi 2021-08-14 17:16:52 +03:00
parent 2d68d40b59
commit 742c1c3eb3
3 changed files with 77 additions and 38 deletions

View File

@ -31,6 +31,7 @@
#include <QCloseEvent> #include <QCloseEvent>
#include <QtMath> #include <QtMath>
#include <QSvgGenerator> #include <QSvgGenerator>
#include <QFileSystemWatcher>
#include "ui_vpmainwindow.h" #include "ui_vpmainwindow.h"
#include "dialogs/vpdialogabout.h" #include "dialogs/vpdialogabout.h"
@ -67,7 +68,8 @@ VPMainWindow::VPMainWindow(const VPCommandLinePtr &cmd, QWidget *parent) :
VAbstractMainWindow(parent), VAbstractMainWindow(parent),
ui(new Ui::VPMainWindow), ui(new Ui::VPMainWindow),
m_cmd(cmd), m_cmd(cmd),
m_statusLabel(new QLabel(this)) m_statusLabel(new QLabel(this)),
m_layoutWatcher(new QFileSystemWatcher(this))
{ {
// // ----- for test purposes, to be removed------------------ // // ----- for test purposes, to be removed------------------
m_layout->LayoutSettings().SetUnit(Unit::Cm); m_layout->LayoutSettings().SetUnit(Unit::Cm);
@ -114,6 +116,14 @@ VPMainWindow::VPMainWindow(const VPCommandLinePtr &cmd, QWidget *parent) :
AboutToShowDockMenu(); AboutToShowDockMenu();
menu->setAsDockMenu(); menu->setAsDockMenu();
#endif //defined(Q_OS_MAC) #endif //defined(Q_OS_MAC)
connect(m_layoutWatcher, &QFileSystemWatcher::fileChanged, this, [this](const QString &path)
{
if (not curFile.isEmpty() && curFile == path)
{
UpdateWindowTitle();
}
});
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -222,15 +232,24 @@ auto VPMainWindow::LoadFile(QString path) -> bool
void VPMainWindow::LayoutWasSaved(bool saved) void VPMainWindow::LayoutWasSaved(bool saved)
{ {
setWindowModified(!saved); setWindowModified(!saved);
not lIsReadOnly ? ui->actionSave->setEnabled(!saved): ui->actionSave->setEnabled(false); not IsLayoutReadOnly() ? ui->actionSave->setEnabled(!saved): ui->actionSave->setEnabled(false);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPMainWindow::SetCurrentFile(const QString &fileName) void VPMainWindow::SetCurrentFile(const QString &fileName)
{ {
if (not curFile.isEmpty() && m_layoutWatcher->files().contains(curFile))
{
m_layoutWatcher->removePath(curFile);
}
curFile = fileName; curFile = fileName;
if (not curFile.isEmpty()) if (not curFile.isEmpty())
{ {
if (not m_layoutWatcher->files().contains(curFile))
{
m_layoutWatcher->addPath(curFile);
}
auto *settings = VPApplication::VApp()->PuzzleSettings(); auto *settings = VPApplication::VApp()->PuzzleSettings();
QStringList files = settings->GetRecentFileList(); QStringList files = settings->GetRecentFileList();
files.removeAll(fileName); files.removeAll(fileName);
@ -292,6 +311,7 @@ void VPMainWindow::ImportRawLayouts(const QStringList &rawLayouts)
} }
m_carrousel->Refresh(); m_carrousel->Refresh();
LayoutWasSaved(false);
} }
else else
{ {
@ -819,16 +839,8 @@ void VPMainWindow::SetCheckBoxValue(QCheckBox *checkbox, bool value)
void VPMainWindow::UpdateWindowTitle() void VPMainWindow::UpdateWindowTitle()
{ {
QString showName; QString showName;
bool isFileWritable = true;
if (not curFile.isEmpty()) if (not curFile.isEmpty())
{ {
#ifdef Q_OS_WIN32
qt_ntfs_permission_lookup++; // turn checking on
#endif /*Q_OS_WIN32*/
isFileWritable = QFileInfo(curFile).isWritable();
#ifdef Q_OS_WIN32
qt_ntfs_permission_lookup--; // turn it off again
#endif /*Q_OS_WIN32*/
showName = StrippedName(curFile); showName = StrippedName(curFile);
} }
else else
@ -846,7 +858,7 @@ void VPMainWindow::UpdateWindowTitle()
showName += QLatin1String("[*]"); showName += QLatin1String("[*]");
if (lIsReadOnly || not isFileWritable) if (IsLayoutReadOnly())
{ {
showName += QStringLiteral(" (") + tr("read only") + QChar(')'); showName += QStringLiteral(" (") + tr("read only") + QChar(')');
} }
@ -942,7 +954,7 @@ auto VPMainWindow::MaybeSave() -> bool
messageBox->setDefaultButton(QMessageBox::Yes); messageBox->setDefaultButton(QMessageBox::Yes);
messageBox->setEscapeButton(QMessageBox::Cancel); messageBox->setEscapeButton(QMessageBox::Cancel);
messageBox->setButtonText(QMessageBox::Yes, curFile.isEmpty() || lIsReadOnly ? tr("Save…") : tr("Save")); messageBox->setButtonText(QMessageBox::Yes, curFile.isEmpty() || IsLayoutReadOnly() ? tr("Save…") : tr("Save"));
messageBox->setButtonText(QMessageBox::No, tr("Don't Save")); messageBox->setButtonText(QMessageBox::No, tr("Don't Save"));
messageBox->setWindowModality(Qt::ApplicationModal); messageBox->setWindowModality(Qt::ApplicationModal);
@ -951,7 +963,7 @@ auto VPMainWindow::MaybeSave() -> bool
switch (ret) switch (ret)
{ {
case QMessageBox::Yes: case QMessageBox::Yes:
if (lIsReadOnly) if (IsLayoutReadOnly())
{ {
return on_actionSaveAs_triggered(); return on_actionSaveAs_triggered();
} }
@ -1095,6 +1107,34 @@ void VPMainWindow::AddSheet()
m_layout->LayoutSettings().SetPiecesGapConverted(1); m_layout->LayoutSettings().SetPiecesGapConverted(1);
} }
//---------------------------------------------------------------------------------------------------------------------
auto VPMainWindow::IsLayoutReadOnly() const -> bool
{
if (curFile.isEmpty())
{
return false;
}
QFileInfo f(curFile);
if (not f.exists())
{
return false;
}
#ifdef Q_OS_WIN32
qt_ntfs_permission_lookup++; // turn checking on
#endif /*Q_OS_WIN32*/
bool fileWritable = f.isWritable();
#ifdef Q_OS_WIN32
qt_ntfs_permission_lookup--; // turn it off again
#endif /*Q_OS_WIN32*/
return not fileWritable;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPMainWindow::on_actionNew_triggered() void VPMainWindow::on_actionNew_triggered()
{ {
@ -1186,7 +1226,7 @@ void VPMainWindow::on_actionOpen_triggered()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VPMainWindow::on_actionSave_triggered() bool VPMainWindow::on_actionSave_triggered()
{ {
if (curFile.isEmpty() || lIsReadOnly) if (curFile.isEmpty() || IsLayoutReadOnly())
{ {
return on_actionSaveAs_triggered(); return on_actionSaveAs_triggered();
} }
@ -1267,6 +1307,11 @@ bool VPMainWindow::on_actionSaveAs_triggered()
fileName += QChar('.') + suffix; fileName += QChar('.') + suffix;
} }
if (not CheckFilePermissions(fileName, this))
{
return false;
}
if (QFileInfo::exists(fileName) && curFile != fileName) if (QFileInfo::exists(fileName) && curFile != fileName)
{ {
// Temporary try to lock the file before saving // Temporary try to lock the file before saving
@ -1279,12 +1324,6 @@ bool VPMainWindow::on_actionSaveAs_triggered()
} }
} }
// Need for restoring previous state in case of failure
// const bool readOnly = m_layout->IsReadOnly();
// m_layout->SetReadOnly(false);
lIsReadOnly = false;
QString error; QString error;
bool result = SaveLayout(fileName, error); bool result = SaveLayout(fileName, error);
if (not result) if (not result)
@ -1296,17 +1335,12 @@ bool VPMainWindow::on_actionSaveAs_triggered()
messageBox.setDetailedText(error); messageBox.setDetailedText(error);
messageBox.setStandardButtons(QMessageBox::Ok); messageBox.setStandardButtons(QMessageBox::Ok);
messageBox.exec(); messageBox.exec();
// Restore previous state
// m_layout->SetReadOnly(readOnly);
// lIsReadOnly = readOnly;
return false; return false;
} }
m_curFileFormatVersion = VLayoutConverter::LayoutMaxVer; m_curFileFormatVersion = VLayoutConverter::LayoutMaxVer;
m_curFileFormatVersionStr = VLayoutConverter::LayoutMaxVerStr; m_curFileFormatVersionStr = VLayoutConverter::LayoutMaxVerStr;
// UpdatePadlock(false);
UpdateWindowTitle(); UpdateWindowTitle();
if (curFile == fileName && not lock.isNull()) if (curFile == fileName && not lock.isNull())

View File

@ -51,6 +51,8 @@ namespace Ui
class VPMainWindow; class VPMainWindow;
} }
class QFileSystemWatcher;
class VPMainWindow : public VAbstractMainWindow class VPMainWindow : public VAbstractMainWindow
{ {
Q_OBJECT Q_OBJECT
@ -435,10 +437,11 @@ private:
QString curFile{}; QString curFile{};
bool isInitialized{false}; bool isInitialized{false};
bool lIsReadOnly{false};
QSharedPointer<VLockGuard<char>> lock{nullptr}; QSharedPointer<VLockGuard<char>> lock{nullptr};
QFileSystemWatcher *m_layoutWatcher;
/** /**
* @brief CreatePiece creates a piece from the given VLayoutPiece data * @brief CreatePiece creates a piece from the given VLayoutPiece data
* @param rawPiece the raw piece data * @param rawPiece the raw piece data
@ -555,6 +558,8 @@ private:
void CreateWindowMenu(QMenu *menu); void CreateWindowMenu(QMenu *menu);
void AddSheet(); void AddSheet();
auto IsLayoutReadOnly() const -> bool;
}; };
#endif // VPMAINWINDOW_H #endif // VPMAINWINDOW_H

View File

@ -264,7 +264,7 @@ auto VAbstractMainWindow::CheckFilePermissions(const QString &path, QWidget *mes
{ {
QMessageBox messageBox(messageBoxParent); QMessageBox messageBox(messageBoxParent);
messageBox.setIcon(QMessageBox::Question); messageBox.setIcon(QMessageBox::Question);
messageBox.setText(tr("The measurements document has no write permissions.")); messageBox.setText(tr("The file has no write permissions."));
messageBox.setInformativeText(tr("Do you want to change the premissions?")); messageBox.setInformativeText(tr("Do you want to change the premissions?"));
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
messageBox.setDefaultButton(QMessageBox::Yes); messageBox.setDefaultButton(QMessageBox::Yes);