[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdegames/ksudoku/src/gui
From:       Andreas Pakulat <apaku () gmx ! de>
Date:       2007-11-01 0:39:04
Message-ID: 1193877544.304423.17924.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 731565 by apaku:

Hardcoded sizeHint is not a good idea. Calculate the proper size using the icon, \
title and description.

This moves the private variables from paint() into the class so they can be shared \
between paint() and sizeHint() to reduce duplication.

BUG:151304
CCMAIL:kde-games-devel@kde.org

 M  +57 -36    gamevariants.cpp  
 M  +25 -19    gamevariants.h  


--- trunk/KDE/kdegames/ksudoku/src/gui/gamevariants.cpp #731564:731565
@@ -32,6 +32,7 @@
 
 #include "puzzle.h"
 
+
 namespace ksudoku {
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -94,7 +95,7 @@
 
 	if (!index.internalPointer())
 		return QVariant();
-	
+
 	GameVariant* gameVariant = static_cast<GameVariant*>(index.internalPointer());
 
 	switch(role) {
@@ -105,7 +106,7 @@
 		case GameVariantDelegate::Description:
 			return gameVariant->description();
 	}
-	
+
 	return QVariant();
 }
 
@@ -125,15 +126,35 @@
 QSize GameVariantDelegate::sizeHint(const QStyleOptionViewItem& option, const \
QModelIndex& index) const {  Q_UNUSED(option);
 	Q_UNUSED(index);
-	return QSize(64, 64);
+	QSize size(m_leftMargin+m_iconWidth+m_rightMargin+m_separatorPixels*2, \
m_iconHeight+m_separatorPixels*2); +
+	QRect contentRect = option.rect.adjusted(m_leftMargin, m_separatorPixels, \
-m_rightMargin, -m_separatorPixels); +	contentRect.adjust(m_iconWidth + \
m_separatorPixels*2, 0, 0, 0); +
+	QFont titleFont(option.font);
+	titleFont.setPointSize(titleFont.pointSize() + 2);
+	titleFont.setWeight(QFont::Bold);
+
+	QString titleStr = option.fontMetrics.elidedText(title(index), Qt::ElideRight, \
contentRect.width()); +	QSize titleSize = option.fontMetrics.size( \
Qt::TextSingleLine, titleStr ); +	contentRect.adjust(0, m_separatorPixels + \
option.fontMetrics.height(), 0, 0); +
+	// Show Description
+	QString descrStr = option.fontMetrics.elidedText(description(index), \
Qt::ElideRight, contentRect.width()); +	QSize descrSize = option.fontMetrics.size( \
Qt::TextSingleLine, descrStr ); +
+	if( descrSize.width() > titleSize.width() )
+		size.setWidth(size.width() + descrSize.width());
+	else
+		size.setWidth(size.width() + titleSize.width());
+
+	if( titleSize.height()+descrSize.height()+m_separatorPixels*2 > m_iconWidth )
+		size.setHeight( titleSize.height()+option.fontMetrics.height()+descrSize.height()+m_separatorPixels*4 \
); +	return size;
 }
 
 void GameVariantDelegate::paint(QPainter* painter, const QStyleOptionViewItem& \
                option, const QModelIndex& index) const {
-	int leftMargin = 16;
-	int rightMargin = 12;
-	int separatorPixels = 8;
-	int iconWidth = 48, iconHeight = 48;
-	
+
 	// show background
 	if (option.state & QStyle::State_Selected) {
 		painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
@@ -142,34 +163,34 @@
 	} else {
 		painter->fillRect(option.rect, option.palette.color(QPalette::Base));
 	}
-	
-	QRect contentRect = option.rect.adjusted(leftMargin, separatorPixels, -rightMargin, \
                -separatorPixels);
-	
+
+	QRect contentRect = option.rect.adjusted(m_leftMargin, m_separatorPixels, \
-m_rightMargin, -m_separatorPixels); +
 	// Show icon
-	
-	QPixmap iconPixmap = KIcon(icon(index), KIconLoader::global()).pixmap(iconWidth, \
iconHeight); +
+	QPixmap iconPixmap = KIcon(icon(index), KIconLoader::global()).pixmap(m_iconWidth, \
m_iconHeight);  painter->drawPixmap(contentRect.left(), (contentRect.height() - \
                iconPixmap.height()) / 2 + contentRect.top(), iconPixmap);
-	contentRect.adjust(iconPixmap.width() + separatorPixels*2, 0, 0, 0);
-	
+	contentRect.adjust(iconPixmap.width() + m_separatorPixels*2, 0, 0, 0);
+
 // 	// Show configuration icon
 // 	if(configurable(index)) {
 // 		QPixmap configPixmap = KIcon("configure", KIconLoader::global()).pixmap(32, \
32);  // 		painter->drawPixmap(contentRect.right() - configPixmap.width(), \
(contentRect.height() - configPixmap.height()) / 2 + contentRect.top(), \
configPixmap);  // 		contentRect.adjust(0, 0, -(configPixmap.width() + \
separatorPixels), 0);  // 	}
-	
+
 	// Show Title
 	QFont titleFont(painter->font());
 	titleFont.setPointSize(titleFont.pointSize() + 2);
 	titleFont.setWeight(QFont::Bold);
