From be336aba41030e8c265244ff025d77d1ff9b60aa Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 20 Aug 2015 11:54:48 +0300 Subject: [PATCH] Watch changes a measurement file. --HG-- branch : feature --- src/app/tape/main.cpp | 4 +- src/app/valentina/mainwindow.cpp | 98 +++++++++++++++++++++++++++----- src/app/valentina/mainwindow.h | 7 ++- 3 files changed, 91 insertions(+), 18 deletions(-) diff --git a/src/app/tape/main.cpp b/src/app/tape/main.cpp index 82e70d372..3bfba9407 100644 --- a/src/app/tape/main.cpp +++ b/src/app/tape/main.cpp @@ -107,7 +107,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main", "Error: Invalid base height argument. Must be 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 158, " - "164, 170, 176, 182 or 188."))); + "164, 170, 176, 182 or 188."))); parser.showHelp(1); } } @@ -127,7 +127,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main", "Error: Invalid base size argument. Must be 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, " - "52, 54 or 56."))); + "52, 54 or 56."))); parser.showHelp(1); } } diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index 02c74aecd..32c321f0b 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -67,6 +67,8 @@ #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) # include #endif +#include +#include Q_LOGGING_CATEGORY(vMainWindow, "v.mainwindow") @@ -76,14 +78,14 @@ Q_LOGGING_CATEGORY(vMainWindow, "v.mainwindow") * @param parent parent widget. */ MainWindow::MainWindow(QWidget *parent) - :MainWindowsNoGUI(parent), ui(new Ui::MainWindow), currentTool(Tool::Arrow), + :MainWindowsNoGUI(parent), ui(new Ui::MainWindow), watcher(new QFileSystemWatcher(this)), currentTool(Tool::Arrow), lastUsedTool(Tool::Arrow), sceneDraw(nullptr), sceneDetails(nullptr), mouseCoordinate(nullptr), helpLabel(nullptr), isInitialized(false), dialogTable(nullptr), dialogTool(nullptr), dialogHistory(nullptr), comboBoxDraws(nullptr), mode(Draw::Calculation), currentDrawIndex(0), currentToolBoxIndex(0), drawMode(true), recentFileActs(), separatorAct(nullptr), - leftGoToStage(nullptr), rightGoToStage(nullptr), autoSaveTimer(nullptr), guiEnabled(true), gradationHeights(nullptr), - gradationSizes(nullptr), + leftGoToStage(nullptr), rightGoToStage(nullptr), autoSaveTimer(nullptr), guiEnabled(true), + gradationHeights(nullptr), gradationSizes(nullptr), #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) toolOptions(nullptr), lock(nullptr) #else @@ -139,6 +141,8 @@ MainWindow::MainWindow(QWidget *parent) connect(ui->listWidget, &QListWidget::currentRowChanged, this, &MainWindow::ShowPaper); ui->dockWidgetLayoutPages->setVisible(false); + + connect(watcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::MeasurementsChanged); } //--------------------------------------------------------------------------------------------------------------------- @@ -233,11 +237,11 @@ void MainWindow::InitScenes() } //--------------------------------------------------------------------------------------------------------------------- -void MainWindow::LoadMeasurements(const QString &path) +bool MainWindow::LoadMeasurements(const QString &path) { if (path.isEmpty()) { - return; + return false; } VMeasurements *m = nullptr; @@ -280,7 +284,7 @@ void MainWindow::LoadMeasurements(const QString &path) QMessageBox::critical(this, tr("Wrong units."), tr("Application doesn't support standard table with inches.")); qCDebug(vMainWindow, "Application doesn't support standard table with inches."); - return; + return false; } m->SetDataSize(); m->SetDataHeight(); @@ -291,17 +295,14 @@ void MainWindow::LoadMeasurements(const QString &path) pattern->ClearVariables(VarType::Measurement); m->ReadMeasurements(); delete m; - doc->SetPath(path); - PatternWasModified(false); - ui->actionShowM->setEnabled(true); } catch (VException &e) { e.CriticalMessageBox(tr("File error."), this); delete m; - return; + return false; } - helpLabel->setText(tr("Measurements loaded")); + return true; } //--------------------------------------------------------------------------------------------------------------------- @@ -984,8 +985,19 @@ void MainWindow::LoadIndividual() if (not mPath.isEmpty()) { - LoadMeasurements(mPath); - doc->LiteParseTree(Document::LiteParse); + if (LoadMeasurements(mPath)) + { + if (not doc->MPath().isEmpty()) + { + watcher->removePath(doc->MPath()); + } + doc->SetPath(mPath); + watcher->addPath(mPath); + PatternWasModified(false); + ui->actionShowM->setEnabled(true); + helpLabel->setText(tr("Measurements loaded")); + doc->LiteParseTree(Document::LiteParse); + } } } @@ -999,8 +1011,19 @@ void MainWindow::LoadStandard() if (not mPath.isEmpty()) { - LoadMeasurements(mPath); - doc->LiteParseTree(Document::LiteParse); + if(LoadMeasurements(mPath)) + { + if (not doc->MPath().isEmpty()) + { + watcher->removePath(doc->MPath()); + } + doc->SetPath(mPath); + watcher->addPath(mPath); + PatternWasModified(false); + ui->actionShowM->setEnabled(true); + helpLabel->setText(tr("Measurements loaded")); + doc->LiteParseTree(Document::LiteParse); + } } } @@ -1036,6 +1059,51 @@ void MainWindow::ShowMeasurements() } } +//--------------------------------------------------------------------------------------------------------------------- +void MainWindow::MeasurementsChanged(const QString &path) +{ + bool mChanges = false; + QFileInfo checkFile(path); + if (checkFile.exists()) + { + mChanges = true; + } + else + { + for(int i=0; i<=1000; i=i+10) + { + if (checkFile.exists()) + { + mChanges = true; + break; + } + else + { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + } + } + + if (mChanges) + { + QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Update measurements"), + tr("Measurement file was changed. Do you want to update?"), + QMessageBox::Yes|QMessageBox::No); + if (reply == QMessageBox::Yes) + { + if(LoadMeasurements(path)) + { + if (not watcher->files().contains(path)) + { + watcher->addPath(path); + } + helpLabel->setText(tr("Measurements updated")); + doc->LiteParseTree(Document::LiteParse); + } + } + } +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief ToolBarOption enable option toolbar. diff --git a/src/app/valentina/mainwindow.h b/src/app/valentina/mainwindow.h index 1dda13336..617652ce8 100644 --- a/src/app/valentina/mainwindow.h +++ b/src/app/valentina/mainwindow.h @@ -37,6 +37,8 @@ #include "tools/vtooluniondetails.h" #include "tools/drawTools/drawtools.h" +#include + namespace Ui { class MainWindow; @@ -164,11 +166,14 @@ private slots: void LoadStandard(); void CreateMeasurements(); void ShowMeasurements(); + void MeasurementsChanged(const QString &path); private: Q_DISABLE_COPY(MainWindow) /** @brief ui keeps information about user interface */ Ui::MainWindow *ui; + QFileSystemWatcher *watcher; + /** @brief tool current tool */ Tool currentTool; @@ -288,7 +293,7 @@ private: void InitScenes(); - void LoadMeasurements(const QString &path); + bool LoadMeasurements(const QString &path); }; #endif // MAINWINDOW_H