Lock file. Save windows geometry.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2015-08-01 20:09:10 +03:00
parent d9ca57d550
commit 6e6b040b60
12 changed files with 154 additions and 54 deletions

View File

@ -42,6 +42,8 @@ DialogMDataBase::DialogMDataBase(const QStringList &list, QWidget *parent)
connect(ui->treeWidget, &QTreeWidget::itemChanged, this, &DialogMDataBase::UpdateChecks);
connect(ui->treeWidget, &QTreeWidget::itemClicked, this, &DialogMDataBase::ShowDescription);
ReadSettings();
}
//---------------------------------------------------------------------------------------------------------------------
@ -56,11 +58,14 @@ DialogMDataBase::DialogMDataBase(QWidget *parent)
InitDataBase();
connect(ui->treeWidget, &QTreeWidget::itemClicked, this, &DialogMDataBase::ShowDescription);
ReadSettings();
}
//---------------------------------------------------------------------------------------------------------------------
DialogMDataBase::~DialogMDataBase()
{
WriteSettings();
delete ui;
}
@ -1510,3 +1515,15 @@ QString DialogMDataBase::MapDiagrams(const QString &number) const
return QString();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMDataBase::ReadSettings()
{
restoreGeometry(qApp->TapeSettings()->GetDataBaseGeometry());
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMDataBase::WriteSettings()
{
qApp->TapeSettings()->SetDataBaseGeometry(saveGeometry());
}

View File

@ -45,7 +45,7 @@ class DialogMDataBase : public QDialog
public:
DialogMDataBase(const QStringList &list, QWidget *parent = 0);
explicit DialogMDataBase(QWidget *parent = 0);
~DialogMDataBase();
virtual ~DialogMDataBase() Q_DECL_OVERRIDE;
QStringList GetNewNames() const;
@ -84,6 +84,9 @@ private:
void AddMeasurement(QTreeWidgetItem *group, const QString &name, const QStringList &list);
QString MapDiagrams(const QString &number) const;
void ReadSettings();
void WriteSettings();
};
#endif // DIALOGMDATABASE_H

View File

@ -16,7 +16,8 @@ HEADERS += \
$$PWD/mapplication.h \
$$PWD/dialogs/dialogabouttape.h \
$$PWD/dialogs/dialognewmeasurements.h \
$$PWD/dialogs/dialogmdatabase.h
$$PWD/dialogs/dialogmdatabase.h \
$$PWD/version.h
FORMS += \
$$PWD/tmainwindow.ui \

View File