-	
+
 	QFont previousFont(painter->font());
 	painter->setFont(titleFont);
 	QString titleStr = painter->fontMetrics().elidedText(title(index), Qt::ElideRight, \
contentRect.width());  painter->drawText(contentRect, titleStr);
-	contentRect.adjust(0, separatorPixels + painter->fontMetrics().height(), 0, 0);
+	contentRect.adjust(0, m_separatorPixels + painter->fontMetrics().height(), 0, 0);
 	painter->setFont(previousFont);
-	
+
 	// Show Description
 	QString descrStr = painter->fontMetrics().elidedText(description(index), \
Qt::ElideRight, contentRect.width());  painter->drawText(contentRect, descrStr);
@@ -190,7 +211,7 @@
 bool GameVariantDelegate::configurable(const QModelIndex& index) const {
 	const GameVariantCollection* collection = dynamic_cast<const \
GameVariantCollection*>(index.model());  if(!collection) return false;
-	
+
 	return collection->variant(index)->canConfigure();
 }
 
@@ -198,9 +219,9 @@
 	if(event->type() == QEvent::MouseButtonPress) {
 		return true;
 	}
-	
+
 	// TODO insert code for handling clicks on buttons in items.
-	
+
 	return QItemDelegate::eventFilter(watched, event);
 }
 
@@ -233,10 +254,10 @@
 		m_solver = new SKSolver(m_order, false);
 		m_solver->init();
 	}
-	
+
 	Puzzle* puzzle = new Puzzle(m_solver, false);
 	puzzle->init();
-	
+
 	return Game(puzzle);
 }
 
@@ -245,10 +266,10 @@
 		m_solver = new SKSolver(m_order, false);
 		m_solver->init();
 	}
-	
+
 	Puzzle* puzzle = new Puzzle(m_solver, true);
 	puzzle->init(difficulty, m_symmetry);
-	
+
 	return Game(puzzle);
 }
 
@@ -286,10 +307,10 @@
 		m_solver = new SKSolver(m_order, true);
 		m_solver->init();
 	}
-	
+
 	Puzzle* puzzle = new Puzzle(m_solver, false);
 	puzzle->init();
-	
+
 	return Game(puzzle);
 }
 
@@ -298,10 +319,10 @@
 		m_solver = new SKSolver(m_order, true);
 		m_solver->init();
 	}
-	
+
 	Puzzle* puzzle = new Puzzle(m_solver, true);
 	puzzle->init(difficulty, m_symmetry);
-	
+
 	return Game(puzzle);
 }
 
@@ -334,26 +355,26 @@
 Game CustomGame::startEmpty() const {
 	if(!m_solver) {
 		m_solver = ksudoku::Serializer::loadCustomShape(m_url, 0, 0);
-		
+
 		if(!m_solver) return Game();
 	}
-	
+
 	Puzzle* puzzle = new Puzzle(m_solver, false);
 	puzzle->init();
-	
+
 	return Game(puzzle);
 }
 
 Game CustomGame::createGame(int difficulty) const {
 	if(!m_solver) {
 		m_solver = ksudoku::Serializer::loadCustomShape(m_url, 0, 0);
-		
+
 		if(!m_solver) return Game();
 	}
-	
+
 	Puzzle* puzzle = new Puzzle(m_solver, true);
 	puzzle->init(difficulty, 1);
-	
+
 	return Game(puzzle);
 }
 
