File indexing completed on 2022-12-06 19:25:46

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2007 by Albert Astals Cid <aacid@kde.org>          *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  ***************************************************************************/
0009 
0010 #include "lateralwidget.h"
0011 
0012 #include <QLabel>
0013 #include <QPushButton>
0014 #include <QStackedWidget>
0015 #include <QVBoxLayout>
0016 
0017 #include <QAction>
0018 #include <QIcon>
0019 #include <KStandardGameAction>
0020 #include <KLocalizedString>
0021 
0022 #include "diceswidget.h"
0023 
0024 lateralWidget::lateralWidget(QWidget *parent) : QWidget(parent), m_demoMode(false)
0025 {
0026     QVBoxLayout *lay = new QVBoxLayout(this);
0027     m_rolls = new QLabel(this);
0028     m_dices = new dicesWidget(this);
0029     m_buttons = new QStackedWidget(this);
0030     m_rollButton = new QPushButton(QIcon::fromTheme( QStringLiteral( "roll") ), i18n("&Roll" ), this);
0031     
0032     QAction *dummyAction = KStandardGameAction::gameNew(nullptr, nullptr, nullptr);
0033     m_newGameButton = new QPushButton(dummyAction->icon(), dummyAction->text(), this);
0034     delete dummyAction;
0035 
0036     m_buttons -> addWidget(m_rollButton);
0037     m_buttons -> addWidget(m_newGameButton);
0038 
0039     disableDemoMode();
0040 
0041     lay -> addWidget(m_rolls, 0, Qt::AlignHCenter);
0042     lay -> addWidget(m_dices);
0043     lay -> addWidget(m_buttons);
0044     lay -> addStretch(1);
0045 
0046     connect(m_rollButton, &QPushButton::clicked, this, &lateralWidget::roll);
0047     connect(m_rollButton, &QPushButton::clicked, this, &lateralWidget::unhighlightAllDice);
0048     connect(m_newGameButton, &QPushButton::clicked, this, &lateralWidget::newGame);
0049     
0050     nextTurn();
0051 }
0052 
0053 void lateralWidget::nextTurn()
0054 {
0055     m_roll = 1;
0056     updateRollLabel();
0057     m_dices -> rollAll();
0058     if (!m_demoMode) m_buttons -> setCurrentWidget(m_rollButton);
0059 }
0060 
0061 void lateralWidget::setEnabled(bool enabled)
0062 {
0063     m_rollButton -> setEnabled(enabled);
0064     //newGameButton enabledness is controlled internally
0065     //m_newGameButton -> setEnabled(enabled);
0066     m_dices -> setEnabled(enabled);
0067 }
0068 
0069 void lateralWidget::enableDemoMode()
0070 {
0071     m_buttons -> setCurrentWidget(m_newGameButton);
0072     m_newGameButton->setEnabled(true);
0073     m_demoMode = true;
0074 }
0075 
0076 void lateralWidget::disableDemoMode()
0077 {
0078     m_buttons -> setCurrentWidget(m_rollButton);
0079     m_newGameButton -> setEnabled(false);
0080     m_demoMode = false;
0081 }
0082 
0083 void lateralWidget::endGame()
0084 {
0085     m_newGameButton -> setEnabled(true);
0086     m_buttons -> setCurrentWidget(m_newGameButton);
0087 }
0088 
0089 int lateralWidget::getDice(int dice) const
0090 {
0091     return m_dices -> getDice(dice);
0092 }
0093 
0094 void lateralWidget::selectDice(int dice, bool selected) 
0095 {
0096     m_dices -> selectDice(dice, selected);
0097 }
0098 
0099 void lateralWidget::highlightDice(int dice, bool selected)
0100 {
0101     m_dices -> highlightDice(dice, selected);
0102 }
0103 
0104 void lateralWidget::unhighlightAllDice()
0105 {
0106     for (int i = 0; i < 5; ++i) m_dices -> highlightDice(i, false);
0107 }
0108 
0109 int lateralWidget::getRolls() const
0110 {
0111     return m_roll;
0112 }
0113 
0114 int lateralWidget::getOnes() const
0115 {
0116     return m_dices -> getOnes();
0117 }
0118 
0119 int lateralWidget::getTwos() const
0120 {
0121     return m_dices -> getTwos();
0122 }
0123 
0124 int lateralWidget::getThrees() const
0125 {
0126     return m_dices -> getThrees();
0127 }
0128 
0129 int lateralWidget::getFours() const
0130 {
0131     return m_dices -> getFours();
0132 }
0133 
0134 int lateralWidget::getFives() const
0135 {
0136     return m_dices -> getFives();
0137 }
0138 
0139 int lateralWidget::getSixs() const
0140 {
0141     return m_dices -> getSixs();
0142 }
0143 
0144 int lateralWidget::getThreeOfAKind() const
0145 {
0146     return m_dices -> getThreeOfAKind();
0147 }
0148 
0149 int lateralWidget::getFourOfAKind() const
0150 {
0151     return m_dices -> getFourOfAKind();
0152 }
0153 
0154 int lateralWidget::getFullHouse() const
0155 {
0156     return m_dices -> getFullHouse();
0157 }
0158 
0159 int lateralWidget::getSStraight() const
0160 {
0161     return m_dices -> getSStraight();
0162 }
0163 
0164 int lateralWidget::getLStraight() const
0165 {
0166     return m_dices -> getLStraight();
0167 }
0168 
0169 int lateralWidget::getKiriki() const
0170 {
0171     return m_dices -> getKiriki();
0172 }
0173 
0174 int lateralWidget::totalSum() const
0175 {
0176     return m_dices -> totalSum();
0177 }
0178 
0179 
0180 void lateralWidget::roll()
0181 {
0182     if (m_dices -> roll())
0183     {
0184         m_roll++;
0185         updateRollLabel();
0186         Q_EMIT rolled();
0187     }
0188 }
0189 
0190 void lateralWidget::newGame()
0191 {
0192     disableDemoMode();
0193     Q_EMIT newGameClicked();
0194 }
0195 
0196 void lateralWidget::updateRollLabel()
0197 {
0198     m_rolls -> setText(i18n("Roll %1 of 3",m_roll));
0199     if (!m_demoMode) setEnabled(m_roll != 3);
0200 }
0201 
0202