//-----------------------------------------------------------------------------
//  Copyright (C) 2002-2010 Thomas S. Ullrich 
//
//  This file is part of "xyscan".
//
//  This file may be used under the terms of the GNU General Public License.
//  This project is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License.
//  
//  Author: Thomas S. Ullrich
//  Last update: November 19, 2009
//-----------------------------------------------------------------------------
#include "xyscanAbout.h"
#include "xyscanVersion.h"
#include <iostream>

using namespace std; 

xyscanAbout::xyscanAbout(QWidget* parent, Qt::WindowFlags fl)
    : QDialog(parent, fl),
      mTitlePixmap(":/images/xyscanSplash.png"),
      mVersionText(QString("Release ") + QString(VERSION) + QString("   (") + QString(__DATE__) + QString(")"))
{
    setModal(true);

    //
    //  Merge version/release string into pixmap
    //
    QPainter p; 
    p.begin( &mTitlePixmap ); 
    int strWidth = p.fontMetrics().width( mVersionText ); 
    int strHeight = p.fontMetrics().height(); 
    p.setPen(Qt::gray);
    p.drawText(QRect(341, 98, strWidth, strHeight), 0, mVersionText ); 
    p.end(); 
    
    //
    //  First define the pixmap label
    //
    int w = mTitlePixmap.width();
    int h = mTitlePixmap.height();
    
    mTitlePixmapLabel = new QLabel(this);

    mTitlePixmapLabel->setGeometry(QRect( 0, 0, w, h));
    mTitlePixmapLabel->setFixedSize(QSize(w, h));
    mTitlePixmapLabel->setLineWidth(0);
	mTitlePixmapLabel->setPixmap(mTitlePixmap);
    mTitleShown = true;

    mTitlePixmapLabel->setScaledContents(false);
    mTitlePixmapLabel->setAlignment(Qt::AlignLeft|Qt::AlignTop);
    mTitlePixmapLabel->setIndent(10); // for text

    //
    //  Now the line (just looks nicer with it)
    //
    mLine = new QFrame(this);
    mLine->setGeometry(QRect(0, h, w, 5));
    mLine->setFrameShape(QFrame::HLine);
    mLine->setFrameShadow(QFrame::Sunken);

    //
    //  The buttons
    //
    mButtonOk = new QPushButton(this);
    mButtonOk->setAutoDefault(true);
    mButtonOk->setDefault(true);
    mButtonOk->setText( tr("&Ok" ));
    mButtonLicense = new QPushButton(this);
    mButtonLicense->setText(tr("&License"));

    //
    //  Then the dialog itself
    //
    setWindowTitle(tr( "About xyscan"));
    setSizeGripEnabled(false);
    setFixedWidth(w);

    //
    //  Layout the buttons
    //
    QSpacerItem* spacer = new QSpacerItem (300,20, QSizePolicy::Expanding, QSizePolicy::Minimum );
    mButtonLayout = new QHBoxLayout;
    mButtonLayout->addWidget(mButtonLicense);
    mButtonLayout->addItem(spacer);
    mButtonLayout->addWidget(mButtonOk);
    mButtonLayout->setMargin(4);

    //
    //  Overall widget layout
    //
	mLayout = new QVBoxLayout(this);
	mLayout->setSpacing(0);
	mLayout->setMargin(0);
    mLayout->addWidget(mTitlePixmapLabel);
    mLayout->addWidget(mLine);
    mLayout->addLayout(mButtonLayout);

    setFixedSize(mLayout->minimumSize());
   
    //
    //  Connect slots
    //
    connect( mButtonOk, SIGNAL(clicked()), this, SLOT(accept()));
    connect( mButtonLicense, SIGNAL(clicked()), this, SLOT(license()));
}

void xyscanAbout::accept()  // Ok button pressed
{
    //
    // Hide it and make sure the next time it pops
    // up the title page is shown.
    //
    hide();
    if (!mTitleShown) license();
}

void xyscanAbout::license()  // License button pressed
{
    if (mTitleShown) {
        QPalette palette;
        palette.setColor(mTitlePixmapLabel->backgroundRole(), Qt::white);// white background
        mTitlePixmapLabel->setPalette(palette);       
        mButtonLicense->setText(tr("Title && &Version"));
        mTitleShown = false;
        mTitlePixmapLabel->setText(tr(
            "Copyright (C) 2002-2010 Thomas S. Ullrich\n"
            "\n"
            "This program is free software; you can redistribute it and/or\n"
            "modify it under the terms of the GNU General Public License as\n"
            "published by the Free Software Foundation; either version 3 of\n"
            "the License, or any later version.\n"
            "\n"
            "This program is distributed  in the hope that it will be useful,\n"
            "but without any warranty; without even the implied warranty of\n"
            "merchantability or fitness for a particular purpose. See the GNU\n"
            "General Public License for more details.\n"
            "\n"
            "You should have received a copy of the GNU General Public\n"
            "License along with this program. If not, see <http://www.gnu.org/licenses/>."
            ));
    }
    else {
        mTitlePixmapLabel->setPixmap(mTitlePixmap);	
        mButtonLicense->setText(tr("&License"));
        mTitleShown = true;
    }
    update();
}


