Subversion Repositories svn1-original

Rev

Rev 381 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QDir>
#include <QPushButton>
#include <QDateTime>

#include "QmEditAwards.h"
#include "ui_QmEditAwards.h"
#include    "consts.h"
#include    "structs.h"
#include    "proto.h"

QmEditAwards::QmEditAwards(const QString& name, const bool silentCreate ,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QmEditAwards)
{
    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()));

    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           : ~QmEditAwards()
**
** DESCRIPTION        : Descructor
**
**
** INPUTS             :
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

QmEditAwards::~QmEditAwards()
{
    delete ui;
}

/*----------------------------------------------------------------------------
** FUNCTION           : save
**
** DESCRIPTION        : Save the text to the output file
**
**
** INPUTS             :
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

void QmEditAwards::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 QmEditAwards::saveAndClose(void)
{
    qDebug("saveAndClose");
    save();
    done(0);
}

/*----------------------------------------------------------------------------
** FUNCTION           : doClear
**
** DESCRIPTION        : Clear the text
**
**
** INPUTS             :
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

void QmEditAwards::doClear(void)
{
    ui->textEdit->clear();
    ui->textEdit->setFocus();
}

/*----------------------------------------------------------------------------
** FUNCTION           : doDelete
**
** DESCRIPTION        : Delete the file from disk
**
**
** INPUTS             :
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

void QmEditAwards::doDelete(void)
{
    file.remove();
    done(0);
}