/**********************************************************************
 * 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.
 **********************************************************************/

#pragma once

#include <QObject>
#include "guiconfig.h"

class QToolButton;
class QLineEdit;

namespace LC
{
namespace Util
{
	/** @brief Manages additional overlay buttons in a QLineEdit.
	 *
	 * This class manages custom buttons (like "Clear") on a QLineEdit or
	 * a derived class. Particularly, it lays outs the buttons next to
	 * each other and updates their positions when the managed line edit
	 * is moved or resized.
	 *
	 * Only one LineEditButtonManager can be installed on a single
	 * QLineEdit. Installing more than one manager will result in an
	 * exception thrown from the constructor.
	 *
	 * The managed line edit owns the created LineEditButtonManager.
	 *
	 * @sa ClearLineEditAddon
	 *
	 * @ingroup GuiUtil
	 */
	class UTIL_GUI_API LineEditButtonManager : public QObject
	{
		Q_OBJECT

		QLineEdit * const Edit_;
		const int FrameWidth_;

		int Pad_;

		QList<QToolButton*> Buttons_;
	public:
		/** @brief Constructs the manager for the line \em edit.
		 *
		 * Constructs the LineEditButtonManager managing the given line
		 * \em edit.
		 *
		 * \em edit becomes the owner of the manager.
		 *
		 * @param[in] edit The line edit to manage.
		 * @exception std::runtime_error the line edit is already managed
		 * by another LineEditButtonManager instance.
		 */
		LineEditButtonManager (QLineEdit *edit);

		/** @brief Adds a \em button to the line edit.
		 *
		 * @param[in] button The button to add to the managed line edit.
		 */
		void Add (QToolButton *button);
	protected:
		bool eventFilter (QObject*, QEvent*);
	private:
		void UpdatePos ();
	};
}
}
