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

List:       kde-commits
Subject:    KDE/kdegames/kiriki/src
From:       Parker Coates <parker.coates () kdemail ! net>
Date:       2010-09-17 4:25:51
Message-ID: 20100917042551.C5BB5AC888 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1176230 by coates:

Add a Hint action to Kiriki.

When triggered the action either highlights the dice to be rolled or
highlights the recommended row in the table. Suggestions come from the
existing Kiriki AI.

Patch by Luiz Romário Santana Rios.
See http://reviewboard.kde.org/r/3793/ for more details.

 M  +20 -0     diceswidget.cpp  
 M  +2 -0      diceswidget.h  
 M  +73 -3     kiriki.cpp  
 M  +7 -0      kiriki.h  
 M  +12 -0     lateralwidget.cpp  
 M  +3 -0      lateralwidget.h  


--- trunk/KDE/kdegames/kiriki/src/diceswidget.cpp #1176229:1176230
@@ -14,12 +14,18 @@
 #include <QPainter>
 #include <QPixmap>
 #include <QMouseEvent>
+#include <QStyleOptionViewItemV4>
 
 #include <krandom.h>
 #include <kstandarddirs.h>
 
 dicesWidget::dicesWidget(QWidget *parent) : QWidget(parent)
 {
+	m_highlightDice[0] =
+	m_highlightDice[1] =
+	m_highlightDice[2] =
+	m_highlightDice[3] =
+	m_highlightDice[4] = 0;
 	setMinimumSize(90, 450);
 	
 	m_images[0] = QPixmap(KStandardDirs::locate("appdata", "images/dice-none.png"));
@@ -63,6 +69,11 @@
 	m_rollDice[dice] = select;
 }
 
+void dicesWidget::highlightDice(int dice, bool highlight)
+{
+	m_highlightDice[dice] = highlight;
+}
+
 int dicesWidget::getOnes() const
 {
 	int n = getSimilar(1);
@@ -168,6 +179,15 @@
 		QPixmap pixmap(m_rollDice[i] ? m_images[0] : m_images[m_dice[i]]);
 		// TODO need suggestions
 		// if (!m_enabled) pixmap = KPixmapEffect::toGray(pixmap, false);
+		if (m_highlightDice[i])
+		{
+			QStyleOptionViewItemV4 option;
+			option.initFrom(this);
+			option.rect = QRect(5, 9 + 90 * i, 80, 80);
+			option.state |= QStyle::State_Selected;
+			option.showDecorationSelected = true;
+			style()->drawControl(QStyle::CE_ItemViewItem, &option, &p);
+		}
 		p.drawPixmap(5, 10 + (10 + 80) * i, pixmap);
 	}
 }
--- trunk/KDE/kdegames/kiriki/src/diceswidget.h #1176229:1176230
@@ -25,6 +25,7 @@
 
 		int getDice(int dice) const;
 		void selectDice(int dice, bool select);
+		void highlightDice(int dice, bool highlight);
 		
 		int getOnes() const;
 		int getTwos() const;
@@ -51,6 +52,7 @@
 		
 		bool m_enabled;
 		bool m_rollDice[5];
+		bool m_highlightDice[5];
 		int m_dice[5];
 		QPixmap m_images[7];
 };
--- trunk/KDE/kdegames/kiriki/src/kiriki.cpp #1176229:1176230
@@ -14,14 +14,17 @@
 #include <QHBoxLayout>
 #include <QHeaderView>
 #include <QItemDelegate>
+#include <QItemSelectionModel>
 #include <QPainter>
 #include <QPrintDialog>
 #include <QPrinter>
+#include <QStyledItemDelegate>
 #include <QTimer>
 #include <QTreeView>
 
 #include <kapplication.h>
 #include <kconfigdialog.h>
+#include <klocalizedstring.h>
 #include <kmessagebox.h>
 #include <kscoredialog.h>
 #include <kstandardaction.h>
@@ -39,7 +42,7 @@
 #include "scores.h"
 #include "settings.h"
 
