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
24using namespace std;
25
26namespace Isis {
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}
Isis exception class.
Definition IException.h:91
void saveTextEdit()
Save the edited text as a new Pvl file.
PvlEditDialog(Pvl &pvl, QWidget *parent=0)
This constructor creates a PvlEditDialog object given a pointer to a Pvl object.
void enableSaveButton()
Allow the "Save Changes" button to be activated.
Container for cube-like labels.
Definition Pvl.h:119
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.