#include "options.h"
#include "options.moc"
#include <qpushbt.h>

const int LIST_WIDTH = 100;
const int BUTTON_WIDTH = 100;
const int BUTTON_HEIGHT = 30;
const int BUTTON_BORDER = 10;
#define OFFSET (BUTTON_HEIGHT + 2*BUTTON_BORDER+separator->height())

OptionsDialog::OptionsDialog(QWidget * parent=0, const char * name=0, bool modal, WFlags f)
        : QDialog(parent, name, modal, f)
{
    list = new QListBox(this, "list");
    connect(list, SIGNAL(highlighted(const char *)), SLOT(ShowDialog(const char *)) );
    dialogs = new QList<QWidget>();
    okButton = new QPushButton("Ok", this);
    connect(okButton, SIGNAL(clicked()), SLOT(accept()) );
    connect(okButton, SIGNAL(clicked()), SLOT(ApplyPressed()) );
    applyButton = new QPushButton("Apply", this);
    connect(applyButton, SIGNAL(clicked()), SLOT(ApplyPressed()) );
    cancelButton = new QPushButton("Cancel", this);
    connect(cancelButton, SIGNAL(clicked()), SLOT(reject()) );
    separator = new KSeparator(this, "separator");
}

void OptionsDialog::AddDialog(QWidget *dlg, const char *name, int index)
{
    CHECK_PTR(dlg);
    dlg->recreate(this, 0, QPoint(LIST_WIDTH, 0), TRUE); 
    dialogs->append(dlg); // put the dialog in the list
    if (index != -1)
        dialogs->insert(index, dlg); // in the right place
    list->insertItem(name, index);
    list->setCurrentItem(0); // we always want to start with the first
    resize(dlg->width() + LIST_WIDTH, dlg->height() + OFFSET); // resize us
    ShowDialog(list->text(0));
}
    
void OptionsDialog::resizeEvent(QResizeEvent *)
{
    list->setGeometry(0,0,LIST_WIDTH, height()-OFFSET);
    QRect rect(LIST_WIDTH,0, width()-LIST_WIDTH, height()-OFFSET);
    for (unsigned int i=0; i<dialogs->count(); i++)
        (dialogs->at(i))->setGeometry(rect); // resize each dialog the same
    separator->setGeometry(0, list->height(), width(), separator->height());
    // now place the buttons
    okButton->setGeometry(BUTTON_BORDER, height() - BUTTON_HEIGHT-BUTTON_BORDER,
                          BUTTON_WIDTH, BUTTON_HEIGHT);
    applyButton->setGeometry(2*BUTTON_BORDER+BUTTON_WIDTH, \
                             height()-BUTTON_HEIGHT-BUTTON_BORDER,  BUTTON_WIDTH, \
                             BUTTON_HEIGHT);
    cancelButton->setGeometry(3*BUTTON_BORDER+2*BUTTON_WIDTH, \
                              height()-BUTTON_HEIGHT-BUTTON_BORDER, \
                              BUTTON_WIDTH, BUTTON_HEIGHT);
    
}


void OptionsDialog::ShowDialog(const char *which)
{
    // hide all dialogs except ours
    for (unsigned int i=0; i<dialogs->count(); i++)
    {
        if (QString(list->text(i)) == QString(which))
        {
            dialogs->at(i)->raise();
        }
        //   else
        //  (dialogs->at(i))->hide();
    }
}

void OptionsDialog::ApplyPressed()
{
    emit ApplyButtonPressed();
}
