//-----------------------------------------------------------------------------
// File: HelpWindow.cpp
//-----------------------------------------------------------------------------
// Project: Kactus 2
// Author: Joni-Matti Maatta
// Date: 7.8.2012
//
// Description:
// Help window that allows browsing the documentation through table of
// contents and index.
//-----------------------------------------------------------------------------

#include "HelpWindow.h"

#include "ContextHelpBrowser.h"
#include "HelpSearchWidget.h"

#include <QHelpContentWidget>
#include <QHelpIndexWidget>
#include <QHelpSearchQueryWidget>
#include <QHelpSearchResultWidget>

//-----------------------------------------------------------------------------
// Function: HelpWindow::HelpWindow()
//-----------------------------------------------------------------------------
HelpWindow::HelpWindow(QHelpEngine* engine, QWidget* parent): QMainWindow(parent),
    engine_(engine),
    menu_(new QMenuBar(this)),
    tocDock_(new QDockWidget(tr("Contents"), this)),
    browser_(new ContextHelpBrowser(engine, "qthelp://fi.tuni.kactus2/doc", this))
{
    setWindowTitle(tr("Kactus2 Help"));
    resize(800, 500);

    // Create the menu bar and all its menus and actions.
    QMenu* fileMenu = menu_->addMenu(tr("File"));
    fileMenu->addAction(tr("Close"), this, SLOT(close()));

    setMenuBar(menu_);

    // Add the table of contents dock.
    tocDock_->setWidget(engine_->contentWidget());
    tocDock_->setFeatures(QDockWidget::NoDockWidgetFeatures);
    addDockWidget(Qt::LeftDockWidgetArea, tocDock_);

    setCentralWidget(browser_);
    setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);

    connect(engine_->indexWidget(), SIGNAL(linkActivated(QUrl const&, QString const&)),
            browser_, SLOT(setSource(QUrl const&)), Qt::UniqueConnection);

    connect(engine_->contentWidget(), SIGNAL(linkActivated(QUrl const&)),
            browser_, SLOT(setSource(QUrl const&)), Qt::UniqueConnection);

    connect(engine_->searchEngine()->resultWidget(), SIGNAL(requestShowLink(QUrl const&)),
            browser_, SLOT(setSource(QUrl const&)), Qt::UniqueConnection);
}

//-----------------------------------------------------------------------------
// Function: HelpWindow::~HelpWindow()
//-----------------------------------------------------------------------------
HelpWindow::~HelpWindow()
{

}

//-----------------------------------------------------------------------------
// Function: HelpWindow::showEvent()
//-----------------------------------------------------------------------------
void HelpWindow::showEvent(QShowEvent* /*event*/)
{
    browser_->onHelpRequested("index.html");
    engine_->contentWidget()->expandToDepth(0);
}
