/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QApplication>
#include <QMap>
#include <util/compat/fontwidth.h>
#include "filepicker.h"

using namespace LC;

FilePicker::FilePicker (FilePicker::Type type, QWidget *parent)
: QWidget (parent)
, ClearOnCancel_ (false)
, Type_ (type)
{
	LineEdit_ = new QLineEdit (this);
	BrowseButton_ = new QPushButton (tr ("Browse..."));
	QHBoxLayout *lay = new QHBoxLayout;
	lay->setContentsMargins (0, 0, 0, 0);
	lay->addWidget (LineEdit_);
	lay->addWidget (BrowseButton_);
	setLayout (lay);
	connect (BrowseButton_,
			SIGNAL (released ()),
			this,
			SLOT (chooseFile ()));
	connect (LineEdit_,
			SIGNAL (textEdited (const QString&)),
			this,
			SIGNAL (textChanged (const QString&)));
	LineEdit_->setMinimumWidth (Util::Compat::Width (QApplication::fontMetrics (),
			"thisismaybeadefaultsettingstring,dont"));
}

void FilePicker::SetText (QString text)
{
	LineEdit_->setText (text);
}

QString FilePicker::GetText () const
{
	return LineEdit_->text ();
}

void FilePicker::SetClearOnCancel (bool clear)
{
	ClearOnCancel_ = clear;
}

void FilePicker::SetFilter (const QString& filter)
{
	Filter_ = filter;
}

void FilePicker::chooseFile ()
{
	QString name;
	switch (Type_)
	{
		case Type::ExistingDirectory:
			name = QFileDialog::getExistingDirectory (this,
					tr ("Select directory"),
					LineEdit_->text (),
					0);
			break;
		case Type::OpenFileName:
			name = QFileDialog::getOpenFileName (this,
					tr ("Select file"),
					LineEdit_->text (),
					Filter_);
			break;
		case Type::SaveFileName:
			name = QFileDialog::getSaveFileName (this,
					tr ("Select file"),
					LineEdit_->text (),
					Filter_);
			break;
	}
	if (name.isEmpty () && !ClearOnCancel_)
		return;

	LineEdit_->setText (name);
	emit textChanged (name);
}

