/****************************************************************************
*                                                                           *
*   WideIconsMenu for Qt3                                                   *
*   Copyright (C) 2009  Piotr Dąbrowski ultr@ultr.pl                        *
*                                                                           *
*   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, see <http://www.gnu.org/licenses/>.   *
*                                                                           *
****************************************************************************/


#define private public
#include <qmenudata.h>
#include <qstyle.h>
#undef private
#include <qbitmap.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qrect.h>
#include <qwidget.h>
#include <math.h>
#include "wideiconsmenu.h"


int WideIconsMenu::pixelMetric( PixelMetric metric, const QWidget *widget ) const
{
	if( metric == QStyle::PM_PopupMenuScrollerHeight )
	{
		// return icon size
		return ICONHEIGHT;
	}
	// orginal pixelMetric
	return ProxyStyle::pixelMetric( metric, widget );
}


void WideIconsMenu::drawControl( ControlElement element, QPainter *p, const QWidget *widget, const QRect &r, const QColorGroup &cg, SFlags how, const QStyleOption &opt ) const
{
	if( element == QStyle::CE_PopupMenuItem )
	{
		if( opt.menuItem()->iconSet() != NULL )
		{
			// save orginal icon
			QPixmap icon( opt.menuItem()->iconSet()->pixmap() );
			// clear orginal icon
			QPixmap emptypixmap( ICONHEIGHT, ICONHEIGHT );
			emptypixmap.fill( Qt::white );
			QBitmap emptymask( ICONHEIGHT, ICONHEIGHT );
			emptymask.fill( Qt::color0 );
			emptypixmap.setMask( emptymask );
			opt.menuItem()->iconset_data = new QIconSet( emptypixmap );
			// move the item's text
			QString movetext = "";
			while( p->fontMetrics().width( movetext ) < ( iconswidth - ICONHEIGHT - ( ( iconswidth>opt.maxIconWidth() ? opt.maxIconWidth() : iconswidth ) - ICONHEIGHT ) / 2 ) )
			{
				movetext += " ";
			}
			opt.menuItem()->text_data = movetext + opt.menuItem()->text_data.stripWhiteSpace();
			// draw the control with no icon, so Style will not scale it down
			ProxyStyle::drawControl( element, p, widget, r, cg, how, opt );
			//
			opt.menuItem()->iconset_data = new QIconSet( icon );
			// draw orginal icon in its full size
			QRect rect(
				r.x() + LEFTMARGIN,
				r.y() + round( ( r.height() - ICONHEIGHT ) / 2.0 ),
				iconswidth,
				ICONHEIGHT
				);
			p->drawPixmap( rect, icon );
			// done
			return;
		}
	}
	// otherwise do the orginal drawing
	ProxyStyle::drawControl( element, p, widget, r, cg, how, opt );
}