-kiriki::kiriki() : KXmlGuiWindow()
+kiriki::kiriki() : KXmlGuiWindow(), m_hintGiven(false)
 {
 	QWidget *w = new QWidget(this);
 	QHBoxLayout *lay = new QHBoxLayout(w);
@@ -48,6 +51,9 @@
 	lay -> addWidget(m_lateral);
 	
 	m_scoresWidget = new QTreeView(w);
+
+	m_delegateHighlighted = new QStyledItemDelegate(m_scoresWidget);
+
 	m_scoresWidget -> setItemDelegate(new QItemDelegate(m_scoresWidget));
 	m_scoresWidget -> setSelectionBehavior(QAbstractItemView::SelectRows);
 	m_scoresWidget -> setRootIsDecorated(false);
@@ -74,12 +80,17 @@
 	KStandardGameAction::highscores(this, SLOT(showHighScores()), actionCollection());
 	KStandardGameAction::print(this, SLOT(print()), actionCollection());
 	KStandardGameAction::quit(kapp, SLOT(quit()), actionCollection());
+	m_hintAction = KStandardGameAction::hint(this, SLOT(showHint()), \
actionCollection());  m_demoAction = KStandardGameAction::demo(this, SLOT(demo()), \
actionCollection());  connect(gameNewAction, SIGNAL(triggered(bool)), m_demoAction, \
SLOT(setChecked(bool)));  connect(gameNewAction, SIGNAL(triggered(bool)), \
m_demoAction, SLOT(setDisabled(bool))); +	connect(gameNewAction, \
SIGNAL(triggered(bool)), m_hintAction, SLOT(setDisabled(bool)));  \
connect(gameNewAction, SIGNAL(triggered(bool)), m_lateral, SLOT(setDemoMode(bool))); \
+	connect(gameNewAction, SIGNAL(triggered(bool)), m_lateral, \
SLOT(unhighlightAllDice()));  connect(this, SIGNAL(demoStarted(bool)), m_demoAction, \
SLOT(setDisabled(bool)));  connect(this, SIGNAL(demoStarted(bool)), m_demoAction, \
SLOT(setChecked(bool))); +	connect(this, SIGNAL(demoStarted(bool)), m_hintAction, \
SLOT(setDisabled(bool))); +	connect(this, SIGNAL(demoStarted(bool)), m_lateral, \
SLOT(unhighlightAllDice()));  connect(m_lateral, SIGNAL(newGameClicked()), \
gameNewAction, SLOT(trigger()));  
 	// Preferences
@@ -98,6 +109,8 @@
 	if (!m_scores -> currentPlayer().isHuman()) return;
 
 	if (index.column() == 0 || index.column() == m_scores -> currentPlayerNumber() + 1) \
play(index); +
+	m_scoresWidget -> setItemDelegateForRow(m_highlightedRowIndex, 0);
 }
 
 void kiriki::play(const QModelIndex &index)
@@ -140,6 +153,7 @@
 	m_scoresWidget -> resizeColumnToContents(0);
 	statusBar()->hide();
 	if (m_demoAction -> isChecked()) playComputer();
+	connect(m_lateral, SIGNAL(rolled()), statusBar(), SLOT(hide()));
 }
 
 void kiriki::demo()
@@ -175,6 +189,7 @@
 	kirikiSettings::setNumberOfPlayers(6);
 	newGame();
 	emit demoStarted();
+	disconnect(m_lateral, SIGNAL(rolled()), statusBar(), SLOT(hide()));
 	kirikiSettings::setPlayer1IsHuman(preDemoHumans[0]);
 	kirikiSettings::setPlayer2IsHuman(preDemoHumans[1]);
 	kirikiSettings::setPlayer3IsHuman(preDemoHumans[2]);
@@ -187,19 +202,66 @@
 	m_lateral->enableDemoMode();
 }
 
+void kiriki::showHint()
+{
+	if (!m_hintGiven && KMessageBox::Cancel == KMessageBox::warningContinueCancel(
+			this,
+			i18n("Asking for a hint will disqualify the current game from entering the high \
score list."), +			i18n("Confirm Hint Request"),
+			KGuiItem(i18n("Give Hint Anyway"), "arrow-right")
+			)
+		) return;
+	m_hintGiven = true;
+
+	for (int i = 0; i < 5; ++i) setComputerDiceValue(i, m_lateral -> getDice(i));
+	ComputerRolling(m_scores -> currentPlayer(), m_lateral -> getRolls());
+	if (computerDiceSelected() && m_lateral -> getRolls() < 3)
+	{
+		int rollAmount = 0;
+		for (int i = 0; i < 5; ++i)
+		{
+			if (getComputerDiceSelected(i))
+			{
+				++rollAmount;
+				m_lateral -> highlightDice(i, true);
+			}
+		}
+		m_scoresWidget->selectionModel()->clearSelection();
+		statusBar()->showMessage(i18np("Roll highlighted die.", "Roll highlighted dice.", \
rollAmount)); +		statusBar()->show();
+	}
+	else
+	{
+		static const int scoreRows[13] = { 1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15, 16 };
+		QModelIndex mi;
+		mi = m_scores -> index(scoreRows[ComputerScoring(m_scores -> currentPlayer())], \
0); +		m_highlightedRowIndex  = mi.row();
+		m_scoresWidget -> setItemDelegateForRow(m_highlightedRowIndex, \
m_delegateHighlighted); +		QItemSelectionModel::SelectionFlags fl = \
QItemSelectionModel::Clear | +		                                         \
QItemSelectionModel::Rows  | +		                                         \
QItemSelectionModel::Select; +		m_scoresWidget -> selectionModel() -> select(mi, fl);
+	}
+}
+
 void kiriki::endGame()
 {
 	const player &p = m_scores -> winner();
 	m_lateral -> setEnabled(false);
 	m_lateral -> endGame();
+	m_hintAction -> setEnabled(false);
 	if (p.isHuman())
 	{
 		KScoreDialog sc(KScoreDialog::Name | KScoreDialog::Score, this);
+		if (m_hintGiven) m_hintGiven = false;
+		else
+		{
 		if (sc.addScore(p.grandTotal()))
 		{
 			sc.exec();
 		}
 	}
+	}
 	if (m_demoAction -> isChecked()) QTimer::singleShot(3000, this, SLOT(demo()));
 }
 
