From de55cb20b3c28239f2b045a59a7485bd3483e49e Mon Sep 17 00:00:00 2001
From: PeterLValve <peterl@valvesoftware.com>
Date: Mon, 24 Mar 2014 12:07:58 -0700
Subject: [PATCH] UI: Update trim dialog so that it does not accept invalid
 input. * Clicking "OK" with invalid input would not set the member variables,
 but would return as accepted, resulting in a failed trim. * Improved error
 messages if trim_frame or trim_len are invalid, and also now prompt the user
 if the entered trim_file already exists.

---
 src/vogleditor/vogleditor_qtrimdialog.cpp | 55 ++++++++++++++++++-----
 src/vogleditor/vogleditor_qtrimdialog.h   |  4 +-
 src/vogleditor/vogleditor_qtrimdialog.ui  | 18 +-------
 3 files changed, 48 insertions(+), 29 deletions(-)

diff --git a/src/vogleditor/vogleditor_qtrimdialog.cpp b/src/vogleditor/vogleditor_qtrimdialog.cpp
index 6a4db8d..aeeef6a 100644
--- a/src/vogleditor/vogleditor_qtrimdialog.cpp
+++ b/src/vogleditor/vogleditor_qtrimdialog.cpp
@@ -1,6 +1,7 @@
 #include "vogleditor_qtrimdialog.h"
 #include "ui_vogleditor_qtrimdialog.h"
 #include <QFileDialog>
+#include <QMessageBox>
 
 vogleditor_QTrimDialog::vogleditor_QTrimDialog(QString parentTraceFile, uint maxFrameIndex, uint maxTrimLength, QWidget *parent) :
     QDialog(parent),
@@ -24,21 +25,53 @@ vogleditor_QTrimDialog::~vogleditor_QTrimDialog()
     delete ui;
 }
 
-void vogleditor_QTrimDialog::on_buttonBox_accepted()
+void vogleditor_QTrimDialog::on_buttonBox_clicked(QAbstractButton *button)
 {
-    // verify all input
-    bool bValidFrame = false;
-    uint tmpFrame = ui->trimFrameLineEdit->text().toUInt(&bValidFrame);
-    bValidFrame = bValidFrame && (tmpFrame <= m_maxFrameIndex);
+    if (button == ui->buttonBox->button(QDialogButtonBox::Ok))
+    {
+        // validate the trim start frame
+        bool bValidFrame = false;
+        uint tmpFrame = ui->trimFrameLineEdit->text().toUInt(&bValidFrame);
+        bValidFrame = bValidFrame && (tmpFrame <= m_maxFrameIndex);
 
-    bool bValidLen = false;
-    uint tmpLen = ui->trimLenLineEdit->text().toUInt(&bValidLen);
-    bValidLen = bValidLen && (tmpLen > 0 && tmpLen < m_maxTrimLength);
+        if (!bValidFrame)
+        {
+            QMessageBox::warning(this, tr("Invalid Trim Frame"), tr("Please enter a valid frame number at which to start the trim."),
+                                 QMessageBox::Ok, QMessageBox::Ok);
+            ui->trimFrameLineEdit->setFocus();
+            return;
+        }
 
-    bool bValidFile = (ui->trimFrameLineEdit->text().isEmpty() == false);
+        // validate the trim length
+        bool bValidLen = false;
+        uint tmpLen = ui->trimLenLineEdit->text().toUInt(&bValidLen);
+        bValidLen = bValidLen && (tmpLen > 0 && tmpLen <= m_maxTrimLength);
+
+        if (!bValidLen)
+        {
+            QMessageBox::warning(this, tr("Invalid Trim Count"), tr("Please enter a valid nubmer of frames to trim."),
+                                 QMessageBox::Ok, QMessageBox::Ok);
+            ui->trimLenLineEdit->setFocus();
+            return;
+        }
+
+        // validate the filename
+        QFile file(ui->trimFileLineEdit->text());
+        if (file.exists())
+        {
+            QString message = ui->trimFileLineEdit->text();
+            message += " already exits.\nWould you like to overwrite it?";
+            int ret = QMessageBox::warning(this, tr("File Already Exists"), tr(message.toStdString().c_str()),
+                                 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
+
+            if (ret == QMessageBox::No)
+            {
+                // return so that the user can update the path
+                ui->trimFileLineEdit->setFocus();
+                return;
+            }
+        }
 
-    if (bValidFrame && bValidLen && bValidFile)
-    {
         m_trim_frame = ui->trimFrameLineEdit->text();
         m_trim_len = ui->trimLenLineEdit->text();
         m_trim_file = ui->trimFileLineEdit->text();
diff --git a/src/vogleditor/vogleditor_qtrimdialog.h b/src/vogleditor/vogleditor_qtrimdialog.h
index a9ec3de..a952cfb 100644
--- a/src/vogleditor/vogleditor_qtrimdialog.h
+++ b/src/vogleditor/vogleditor_qtrimdialog.h
@@ -4,6 +4,8 @@
 #include <QDialog>
 #include <QString>
 
+class QAbstractButton;
+
 namespace Ui {
 class vogleditor_QTrimDialog;
 }
@@ -32,7 +34,7 @@ public:
     }
 
 private slots:
-    void on_buttonBox_accepted();
+    void on_buttonBox_clicked(QAbstractButton *button);
 
     void on_buttonBox_rejected();
 
diff --git a/src/vogleditor/vogleditor_qtrimdialog.ui b/src/vogleditor/vogleditor_qtrimdialog.ui
index d9cef6c..4851273 100644
--- a/src/vogleditor/vogleditor_qtrimdialog.ui
+++ b/src/vogleditor/vogleditor_qtrimdialog.ui
@@ -38,7 +38,7 @@
      <item>
       <widget class="QLabel" name="label_2">
        <property name="text">
-        <string>trim_frame:</string>
+        <string>trim_frame</string>
        </property>
       </widget>
      </item>
@@ -161,22 +161,6 @@
  </widget>
  <resources/>
  <connections>
-  <connection>
-   <sender>buttonBox</sender>
-   <signal>accepted()</signal>
-   <receiver>vogleditor_QTrimDialog</receiver>
-   <slot>accept()</slot>
-   <hints>
-    <hint type="sourcelabel">
-     <x>248</x>
-     <y>254</y>
-    </hint>
-    <hint type="destinationlabel">
-     <x>157</x>
-     <y>274</y>
-    </hint>
-   </hints>
-  </connection>
   <connection>
    <sender>buttonBox</sender>
    <signal>rejected()</signal>
-- 
2.45.2