Isis 3 Programmer Reference
TemplateEditorWidget.cpp
1 #include "TemplateEditorWidget.h"
2 #include "ui_TemplateEditorWidget.h"
3 
4 #include "Directory.h"
5 #include "IException.h"
6 #include "Template.h"
7 
8 #include <QCheckBox>
9 #include <QCloseEvent>
10 #include <QDir>
11 #include <QFileDialog>
12 #include <QMessageBox>
13 
14 namespace Isis {
15 
16 
17  TemplateEditorWidget::TemplateEditorWidget(Template* currentTemplate, Directory *directory,
18  QWidget *parent) : m_ui(new Ui::TemplateEditorWidget) {
19  m_ui->setupUi(this);
20  m_template = currentTemplate;
21  m_directory = directory;
22  m_fileType = m_template->templateType();
23  m_textChanged = false;
24 
25  QFile templateFile(m_template->fileName());
26  templateFile.open(QFile::ReadOnly | QFile::Text);
27  QTextStream textStream(&templateFile);
28  m_ui->templateTextEdit->setText(textStream.readAll());
29  templateFile.close();
30 
31  connect(m_ui->templateTextEdit, SIGNAL(textChanged()), this, SLOT(textChanged()));
32  connect(m_ui->templateTextSave, SIGNAL (released()),this, SLOT (saveText()));
33  connect(m_ui->templateTextSaveAs, SIGNAL (released()),this, SLOT (saveAsText()));
34 
35  }
36 
37 
38  TemplateEditorWidget::~TemplateEditorWidget() {
39 
40  saveOption();
41 
42  delete m_ui;
43  }
44 
45 
51  //We create a new QFile just in case the template's file name has changed
52  QFile templateFile(m_template->fileName());
53 
54  if (templateFile.open(QFile::WriteOnly | QFile::Text)) {
55  templateFile.resize(0);
56  templateFile.write(m_ui->templateTextEdit->toPlainText().toUtf8());
57  templateFile.close();
58  }
59 
60  m_textChanged = false;
61  }
62 
63 
69 
70  // Create a filedialog for the user to choose a save location and name as well as whether they
71  // would like to import the saved template file into the project.
72  QFileDialog *fileDialog = new QFileDialog(qobject_cast<QWidget *>(parent()),
73  "Save Template File");
74  QGridLayout *layout = static_cast<QGridLayout*>(fileDialog->layout());
75  QCheckBox *checkbox = new QCheckBox(this);
76  checkbox->setText("Automatically Import Template File on Save");
77  checkbox->setCheckState(Qt::Checked);
78  layout->addWidget( checkbox );
79 
80  fileDialog->setAcceptMode(QFileDialog::AcceptSave);
81  fileDialog->setDirectory(QDir::currentPath());
82  if (m_fileType == "maps") {
83  fileDialog->setNameFilter("Maps (*.map);;All Files (*)");
84  }
85  else if (m_fileType == "registrations") {
86  fileDialog->setNameFilter("Registrations (*.def);;All Files (*)");
87  }
88  fileDialog->exec();
89 
90  // Simply return if the user cancelled the save
91  if (!fileDialog->result()){
92  return;
93  }
94 
95  // Add file extension based on selected filter if extension not provided, defaulting to ".def"
96  // or ".map", respectively.
97  QString extension = fileDialog->selectedNameFilter().split("(")[1].mid(1, 4);
98 
99  if (QString::compare(extension, ".def") == 0 || QString::compare(extension, ".map") == 0) {
100  fileDialog->setDefaultSuffix(extension);
101  }
102  else {
103  if (m_fileType == "maps") {
104  fileDialog->setDefaultSuffix(".map");
105  }
106  else if (m_fileType == "registrations") {
107  fileDialog->setDefaultSuffix(".def");
108  }
109  }
110  QString templateFileName = fileDialog->selectedFiles().first();
111 
112  // Write the file out
113  QFile templateFile(templateFileName);
114  if (templateFile.open(QFile::WriteOnly | QFile::Text)) {
115  templateFile.resize(0);
116  templateFile.write(m_ui->templateTextEdit->toPlainText().toUtf8());
117  templateFile.close();
118  }
119 
120  // Import the newly created template file to the project if user checked the optional checkbox.
121  if (checkbox->checkState()) {
122  if (templateFile.exists()) {
123  QDir templateFolder = m_directory->project()->addTemplateFolder(m_fileType + "/import");
124 
125  TemplateList *templateList = new TemplateList(templateFolder.dirName(), m_fileType, m_fileType
126  + "/" + templateFolder.dirName() );
127 
128  QFile::copy(templateFileName, templateFolder.path() + "/" + templateFileName.split("/").last());
129  templateList->append(new Template(templateFolder.path() + "/"
130  + templateFileName.split("/").last(),
131  m_fileType,
132  templateFolder.dirName()));
133 
134  m_directory->project()->addTemplates(templateList);
135  m_directory->project()->setClean(false);
136  }
137  else {
139  QString("Could not import file [%1]").arg(templateFileName),
140  _FILEINFO_); }
141  }
142 
143  m_textChanged = false;
144  }
145 
146 
152  m_textChanged = true;
153  }
154 
155 
162 
163  if (m_textChanged) {
164  QMessageBox *box = new QMessageBox(QMessageBox::NoIcon, QString("Current Template Has Unsaved Changes"),
165  QString("Would you like to save your current template?"),
166  NULL, qobject_cast<QWidget *>(parent()), Qt::Dialog);
167  QPushButton *save = box->addButton("Save", QMessageBox::AcceptRole);
168  box->addButton("Don't Save", QMessageBox::RejectRole);
169  QPushButton *cancel = box->addButton("Cancel", QMessageBox::NoRole);
170  box->exec();
171 
172  if (box->clickedButton() == (QAbstractButton*)cancel) {
173  return;
174  }
175  else if (box->clickedButton() == (QAbstractButton*)save) {
176  saveAsText();
177  }
178  }
179  m_textChanged = false;
180  }
181 }
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Method to write this Template object&#39;s member data to an XML stream.
Definition: Template.cpp:105
void saveText()
Called when a user clicks the "Save" button.
Template(QString fileName, QString templateType, QString importName, QObject *parent=0)
Create a Template from template file&#39;s name.
Definition: Template.cpp:19
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:171
void saveAsText()
Called when a user clicks the "Save As" button.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
void saveOption()
This slot is called when the widget is closed (either via the widget itself or on project close)...
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void textChanged()
Slot called when text within widget has been changed.