Refactoring.

This commit is contained in:
Roman Telezhynskyi 2023-05-09 19:28:11 +03:00
parent fbbcf5320f
commit 4a7ce91f65
2 changed files with 73 additions and 79 deletions

View File

@ -50,11 +50,10 @@
#include "../vformat/vwatermark.h" #include "../vformat/vwatermark.h"
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
WatermarkWindow::WatermarkWindow(const QString &patternPath, QWidget *parent) : WatermarkWindow::WatermarkWindow(const QString &patternPath, QWidget *parent)
QMainWindow(parent), : QMainWindow(parent),
ui(new Ui::WatermarkWindow), ui(new Ui::WatermarkWindow),
m_patternPath(patternPath), m_patternPath(patternPath)
m_okPathColor()
{ {
ui->setupUi(this); ui->setupUi(this);
m_okPathColor = ui->lineEditPath->palette().color(ui->lineEditPath->foregroundRole()); m_okPathColor = ui->lineEditPath->palette().color(ui->lineEditPath->foregroundRole());
@ -274,7 +273,7 @@ void WatermarkWindow::showEvent(QShowEvent *event)
// do your init stuff here // do your init stuff here
QSize sz = VAbstractApplication::VApp()->Settings()->GetWatermarkEditorSize(); QSize sz = VAbstractApplication::VApp()->Settings()->GetWatermarkEditorSize();
if (sz.isEmpty() == false) if (not sz.isEmpty())
{ {
resize(sz); resize(sz);
} }
@ -384,78 +383,76 @@ auto WatermarkWindow::on_actionSave_triggered() -> bool
{ {
return on_actionSaveAs_triggered(); return on_actionSaveAs_triggered();
} }
else
if (m_curFileFormatVersion < VWatermarkConverter::WatermarkMaxVer &&
not ContinueFormatRewrite(m_curFileFormatVersionStr, VWatermarkConverter::WatermarkMaxVerStr))
{ {
if (m_curFileFormatVersion < VWatermarkConverter::WatermarkMaxVer return false;
&& not ContinueFormatRewrite(m_curFileFormatVersionStr, VWatermarkConverter::WatermarkMaxVerStr)) }
// #ifdef Q_OS_WIN32
// qt_ntfs_permission_lookup++; // turn checking on
// #endif /*Q_OS_WIN32*/
const bool isFileWritable = QFileInfo(m_curFile).isWritable();
// #ifdef Q_OS_WIN32
// qt_ntfs_permission_lookup--; // turn it off again
// #endif /*Q_OS_WIN32*/
if (not isFileWritable)
{
QMessageBox messageBox(this);
messageBox.setIcon(QMessageBox::Question);
messageBox.setText(tr("The document has no write permissions."));
messageBox.setInformativeText(tr("Do you want to change the permissions?"));
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
messageBox.setDefaultButton(QMessageBox::Yes);
if (messageBox.exec() == QMessageBox::Yes)
{ {
return false; // #ifdef Q_OS_WIN32
} // qt_ntfs_permission_lookup++; // turn checking on
// #endif /*Q_OS_WIN32*/
bool changed =
QFile::setPermissions(m_curFile, QFileInfo(m_curFile).permissions() | QFileDevice::WriteUser);
// #ifdef Q_OS_WIN32
// qt_ntfs_permission_lookup--; // turn it off again
// #endif /*Q_OS_WIN32*/
//#ifdef Q_OS_WIN32 if (not changed)
// qt_ntfs_permission_lookup++; // turn checking on
//#endif /*Q_OS_WIN32*/
const bool isFileWritable = QFileInfo(m_curFile).isWritable();
//#ifdef Q_OS_WIN32
// qt_ntfs_permission_lookup--; // turn it off again
//#endif /*Q_OS_WIN32*/
if (not isFileWritable)
{
QMessageBox messageBox(this);
messageBox.setIcon(QMessageBox::Question);
messageBox.setText(tr("The document has no write permissions."));
messageBox.setInformativeText(tr("Do you want to change the permissions?"));
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
messageBox.setDefaultButton(QMessageBox::Yes);
if (messageBox.exec() == QMessageBox::Yes)
{
//#ifdef Q_OS_WIN32
// qt_ntfs_permission_lookup++; // turn checking on
//#endif /*Q_OS_WIN32*/
bool changed = QFile::setPermissions(m_curFile,
QFileInfo(m_curFile).permissions() | QFileDevice::WriteUser);
//#ifdef Q_OS_WIN32
// qt_ntfs_permission_lookup--; // turn it off again
//#endif /*Q_OS_WIN32*/
if (not changed)
{
QMessageBox messageBoxWarning(this);
messageBoxWarning.setIcon(QMessageBox::Warning);
messageBoxWarning.setText(tr("Cannot set permissions for %1 to writable.").arg(m_curFile));
messageBoxWarning.setInformativeText(tr("Could not save the file."));
messageBoxWarning.setDefaultButton(QMessageBox::Ok);
messageBoxWarning.setStandardButtons(QMessageBox::Ok);
messageBoxWarning.exec();
return false;
}
}
else
{ {
QMessageBox messageBoxWarning(this);
messageBoxWarning.setIcon(QMessageBox::Warning);
messageBoxWarning.setText(tr("Cannot set permissions for %1 to writable.").arg(m_curFile));
messageBoxWarning.setInformativeText(tr("Could not save the file."));
messageBoxWarning.setDefaultButton(QMessageBox::Ok);
messageBoxWarning.setStandardButtons(QMessageBox::Ok);
messageBoxWarning.exec();
return false; return false;
} }
} }
QString error;
bool result = SaveWatermark(m_curFile, error);
if (result)
{
m_curFileFormatVersion = VWatermarkConverter::WatermarkMaxVer;
m_curFileFormatVersionStr = VWatermarkConverter::WatermarkMaxVerStr;
}
else else
{ {
QMessageBox messageBox(this); return false;
messageBox.setIcon(QMessageBox::Warning);
messageBox.setText(tr("Could not save the file"));
messageBox.setDefaultButton(QMessageBox::Ok);
messageBox.setDetailedText(error);
messageBox.setStandardButtons(QMessageBox::Ok);
messageBox.exec();
} }
return result;
} }
QString error;
bool result = SaveWatermark(m_curFile, error);
if (result)
{
m_curFileFormatVersion = VWatermarkConverter::WatermarkMaxVer;
m_curFileFormatVersionStr = VWatermarkConverter::WatermarkMaxVerStr;
}
else
{
QMessageBox messageBox(this);
messageBox.setIcon(QMessageBox::Warning);
messageBox.setText(tr("Could not save the file"));
messageBox.setDefaultButton(QMessageBox::Ok);
messageBox.setDetailedText(error);
messageBox.setStandardButtons(QMessageBox::Ok);
messageBox.exec();
}
return result;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -607,10 +604,7 @@ auto WatermarkWindow::ContinueFormatRewrite(const QString &currentFormatVersion,
VAbstractApplication::VApp()->Settings()->SetConfirmFormatRewriting(not msgBox.isChecked()); VAbstractApplication::VApp()->Settings()->SetConfirmFormatRewriting(not msgBox.isChecked());
return true; return true;
} }
else return false;
{
return false;
}
} }
return true; return true;
} }
@ -652,7 +646,7 @@ void WatermarkWindow::SetCurrentFile(const QString &fileName)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
auto WatermarkWindow::OpenNewEditor(const QString &fileName) const -> bool auto WatermarkWindow::OpenNewEditor(const QString &fileName) -> bool
{ {
if (this->isWindowModified() || not m_curFile.isEmpty()) if (this->isWindowModified() || not m_curFile.isEmpty())
{ {

View File

@ -44,7 +44,7 @@ class WatermarkWindow : public QMainWindow
public: public:
explicit WatermarkWindow(const QString &patternPath, QWidget *parent = nullptr); explicit WatermarkWindow(const QString &patternPath, QWidget *parent = nullptr);
~WatermarkWindow(); ~WatermarkWindow() override;
auto CurrentFile() const -> QString; auto CurrentFile() const -> QString;
@ -52,13 +52,13 @@ public:
signals: signals:
void New(); void New();
void OpenAnother(const QString &fileName) const; void OpenAnother(const QString &fileName);
protected: protected:
virtual void closeEvent(QCloseEvent *event) override; void closeEvent(QCloseEvent *event) override;
virtual void changeEvent(QEvent* event) override; void changeEvent(QEvent *event) override;
virtual void showEvent(QShowEvent *event) override; void showEvent(QShowEvent *event) override;
virtual void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
private slots: private slots:
void on_actionNew_triggered(); void on_actionNew_triggered();
@ -76,7 +76,7 @@ private:
QString m_patternPath; QString m_patternPath;
QString m_curFile{}; QString m_curFile{};
VWatermarkData m_data{}; VWatermarkData m_data{};
QColor m_okPathColor; QColor m_okPathColor{};
bool m_isInitialized{false}; bool m_isInitialized{false};
unsigned m_curFileFormatVersion{0x0}; unsigned m_curFileFormatVersion{0x0};
@ -92,7 +92,7 @@ private:
auto ContinueFormatRewrite(const QString &currentFormatVersion, const QString &maxFormatVersion) -> bool; auto ContinueFormatRewrite(const QString &currentFormatVersion, const QString &maxFormatVersion) -> bool;
auto SaveWatermark(const QString &fileName, QString &error) -> bool; auto SaveWatermark(const QString &fileName, QString &error) -> bool;
void SetCurrentFile(const QString &fileName); void SetCurrentFile(const QString &fileName);
auto OpenNewEditor(const QString &fileName = QString()) const -> bool; auto OpenNewEditor(const QString &fileName = QString()) -> bool;
void Clear(); void Clear();
auto IgnoreLocking(int error, const QString &path) -> bool; auto IgnoreLocking(int error, const QString &path) -> bool;
void ShowWatermark(); void ShowWatermark();