#include "projectfilesystemwidgetplugin.h"
#include "projectfilesystemwidget.h"
#include <utils/utilsicons.h>
#include <QToolButton>
#include <QSettings>
#include <QMessageBox>


ProjectFSWidgetFactory::ProjectFSWidgetFactory() {
	setDisplayName(QString::fromLatin1("Project filesystem"));
	setId("project_filesystem");
}


Core::NavigationView ProjectFSWidgetFactory::createWidget() {
	Core::NavigationView view;
	view.widget = new ProjectFilesystemWidget();
	QToolButton * btn = new QToolButton();
	btn->setIcon(Utils::Icons::RELOAD.icon());
	btn->setToolTip(tr("Reload tree"));
	view.dockToolBarWidgets << btn;
	connect(btn, SIGNAL(clicked()), view.widget, SLOT(projectsChanged()));
	btn = new QToolButton();
	btn->setIcon(Utils::Icons::FILTER.icon());
	btn->setToolTip(tr("Setup filters ..."));
	view.dockToolBarWidgets << btn;
	connect(btn, SIGNAL(clicked()), view.widget, SLOT(filterClicked()));
	return view;
}


void ProjectFSWidgetFactory::saveSettings(QSettings * settings, int position, QWidget * widget) {
	//QMessageBox::information(0, "", QString::fromLatin1(widget->metaObject()->className()));
	ProjectFilesystemWidget * w = qobject_cast<ProjectFilesystemWidget * >(widget);
	if (!w) return;
	settings->beginGroup("ProjectFilesystem");
	settings->beginWriteArray("widget");
	settings->setArrayIndex(position);
	settings->setValue("filters", w->currentFilters().toVariant());
	settings->setValue("search", w->currentSearch());
	settings->endArray();
	settings->endGroup();
	settings->sync();
}


void ProjectFSWidgetFactory::restoreSettings(QSettings * settings, int position, QWidget * widget) {
	ProjectFilesystemWidget * w = qobject_cast<ProjectFilesystemWidget * >(widget);
	if (!w) return;
	settings->beginGroup("ProjectFilesystem");
	settings->beginReadArray("widget");
	settings->setArrayIndex(position);
	w->setCurrentFilter(FilterDialog::Filter(settings->value("filters", FilterDialog::Filter().toVariant())));
	w->setCurrentSearch(settings->value("search", QString()).toString());
	settings->endArray();
	settings->endGroup();
}
