00001 #include "widgetdialog.h"
00002 
00003 #include <QVBoxLayout>
00004 #include <QDialogButtonBox>
00005 #include <QAbstractButton>
00006 
00007 using namespace ui;
00008 
00009 WidgetDialog::WidgetDialog(QWidget* widget, QWidget* parent,
00010                            const QStringList& options) : QDialog(parent)
00011 {
00012     if (widget != 0)
00013     {
00014         widget->setParent(this);
00015 
00016         QVBoxLayout* layout = new QVBoxLayout(this);
00017         layout->addWidget(widget);
00018 
00019         QDialogButtonBox* buttons = new QDialogButtonBox(this);
00020         layout->addWidget(buttons);
00021 
00022         if (options.isEmpty())
00023         {
00024             buttons->addButton(QDialogButtonBox::Ok);
00025             buttons->addButton(QDialogButtonBox::Cancel);
00026         }
00027         else
00028         {
00029             Q_FOREACH (QString option, options)
00030             {
00031                 if (option.toLower() == "ok")
00032                     buttons->addButton(QDialogButtonBox::Ok);
00033                 else if (option.toLower() == "cancel")
00034                     buttons->addButton(QDialogButtonBox::Cancel);
00035                 else if (option.toLower() == "yes")
00036                     buttons->addButton(QDialogButtonBox::Yes);
00037                 else if (option.toLower() == "no")
00038                     buttons->addButton(QDialogButtonBox::No);
00039                 else
00040                     buttons->addButton(option, QDialogButtonBox::AcceptRole);
00041             }
00042 
00043             connect(buttons, SIGNAL(clicked(QAbstractButton*)),
00044                     this, SLOT(handleClick(QAbstractButton*)));
00045         }
00046 
00047         connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
00048         connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
00049         resize(widget->sizeHint());
00050     }
00051 }
00052 
00053 QString WidgetDialog::chosenOption()
00054 {
00055     return clickedText;
00056 }
00057 
00058 void WidgetDialog::handleClick(QAbstractButton* button)
00059 {
00060     clickedText = button->text();
00061 }