--- trunk/KDE/kdegames/ksudoku/src/gui/gamevariants.h #731564:731565
@@ -43,26 +43,26 @@
 	void setDescription(const QString& descr);
 	QString icon() const { return m_icon; }
 	void setIcon(const QString& icon);
-	
+
 	/// This method returs whether the variant has an configure option
 	virtual bool canConfigure() const = 0;
-	
+
 	/// Shows a configure dialog and changes the settings
 	virtual bool configure() = 0;
-	
+
 	/// Whether this variant can be started without any values in the grid
 	virtual bool canStartEmpty() const = 0;
-	
+
 	/// Creates a game without a puzzle but with an empty grid
 	virtual Game startEmpty() const = 0;
-	
+
 	/// Creates a instance of this game variant
 	virtual Game createGame(int difficulty) const = 0;
-	
+
 	/// Creates the correct view for the game.
 	/// Game needs to be compatible with this GameVariant
 	virtual KsView* createView(const Game& game) const = 0;
-	
+
 private:
 	QString m_name;
 	QString m_description;
@@ -75,10 +75,10 @@
 public:
 	GameVariantCollection(QObject* parent, bool autoDel);
 	~GameVariantCollection();
-		
+
 public:
 	void addVariant(GameVariant* variant);
-	
+
 public:
 	QVariant data(const QModelIndex &index, int role) const;
 	int rowCount(const QModelIndex&) const;
@@ -87,7 +87,7 @@
 
 signals:
 	void newVariant(GameVariant* variant);
-	
+
 public:
 	QList<GameVariant*> m_variants;
 	bool m_autoDelete;
@@ -99,7 +99,7 @@
 	enum Roles {
 		Description = 33
 	};
-	
+
 public:
 	GameVariantDelegate(QObject* parent = 0);
 public:
@@ -114,12 +114,18 @@
 	QString description(const QModelIndex& index) const;
 	QString icon(const QModelIndex& index) const;
 	bool configurable(const QModelIndex& index) const;
+private:
+	static const int m_iconWidth = 48;
+	static const int m_iconHeight = 48;
+	static const int m_leftMargin = 16;
+	static const int m_rightMargin = 12;
+	static const int m_separatorPixels = 8;
 };
 
 class SudokuGame : public GameVariant {
 public:
 	SudokuGame(const QString& name, uint order, GameVariantCollection* collection=0);
-	
+
 public:
 	bool canConfigure() const;
 	bool configure();
@@ -127,18 +133,18 @@
 	Game startEmpty() const;
 	Game createGame(int difficulty) const;
 	KsView* createView(const Game& game) const;
-	
+
 private:
 	uint m_order;
 	uint m_symmetry;
-	
+
 	mutable SKSolver* m_solver;
 };
 
 class RoxdokuGame : public GameVariant {
 public:
 	RoxdokuGame(const QString& name, uint order, GameVariantCollection* collection=0);
-	
+
 public:
 	bool canConfigure() const;
 	bool configure();
@@ -146,18 +152,18 @@
 	Game startEmpty() const;
 	Game createGame(int difficulty) const;
 	KsView* createView(const Game& game) const;
-	
+
 private:
 	uint m_order;
 	uint m_symmetry;
-	
+
 	mutable SKSolver* m_solver;
 };
 
 class CustomGame : public GameVariant {
 public:
 	CustomGame(const QString& name, const KUrl& url, GameVariantCollection* \
                collection=0);
-	
+
 public:
 	bool canConfigure() const;
 	bool configure();
@@ -165,7 +171,7 @@
 	Game startEmpty() const;
 	Game createGame(int difficulty) const;
 	KsView* createView(const Game& game) const;
-	
+
 private:
 	KUrl m_url;
 	mutable SKSolver* m_solver;


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic