ProfileWindow::ProfileWindow(QWidget *parent, RunnerDatabase * databaseAccess)
    : QDialog(parent)
{
    //Sets up window UI
    setupUi(this);
    TheDatabase = NULL;
    LoadedProfileIndex = 0;
    ChangesMade = false;

    //Grab pointer to Database Access Object
    TheDatabase = databaseAccess;

    //Add profile names to first list
    loadProfilesToList();

    //Form Connections for buttons
    connect(ProfileList,SIGNAL(itemPressed(QListWidgetItem*)),this,SLOT(showSelectedProfileInfo()));
    connect(ProfileAddButton,SIGNAL(clicked()),this,SLOT(addNewProfile()));
    connect(ProfileRemoveButton,SIGNAL(clicked()),this,SLOT(removeProfile()));
    connect(AthleteAddButton,SIGNAL(clicked()),this,SLOT(addNewAthlete()));
    connect(AthleteRemoveButton,SIGNAL(clicked()),this,SLOT(removeAthlete()));
    connect(ButtonBox,SIGNAL(accepted()),this,SLOT(closeAndSave()));
}
QWidget *ProfileOptionsPage::createPage(QWidget *parent)
{
    m_configWidget = new QWidget(parent);

    m_profilesView = new QTreeView(m_configWidget);
    m_profilesView->setUniformRowHeights(true);
    m_profilesView->header()->setStretchLastSection(true);

    m_addButton = new QPushButton(tr("Add"), m_configWidget);
    m_cloneButton = new QPushButton(tr("Clone"), m_configWidget);
    m_delButton = new QPushButton(tr("Remove"), m_configWidget);
    m_makeDefaultButton = new QPushButton(tr("Make Default"), m_configWidget);

    QVBoxLayout *buttonLayout = new QVBoxLayout();
    buttonLayout->setContentsMargins(0, 0, 0, 0);
    buttonLayout->addWidget(m_addButton);
    buttonLayout->addWidget(m_cloneButton);
    buttonLayout->addWidget(m_delButton);
    buttonLayout->addWidget(m_makeDefaultButton);
    buttonLayout->addItem(new QSpacerItem(10, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));

    QVBoxLayout *verticalLayout = new QVBoxLayout();
    verticalLayout->addWidget(m_profilesView);

    QHBoxLayout *horizontalLayout = new QHBoxLayout(m_configWidget);
    horizontalLayout->addLayout(verticalLayout);
    horizontalLayout->addLayout(buttonLayout);

    Q_ASSERT(!m_model);
    m_model = new Internal::ProfileModel(verticalLayout);
    connect(m_model, SIGNAL(profileStateChanged()), this, SLOT(updateState()));

    m_profilesView->setModel(m_model);
    m_profilesView->header()->setResizeMode(0, QHeaderView::Stretch);
    m_profilesView->expandAll();

    m_selectionModel = m_profilesView->selectionModel();
    connect(m_selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(profileSelectionChanged()));
    connect(ProfileManager::instance(), SIGNAL(profileAdded(ProjectExplorer::Profile*)),
            this, SLOT(profileSelectionChanged()));
    connect(ProfileManager::instance(), SIGNAL(profileRemoved(ProjectExplorer::Profile*)),
            this, SLOT(profileSelectionChanged()));
    connect(ProfileManager::instance(), SIGNAL(profileUpdated(ProjectExplorer::Profile*)),
            this, SLOT(profileSelectionChanged()));

    // Set up add menu:
    connect(m_addButton, SIGNAL(clicked()), this, SLOT(addNewProfile()));
    connect(m_cloneButton, SIGNAL(clicked()), this, SLOT(cloneProfile()));
    connect(m_delButton, SIGNAL(clicked()), this, SLOT(removeProfile()));
    connect(m_makeDefaultButton, SIGNAL(clicked()), this, SLOT(makeDefaultProfile()));

    m_searchKeywords = tr("Profiles");

    updateState();

    if (m_toShow)
        m_selectionModel->select(m_model->indexOf(m_toShow),
                                 QItemSelectionModel::Clear
                                 | QItemSelectionModel::SelectCurrent
                                 | QItemSelectionModel::Rows);
    m_toShow = 0;

    return m_configWidget;
}