/***************************************************************************
 *   Copyright (c) 2009  Peter Mylemans <peter.mylemans@gmail.com>         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include <QSplitter>

#include "MainWindow.h"
#include "widgets/SideBar.h"
#include "collection/CollectionBrowser.h"
#include "views/MovieView.h"

MainWindow::MainWindow()
    : QMainWindow(0), m_oldSplitSize(0.1f)
{
    // TODO: remove me.
    setMinimumSize(800, 600);

    m_splitter = new QSplitter(this);

    m_sideBar = new SideBar(m_splitter);
    connect(m_sideBar, SIGNAL(collapsedChanged(bool)), this, SLOT(sideBarCollapsedChanges(bool)));

    createTabs();

    m_splitter->addWidget(sideBar());
    m_splitter->setCollapsible(0, false);

    m_contextWidget = new ContextWidget(m_splitter);
    m_splitter->addWidget(m_contextWidget);

    sideBar()->setActiveTab(0);
    setCentralWidget(m_splitter);
}

void MainWindow::sideBarCollapsedChanges(bool collapsed)
{
    QList<int> sizes;
    if (collapsed) {
        // Save split percentage
        m_oldSplitSize = (float)m_splitter->sizes().at(0) / (float)m_splitter->width();

        // Reset split size to minimal size.
        sizes.append(sideBar()->sizeHint().width());
    } else {
        // Restore split percentage
        sizes.append((int)(m_oldSplitSize * (float)m_splitter->width()));
    }

    sizes.append(m_splitter->width() - sizes.first());
    m_splitter->setSizes(sizes);
}

void MainWindow::createTabs()
{
    // Local collection
    sideBar()->addTab("Collection", new CollectionBrowser(this));
}
