| 1 |
#include "polfileeditor.h" |
| 2 |
#include "ui_polfileeditor.h" |
| 3 |
#include <QTextStream> |
| 4 |
#include <QFileInfo> |
| 5 |
#include <QFileDialog> |
| 6 |
#include <QDebug> |
| 7 |
|
| 8 |
using namespace xmpsolve; |
| 9 |
|
| 10 |
PolFileEditor::PolFileEditor(QWidget *parent, QString path) : |
| 11 |
QWidget(parent), |
| 12 |
ui(new Ui::PolFileEditor) |
| 13 |
{ |
| 14 |
ui->setupUi(this); |
| 15 |
|
| 16 |
m_polFilePath = path; |
| 17 |
|
| 18 |
QString polFileContent; |
| 19 |
|
| 20 |
if (! path.isEmpty()) { |
| 21 |
QFile polFile(m_polFilePath); |
| 22 |
|
| 23 |
if (polFile.open(QIODevice::ReadOnly)) { |
| 24 |
QTextStream in(&polFile); |
| 25 |
polFileContent = in.readAll(); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
m_syntaxHighlighter = new PolSyntaxHighlighter(ui->plainTextEdit->document()); |
| 30 |
ui->plainTextEdit->setPlainText(polFileContent); |
| 31 |
m_syntaxHighlighter->setDocument(ui->plainTextEdit->document()); |
| 32 |
|
| 33 |
QObject::connect(ui->plainTextEdit, SIGNAL(modificationChanged(bool)), |
| 34 |
this, SLOT(onTextEditChanged(bool))); |
| 35 |
|
| 36 |
m_state = SAVED; |
| 37 |
} |
| 38 |
|
| 39 |
void PolFileEditor::savePolFile(QString path) |
| 40 |
{ |
| 41 |
if (! path.isEmpty ()) |
| 42 |
m_polFilePath = path; |
| 43 |
|
| 44 |
if (m_polFilePath.isEmpty()) { |
| 45 |
m_polFilePath = QFileDialog::getSaveFileName(this, "Save .pol file", "", "Pol files (*.pol)"); |
| 46 |
if (m_polFilePath.isEmpty()) |
| 47 |
return; |
| 48 |
filenameChanged(m_polFilePath); |
| 49 |
} |
| 50 |
|
| 51 |
QFile outFile(m_polFilePath); |
| 52 |
|
| 53 |
if (outFile.open(QIODevice::WriteOnly)) { |
| 54 |
QTextStream out(&outFile); |
| 55 |
out << ui->plainTextEdit->toPlainText(); |
| 56 |
} |
| 57 |
|
| 58 |
m_state = SAVED; |
| 59 |
ui->plainTextEdit->document()->setModified(false); |
| 60 |
stateChanged(m_state); |
| 61 |
} |
| 62 |
|
| 63 |
bool PolFileEditor::isEmpty() |
| 64 |
{ |
| 65 |
return ui->plainTextEdit->toPlainText() == ""; |
| 66 |
} |
| 67 |
|
| 68 |
QString PolFileEditor::content() |
| 69 |
{ |
| 70 |
return ui->plainTextEdit->toPlainText(); |
| 71 |
} |
| 72 |
|
| 73 |
PolFileEditor::State PolFileEditor::state() |
| 74 |
{ |
| 75 |
return m_state; |
| 76 |
} |
| 77 |
|
| 78 |
void PolFileEditor::onTextEditChanged(bool modified) |
| 79 |
{ |
| 80 |
m_state = modified ? MODIFIED : SAVED; |
| 81 |
stateChanged(m_state); |
| 82 |
} |
| 83 |
|
| 84 |
QString PolFileEditor::currentPolFile() |
| 85 |
{ |
| 86 |
if (m_polFilePath.isEmpty()) |
| 87 |
return m_polFilePath; |
| 88 |
else { |
| 89 |
QFileInfo info(m_polFilePath); |
| 90 |
return info.absoluteFilePath(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
PolFileEditor::~PolFileEditor() |
| 95 |
{ |
| 96 |
delete ui; |
| 97 |
delete m_syntaxHighlighter; |
| 98 |
} |