1 #include "TemplateEditorWidget.h" 2 #include "ui_TemplateEditorWidget.h" 11 #include <QFileDialog> 12 #include <QMessageBox> 17 TemplateEditorWidget::TemplateEditorWidget(Template* currentTemplate, Directory *directory,
18 QWidget *parent) : m_ui(new
Ui::TemplateEditorWidget) {
20 m_template = currentTemplate;
21 m_directory = directory;
22 m_fileType = m_template->templateType();
23 m_textChanged =
false;
25 QFile templateFile(m_template->fileName());
26 templateFile.open(QFile::ReadOnly | QFile::Text);
27 QTextStream textStream(&templateFile);
28 m_ui->templateTextEdit->setText(textStream.readAll());
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()));
38 TemplateEditorWidget::~TemplateEditorWidget() {
52 QFile templateFile(m_template->fileName());
54 if (templateFile.open(QFile::WriteOnly | QFile::Text)) {
55 templateFile.resize(0);
56 templateFile.write(m_ui->templateTextEdit->toPlainText().toUtf8());
60 m_textChanged =
false;
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 );
80 fileDialog->setAcceptMode(QFileDialog::AcceptSave);
81 fileDialog->setDirectory(QDir::currentPath());
82 if (m_fileType ==
"maps") {
83 fileDialog->setNameFilter(
"Maps (*.map);;All Files (*)");
85 else if (m_fileType ==
"registrations") {
86 fileDialog->setNameFilter(
"Registrations (*.def);;All Files (*)");
91 if (!fileDialog->result()){
97 QString extension = fileDialog->selectedNameFilter().split(
"(")[1].mid(1, 4);
99 if (QString::compare(extension,
".def") == 0 || QString::compare(extension,
".map") == 0) {
100 fileDialog->setDefaultSuffix(extension);
103 if (m_fileType ==
"maps") {
104 fileDialog->setDefaultSuffix(
".map");
106 else if (m_fileType ==
"registrations") {
107 fileDialog->setDefaultSuffix(
".def");
110 QString templateFileName = fileDialog->selectedFiles().first();
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();
121 if (checkbox->checkState()) {
122 if (templateFile.exists()) {
123 QDir templateFolder = m_directory->project()->addTemplateFolder(m_fileType +
"/import");
126 +
"/" + templateFolder.dirName() );
128 QFile::copy(templateFileName, templateFolder.path() +
"/" + templateFileName.split(
"/").last());
129 templateList->append(
new Template(templateFolder.path() +
"/" 130 + templateFileName.split(
"/").last(),
132 templateFolder.dirName()));
134 m_directory->project()->addTemplates(templateList);
135 m_directory->project()->setClean(
false);
139 QString(
"Could not import file [%1]").arg(templateFileName),
143 m_textChanged =
false;
152 m_textChanged =
true;
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);
172 if (box->clickedButton() == (QAbstractButton*)cancel) {
175 else if (box->clickedButton() == (QAbstractButton*)
save) {
179 m_textChanged =
false;
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Method to write this Template object's member data to an XML stream.
Template(QString fileName, QString templateType, QString importName, QObject *parent=0)
Create a Template from template file's name.
A type of error that occurred when performing an actual I/O operation.
#define _FILEINFO_
Macro for the filename and line number.
Namespace for ISIS/Bullet specific routines.