/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   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 "veneer/settings.h"

#include <kconfiggroup.h>
#include <kglobal.h>

#include <QApplication>
#include <QDesktopWidget>
#include <QDialog>

namespace Veneer
{

	Settings* Veneer::Settings::self()
	{
		static Settings* self_ = NULL;

		if (self_ == NULL)
			self_ = new Settings();

		return self_;
	}

	void saveDialogSize(const QString& group, QDialog* dlg)
	{
		int screenNumber = QApplication::desktop()->screenNumber(dlg->parentWidget());
		QRect desktopGeometry = QApplication::desktop()->screenGeometry(screenNumber);

		const QSize sizeToSave = dlg->size();

		KConfigGroup cfg(KGlobal::config(), group);
		cfg.writeEntry(QString("width_%1").arg(desktopGeometry.width()), sizeToSave.width());
		cfg.writeEntry(QString("height_%1").arg(desktopGeometry.height()), sizeToSave.height());
	}

	void restoreDialogSize(const QString& group, QDialog* dlg)
	{
		int screenNumber = QApplication::desktop()->screenNumber(dlg->parentWidget());
		QRect desktopGeometry = QApplication::desktop()->screenGeometry(screenNumber);

		int width = dlg->sizeHint().width();
		int height = dlg->sizeHint().height();

		KConfigGroup cfg(KGlobal::config(), group);
		width = cfg.readEntry<int>(QString("width_%1").arg(desktopGeometry.width()), width);
		height= cfg.readEntry<int>(QString("height_%1").arg(desktopGeometry.height()), height);

		dlg->resize(width, height);
	}

}

