Commit 6b37a284 authored by Austin Sanders's avatar Austin Sanders Committed by amystamile-usgs
Browse files

replaced PvlEditDialog that was accidentally removed in prev commit

parent 7aab8dea
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
ifeq ($(ISISROOT), $(BLANK))
.SILENT:
error:
	echo "Please set ISISROOT";
else
	include $(ISISROOT)/make/isismake.objs
endif
 No newline at end of file
+145 −0
Original line number Diff line number Diff line
#include "PvlEditDialog.h"

#include <fstream>
#include <sstream>

#include <QFileDialog>
#include <QListWidget>
#include <QString>
#include <QMessageBox>
#include <QPushButton>
#include <QTextEdit>
#include <QtGui>
#include <QVBoxLayout>

#include "IException.h"
#include "Pvl.h"

using namespace std;

namespace Isis {
  /**
   * This constructor creates a PvlEditDialog object given a
   * pointer to a Pvl object.
   *
   * @param pvl Pvl file to be viewed or edited
   * @author 2008-12-10 Jeannie Walldren
   * @internal
   *   @history 2008-12-10 Jeannie Walldren - Original Version
   *   @history 2008-12-15 Jeannie Walldren - Removed unneccesary
   *            error catch.
   */
  PvlEditDialog::PvlEditDialog(Pvl &pvl, QWidget *parent) : QDialog(parent) {

    // Create text edit box and fill it with pvl file contents
    p_textEdit = new QTextEdit;
    fstream input;

    // open as input from pvl file
    input.open(pvl.fileName().toLatin1().data(), ios::in);
    string output;

    // read first line of input and write as first output line
    getline(input, output);
    while(!input.eof()) {
      // append this line of output to the QTextEdit box
      p_textEdit->append(QString::fromStdString(output));

      // read next line of input and write as next output line
      getline(input, output);
    }
    input.close();

    //  Create Close and Save buttons in an QHBoxLayout
    p_saveButton = new QPushButton("Save Changes &As...");
    p_saveButton->setEnabled(false);
    QPushButton *closeButton = new QPushButton("&Close");

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(p_saveButton);
    buttonLayout->addWidget(closeButton);

    // Add QTextEdit box and button layout to QVBoxLayout
    // and set this to the layout of the QDialog window
    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addWidget(p_textEdit);
    vLayout->addLayout(buttonLayout);

    setLayout(vLayout);
    QString titleBar = "Pvl File: " + QString(pvl.fileName()) ;
    setWindowTitle(titleBar);

    // Add functionality to buttons
    connect(p_textEdit, SIGNAL(textChanged()), this, SLOT(enableSaveButton()));
    connect(p_saveButton, SIGNAL(clicked()), this, SLOT(saveTextEdit()));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));

  }

  /**
   * Allow the "Save Changes" button to be activated.
   *
   * @author 2008-12-10 Jeannie Walldren
   * @internal
   *   @history 2008-12-10 Jeannie Walldren - Original Version.
   */
  void PvlEditDialog::enableSaveButton() {
    p_saveButton->setEnabled(true);
  }

  /**
   * Save the edited text as a new Pvl file.
   * @author 2008-12-10 Jeannie Walldren
   * @internal
   *   @history 2008-12-10 Jeannie Walldren - Original Version.
   *   @history 2008-12-15 Jeannie Walldren - Added a verification
   *            that the edited file is in Pvl format before
   *            allowing save. Replaced error throw with
   *            QMessageBox warning.
   */
  void PvlEditDialog::saveTextEdit() {
    // Check validity of format by placing QTextEdit contents into a Pvl object
    Pvl pvl;
    stringstream ss;
    string textEditContents = p_textEdit->toPlainText().toStdString();

    //fill stringstream with contents of QTextEdit
    ss << textEditContents;

    try {
      // fill pvl with contents of stringstream
      ss >> pvl;
    }
    catch(IException &e) {
      // catch errors in Pvl format when populating pvl object
      QString message = e.toString();
      QMessageBox::warning((QWidget *)parent(), "Error", message);
      return;
    }

    // get a window to choose a name and location for the saved file
    // default: look in user's current directory for *.def or *.pvl files
    QString filter = "Select registration template (*.def *.pvl);;";
    filter += "All (*)";
    QString pvlFile = QFileDialog::getSaveFileName((QWidget *)parent(),
                      "Select a registration template",
                      ".",
                      filter);
    if(!pvlFile.isEmpty()) {
      // convert QString to std::string needed to open file stream
      QString saveFile = pvlFile;
      try {
        pvl.write(saveFile);
      }
      catch(IException &e) {
        // report exception(s) to mesage box
        QString message = e.toString();
        QMessageBox::warning((QWidget *)parent(), "Error", message);
        return;
      }
    }

    // refresh titleBar with new file name
    setWindowTitle("Pvl File: " + pvlFile);
  }
}
+57 −0
Original line number Diff line number Diff line
#ifndef PvlEditDialog_h
#define PvlEditDialog_h

#include <QDialog>
#include <QString>

#include "Pvl.h"

class QLabel;
class QLineEdit;
class QListWidget;
class QPushButton;
class QTextEdit;

#include <vector>

namespace Isis {
  /**
   * PvlEditDialog creates a QDialog window in which a QTextEdit
   * box displays the contents of a pvl file.  This file may be
   * viewed or edited and saved as a new pvl file.
   *
   *  @ingroup ApplicationInterface
   *
   * @author 2008-12-10 Jeannie Walldren
   * @internal
   *   @history 2008-12-10 Jeannie Walldren - Original version
   *            written to view and edit the template file in the
   *            qnet application.
   *   @history 2008-12-10 Jeannie Walldren - Changed namespace
   *            from Qisis to Isis
   *   @history 2008-12-10 Jeannie Walldren - Cleaned up code and
   *            fixed bugs arising from moving this class from
   *            Qisis and to Isis. Added moc commands to Makefile.
   *   @history 2008-12-15 Jeannie Walldren - Added a verification
   *            that the edited file is in Pvl format.  Replaced
   *            error throws with QMessage warning boxes.
   */
  class PvlEditDialog : public QDialog {
      Q_OBJECT

    public:
      PvlEditDialog(Pvl &pvl, QWidget *parent = 0);

    private:
      QTextEdit *p_textEdit;
      QPushButton *p_saveButton;

    private slots:
      void enableSaveButton();
      void saveTextEdit();
  };
};

#endif

+3 −0
Original line number Diff line number Diff line

Unit test for PvlEditDialog
Replace this when we decide how to do unit tests for interactive objects
+14 −0
Original line number Diff line number Diff line
#include <iostream>
#include "PvlEditDialog.h"
#include "Preference.h"

using namespace std;
using namespace Isis;

int main(int argc, char *argv[]) {
  Preference::Preferences(true);
  cout << endl << "Unit test for PvlEditDialog" << endl;
  cout << "Replace this when we decide how to do unit tests for interactive objects" << endl;
  return 0;
}