Isis 3 Programmer Reference
GuiEditFile.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "FileName.h"
8#include "GuiEditFile.h"
9#include "UserInterface.h"
10
11#include <QDir>
12#include <QFileDialog>
13#include <QList>
14#include <QPushButton>
15#include <QStatusBar>
16#include <QTextEdit>
17#include <QTextStream>
18#include <QMenuBar>
19#include <QMessageBox>
20
21using namespace Isis;
22using namespace std;
23
24namespace Isis {
25
26 // Singleton
28
38 void GuiEditFile::EditFile(UserInterface & pUI, QString psFile) {
39 if (m_instance == NULL) {
40 m_instance = new GuiEditFile(pUI, psFile);
41 }
42 else {
43 m_instance->showWindow(psFile);
44 }
45 }
46
54 void GuiEditFile::showWindow(QString psFile) {
55 m_fileName = QString(psFile);
56 m_editWin->setWindowTitle (m_fileName);
58 m_editWin->show();
59 }
60
69 GuiEditFile::GuiEditFile(UserInterface & pUI, QString psFile) {
70 // Init data members
71 m_parent = pUI.TheGui();
72 m_fileName = QString(psFile);
73 m_editFile=NULL;
74
75 // Main Window
76 m_editWin = new QMainWindow(m_parent, Qt::SubWindow);
77 m_editWin->setWindowTitle (m_fileName);
78 m_editWin->resize (400, 600);
79
80 // Status bar
81 QStatusBar *statusBar = new QStatusBar(m_editWin);
82 m_editWin->setStatusBar(statusBar);
83
84 // Menu bar
85 QMenuBar *menuBar = new QMenuBar(m_editWin);
86 QMenu *fileMenu = menuBar->addMenu("&File");
87
88 // Action File-> Open
89 m_open = new QAction(menuBar);
90 m_open->setShortcut(Qt::CTRL + Qt::Key_O);
91 m_open->setText("&Open...");
92 m_open->setToolTip("Open File");
93 QString whatsThis = "Open a file to edit";
94 m_open->setWhatsThis(whatsThis);
95 connect(m_open, SIGNAL(triggered()), this, SLOT(open()));
96 fileMenu->addAction(m_open);
97
98 // Action File-> Save
99 m_save = new QAction(menuBar);
100 m_save->setShortcut(Qt::CTRL + Qt::Key_S);
101 m_save->setText("&Save...");
102 m_save->setToolTip("Save File");
103 m_save->setWhatsThis("Save the current file");
104 connect(m_save, SIGNAL(triggered()), this, SLOT(saveFile()));
105 fileMenu->addAction(m_save);
106
107 // Action File->Save As
108 m_saveAs = new QAction(menuBar);
109 m_saveAs->setText("Save &As...");
110 m_saveAs->setShortcut(Qt::CTRL + Qt::Key_A);
111 m_saveAs->setToolTip("Save As File");
112 m_saveAs->setWhatsThis("Save the current file into another file");
113 connect(m_saveAs, SIGNAL(triggered()), this, SLOT(saveAs()));
114 fileMenu->addAction(m_saveAs);
115
116 // Action File->close
117 m_close = new QAction(menuBar);
118 m_close->setText("&Close...");
119 m_close->setShortcut(Qt::CTRL + Qt::Key_C);
120 m_close->setToolTip("Close File");
121 m_close->setWhatsThis("Close the current file");
122 connect(m_close, SIGNAL(triggered()), this, SLOT(closeFile()));
123 fileMenu->addAction(m_close);
124
125 // Action Exit
126 m_exit = menuBar->addAction("&Exit");
127 m_exit->setShortcut(Qt::CTRL + Qt::Key_E);
128 m_exit->setText("&Exit...");
129 m_exit->setToolTip("Exit");
130 m_exit->setWhatsThis("Exit the Editor");
131 //connect(m_exit, SIGNAL(triggered()), m_editWin, SLOT(close()));
132 connect(m_exit, SIGNAL(triggered()), this, SLOT(closeWin()));
133
134 m_editWin->setMenuBar(menuBar);
135
136 // Text Edit
137 m_textChanged = false;
138 m_txtEdit = new QTextEdit(m_editWin);
139 m_txtEdit->setUndoRedoEnabled(true);
140 m_txtEdit->resize(400,500);
141 m_editWin->setCentralWidget(m_txtEdit);
142 if (m_fileName.length() > 0){
144 }
145 connect(m_txtEdit, SIGNAL(textChanged()), this, SLOT(setTextChanged()));
146
147 m_editWin->show();
148 }
149
156 delete m_editFile;
157 m_editFile = NULL;
158
159 delete m_txtEdit;
160 m_txtEdit = NULL;
161
162 delete m_editWin;
163 m_editWin = NULL;
164
165 delete m_instance;
166 m_instance = NULL;
167 }
168
175 m_editWin->close();
176 m_txtEdit->clear();
177 }
178
185 m_textChanged = true;
186 }
187
194 //Set up the list of filters that are default with this dialog.
195 //cerr << "setTextChanged=" << m_textChanged << endl;
196 if (m_textChanged) {
197 if(QMessageBox::question((QWidget *)parent(), tr("Save File?"),
198 tr("Are you sure you want to save this file?"),
199 tr("&Save"), tr("&Cancel"),
200 QString(), 1, 0)) {
201 saveFile();
202 }
203 }
204
205 QString sFilterList("All files (*)");
206 QDir currDir = QDir::current();
207 QString sOpen("Open");
208 QFileDialog *fileDialog = new QFileDialog((QWidget *)parent(), sOpen, currDir.dirName(), sFilterList);
209 fileDialog->show();
210 connect(fileDialog, SIGNAL(fileSelected(QString)), this, SLOT(OpenFile(QString)));
211 }
212
219 if (m_textChanged) {
220 if(QMessageBox::question((QWidget *)parent(), tr("Save File?"),
221 tr("Changes have been made to the file. Do you want to Save?"),
222 QMessageBox::Save, QMessageBox::No) == QMessageBox::Save) {
223 saveFile();
224 }
225 }
226 m_editFile->close();
227 m_txtEdit->clear();
228 m_textChanged = false;
229 m_editWin->setWindowTitle(tr(""));
230 }
231
239 void GuiEditFile::OpenFile(QString psOutFile){
240 //cerr << "Open File " << psOutFile.toStdString() << "\n";
241 if(psOutFile.isEmpty()){
242 QMessageBox::information((QWidget *)parent(), "Error", "No output file selected");
243 return;
244 }
245
246 if (m_editFile != NULL) {
247 m_txtEdit->clear();
248 delete (m_editFile);
249 }
250 m_editFile = new QFile(psOutFile);
251 //m_editWin->setWindowTitle(FileName(psOutFile.toStdString()).Name().c_str());
252 windowTitle(psOutFile);
253
254 if (m_editFile->open(QIODevice::ReadWrite)){
255 char buf[1024];
256 QString bufStr;
257 qint64 lineLength = m_editFile->readLine(buf, sizeof(buf));
258 while (lineLength != -1) {
259 bufStr = QString(buf);
260 //std::cerr << bufStr.toStdString();
261 bufStr.remove('\n'); // puts an additional new line
262 m_txtEdit->append(bufStr);
263 lineLength = m_editFile->readLine(buf, sizeof(buf));
264 }
265 }
266 else {
267 m_txtEdit->append("\nThis file cannot be edited. Please check the file's Write permissions");
268 }
269 m_textChanged = false;
270 }
271
278 if (m_editFile){
279 clearFile();
280 QTextStream out(m_editFile);
281 out << m_txtEdit->document()->toPlainText();
282 m_textChanged = false;
283 }
284 }
285
292 QString sFilterList("All files (*)");
293 QDir currDir = QDir::current();
294 QString sOpen("Open");
295 QFileDialog *saveAsDialog = new QFileDialog((QWidget *)parent(), sOpen, currDir.dirName(), sFilterList);
296
297 QList<QPushButton *> allPButtons = saveAsDialog->findChildren<QPushButton *>();
298 // Edit the first (Open) button title.
299 allPButtons[0]->setText("&Save");
300 // Edit the second (Cancel) button title.
301 allPButtons[1]->setText("&Close");
302
303 saveAsDialog->show();
304 connect(saveAsDialog, SIGNAL(fileSelected(QString)), this, SLOT(saveAsFile(QString)));
305 }
306
314 void GuiEditFile::saveAsFile(QString psNewFile) {
315 m_editFile->close();
316 delete(m_editFile);
317 m_editFile = new QFile(psNewFile);
318 m_editFile->open(QFile::ReadWrite);
319 saveFile();
320 windowTitle(psNewFile);
321 }
322
330 if (m_editFile){
331 m_editFile->close();
332 //m_editFile->open(QIODevice::Truncate | QIODevice::ReadWrite);
333 m_editFile->open(QFile::ReadWrite | QFile::Truncate);
334 }
335 }
336
344 void GuiEditFile::windowTitle(QString & psfile) {
345 m_editWin->setWindowTitle(FileName(psfile).name());
346 }
347
348}
File name manipulation and expansion.
Definition FileName.h:100
Opens a window in Gui Application to be able to edit, save and create text files.
Definition GuiEditFile.h:54
QAction * m_exit
Action Exit.
Definition GuiEditFile.h:97
bool m_textChanged
Flag to indicate text changed.
Definition GuiEditFile.h:90
static void EditFile(UserInterface &pUI, QString psFile="")
Creates a single instance of the GuiEditFile.
void saveAsFile(QString)
Save the contents of text editor to another file.
void showWindow(QString psFile="")
If there is already an instance of this object, then display the window.
QString m_fileName
Current file open.
Definition GuiEditFile.h:86
QAction * m_close
Action Close.
Definition GuiEditFile.h:96
void saveAs()
For action File->Save As.
void clearFile()
Delete the contents of a file.
QAction * m_open
Actioons.
Definition GuiEditFile.h:93
QMainWindow * m_editWin
Editor window.
Definition GuiEditFile.h:87
void OpenFile(QString)
To display the contents of an opened file.
void windowTitle(QString &psfile)
display only the file base name
GuiEditFile(UserInterface &pUI, QString psFile="")
Constructor.
void setTextChanged()
Indicator that the text has changed.
QAction * m_save
Action Save.
Definition GuiEditFile.h:94
QWidget * m_parent
Parent widget.
Definition GuiEditFile.h:85
static GuiEditFile * m_instance
Instance of this object - singleton.
Definition GuiEditFile.h:84
void closeFile()
For action File->Close.
~GuiEditFile()
Destructor.
QFile * m_editFile
File pointer to current file.
Definition GuiEditFile.h:89
void closeWin()
For action Exit (close the window)
void saveFile()
For action File->Save.
QTextEdit * m_txtEdit
Text Editor.
Definition GuiEditFile.h:88
void open()
For action File->Open.
QAction * m_saveAs
Action Save As.
Definition GuiEditFile.h:95
Command Line and Xml loader, validation, and access.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.