@ -37,6 +37,7 @@
#include "../ifc/xml/vvstconverter.h"
#include "../qmuparser/qmudef.h"
#include "../vtools/dialogs/support/dialogeditwrongformula.h"
#include "version.h"
#include "mapplication.h" // Should be last because of definning qApp
#include <QFileDialog>
@ -44,9 +45,14 @@
#include <QMessageBox>
#include <QComboBox>
#include <QProcess>
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
# include <QLockFile>
#endif
#define DIALOG_MAX_FORMULA_HEIGHT 64
Q_LOGGING_CATEGORY(tMainWindow, "t.mainwindow")
//---------------------------------------------------------------------------------------------------------------------
TMainWindow::TMainWindow(QWidget *parent)
:QMainWindow(parent),
@ -58,7 +64,8 @@ TMainWindow::TMainWindow(QWidget *parent)
curFile(),
gradationHeights(nullptr),
gradationSizes(nullptr),
formulaBaseHeight(0)
formulaBaseHeight(0),
lock(nullptr)
{
ui->setupUi(this);
ui->tabWidget->setVisible(false);
@ -70,6 +77,8 @@ TMainWindow::TMainWindow(QWidget *parent)
SetupMenu();
setWindowTitle(tr("untitled %1").arg(qApp->MainWindows().size()+1));
ReadSettings();
}
//---------------------------------------------------------------------------------------------------------------------
@ -84,6 +93,11 @@ TMainWindow::~TMainWindow()
{
delete m;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
delete lock; // Unlock pattern file
#endif
delete ui;
}
@ -105,10 +119,24 @@ void TMainWindow::LoadFile(const QString &path)
if (list.at(i)->CurrentFile() == path)
{
list.at(i)->activateWindow();
close();
return;
}
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
lock = new QLockFile(QFileInfo(path).fileName()+".lock");
lock->setStaleLockTime(0);
if (not MApplication::TryLock(lock))
{
if (lock->error() == QLockFile::LockFailedError)
{
qCCritical(tMainWindow, "%s", tr("This file already opened in another window.").toUtf8().constData());
return;
}
}
#endif //QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
try
{
data = new VContainer(qApp->TrVars(), &mUnit);
@ -277,6 +305,7 @@ void TMainWindow::closeEvent(QCloseEvent *event)
{
if (MaybeSave())
{
WriteSettings();
event->accept();
deleteLater();
}
@ -1722,6 +1751,25 @@ void TMainWindow::GUIReadOnly(bool ro)
Controls(); // Buttons remove, up, down
}
//---------------------------------------------------------------------------------------------------------------------
void TMainWindow::ReadSettings()
{
restoreGeometry(qApp->TapeSettings()->GetGeometry());
restoreState(qApp->TapeSettings()->GetWindowState());
restoreState(qApp->TapeSettings()->GetToolbarsState(), APP_VERSION);
// Stack limit
//qApp->getUndoStack()->setUndoLimit(qApp->TapeSettings()->GetUndoCount());
}
//---------------------------------------------------------------------------------------------------------------------
void TMainWindow::WriteSettings()
{
qApp->TapeSettings()->SetGeometry(saveGeometry());
qApp->TapeSettings()->SetWindowState(saveState());
qApp->TapeSettings()->SetToolbarsState(saveState(APP_VERSION));
}
//---------------------------------------------------------------------------------------------------------------------
void TMainWindow::SetDecimals()
{

View File

@ -42,6 +42,9 @@ namespace Ui
class QComboBox;
class QTableWidgetItem;
class QLabel;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
class QLockFile;
#endif
class TMainWindow : public QMainWindow
{
@ -116,6 +119,10 @@ private:
QComboBox *gradationSizes;
int formulaBaseHeight;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
QLockFile *lock;
#endif
void SetupMenu();
void InitWindow();
void InitTable();
@ -148,6 +155,9 @@ private:
void Open(const QString &pathTo, const QString &filter);
void GUIReadOnly(bool ro);
void ReadSettings();
void WriteSettings();
};
#endif // TMAINWINDOW_H

View File

@ -48,9 +48,6 @@
#include <QMessageBox>
#include <QThread>
#include <QDateTime>
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
# include <QLockFile>
#endif
#include <QtXmlPatterns>
Q_LOGGING_CATEGORY(vApp, "v.application")
@ -274,46 +271,6 @@ bool VApplication::notify(QObject *receiver, QEvent *event)
return false;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
//---------------------------------------------------------------------------------------------------------------------
bool VApplication::TryLock(QLockFile *lock)
{
if (lock == nullptr)
{
return false;
}
if (lock->tryLock())
{
return true;
}
else
{
if (lock->error() == QLockFile::LockFailedError)
{
// This happens if a stale lock file exists and another process uses that PID.
// Try removing the stale file, which will fail if a real process is holding a
// file-level lock. A false error is more problematic than not locking properly
// on corner-case systems.
if (lock->removeStaleLockFile() == false || lock->tryLock() == false)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
return false;
}
}
#endif //QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
//---------------------------------------------------------------------------------------------------------------------
QString VApplication::translationsPath() const
{

View File

@ -39,9 +39,6 @@ class VApplication;// use in define
class VMainGraphicsView;
class VPattern;
class QFile;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
class QLockFile;
#endif
#if defined(qApp)
#undef qApp
@ -62,10 +59,6 @@ public:
void InitOptions();
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
static bool TryLock(QLockFile *lock);
#endif //QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
QString translationsPath() const;
QTimer *getAutoSaveTimer() const;

View File

@ -43,6 +43,9 @@ namespace Ui
}
class VToolOptionsPropertyBrowser;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
class QLockFile;
#endif
/**
* @brief The MainWindow class main windows.

View File

@ -29,6 +29,10 @@
#include "vabstractapplication.h"
#include "../vmisc/def.h"
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
# include <QLockFile>
#endif
//---------------------------------------------------------------------------------------------------------------------
VAbstractApplication::VAbstractApplication(int &argc, char **argv)
:QApplication(argc, argv),
@ -112,3 +116,43 @@ double VAbstractApplication::fromPixel(double pix) const
{
return FromPixel(pix, _patternUnit);
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
//---------------------------------------------------------------------------------------------------------------------
bool VAbstractApplication::TryLock(QLockFile *lock)
{
if (lock == nullptr)
{
return false;
}
if (lock->tryLock())
{
return true;
}
else
{
if (lock->error() == QLockFile::LockFailedError)
{
// This happens if a stale lock file exists and another process uses that PID.
// Try removing the stale file, which will fail if a real process is holding a
// file-level lock. A false error is more problematic than not locking properly
// on corner-case systems.
if (lock->removeStaleLockFile() == false || lock->tryLock() == false)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
return false;
}
}
#endif //QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)

View File

@ -39,6 +39,9 @@ class VTranslateVars;
class VAbstractPattern;
class VMainGraphicsView;
class QUndoStack;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
class QLockFile;
#endif
#if defined(qApp)
#undef qApp
@ -86,6 +89,10 @@ public:
QUndoStack *getUndoStack() const;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
static bool TryLock(QLockFile *lock);
#endif //QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
protected:
QUndoStack *undoStack;

View File

@ -32,6 +32,7 @@
#include <QDir>
const QString VTapeSettings::SettingPathsTemplates = QStringLiteral("paths/templates");
const QString VTapeSettings::SettingDataBaseGeometry = QStringLiteral("database/geometry");
//---------------------------------------------------------------------------------------------------------------------
VTapeSettings::VTapeSettings(Format format, Scope scope, const QString &organization, const QString &application,
@ -161,3 +162,15 @@ void VTapeSettings::SetPathTemplate(const QString &value)
{
setValue(SettingPathsTemplates, value);
}
//---------------------------------------------------------------------------------------------------------------------
QByteArray VTapeSettings::GetDataBaseGeometry() const
{
return value(SettingDataBaseGeometry).toByteArray();
}
//---------------------------------------------------------------------------------------------------------------------
void VTapeSettings::SetDataBaseGeometry(const QByteArray &value)
{
setValue(SettingDataBaseGeometry, value);
}

View File

@ -44,10 +44,14 @@ public:
QString GetPathTemplate() const;
void SetPathTemplate(const QString &value);
QByteArray GetDataBaseGeometry() const;
void SetDataBaseGeometry(const QByteArray &value);
private:
Q_DISABLE_COPY(VTapeSettings)
static const QString SettingPathsTemplates;
static const QString SettingDataBaseGeometry;
};
#endif // VTAPESETTINGS_H