/***************************************************************************
 *   Copyright (C) 2007 by Siemens                    					   *
 *   flaemig.christian@siemens.com		     							   *
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.             *
 ***************************************************************************/


#include <QtGui>
#include "fmbmonitor.h"

fmbMonitor::fmbMonitor()
{

	setWindowTitle("FM-Bus Monitor - © Siemens AG - Nur für den internen Gebrauch");

    createActions();
    createMenus();
     
    staticLabel_Device_Status = new QLabel("Gerätestauts: ");
    staticLabel_Filter_Status = new QLabel("Filterkonfiguration: ");
    varLabel_Device_Status = new QLabel("Gerät nicht verbunden");
    varLabel_Filter_Status = new QLabel("Kein Filter eingestelt");

    upperLayout = new QGridLayout();
    upperLayout->addWidget(staticLabel_Device_Status, 1, 1);
    upperLayout->addWidget(varLabel_Device_Status, 1, 2);
    upperLayout->addWidget(staticLabel_Filter_Status, 2, 1);
    upperLayout->addWidget(varLabel_Filter_Status, 2, 2);
    
	createTabWidget();
	createStatusBar();

    mainLayout = new QVBoxLayout();
    mainLayout->addLayout(upperLayout);
    mainLayout->addWidget(tabWidget);
    setLayout(mainLayout);

    //setCentralWidget(tabWidget);

}


void fmbMonitor::about()
{
      QMessageBox::about(this, "About FM-Bus Monitor",
                         "<H1>FM Bus-Monitor</H1>\n\n"
                         "<font color=red><H2>Nur für den internen Gebrauch</H2></font>\n"
                         "<p>Diese Software enthält lizenzpflichtige Bestandteile (QT4) und ist "
                         "nur für den internen Gebrauch zugelassen</p>"
                         "<p>Copyright (C) 2007 by Siemens</p>"
                         "<a href=\"mailto:flaemig.christian@siemens.com\">flaemig.christian@siemens.com</a>"
                         );
}

void fmbMonitor::saveLog()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Log speichern"),".\\", "Log-Dateien (*.log)");
    QFile file;
    file.setFileName(fileName);
    file.open(QIODevice::WriteOnly);
    file.write("Eine leere Log-Datei", 20);
    file.close();
}

void fmbMonitor::closeEvent(QCloseEvent *event)
{
      event->accept();
}

void fmbMonitor::startAllViews()
{
    startAllViewsAct->setEnabled(false);
    stopAllViewsAct->setEnabled(true);
}

void fmbMonitor::stopAllViews()
{
    stopAllViewsAct->setEnabled(false);
    startAllViewsAct->setEnabled(true);
}

void fmbMonitor::config()
{
	QMessageBox::information(this,
				tr("Konfiguration"),
				tr("Hier kommt all das rein, was man später mal einstellen muss"),0 ,0);
}

void fmbMonitor::saveConfig()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Konfiguration speichern"),".\\", "Konfigurations-Dateien (*.cfg)");

    QFile file;
    file.setFileName(fileName);
    file.open(QIODevice::WriteOnly);
    file.write("Eine leere Config-Datei", 20);
    file.close();
}

void fmbMonitor::loadConfig()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Konfiguration laden"),".\\", "Konfigurations-Dateien (*.cfg)");}

void fmbMonitor::filter()
{
	QMessageBox::information(this,
				tr("Filtereinstellungen"),
				tr("Hier kann man später die Filter konfigurieren"), 0, 0);
}

void fmbMonitor::saveFilter()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Filter speichern"),".\\", "Filter-Dateien (*.fil)");

    QFile file;
    file.setFileName(fileName);
    file.open(QIODevice::WriteOnly);
    file.write("Eine leere Filter-Datei", 20);
    file.close();
}


void fmbMonitor::loadFilter()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Filter laden"),".\\", "Filter-Dateien (*.fil)");
}

