Isis 3 Programmer Reference
PvlEditDialog.cpp
1 #include "PvlEditDialog.h"
2 
3 #include <fstream>
4 #include <sstream>
5 
6 #include <QFileDialog>
7 #include <QListWidget>
8 #include <QString>
9 #include <QMessageBox>
10 #include <QPushButton>
11 #include <QTextEdit>
12 #include <QtGui>
13 #include <QVBoxLayout>
14 
15 #include "IException.h"
16 #include "Pvl.h"
17 
18 using namespace std;
19 
20 namespace Isis {
32  PvlEditDialog::PvlEditDialog(Pvl &pvl, QWidget *parent) : QDialog(parent) {
33 
34  // Create text edit box and fill it with pvl file contents
35  p_textEdit = new QTextEdit;
36  fstream input;
37 
38  // open as input from pvl file
39  input.open(pvl.fileName().toLatin1().data(), ios::in);
40  string output;
41 
42  // read first line of input and write as first output line
43  getline(input, output);
44  while(!input.eof()) {
45  // append this line of output to the QTextEdit box
46  p_textEdit->append(QString::fromStdString(output));
47 
48  // read next line of input and write as next output line
49  getline(input, output);
50  }
51  input.close();
52 
53  // Create Close and Save buttons in an QHBoxLayout
54  p_saveButton = new QPushButton("Save Changes &As...");
55  p_saveButton->setEnabled(false);
56  QPushButton *closeButton = new QPushButton("&Close");
57 
58  QHBoxLayout *buttonLayout = new QHBoxLayout;
59  buttonLayout->addWidget(p_saveButton);
60  buttonLayout->addWidget(closeButton);
61 
62  // Add QTextEdit box and button layout to QVBoxLayout
63  // and set this to the layout of the QDialog window
64  QVBoxLayout *vLayout = new QVBoxLayout;
65  vLayout->addWidget(p_textEdit);
66  vLayout->addLayout(buttonLayout);
67 
68  setLayout(vLayout);
69  QString titleBar = "Pvl File: " + QString(pvl.fileName()) ;
70  setWindowTitle(titleBar);
71 
72  // Add functionality to buttons
73  connect(p_textEdit, SIGNAL(textChanged()), this, SLOT(enableSaveButton()));
74  connect(p_saveButton, SIGNAL(clicked()), this, SLOT(saveTextEdit()));
75  connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));
76 
77  }
78 
87  p_saveButton->setEnabled(true);
88  }
89 
101  // Check validity of format by placing QTextEdit contents into a Pvl object
102  Pvl pvl;
103  stringstream ss;
104  string textEditContents = p_textEdit->toPlainText().toStdString();
105 
106  //fill stringstream with contents of QTextEdit
107  ss << textEditContents;
108 
109  try {
110  // fill pvl with contents of stringstream
111  ss >> pvl;
112  }
113  catch(IException &e) {
114  // catch errors in Pvl format when populating pvl object
115  QString message = e.toString();
116  QMessageBox::warning((QWidget *)parent(), "Error", message);
117  return;
118  }
119 
120  // get a window to choose a name and location for the saved file
121  // default: look in user's current directory for *.def or *.pvl files
122  QString filter = "Select registration template (*.def *.pvl);;";
123  filter += "All (*)";
124  QString pvlFile = QFileDialog::getSaveFileName((QWidget *)parent(),
125  "Select a registration template",
126  ".",
127  filter);
128  if(!pvlFile.isEmpty()) {
129  // convert QString to std::string needed to open file stream
130  QString saveFile = pvlFile;
131  try {
132  pvl.write(saveFile);
133  }
134  catch(IException &e) {
135  // report exception(s) to mesage box
136  QString message = e.toString();
137  QMessageBox::warning((QWidget *)parent(), "Error", message);
138  return;
139  }
140  }
141 
142  // refresh titleBar with new file name
143  setWindowTitle("Pvl File: " + pvlFile);
144  }
145 }
void enableSaveButton()
Allow the "Save Changes" button to be activated.
Namespace for the standard library.
Container for cube-like labels.
Definition: Pvl.h:135
QString toString() const
Returns a string representation of this exception.
Definition: IException.cpp:553
QString fileName() const
Returns the filename used to initialise the Pvl object.
Definition: PvlContainer.h:246
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void saveTextEdit()
Save the edited text as a new Pvl file.
void write(const QString &file)
Opens and writes PVL information to a file and handles the end of line sequence.
Definition: Pvl.cpp:116