/****************************************************************************
** $Id: qt/examples/application/application.cpp   2.3.0   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

// Here's the NOMAD license, since modifications were made to the original
// 'application.cpp' file.

/******************************************************************************
* NOMAD -  Nonlinear Optimization for Mixed vAriables and Derivatives.        *
* Copyright (C) 2001-2004  Gilles Couture - Ecole Polytechnique,              *
*                          Charles Audet - Ecole Polytechnique,               *
*		           John Dennis - Rice University.                     *
*                                                                             *
* Author: Gilles Couture                                                      *
* École Polytechnique de Montréal - GERAD                                     *
* C.P. 6079, Succ. Centre-ville, Montréal (Québec) H3C 3A7 Canada             *
* Tel:  (514) 340-6053 x6928                                                  *
* Email: Gilles.Couture@gerad.ca                                              *
* Fax: (514) 340-5665                                                         *
*                                                                             *
* This program is free software; you can redistribute it and/or               *
* modify it under the terms of the GNU General Public License                 *
* as published by the Free Software Foundation. You can find the              *
* GNU General Public License at:                                              *
*             http://www.gnu.org/copyleft/gpl.html                            *
*                                                                             *
* This program is distributed in the hope that it will be useful,             *
* but WITHOUT ANY WARRANTY; without even the implied warranty of              *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
* GNU General Public License for more details.                                *
*                                                                             *
* You should have received a copy of the GNU General Public License           *
* along with this program; if not, write to the Free Software                 *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
*                                                                             *
* You can find information on the NOMAD software at:                          *
*             http://www.gerad.ca/NOMAD/                                      *
******************************************************************************/

#include <iostream>
#include <QAction>
#include <QApplication>
#include <QCloseEvent>
#include <QFile>
#include <QFileDialog>
#include <QImage>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QPaintDevice>
#include <QPainter>
#include <QStatusBar>
#include <QString>
#include <QTextEdit>
#include <QTextStream>
using namespace std;
#include "texteditor.h"

TextEditor::TextEditor(QString & file)
  : QMainWindow(0)
{
  file_name = file;

  save_act = new QAction(tr("Save"), this);
  connect(save_act, SIGNAL(triggered()), this, SLOT(save()));

  save_as_act = new QAction(tr("Save as"), this);
  connect(save_as_act, SIGNAL(triggered()), this, SLOT(saveAs()));

  close_act = new QAction(tr("Close"), this);
  connect(close_act, SIGNAL(triggered()), this, SLOT(close()));

  file_m = new QMenu(tr("File"), this);
  file_m->addAction(save_act);
  file_m->addAction(save_as_act);
  file_m->addSeparator();
  file_m->addAction(close_act);

  // The item is put on the menu bar.
  menu = new QMenuBar(this);
  menu->addMenu(file_m);

  setMenuBar(menu);

  editor = new QTextEdit(this);
  // editor->setTextFormat(Qt::PlainText);
  editor->setFont(QFont("Courier", 10));
  editor->setFocus();
  setCentralWidget(editor);
  statusBar()->showMessage("Ready", 2000);
  resize(450, 600);
}

TextEditor::~TextEditor()
{}

void TextEditor::load()
{
  QString fn = QFileDialog::getOpenFileName(this);
  if (!fn.isEmpty())
    load(fn);
  else
    statusBar()->showMessage("Loading aborted", 2000);
}

void TextEditor::load(QString & file)
{
  QFile qfile(file);
  if (!qfile.open(QIODevice::ReadOnly))
    return;

  editor->clear();
  QTextStream stream(&qfile);
  editor->setPlainText(stream.readAll());
  qfile.close();

  editor->repaint();
  editor->document()->setModified(false);
  setWindowTitle(file);
  statusBar()->showMessage(QString("Loaded document %1").arg(file), 2000);
}

void TextEditor::save()
{
  if (file_name.isEmpty())
    {
      saveAs();
      return;
    }

  QFile file(file_name);
  // A check to see if the file is open.
  if (!file.open(QIODevice::WriteOnly))
    {
      statusBar()->showMessage(QString("Could not write to %1").arg(file_name),
			       2000);
      return;
    }

  // Else the file is open. The text is written in the file.
  QTextStream stream(&file);
  stream << editor->toPlainText();
  file.close();

  editor->document()->setModified(false);
  setWindowTitle(file_name);
  statusBar()->showMessage(QString("File %1 saved").arg(file_name), 2000);
}

void TextEditor::saveAs()
{
  QString fn = QFileDialog::getSaveFileName(this);
  if (!fn.isEmpty())
    {
      file_name = fn;
      save();
    }
  else
    statusBar()->showMessage("Saving aborted", 2000);
}

void TextEditor::closeEvent(QCloseEvent * ce)
{
  if (!editor->document()->isModified())
    {
      ce->accept();
      return;
    }

  switch(QMessageBox::information(this, "Qt Application Example",
				  "The document has been changed since "
				  "the last save.",
				  "Save Now", "Cancel", "Leave Anyway",
				  0, 1))
    {
      case 0:   save();
                ce->accept();
		break;
      case 1:
      default:  // just for sanity
	        ce->ignore();
		break;
      case 2:   ce->accept();
	        break;
    }
}

void TextEditor::about()
{
  QMessageBox::about(this, "Text Editor",
		     "This simple text editor captures data\n"
		     "for Nomad (like the starting point).");
}
