Commit a5c323c1 authored by Benjamin Meyer's avatar Benjamin Meyer
Browse files

Moved some of the settings from a Menu to a real settings dialog

managed by KAutoConfig
Window size and toolbars are now managed by KMainWindow
General code cleanup and improvments.

svn path=/trunk/kdegames/kjumpingcube/; revision=210258
parent ae94bb25
Matthias Kiefer <matthias.kiefer@gmx.de>
Benjamin Meyer <ben@meyerhome.net>
chagnes 1.1:
- Added Settings dialog
- finetuned the code
- removed a lot of unessessary code with newer qt3 code.
changes 0.7.3:
- finetuning and some bugfixes
......
......@@ -8,7 +8,7 @@ KDE_ICON = kjumpingcube
bin_PROGRAMS = kjumpingcube
kjumpingcube_SOURCES = kjumpingcube.cpp kcubeboxwidget.cpp main.cpp \
kcubewidget.cpp cube.cpp \
brain.cpp cubebox.cpp
brain.cpp cubebox.cpp settings.ui
xdg_apps_DATA = kjumpingcube.desktop
......
......@@ -29,6 +29,7 @@
#include <kapplication.h>
#undef DEBUG // uncomment this to get useful messages
#include <assert.h>
#ifdef DEBUG
#include <iostream.h>
......
......@@ -27,7 +27,6 @@
class Cube;
class KCubeBoxWidget;
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
......@@ -46,12 +45,10 @@ public:
CubeBox(const CubeBox&);
CubeBox(KCubeBoxWidget&);
virtual ~CubeBox();
CubeBox& operator= (const CubeBox& box);
CubeBox& operator= (KCubeBoxWidget& box);
bool simulateMove(Player fromWhom,int row, int column);
double assessField(Player forWhom) const;
bool playerWon(Player who) const;
......@@ -62,3 +59,4 @@ private:
};
#endif // CUBEBOX_H
......@@ -22,7 +22,9 @@
#ifndef CUBEBOXBASE_H
#define CUBEBOXBASE_H
#ifdef DEBUG
#include <assert.h>
#endif
template<class T>
class CubeBoxBase
......@@ -241,3 +243,4 @@ void CubeBoxBase<T>::increaseNeighbours(int forWhom,int row,int column)
*/
#endif // CUBEBOXBASE_H
......@@ -23,6 +23,8 @@
#include "kcubewidget.h"
#include "cubebox.h"
#include <kapplication.h>
#include <kconfig.h>
#include <qpalette.h>
#include <qlayout.h>
#include <qtimer.h>
......@@ -79,14 +81,32 @@ KCubeBoxWidget::KCubeBoxWidget(const KCubeBoxWidget& box,QWidget *parent,const c
KCubeBoxWidget::~KCubeBoxWidget()
{
if(isActive())
stopActivities();
if(cubes)
deleteCubes();
if(undoBox)
delete undoBox;
}
void KCubeBoxWidget::readSettings(){
KConfig *config=kapp->config();
config->setGroup("Game");
QColor c1=QColor("darkred");
QColor c2=QColor("darkblue");
QPalette color1(config->readColorEntry("Color1",&c1));
QPalette color2(config->readColorEntry("Color2",&c2));
setColor(KCubeBoxWidget::One,color1);
setColor(KCubeBoxWidget::Two,color2);
setDim(config->readNumEntry("CubeDim",6));
brain.setSkill((Brain::Skill)config->readNumEntry("Skill",(int)Brain::Average));
bool flag=config->readBoolEntry("Computer_Pl_1",false);
setComputerplayer(KCubeBoxWidget::One,flag);
flag=config->readBoolEntry("Computer_Pl_2",true);
setComputerplayer(KCubeBoxWidget::Two,flag);
}
KCubeBoxWidget& KCubeBoxWidget::operator=(const KCubeBoxWidget& box)
{
......@@ -106,12 +126,9 @@ KCubeBoxWidget& KCubeBoxWidget::operator=(const KCubeBoxWidget& box)
currentPlayer=box.currentPlayer;
}
return *this;
}
KCubeBoxWidget& KCubeBoxWidget::operator=(CubeBox& box)
{
if(dim()!=box.dim())
......@@ -129,9 +146,6 @@ KCubeBoxWidget& KCubeBoxWidget::operator=(CubeBox& box)
return *this;
}
void KCubeBoxWidget::reset()
{
......@@ -152,7 +166,6 @@ void KCubeBoxWidget::reset()
checkComputerplayer(One);
}
void KCubeBoxWidget::undo()
{
if(isActive())
......@@ -215,12 +228,6 @@ void KCubeBoxWidget::setDim(int d)
}
}
void KCubeBoxWidget::setSkill(Brain::Skill skill)
{
brain.setSkill(skill);
}
void KCubeBoxWidget::setComputerplayer(Player player,bool flag)
{
if(player==One)
......@@ -247,8 +254,7 @@ void KCubeBoxWidget::stopActivities()
}
void KCubeBoxWidget::saveGame(KConfigBase* config)
void KCubeBoxWidget::saveProperties(KConfigBase* config)
{
if(isMoving())
{
......@@ -278,13 +284,15 @@ void KCubeBoxWidget::saveGame(KConfigBase* config)
list.clear();
}
config->writeEntry("CubeDim",dim());
}
void KCubeBoxWidget::restoreGame(KConfigBase* config)
void KCubeBoxWidget::readProperties(KConfigBase* config)
{
QStrList list;
list.setAutoDelete(true);
QString owner, value, key;
setDim(config->readNumEntry("CubeDim",5));
int cubeDim=dim();
for(int row=0; row < cubeDim ; row++)
......@@ -308,7 +316,6 @@ void KCubeBoxWidget::restoreGame(KConfigBase* config)
checkComputerplayer((Player)onTurn);
}
/* ***************************************************************** **
** slots **
** ***************************************************************** */
......@@ -323,7 +330,6 @@ void KCubeBoxWidget::setNormalCursor()
{
setCursor(KCursor::handCursor());
}
void KCubeBoxWidget::stopHint()
{
......@@ -337,7 +343,6 @@ void KCubeBoxWidget::stopHint()
}
bool KCubeBoxWidget::checkClick(int row,int column)
{
if(isActive())
......@@ -353,8 +358,6 @@ bool KCubeBoxWidget::checkClick(int row,int column)
return false;
}
void KCubeBoxWidget::checkComputerplayer(Player player)
{
// checking if a process is running or the Widget isn't shown yet
......@@ -437,7 +440,7 @@ void KCubeBoxWidget::init()
computerPlOne=false;
computerPlTwo=false;
KCubeWidget::enableClicks(true);
readSettings();
connect(moveTimer,SIGNAL(timeout()),SLOT(nextLoopStep()));
connect(this,SIGNAL(startedThinking()),SLOT(setWaitCursor()));
......@@ -554,7 +557,6 @@ void KCubeBoxWidget::startLoop()
moveTimer->start(moveDelay);
}
void KCubeBoxWidget::stopLoop()
{
moveTimer->stop();
......@@ -562,7 +564,6 @@ void KCubeBoxWidget::stopLoop()
KCubeWidget::enableClicks(true);
}
void KCubeBoxWidget::nextLoopStep()
{
// search cube with to many points
......@@ -609,7 +610,6 @@ void KCubeBoxWidget::nextLoopStep()
}
}
bool KCubeBoxWidget::hasPlayerWon(Player player)
{
for(int i=0;i<dim();i++)
......@@ -710,3 +710,4 @@ void KCubeBoxWidget::increaseNeighbours(KCubeBoxWidget::Player forWhom,int row,i
}
#include "kcubeboxwidget.moc"
......@@ -69,12 +69,6 @@ public:
/** undo last move */
void undo();
/**
* computes a possibility to move and shows it by highlightning
* this cube
*/
void getHint();
/**
* set colors that are used to show owners of the cubes
*
......@@ -86,12 +80,6 @@ public:
* sets number of Cubes in a row/column to 'size'.
*/
virtual void setDim(int dim);
/**
* sets skill of computerplayer and 'getHint()'
*
* @return previous level
*/
void setSkill(Brain::Skill skill);
/**
* sets player 'player' as computer or human
......@@ -118,14 +106,21 @@ public:
* checks if 'player' is a computerplayer an computes next move if TRUE
*/
void checkComputerplayer(Player player);
void saveGame(KConfigBase *);
void restoreGame(KConfigBase *);
inline void saveGame(KConfigBase *c) { saveProperties(c); };
inline void restoreGame(KConfigBase *c) { readProperties(c); };
public slots:
/** stops all activities like getting a hint or doing a move */
void stopActivities();
/**
* computes a possibility to move and shows it by highlightning
* this cube
*/
void getHint();
void readSettings();
signals:
void playerChanged(int newPlayer);
void playerWon(int player);
......@@ -133,23 +128,22 @@ signals:
void startedThinking();
void stoppedMoving();
void stoppedThinking();
protected:
virtual void deleteCubes();
virtual void initCubes();
void saveProperties(KConfigBase *);
void readProperties(KConfigBase *);
protected slots:
/** sets the cursor to an waitcursor */
void setWaitCursor();
/** restores the original cursor */
void setNormalCursor();
private:
void init();
QGridLayout *layout;
CubeBox *undoBox;
......@@ -168,7 +162,6 @@ private:
bool computerPlOne;
bool computerPlTwo;
/**
* increases the cube at row 'row' and column 'column' ,
* and starts the Loop for checking the playingfield
......@@ -176,6 +169,7 @@ private:
void doMove(int row,int column);
void increaseNeighbours(KCubeBoxWidget::Player forWhom,int row,int column);
private slots:
void nextLoopStep();
/**
......@@ -190,3 +184,4 @@ private slots:
};
#endif // KCUBEBOXWIDGET_H
......@@ -20,9 +20,15 @@
**************************************************************************** */
#include "kjumpingcube.h"
#include "brain.h"
#include "kcubeboxwidget.h"
#include "version.h"
// Settings
#include "settings.h"
#include "kautoconfig.h"
#include <kdialogbase.h>
#include <kiconloader.h>
#include <qfileinfo.h>
#include <qregexp.h>
......@@ -30,11 +36,7 @@
#include <kglobal.h>
#include <klocale.h>
#include <kkeydialog.h>
#include <kiconloader.h>
#include <kcolordialog.h>
#include <kfiledialog.h>
#include <ksimpleconfig.h>
#include <kmenubar.h>
#include <kmessagebox.h>
#include <ktempfile.h>
#include <kstdaction.h>
......@@ -42,7 +44,6 @@
#include <kaction.h>
#include <kio/netaccess.h>
#include <kstatusbar.h>
#include <kdebug.h>
#define ID_STATUS_TURN 1000
......@@ -64,9 +65,6 @@ KJumpingCube::KJumpingCube()
initKAction();
connect(toolBar(),SIGNAL(moved(BarPosition)),SLOT(barPositionChanged()));//FIXME: crashing :-(
QString s;
// init statusbar
s = i18n("On turn: Player %1").arg(1);
......@@ -75,216 +73,62 @@ KJumpingCube::KJumpingCube()
statusBar()->setItemAlignment (ID_STATUS_TURN,AlignLeft | AlignVCenter);
statusBar()->setFixedHeight( statusBar()->sizeHint().height());
KConfig *config=kapp->config();
// read config
{
KConfigGroupSaver cgs(config,"Window");
int barPos = config->readNumEntry("ToolbarPos",(int)(KToolBar::Top));
toolBar()->setBarPos((KToolBar::BarPosition)barPos);
bool visible=config->readBoolEntry("Toolbar",true);
if (toolBar()->isHidden() == visible)
showToolbar->activate();
visible=config->readNumEntry("Statusbar",true);
if (statusBar()->isHidden() == visible)
showStatusbar->activate();
QSize defSize(400,400);
QSize winSize=config->readSizeEntry("Size",&defSize);
resize(winSize);
}
{
KConfigGroupSaver cgs(config,"Game");
QColor c1=QColor("darkred");
QColor c2=QColor("darkblue");
QPalette color1(config->readColorEntry("Color1",&c1));
QPalette color2(config->readColorEntry("Color2",&c2));
view->setColor(KCubeBoxWidget::One,color1);
view->setColor(KCubeBoxWidget::Two,color2);
view->setDim(config->readNumEntry("CubeDim",6));
updatePlayfieldMenu(view->dim());
view->setSkill((Brain::Skill)config->readNumEntry("Skill",(int)Brain::Average));
updateSkillMenu((int)view->skill());
bool flag=config->readBoolEntry("Computer Pl.1",false);
view->setComputerplayer(KCubeBoxWidget::One,flag);
changeComputer1->setChecked(flag);
flag=config->readBoolEntry("Computer Pl.2",false);
view->setComputerplayer(KCubeBoxWidget::Two,flag);
changeComputer2->setChecked(flag);
}
resize(400,400);
setAutoSaveSettings();
}
void KJumpingCube::initKAction()
{
KStdGameAction::gameNew(this, SLOT(newGame()), actionCollection());
KStdGameAction::load(this, SLOT(openGame()), actionCollection());
KStdGameAction::save(this, SLOT(save()), actionCollection());
KStdGameAction::saveAs(this, SLOT(saveAs()), actionCollection());
KStdGameAction::quit(this, SLOT(close()), actionCollection());
hintAction = KStdGameAction::hint(this, SLOT(getHint()), actionCollection());
stopAction = new KAction(i18n("Stop &Thinking"), "stop", Qt::Key_Escape, this, SLOT(stop()), actionCollection(), "game_stop");
stopAction->setEnabled(FALSE);
undoAction = KStdGameAction::undo(this, SLOT(undo()), actionCollection());
undoAction->setEnabled(FALSE);
showToolbar = KStdAction::showToolbar(this, SLOT(toggleToolbar()), actionCollection());
showStatusbar = KStdAction::showStatusbar(this, SLOT(toggleStatusbar()), actionCollection());
KStdAction::keyBindings(this, SLOT(configureKeyBindings()), actionCollection());
QStringList plist;
plist.append(i18n("&5x5"));
plist.append(i18n("&6x6"));
plist.append(i18n("&7x7"));
plist.append(i18n("&8x8"));
plist.append(i18n("&9x9"));
plist.append(i18n("&10x10"));
optionField = new KSelectAction(i18n("&Playfield"), 0, this, SLOT(fieldChange()), actionCollection(), "options_field");
optionField->setItems(plist);
QStringList slist;
slist.append(i18n("&Beginner"));
slist.append(i18n("&Average"));
slist.append(i18n("&Expert"));
optionSkill = KStdGameAction::chooseGameType(this, SLOT(skillChange()), actionCollection());
optionSkill->setItems(slist);
changeComputer1 = new KToggleAction(i18n("Computer Plays Player &1"), 0, this, SLOT(changeComputerPlayer1()), actionCollection(), "options_change_computer1");
changeComputer2 = new KToggleAction(i18n("Computer Plays Player &2"), 0, this, SLOT(changeComputerPlayer2()), actionCollection(), "options_change_computer2");
(void)new KAction(i18n("Color Player &1"), 0, this, SLOT(changeColor1()), actionCollection(), "options_change_color1");
(void)new KAction(i18n("Color Player &2"), 0, this, SLOT(changeColor2()), actionCollection(), "options_change_color2");
// finally create toolbar and menubar
createGUI();
void KJumpingCube::initKAction() {
KStdGameAction::gameNew(this, SLOT(newGame()), actionCollection());
KStdGameAction::load(this, SLOT(openGame()), actionCollection());
KStdGameAction::save(this, SLOT(save()), actionCollection());
KStdGameAction::saveAs(this, SLOT(saveAs()), actionCollection());
KStdGameAction::quit(this, SLOT(close()), actionCollection());
hintAction = KStdGameAction::hint(view, SLOT(getHint()), actionCollection());
stopAction = new KAction(i18n("Stop &Thinking"), "stop",
Qt::Key_Escape, this, SLOT(stop()), actionCollection(), "game_stop");
stopAction->setEnabled(FALSE);
undoAction = KStdGameAction::undo(this, SLOT(undo()), actionCollection());
undoAction->setEnabled(FALSE);
showToolbar = KStdAction::showToolbar(this, SLOT(toggleToolbar()),
actionCollection());
showStatusbar = KStdAction::showStatusbar(this, SLOT(toggleStatusbar()),
actionCollection());
KStdAction::keyBindings(this, SLOT(configureKeyBindings()),
actionCollection());
(void)new KAction(i18n("&Configure KJumpingCube..."), 0, this,
SLOT(showOptions()), actionCollection(), "configure_kjumpingcube" );
// finally create toolbar and menubar
createGUI();
}
KJumpingCube::~KJumpingCube()
{
saveSettings();
if(view)
{
if(view->isActive())
view->stopActivities();
delete view;
}
KJumpingCube::~KJumpingCube(){
delete view;
}
void KJumpingCube::saveProperties(KConfig *config)
{
config->writeEntry("Toolbar", showToolbar->isChecked());
config->writeEntry("Statusbar", showStatusbar->isChecked());
config->writeEntry("CubeDim",view->dim());
config->writeEntry("Color1",view->color(KCubeBoxWidget::One).active().background());
config->writeEntry("Color2",view->color(KCubeBoxWidget::Two).active().background());
config->writeEntry("Skill",(int)view->skill());
config->writeEntry("Computer Pl.1", changeComputer1->isChecked());
config->writeEntry("Computer Pl.2", changeComputer2->isChecked());
view->saveGame(config);
void KJumpingCube::toggleToolbar(){
if (toolBar()->isHidden())
toolBar()->show();
else
toolBar()->hide();
}
void KJumpingCube::toggleToolbar()
{
if (!toolBar()->isHidden()) {
toolBar()->hide();
} else {
toolBar()->show();
}
}
void KJumpingCube::toggleStatusbar()
{
if (!statusBar()->isHidden()) {
statusBar()->hide();
} else {
statusBar()->show();
}
}
void KJumpingCube::readProperties(KConfig *config)
{
bool visible=config->readBoolEntry("Toolbar",true);
if (toolBar()->isHidden() == visible)
showToolbar->activate();
visible=config->readNumEntry("Statusbar",true);
if (statusBar()->isHidden() == visible)
showStatusbar->activate();
QColor c1=view->color(KCubeBoxWidget::One).active().background();
QColor c2=view->color(KCubeBoxWidget::Two).active().background();
QPalette color1(config->readColorEntry("Color1",&c1));
QPalette color2(config->readColorEntry("Color2",&c2));
view->setColor(KCubeBoxWidget::One,color1);
view->setColor(KCubeBoxWidget::Two,color2);
view->setDim(config->readNumEntry("CubeDim",view->dim()));
updatePlayfieldMenu(view->dim());
view->setSkill((Brain::Skill)config->readNumEntry("Skill",(int)view->skill()));
updateSkillMenu((int)view->skill());
bool flag=config->readBoolEntry("Computer Pl.1",false);
view->setComputerplayer(KCubeBoxWidget::One,flag);
changeComputer1->setChecked(flag);
flag=config->readBoolEntry("Computer Pl.2",false);
view->setComputerplayer(KCubeBoxWidget::Two,flag);
changeComputer2->setChecked(flag);
view->restoreGame(config);
}
void KJumpingCube::saveSettings()
{
KConfig *config=kapp->config();
{
KConfigGroupSaver cfs(config,"Window");
config->writeEntry("Toolbar", showToolbar->isChecked());
config->writeEntry("Statusbar", showStatusbar->isChecked());
config->writeEntry("Size",size());
}
{
KConfigGroupSaver cfs2(config,"Game");
config->writeEntry("CubeDim",view->dim());
config->writeEntry("Color1",view->color(KCubeBoxWidget::One).active().background());
config->writeEntry("Color2",view->color(KCubeBoxWidget::Two).active().background());
config->writeEntry("Skill",(int)view->skill());
config->writeEntry("Computer Pl.1", changeComputer1->isChecked());
config->writeEntry("Computer Pl.2", changeComputer2->isChecked());
}
config->sync();
statusBar()->message(i18n("settings saved"),MESSAGE_TIME);
void KJumpingCube::toggleStatusbar(){
if (statusBar()->isHidden())
statusBar()->show();
else
statusBar()->hide();
}
void KJumpingCube::newGame()
{
void KJumpingCube::newGame(){
undoAction->setEnabled(false);
view->reset();
statusBar()->message(i18n("New Game"),MESSAGE_TIME);