#include <QtGui/QComboBox>
#include <QtGui/QDoubleValidator>
#include <QtCore/QFile>
#include <QtGui/QGridLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtCore/QLocale>
#include <QtGui/QPushButton>
#include <QtUiTools/QtUiTools>
#include <QtGui/QVBoxLayout>
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include <maya/MObject.h>
#include <maya/MStringArray.h>
#include <qtForms.h>
CubeCreator::CubeCreator(QWidget* parent)
:   QDialog(parent)
,   fCurValue(0.0)
{
    
    setupUi(this);
    
    setAttribute(Qt::WA_DeleteOnClose, true);
    
    sizeField->setValidator(new QDoubleValidator(-10.0, 10.0, 2, sizeField));
    
    
    connect(
        sizeSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderChanged(int))
    );
    connect(
        sizeField, SIGNAL(textEdited(const QString&)),
        this, SLOT(fieldChanged(const QString&))
    );
    show();
}
void CubeCreator::accept()
{
    
    
        MString(
"polyCube -w ") + fCurValue + 
" -h " + fCurValue
 
            + " -d " + fCurValue,
        names
    );
    
    emit objectCreated(names[0].asChar());
}
void CubeCreator::fieldChanged(const QString& newValue)
{
    QLocale defaultLocale;
    
    
    
    fCurValue = defaultLocale.toDouble(newValue);
    sizeSlider->setValue((int)(fCurValue * 100.0));
}
void CubeCreator::sliderChanged(int newValue)
{
    QLocale defaultLocale;
    
    
    
    fCurValue = ((double)newValue) / 100.0;
    sizeField->setText(defaultLocale.toString(fCurValue));
}
SphereCreator::SphereCreator(QWidget* parent)
:   QWidget(parent)
,   fCurValue(0.0)
{
    
    QUiLoader   loader;
    QFile       file(":/sphereForm.ui");
    file.open(QFile::ReadOnly);
    fForm = loader.load(&file, this);
    file.close();
    if (fForm) {
        
        fForm->setAttribute(Qt::WA_DeleteOnClose, true);
        
        fButtonBox = fForm->findChild<QDialogButtonBox*>("buttonBox");
        fField = fForm->findChild<QLineEdit*>("sizeField");
        fSlider = fForm->findChild<QSlider*>("sizeSlider");
    
        
        
        connect(fButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
    
        
        fField->setValidator(new QDoubleValidator(-10.0, 10.0, 2, fField));
    
        
        
        connect(
            fSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderChanged(int))
        );
        connect(
            fField, SIGNAL(textEdited(const QString&)),
            this, SLOT(fieldChanged(const QString&))
        );
        
        connect(
            fForm, SIGNAL(destroyed(QObject*)), this, SLOT(deleteLater())
        );
        fForm->show();
    }
}
void SphereCreator::accept()
{
    QString     objectName(names[0].asChar());
    emit objectCreated(objectName);
}
void SphereCreator::fieldChanged(const QString& newValue)
{
    QLocale defaultLocale;
    
    
    
    fCurValue = defaultLocale.toDouble(newValue);
    fSlider->setValue((int)(fCurValue * 100.0));
}
void SphereCreator::sliderChanged(int newValue)
{
    QLocale defaultLocale;
    
    
    
    fCurValue = ((double)newValue) / 100.0;
    fField->setText(defaultLocale.toString(fCurValue));
}
ObjectTypeDialog::ObjectTypeDialog(QWidget* parent)
:   QDialog(parent)
,   fSelectObjList(0)
{
    
    QLabel* selectObjLabel = new QLabel("Object Type");
    selectObjLabel->setAlignment(Qt::AlignRight);
    fSelectObjList = new QComboBox();
    fSelectObjList->addItem("None");
    fSelectObjList->addItem("Cube");
    fSelectObjList->addItem("Sphere");
    selectObjLabel->setBuddy(fSelectObjList);
    QLabel* mostRecentLabel = new QLabel("Most recently created");
    mostRecentLabel->setAlignment(Qt::AlignRight);
    fMostRecentField = new QLineEdit();
    fMostRecentField->setReadOnly(true);
    mostRecentLabel->setBuddy(fMostRecentField);
    QPushButton*    closeButton = new QPushButton("Close");
    
    QGridLayout*    gridLayout = new QGridLayout();
    gridLayout->addWidget(selectObjLabel, 0, 0, Qt::AlignRight);
    gridLayout->addWidget(fSelectObjList, 0, 1, Qt::AlignLeft);
    gridLayout->addWidget(mostRecentLabel, 1, 0, Qt::AlignRight);
    gridLayout->addWidget(fMostRecentField, 1, 1, Qt::AlignLeft);
    
    QVBoxLayout*    mainLayout = new QVBoxLayout();
    mainLayout->addLayout(gridLayout);
    mainLayout->addWidget(closeButton, 0, Qt::AlignHCenter);
    
    setLayout(mainLayout);
    
    
    connect(
        fSelectObjList, SIGNAL(currentIndexChanged(const QString&)),
        this, SLOT(displayObjectDialog(const QString&))
    );
    
    setAttribute(Qt::WA_DeleteOnClose, true);
    connect(
        closeButton, SIGNAL(clicked()), this, SLOT(close())
    );
    
    show();
}
ObjectTypeDialog::~ObjectTypeDialog()
{
    
    if (!fCurrentDialog.isNull()) delete fCurrentDialog;
}
void ObjectTypeDialog::displayObjectDialog(const QString& item)
{
    
    if (!fCurrentDialog.isNull()) {
        delete fCurrentDialog;
    }
    if (item == "Cube") {
        fCurrentDialog = new CubeCreator();
    }
    else if (item == "Sphere") {
        fCurrentDialog = new SphereCreator();
    }
    if (!fCurrentDialog.isNull()) {
        
        
        connect(
            fCurrentDialog, SIGNAL(objectCreated(const QString&)),
            fMostRecentField, SLOT(setText(const QString&))
        );
    
        
        connect(
            fCurrentDialog, SIGNAL(destroyed(QObject*)),
            this, SLOT(resetSelector())
        );
    }
}
void ObjectTypeDialog::resetSelector()
{
    
    fSelectObjList->setCurrentIndex(0);
}
QPointer<ObjectTypeDialog>  qtFormsCmd::objectCreator;
const MString           qtFormsCmd::commandName(
"qtForms");
 
