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