24 #include "ui_settings.h"
27 #include <kmahjonggconfigdialog.h>
31 #include <QPixmapCache>
33 #include <QDesktopWidget>
37 #include <KConfigDialog>
38 #include <KInputDialog>
40 #include <KMessageBox>
41 #include <KStandardGameAction>
42 #include <KStandardAction>
44 #include <KScoreDialog>
47 #include <kio/netaccess.h>
49 #include <ktoggleaction.h>
50 #include <kactioncollection.h>
53 #define ID_STATUS_TILENUMBER 1
54 #define ID_STATUS_MESSAGE 2
55 #define ID_STATUS_GAME 3
58 static const char *
gameMagic =
"kmahjongg-gamedata";
69 class Settings :
public QWidget,
public Ui::Settings
88 setMinimumSize(320, 320);
95 boardEditor =
new Editor();
96 boardEditor->setModal(
false);
104 gameTimer =
new KGameClock(
this);
106 connect(gameTimer, SIGNAL(timeChanged(
QString)),
this, SLOT(displayTime(
QString)));
109 bDemoModeActive =
false;
112 connect(bw, SIGNAL(tileNumberChanged(
int,
int,
int)), SLOT(
showTileNumber(
int,
int,
int)));
114 connect(bw, SIGNAL(
gameOver(
unsigned short,
unsigned short)),
this,
115 SLOT(
gameOver(
unsigned short,
unsigned short)));
116 connect(bw, SIGNAL(gameCalculated()),
this, SLOT(
timerReset()));
129 KStandardGameAction::gameNew(
this, SLOT(
newGame()), actionCollection());
130 KStandardGameAction::load(
this, SLOT(loadGame()), actionCollection());
131 KStandardGameAction::save(
this, SLOT(saveGame()), actionCollection());
132 KStandardGameAction::quit(
this, SLOT(close()), actionCollection());
133 KStandardGameAction::restart(
this, SLOT(restartGame()), actionCollection());
136 newNumGame->
setText(i18n(
"New Numbered Game..."));
137 connect(newNumGame, SIGNAL(triggered(
bool)), SLOT(startNewNumeric()));
139 QAction *action = KStandardGameAction::hint(bw, SLOT(helpMove()),
this);
140 actionCollection()->addAction(action->
objectName(), action);
143 shuffle->
setText(i18n(
"Shu&ffle"));
145 connect(shuffle, SIGNAL(triggered(
bool)), bw, SLOT(shuffle()));
147 KAction *angleccw = actionCollection()->addAction(
QLatin1String(
"view_angleccw"));
148 angleccw->setText(i18n(
"Rotate View Counterclockwise"));
149 angleccw->setIcon(KIcon(
QLatin1String(
"object-rotate-left")));
150 angleccw->setShortcuts(KShortcut(
"f"));
151 connect(angleccw, SIGNAL(triggered(
bool)), bw, SLOT(angleSwitchCCW()));
153 KAction *anglecw = actionCollection()->addAction(
QLatin1String(
"view_anglecw"));
154 anglecw->setText(i18n(
"Rotate View Clockwise"));
155 anglecw->setIcon(KIcon(
QLatin1String(
"object-rotate-right")));
156 anglecw->setShortcuts(KShortcut(
"g"));
157 connect(anglecw, SIGNAL(triggered(
bool)), bw, SLOT(angleSwitchCW()));
159 demoAction = KStandardGameAction::demo(
this, SLOT(demoMode()), actionCollection());
161 KStandardGameAction::highscores(
this, SLOT(showHighscores()), actionCollection());
162 pauseAction = KStandardGameAction::pause(
this, SLOT(pause()), actionCollection());
165 undoAction = KStandardGameAction::undo(
this, SLOT(undo()), actionCollection());
166 redoAction = KStandardGameAction::redo(
this, SLOT(redo()), actionCollection());
170 boardEdit->
setText(i18n(
"&Board Editor"));
171 connect(boardEdit, SIGNAL(triggered(
bool)), SLOT(slotBoardEditor()));
174 KStandardAction::preferences(
this, SLOT(showSettings()), actionCollection());
176 setupGUI(qApp->desktop()->availableGeometry().size() * 0.7);
181 gameTimerLabel =
new QLabel(i18n(
"Time: 0:00:00"), statusBar());
182 statusBar()->addWidget(gameTimerLabel);
186 statusBar()->addWidget(timerDivider);
188 tilesLeftLabel =
new QLabel(i18n(
"Removed: 0000/0000"), statusBar());
189 statusBar()->addWidget(tilesLeftLabel, 1);
193 statusBar()->addWidget(tileDivider);
195 gameNumLabel =
new QLabel(i18n(
"Game: 000000000000000000000"), statusBar());
196 statusBar()->addWidget(gameNumLabel);
200 statusBar()->addWidget(gameNumDivider);
202 statusLabel =
new QLabel(
"Kmahjongg", statusBar());
203 statusBar()->addWidget(statusLabel);
206 void KMahjongg::displayTime(
const QString& timestring)
208 gameTimerLabel->
setText(i18n(
"Time: ") + timestring);
217 void KMahjongg::startNewNumeric()
220 int s = KInputDialog::getInteger(i18n(
"New Game"), i18n(
"Enter game number:"), 0, 0, INT_MAX, 1,
228 void KMahjongg::undo()
234 void KMahjongg::redo()
243 void KMahjongg::showSettings()
245 if (KConfigDialog::showDialog(
"settings")) {
250 KMahjonggConfigDialog *dialog =
new KMahjonggConfigDialog(
this,
"settings",
Prefs::self());
253 dialog->addPage(
new Settings(0), i18n(
"General"),
"games-config-options");
256 dialog->addTilesetPage();
257 dialog->addBackgroundPage();
258 dialog->setHelp(
QString(),
"kmahjongg");
260 connect(dialog, SIGNAL(settingsChanged(
QString)), bw, SLOT(loadSettings()));
261 connect(dialog, SIGNAL(settingsChanged(
QString)), boardEditor, SLOT(setTilesetFromSettings()));
267 void KMahjongg::demoMode()
269 if (bDemoModeActive) {
280 void KMahjongg::pause()
293 void KMahjongg::showHighscores()
295 KScoreDialog ksdialog(KScoreDialog::Name | KScoreDialog::Time,
this);
300 void KMahjongg::slotBoardEditor()
302 boardEditor->setVisible(
true);
315 if (!bDemoModeActive) {
336 gameTimer->restart();
341 if (event->
type() == QEvent::WindowStateChange) {
344 if ((isMinimized() && stateEvent->
oldState() != Qt::WindowMinimized)
345 || (!isMinimized() && stateEvent->
oldState() == Qt::WindowMinimized)) {
359 KMessageBox::information(
this, i18n(
"You have won!"));
364 int elapsed = gameTimer->seconds();
370 time = (60 * 20) - gameTimer->seconds();
377 score += (numRemoved * 20);
381 score -= (cheats * 20);
388 KScoreDialog ksdialog(KScoreDialog::Name | KScoreDialog::Time,
this);
390 KScoreDialog::FieldInfo scoreInfo;
391 scoreInfo[KScoreDialog::Score].setNum(score);
392 scoreInfo[KScoreDialog::Time] = gameTimer->timeString();
393 if(ksdialog.addScore(scoreInfo, KScoreDialog::AskName)) {
406 QString str = i18n(
"Game number: %1", board);
414 QString szBuffer = i18n(
"Removed: %1/%2 Combinations left: %3", iMaximum-iCurrent, iMaximum,
416 tilesLeftLabel->
setText(szBuffer);
424 if (!bDemoModeActive && !
is_paused && !mFinished) {
431 bDemoModeActive = bActive;
434 demoAction->setChecked(bActive ||
is_paused);
437 stateChanged(
"paused");
438 }
else if (mFinished) {
439 stateChanged(
"finished");
440 }
else if (bActive) {
441 stateChanged(
"active");
443 stateChanged(
"inactive");
449 void KMahjongg::restartGame()
451 if (!bDemoModeActive) {
465 pauseAction->setChecked(
false);
472 void KMahjongg::loadGame()
477 KUrl url = KFileDialog::getOpenUrl(KUrl(),
"*.kmgame",
this, i18n(
"Load Game" ));
483 KIO::NetAccess::download(url, fname,
this);
488 if (!infile.open(QIODevice::ReadOnly)) {
489 KMessageBox::sorry(
this, i18n(
"Could not read from file. Aborting."));
500 KMessageBox::sorry(
this, i18n(
"File is not a KMahjongg game."));
511 in.setVersion(QDataStream::Qt_4_0);
513 KMessageBox::sorry(
this, i18n(
"File format not recognized."));
524 in >> theBackgroundName;
526 in >> theBoardLayoutName;
531 gameTimer->setTime(seconds);
540 KIO::NetAccess::removeTempFile(fname);
549 void KMahjongg::saveGame()
555 KUrl url = KFileDialog::getSaveUrl(KUrl(),
"*.kmgame",
this, i18n(
"Save Game"));
563 if (!url.isLocalFile()) {
564 KMessageBox::sorry(
this, i18n(
"Only saving to local files currently supported."));
570 QFile outfile(url.path());
572 if (!outfile.open(QIODevice::WriteOnly)) {
573 KMessageBox::sorry(
this, i18n(
"Could not write saved game."));
584 out.setVersion(QDataStream::Qt_4_0);
591 out << gameTimer->seconds();
600 #include "kmahjongg.moc"
void setText(const QString &text)
KMahjongg(QWidget *parent=0)
Constructor.
void setIcon(const QIcon &icon)
void setFrameStyle(int style)
void setupKAction()
Method Description.
void setupStatusBar()
Method Description.
static int gameDataVersion
void setCacheLimit(int n)
static const char * gameMagic
void demoModeChanged(bool bActive)
Slot Description.
void newGame()
Slot Description.
void showStatusText(const QString &msg, long board)
Slot Description.
void showTileNumber(int iMaximum, int iCurrent, int iLeft)
Slot Description.
void timerReset()
Slot Description.
bool saveToStream(QDataStream &out)
void setText(const QString &)
Qt::WindowStates oldState() const
bool loadFromStream(QDataStream &in)
void gameOver(unsigned short removed, unsigned short cheats)
Slot Description.
void setTilesetFromSettings()
Load the settings from prefs.
static QRect editorGeometry()
Get EditorGeometry.
void startNewGame(int num=-1)
Slot Description.
~KMahjongg()
Default Destructor.
void setDisplayedWidth()
Load BoardLayout from file.
void changeEvent(QEvent *event)
Method Override.
int compare(const QString &other) const