/***************************************************************************
 *   Copyright (C) 2008 by S. MANKOWSKI / G. DE BURE support@mankowski.fr  *
 *                                                                         *
 *   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, see <http://www.gnu.org/licenses/>  *
 ***************************************************************************/
/** @file
 * A plugin to select all
 *
 * @author Stephane MANKOWSKI
 */
#include "skgselectallplugin.h"

#include <KActionCollection>
#include <KStandardAction>
#include <KAboutData>
#include <KGenericFactory>

#include <QAbstractItemView>

#include "skgmainpanel.h"
#include "skgtreeview.h"
#include "skgtraces.h"
/**
 * This plugin factory.
 */
K_PLUGIN_FACTORY(SKGSelectAllPluginFactory, registerPlugin<SKGSelectAllPlugin>();)
/**
 * This plugin export.
 */
K_EXPORT_PLUGIN(SKGSelectAllPluginFactory("skg_selectall", "skg_selectall"))

SKGSelectAllPlugin::SKGSelectAllPlugin(QWidget* iWidget, QObject* iParent, const QVariantList& /*iArg*/) :
    SKGInterfacePlugin(iParent),
    m_currentDocument(NULL), m_selectAllAction(NULL)
{
    Q_UNUSED(iWidget);
    SKGTRACEINFUNC(10);
}

SKGSelectAllPlugin::~SKGSelectAllPlugin()
{
    SKGTRACEINFUNC(10);
    m_currentDocument = NULL;
    m_selectAllAction = NULL;
}

bool SKGSelectAllPlugin::setupActions(SKGDocument* iDocument, const QStringList& iArgument)
{
    SKGTRACEINFUNC(10);
    Q_UNUSED(iArgument);

    m_currentDocument = iDocument;

    setComponentData(KGlobal::mainComponent());
    setXMLFile("../skg_selectall/skg_selectall.rc");

    // Menu
    m_selectAllAction = actionCollection()->addAction(KStandardAction::SelectAll, "edit_select_all", this, SLOT(onSelectAll()));
    registerGlobalAction("edit_select_all", m_selectAllAction);

    return true;
}

void SKGSelectAllPlugin::refresh()
{
    SKGTRACEINFUNC(10);
    if (m_selectAllAction) {
        m_selectAllAction->setEnabled(false);
    }
    if (SKGMainPanel::getMainPanel()) {
        SKGTabPage* page = SKGMainPanel::getMainPanel()->currentPage();
        if (page) {
            QAbstractItemView* view = qobject_cast<QAbstractItemView*>(page->mainWidget());
            if (view && m_selectAllAction) {
                m_selectAllAction->setEnabled(true);
            }
        }
    }
}

QString SKGSelectAllPlugin::title() const
{
    return i18nc("Select all objects in a list", "Select all");
}

QString SKGSelectAllPlugin::icon() const
{
    return "edit-select-all";
}

QString SKGSelectAllPlugin::toolTip() const
{
    return i18nc("Select all objects in a list", "Select all");
}

int SKGSelectAllPlugin::getOrder() const
{
    return 6;
}

QStringList SKGSelectAllPlugin::tips() const
{
    QStringList output;
    return output;
}

void SKGSelectAllPlugin::onSelectAll()
{
    SKGTRACEINFUNC(10);
    if (SKGMainPanel::getMainPanel()) {
        SKGTabPage* page = SKGMainPanel::getMainPanel()->currentPage();
        if (page) {
            QAbstractItemView* view = qobject_cast<QAbstractItemView*>(page->mainWidget());
            if (view) {
                view->selectAll();
            }
        }
    }
}

#include "skgselectallplugin.moc"
