#include "ContextWidget.h"

#include <QVBoxLayout>

ContextWidget::ContextWidget(QWidget *parent)
    : QFrame(parent), m_currentView(NULL)
{
    setLayout(new QVBoxLayout(this));
}

void ContextWidget::changeContext(ContextModel *model)
{
    if (model == NULL)
        return;

    // If current view supports the model, we only need to update it.
    if (m_currentView != NULL && m_currentView->supportsModelType(model->modelId())) {
        m_currentView->updateForModel(model);
        return;
    }

    // Change view to supported model.
    if (m_currentView != NULL)
        m_currentView->hide();
    m_currentView = NULL;
    for (int i = m_views.size() - 1; i >= 0; i--) {
        ContextView *view = m_views[i];
        if (view->supportsModelType(model->modelId())) {
            m_currentView = view;
            break;
        }
    }
    if (m_currentView != NULL) {
        m_currentView->show();
        m_currentView->updateForModel(model);
    }
}

void ContextWidget::registerView(ContextView *view)
{
    // Reparent if necessary.
    if (view->parentWidget() != this)
        view->setParent(this);

    view->hide();

    m_views.append(view);
    layout()->addWidget(view);
}