@@ -275,10 +337,18 @@
 		while (m_scores -> currentPlayer().allScores()) m_scores -> nextPlayer();
 		
 		m_lateral -> nextTurn();
-		if (!m_scores -> currentPlayer().isHuman()) \
                QTimer::singleShot(kirikiSettings::waitTime(), this, \
                SLOT(playComputer()));
-		else m_lateral -> setEnabled(true);
+		if (!m_scores -> currentPlayer().isHuman())
+		{
+			m_hintAction -> setEnabled(false);
+			QTimer::singleShot(kirikiSettings::waitTime(), this, SLOT(playComputer()));
 	 }
+		else
+		{
+			m_hintAction -> setEnabled(true);
+			m_lateral -> setEnabled(true);
 }
+	 }
+}
 
 void kiriki::playComputer()
 {
--- trunk/KDE/kdegames/kiriki/src/kiriki.h #1176229:1176230
@@ -13,8 +13,10 @@
 #include <kxmlguiwindow.h>
 
 class QModelIndex;
+class QStyledItemDelegate;
 class QTreeView;
 
+class KAction;
 class KToggleAction;
 
 class lateralWidget;
@@ -37,6 +39,7 @@
 		void showPreferences();
 		void print();
 		void playComputer();
+		void showHint();
 	
 	private:
 		void endGame();
@@ -48,6 +51,10 @@
 		scores *m_scores;
 		lateralWidget *m_lateral;
 		KToggleAction *m_demoAction;
+		QStyledItemDelegate *m_delegateHighlighted;
+		int m_highlightedRowIndex;
+		bool m_hintGiven;
+		KAction* m_hintAction;
 };
 
 #endif
--- trunk/KDE/kdegames/kiriki/src/lateralwidget.cpp #1176229:1176230
@@ -48,6 +48,7 @@
 	lay -> addStretch(1);
 
 	connect(m_rollButton, SIGNAL(clicked(bool)), this, SLOT(roll()));
+	connect(m_rollButton, SIGNAL(clicked(bool)), this, SLOT(unhighlightAllDice()));
 	connect(m_newGameButton, SIGNAL(clicked(bool)), this, SLOT(newGame()));
 	
 	nextTurn();
@@ -97,6 +98,16 @@
 	m_dices -> selectDice(dice, selected);
 }
 
+void lateralWidget::highlightDice(int dice, bool selected)
+{
+	m_dices -> highlightDice(dice, selected);
+}
+
+void lateralWidget::unhighlightAllDice()
+{
+	for (int i = 0; i < 5; ++i) m_dices -> highlightDice(i, false);
+}
+
 int lateralWidget::getRolls() const
 {
 	return m_roll;
@@ -174,6 +185,7 @@
 	{
 		m_roll++;
 		updateRollLabel();
+		emit rolled();
 	}
 }
 
--- trunk/KDE/kdegames/kiriki/src/lateralwidget.h #1176229:1176230
@@ -31,6 +31,7 @@
 
 		int getDice(int dice) const;
 		void selectDice(int dice, bool select);
+		void highlightDice(int dice, bool highlight);
 		int getRolls() const;
 		
 		int getOnes() const;
@@ -49,12 +50,14 @@
 		
 	signals:
 		void newGameClicked();
+		void rolled();
 
 	public slots:
 		void roll();
 		void newGame();
 		void enableDemoMode();
 		void setDemoMode(bool demoMode);
+		void unhighlightAllDice();
 	
 	private:
 		void updateRollLabel();


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

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