From 8bc20fa82f6e5446986275914894d8304738b72d Mon Sep 17 00:00:00 2001
From: Roman Telezhynskyi <kroluku@gmail.com>
Date: Tue, 11 Aug 2015 17:52:18 +0300
Subject: [PATCH] Import measurements from a pattern file.

--HG--
branch : feature
---
 src/app/tape/tmainwindow.cpp | 101 +++++++++++++++++++++++++++++++++++
 src/app/tape/tmainwindow.h   |   3 ++
 src/app/tape/tmainwindow.ui  |   9 ++++
 3 files changed, 113 insertions(+)

diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp
index ea0e90928..4ea535d5c 100644
--- a/src/app/tape/tmainwindow.cpp
+++ b/src/app/tape/tmainwindow.cpp
@@ -36,6 +36,8 @@
 #include "../ifc/ifcdef.h"
 #include "../ifc/xml/vvitconverter.h"
 #include "../ifc/xml/vvstconverter.h"
+#include "../ifc/xml/vpatternconverter.h"
+#include "vlitepattern.h"
 #include "../qmuparser/qmudef.h"
 #include "../vtools/dialogs/support/dialogeditwrongformula.h"
 #include "version.h"
@@ -781,6 +783,96 @@ void TMainWindow::AddKnown()
     delete dialog;
 }
 
+//---------------------------------------------------------------------------------------------------------------------
+void TMainWindow::ImportFromPattern()
+{
+    if (m == nullptr)
+    {
+        return;
+    }
+
+    const QString filter(tr("Pattern files (*.val)"));
+    //Use standard path to individual measurements
+    const QString pathTo = qApp->TapeSettings()->GetPathTemplate();
+
+    const QString mPath = QFileDialog::getOpenFileName(this, tr("Import from a pattern"), pathTo, filter);
+    if (mPath.isEmpty())
+    {
+        return;
+    }
+
+#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
+    QLockFile *lock = new QLockFile(QFileInfo(mPath).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)
+
+#ifdef Q_OS_WIN32
+    qt_ntfs_permission_lookup++; // turn checking on
+#endif /*Q_OS_WIN32*/
+
+    QStringList measurements;
+    try
+    {
+        VPatternConverter converter(mPath);
+        converter.Convert();
+
+        VDomDocument::ValidateXML(VPatternConverter::CurrentSchema, mPath);
+        VLitePattern *doc = new VLitePattern();
+        doc->setXMLContent(mPath);
+        measurements = doc->ListMeasurements();
+        delete doc; // close a pattern
+    }
+    catch (VException &e)
+    {
+        e.CriticalMessageBox(tr("File error."), this);
+        return;
+    }
+
+#ifdef Q_OS_WIN32
+    qt_ntfs_permission_lookup--; // turn it off again
+#endif /*Q_OS_WIN32*/
+
+    delete lock; // release a pattern file
+
+    measurements = FilterMeasurements(measurements, m->ListAll());
+
+    qint32 currentRow;
+
+    if (ui->tableWidget->currentRow() == -1)
+    {
+        currentRow  = ui->tableWidget->rowCount() + measurements.size() - 1;
+        for (int i = 0; i < measurements.size(); ++i)
+        {
+            m->AddEmpty(measurements.at(i));
+        }
+    }
+    else
+    {
+        currentRow  = ui->tableWidget->currentRow() + measurements.size();
+        QTableWidgetItem *nameField = ui->tableWidget->item(ui->tableWidget->currentRow(), 0);
+        QString after = nameField->text();
+        for (int i = 0; i < measurements.size(); ++i)
+        {
+            m->AddEmptyAfter(after, measurements.at(i));
+            after = measurements.at(i);
+        }
+    }
+
+    RefreshData();
+
+    ui->tableWidget->selectRow(currentRow);
+
+    MeasurementsWasSaved(false);
+}
+
 //---------------------------------------------------------------------------------------------------------------------
 void TMainWindow::ChangedSize(const QString &text)
 {
@@ -1206,6 +1298,7 @@ void TMainWindow::SetupMenu()
     connect(ui->actionAddCustom, &QAction::triggered, this, &TMainWindow::AddCustom);
     connect(ui->actionAddKnown, &QAction::triggered, this, &TMainWindow::AddKnown);
     connect(ui->actionDatabase, &QAction::triggered, qApp, &MApplication::ShowDataBase);
+    connect(ui->actionImportFromPattern, &QAction::triggered, this, &TMainWindow::ImportFromPattern);
 
     // Window
     connect(ui->menuWindow, &QMenu::aboutToShow, this, &TMainWindow::AboutToShowWindowMenu);
@@ -1333,6 +1426,7 @@ void TMainWindow::InitWindow()
 
     ui->actionAddCustom->setEnabled(true);
     ui->actionAddKnown->setEnabled(true);
+    ui->actionImportFromPattern->setEnabled(true);
     ui->actionReadOnly->setEnabled(true);
     ui->actionSaveAs->setEnabled(true);
 
@@ -1800,6 +1894,13 @@ void TMainWindow::WriteSettings()
     qApp->TapeSettings()->SetToolbarsState(saveState(APP_VERSION));
 }
 
+//---------------------------------------------------------------------------------------------------------------------
+QStringList TMainWindow::FilterMeasurements(const QStringList &mNew, const QStringList &mFilter)
+{
+    const QSet<QString> import = mNew.toSet().subtract(mFilter.toSet());
+    return QStringList(import.toList());
+}
+
 //---------------------------------------------------------------------------------------------------------------------
 void TMainWindow::SetDecimals()
 {
diff --git a/src/app/tape/tmainwindow.h b/src/app/tape/tmainwindow.h
index 71012ecfc..cb6caa939 100644
--- a/src/app/tape/tmainwindow.h
+++ b/src/app/tape/tmainwindow.h
@@ -91,6 +91,7 @@ private slots:
 
     void AddCustom();
     void AddKnown();
+    void ImportFromPattern();
 
     void ChangedSize(const QString &text);
     void ChangedHeight(const QString & text);
@@ -163,6 +164,8 @@ private:
 
     void ReadSettings();
     void WriteSettings();
+
+    QStringList FilterMeasurements(const QStringList &mNew, const QStringList &mFilter);
 };
 
 #endif // TMAINWINDOW_H
diff --git a/src/app/tape/tmainwindow.ui b/src/app/tape/tmainwindow.ui
index 11fd59094..293b74701 100644
--- a/src/app/tape/tmainwindow.ui
+++ b/src/app/tape/tmainwindow.ui
@@ -618,6 +618,7 @@
     <addaction name="actionAddKnown"/>
     <addaction name="actionAddCustom"/>
     <addaction name="actionDatabase"/>
+    <addaction name="actionImportFromPattern"/>
    </widget>
    <addaction name="menuFile"/>
    <addaction name="menuMeasurements"/>
@@ -800,6 +801,14 @@
     <string>Preferences</string>
    </property>
   </action>
+  <action name="actionImportFromPattern">
+   <property name="enabled">
+    <bool>false</bool>
+   </property>
+   <property name="text">
+    <string>Import from a pattern</string>
+   </property>
+  </action>
  </widget>
  <layoutdefault spacing="6" margin="11"/>
  <resources>