
//  Copyright (C) 2009 by Arnaud Dupuis
//  a.dupuis@infinityperl.org
//  http://www.infinityperl.org
// 
//  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 3 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.,
//  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

#ifndef ASSUMA_SEARCHWIDGET
#define ASSUMA_SEARCHWIDGET

#include <QWidget>
#include <QMap>
#include <QTreeWidget>
#include "ui_assuma_searchwidget.h"

class QString;
class QPixmap;
class QMenu;

//!  A search class.
/*!
  AssumaSearchWidget is a widget which can be used to search for users in a QTreeWidget.
  It can hide dynamically all items which don't match the query and can present checkable options to extend research easily.
*/

class AssumaSearchWidget : public QWidget, public Ui::AssumaSearchWidget
{
	Q_OBJECT
	/*!
	A property to allow developpers to change the top right corner image in the Qt Designer.
	*/
	Q_PROPERTY(QPixmap topRightCornerPixmap READ cornerPixmap WRITE setCornerPixmap DESIGNABLE true USER true)
	public:
                //! Constructor
                /*!
                  Construct a new AssumaSearchWidget object.
                  \param parent the parent widget
                  \param tw a tree widget to search in
                */
		AssumaSearchWidget(QWidget *parent=0, QTreeWidget *tw=0);
                /*!
                  Returns the pixmap in the right corner as a QPixmap.
                  \return A QPixmap.
                */
		QPixmap cornerPixmap();
                /*!
                  Returns the QTreeWidget this widget is searching in.
                  \return A QTreeWidget.
                */
		QTreeWidget * treeWidget();
                /*!
                  Returns the size hint for the whole widget.
                  \return A const QSize.
                */
		QSize sizeHint () const;
	public slots:
                /*!
                    Set the right corner pixmap.
                    \param pix the pixmap to put in the right up corner.
                */
		void setCornerPixmap(const QPixmap&);
                /*!
                    Start to search the QTreeWidget for items matching the query.
                    \param query the filter that the items have to match.
                */
		void performSearch(const QString &);
                /*!
                    Set the map of columns. This QMap is build with QString as keys (columns name) and int as values (the column number)
                    \warning This way to do thing is absolutly dirty and inefficient, it will change in the 3.1 version
                    \param map the columns map.
                */
		void setColumnMap( const QMap<QString, int> );
                /*!
                    Set the QTreeWidget to search in.
                    \param tw the QTreeWidget.
                */
		void setTreeWidget(QTreeWidget *);
		/*!
		Load a theme for the UI.
		\param themeName the name of the theme to load. This is the name of a directory in the theme/ directory.
		*/
		void loadTheme( const QString & themeName = "default" );
		/*!
		Update (or create) the menu to select fields to search into depending on the QTreeWidget.
		*/
		void updateFieldSearchMenu();
	private slots:
		void on_liveSearchBox_stateChanged( int );
		void on_searchQuery_textChanged(const QString &);
		void on_caseSensitiveBox_stateChanged ( int );
		void on_clearSearchButton_clicked(bool);
		void on_doSearchButton_clicked(bool);
		void populateMenu( QMenu * );
                void updateSearch();
		void on_hideFieldsCheckBox_stateChanged ( int );
	private:
		int elementColumn(const QString & t);
		QTreeWidget *_treeWidget;
		QMap<QString, int> columnMap;
};

#endif