void qtFormsCmd::cleanup()
{
    if (!objectCreator.isNull()) delete objectCreator;
}
{
    
    
    if (objectCreator.isNull()) {
        objectCreator = new ObjectTypeDialog();
    }
    else {
        objectCreator->showNormal();
        objectCreator->raise();
    }
}
{
    MFnPlugin   pluginFn(plugin, 
"Autodesk, Inc.", 
"1.0", 
"Any", &st);
 
    if (!st) {
            MString(
"qtForms - could not initialize plugin: ")
 
        );
        return st;
    }
    
    st = pluginFn.registerCommand(qtFormsCmd::commandName, qtFormsCmd::creator);
    if (!st) {
            MString(
"qtForms - could not register '")
 
            + qtFormsCmd::commandName + "' command: "
        );
        return st;
    }
    return st;
}
{
    MFnPlugin   pluginFn(plugin, 
"Autodesk, Inc.", 
"1.0", 
"Any", &st);
 
    if (!st) {
            MString(
"qtForms - could not uninitialize plugin: ")
 
        );
        return st;
    }
    
    qtFormsCmd::cleanup();
    
    st = pluginFn.deregisterCommand(qtFormsCmd::commandName);
    if (!st) {
            MString(
"qtForms - could not deregister '")
 
            + qtFormsCmd::commandName + "' command: "
        );
        return st;
    }
    return st;
}