void fmbMonitor::createActions()
{
    saveLogAct = new QAction(tr("Log &Speichern"), this);
    saveLogAct->setShortcut(tr("Ctrl+S"));
    saveLogAct->setStatusTip(tr("Speichert die empfangenen Daten"));
    connect(saveLogAct, SIGNAL(triggered()), this, SLOT(saveLog()));

    exitAct = new QAction(tr("E&xit"), this);
    exitAct->setShortcut(tr("Ctrl+Q"));
    exitAct->setStatusTip(tr("Exit the application"));
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

    startAllViewsAct = new QAction(tr("Alle Views starten"), this);
    startAllViewsAct->setStatusTip(tr("Startet die Logging-Funktion auf allen Views"));
    connect(startAllViewsAct, SIGNAL(triggered()), this, SLOT(startAllViews()));

    stopAllViewsAct = new QAction (tr("Alle Views stoppen"), this);
    stopAllViewsAct->setStatusTip(tr("Stoppt die Logging-Funktion auf allen Views"));
    stopAllViewsAct->setEnabled(false);
    connect(stopAllViewsAct, SIGNAL(triggered()), this, SLOT(stopAllViews()));

    configAct = new QAction(tr("&Config"), this);
    configAct->setShortcut(tr("Ctrl+K"));
    configAct->setStatusTip(tr("Öffnet den Konfigurationsdialog"));
    connect(configAct, SIGNAL(triggered()), this, SLOT(config()));

    saveConfigAct = new QAction(tr("Konfiguration &Speichern"), this);
    saveConfigAct->setStatusTip(tr("Speichert die Konfigurationen der Software"));
    connect(saveConfigAct, SIGNAL(triggered()), this, SLOT(saveConfig()));

    loadConfigAct = new QAction(tr("Konfiguration &Laden"), this);
    loadConfigAct->setStatusTip(tr("Lädt die Konfigurationen der Software"));
    connect(loadConfigAct, SIGNAL(triggered()), this, SLOT(loadConfig()));


    filterAct = new QAction(tr("&Filter"), this);
    filterAct->setShortcut(tr("Ctrl+F"));
    filterAct->setStatusTip(tr("Konfiguriert die Filter"));
    connect(filterAct, SIGNAL(triggered()), this, SLOT(filter()));

    saveFilterAct = new QAction(tr("Filter &Speichern"), this);
    saveFilterAct->setStatusTip(tr("Speichert die Filtereinstellungen"));
    connect(saveFilterAct, SIGNAL(triggered()), this, SLOT(saveFilter()));

    loadFilterAct = new QAction(tr("Filter &Laden"), this);
    loadFilterAct->setStatusTip(tr("Lädt Filtereinstellungen"));
    connect(loadFilterAct, SIGNAL(triggered()), this, SLOT(loadFilter()));


    aboutAct = new QAction(tr("&About"), this);
    aboutAct->setStatusTip(tr("Show the application's About box"));
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}

void fmbMonitor::createMenus()
{
      fileMenu = menuBar()->addMenu(tr("&Datei"));
      fileMenu->addAction(saveLogAct);            
      fileMenu->addAction(exitAct);

      functionMenu = menuBar()->addMenu(tr("F&unktion"));
      functionMenu->addAction(startAllViewsAct);
      functionMenu->addAction(stopAllViewsAct);
	  

      configMenu = menuBar()->addMenu(tr("&Konfiguration"));
      configMenu->addAction(configAct);
      configMenu->addAction(saveConfigAct);
      configMenu->addAction(loadConfigAct);

      filterMenu = menuBar()->addMenu(tr("&Filter"));
      filterMenu->addAction(filterAct);
      filterMenu->addAction(saveFilterAct);
      filterMenu->addAction(loadFilterAct);

      menuBar()->addSeparator();

      helpMenu = menuBar()->addMenu(tr("&Help"));
      helpMenu->addAction(aboutAct);
}

void fmbMonitor::createTabWidget()
{
	QString dummyString("Start");

	tabWidget = new QTabWidget();
	

	startTab = new GeneralTab(this, QString("Start"));
	startTab->setSize(300,400);
	

	QPushButton *dummyButton1 = new QPushButton("GoTo Ende");
	QVBoxLayout *dummyLayout1 = new QVBoxLayout(this);
	dummyLayout1->addWidget(dummyButton1);
	startTab->setLayout(dummyLayout1);


	endTab = new GeneralTab(this, QString("Ende"));
	endTab->setSize(300,400);

	QPushButton *dummyButton2 = new QPushButton("GoTo Start");
	QVBoxLayout *dummyLayout2 = new QVBoxLayout(this);
	dummyLayout2->addWidget(dummyButton2);
	endTab->setLayout(dummyLayout2);

	tabWidget->addTab(startTab, startTab->getName());
	tabWidget->addTab(endTab, endTab->getName());

    
}

void fmbMonitor::createStatusBar()
{
      statusBar()->showMessage(tr("Ready"));
}

fmbMonitor::~fmbMonitor()
{

}

