#include #include #include #include #include #include #include "qmeditaddendum.h" #include "ui_qmeditaddendum.h" #include "consts.h" #include "structs.h" #include "proto.h" QmEditAddendum::QmEditAddendum(const QString& name, const bool silentCreate ,QWidget *parent) : QDialog(parent), ui(new Ui::QmEditAddendum) { ui->setupUi(this); // First row of buttons QPushButton * delButton = new QPushButton("Delete"); delButton->setToolTip("Close and delete the message"); ui->horizontalLayout1->addWidget(delButton); connect(delButton,SIGNAL(clicked()), this, SLOT(doDelete())); QSpacerItem *horizontalSpacer1; horizontalSpacer1 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); ui->horizontalLayout1->addItem(horizontalSpacer1); QPushButton * saveButton = new QPushButton("Save"); saveButton->setToolTip("Save message and keep editing"); ui->horizontalLayout1->addWidget(saveButton); connect(saveButton,SIGNAL(clicked()), this, SLOT(save())); QPushButton * doneButton = new QPushButton("Save and Close"); doneButton->setToolTip("Save message and close editor"); ui->horizontalLayout1->addWidget(doneButton); connect(doneButton,SIGNAL(clicked()), this, SLOT(saveAndClose())); QPushButton * cancelButton = new QPushButton("Cancel"); cancelButton->setToolTip("Exit the dialog now. Changes made since the last save will be lost."); ui->horizontalLayout1->addWidget(cancelButton); connect(cancelButton,SIGNAL(clicked()), this, SLOT(reject())); // Second Row of Buttons QPushButton * clearButton = new QPushButton("Clear"); clearButton->setToolTip("Clear the text. Continue editing"); ui->horizontalLayout2->addWidget(clearButton); connect(clearButton,SIGNAL(clicked()), this, SLOT(doClear())); QPushButton * timeButton = new QPushButton("TimeStamp"); timeButton->setToolTip("Insert a timestamp into the message"); ui->horizontalLayout2->addWidget(timeButton); connect(timeButton,SIGNAL(clicked()), this, SLOT(doTimeStamp())); QSpacerItem *horizontalSpacer2; horizontalSpacer2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); ui->horizontalLayout2->addItem(horizontalSpacer2); // Attempt to open the file name specified // qDebug("Edit: %s", name); setWindowTitle(name); /* ** Read the file into the message area */ file.setFileName(name); if (!file.open(QFile::ReadOnly | QFile::Text)) { if (! silentCreate) { QMessageBox::warning(this, tr("Application"), tr("Cannot read file %1:\n%2.") .arg(name) .arg(file.errorString())); return; } } QTextStream in(&file); ui->textEdit->setPlainText(in.readAll()); file.close(); /* ** Put the user into the editor */ ui->textEdit->setFocus(); } /*---------------------------------------------------------------------------- ** FUNCTION : ~QmEditAddendum() ** ** DESCRIPTION : Descructor ** ** ** INPUTS : ** ** RETURNS : ** ----------------------------------------------------------------------------*/ QmEditAddendum::~QmEditAddendum() { delete ui; } /*---------------------------------------------------------------------------- ** FUNCTION : save ** ** DESCRIPTION : Save the text to the output file ** ** ** INPUTS : ** ** RETURNS : ** ----------------------------------------------------------------------------*/ void QmEditAddendum::save(void) { //qDebug("Save File"); if (file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate)) { QTextStream out(&file); out << ui->textEdit->toPlainText(); file.close(); } } /*---------------------------------------------------------------------------- ** FUNCTION : saveAndClose ** ** DESCRIPTION : Save the text and close the dialog ** ** ** INPUTS : ** ** RETURNS : ** ----------------------------------------------------------------------------*/ void QmEditAddendum::saveAndClose(void) { qDebug("saveAndClose"); save(); done(0); } /*---------------------------------------------------------------------------- ** FUNCTION : doClear ** ** DESCRIPTION : Clear the text ** ** ** INPUTS : ** ** RETURNS : ** ----------------------------------------------------------------------------*/ void QmEditAddendum::doClear(void) { ui->textEdit->clear(); ui->textEdit->setFocus(); } /*---------------------------------------------------------------------------- ** FUNCTION : doDelete ** ** DESCRIPTION : Delete the file from disk ** ** ** INPUTS : ** ** RETURNS : ** ----------------------------------------------------------------------------*/ void QmEditAddendum::doDelete(void) { file.remove(); done(0); } /*---------------------------------------------------------------------------- ** FUNCTION : doTimeStamp ** ** DESCRIPTION : Insert a Date-Time into the text ** ** ** INPUTS : ** ** RETURNS : ** ----------------------------------------------------------------------------*/ void QmEditAddendum::doTimeStamp(void) { ui->textEdit->insertPlainText(QDateTime::currentDateTime().toString("dd-MMM-yy hh:mm:ss").append(":")); ui->textEdit->setFocus(); }