• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

kgoldrunner

  • sources
  • kde-4.14
  • kdegames
  • kgoldrunner
  • src
kgrdialog.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright 2003 Marco Krüger <grisuji@gmx.de> *
3  * Copyright 2003 Ian Wadham <iandw.au@gmail.com> *
4  * Copyright 2009 Ian Wadham <iandw.au@gmail.com> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18  ****************************************************************************/
19 
20 #include "kgrdialog.h"
21 
22 #include "kgrglobals.h"
23 
24 #include <QFile>
25 #include <QTextStream>
26 #include <QVBoxLayout>
27 #include <QLabel>
28 #include <QButtonGroup>
29 
30 #include <KGlobalSettings>
31 
32 /*******************************************************************************
33 *************** DIALOG BOX TO CREATE/EDIT A LEVEL NAME AND HINT ****************
34 *******************************************************************************/
35 
36 KGrNHDialog::KGrNHDialog (const QString & levelName, const QString & levelHint,
37  QWidget * parent)
38  : KDialog (parent)
39 {
40  setCaption (i18n ("Edit Name & Hint"));
41  setButtons (KDialog::Ok | KDialog::Cancel);
42  setDefaultButton (KDialog::Ok);
43  int margin = marginHint();
44  int spacing = spacingHint();
45  QWidget * dad = new QWidget (this);
46  setMainWidget (dad);
47 
48  QVBoxLayout * mainLayout = new QVBoxLayout (dad);
49  mainLayout->setSpacing (spacing);
50  mainLayout->setMargin (margin);
51 
52  QLabel * nameL = new QLabel (i18n ("Name of level:"), dad);
53  mainLayout->addWidget (nameL);
54  nhName = new QLineEdit (dad);
55  mainLayout->addWidget (nhName);
56 
57  QLabel * mleL = new QLabel (i18n ("Hint for level:"), dad);
58  mainLayout->addWidget (mleL);
59 
60  // Set up a widget to hold the wrapped text, using \n for paragraph breaks.
61  mle = new QTextEdit (dad);
62  mle-> setAcceptRichText (false);
63  mainLayout->addWidget (mle);
64 
65  // Base the geometry of the text box on the playing area.
66  QPoint p = parent->mapToGlobal (QPoint (0,0));
67  int c = parent->width() / (FIELDWIDTH + 4);
68  dad-> move (p.x()+4*c, p.y()+4*c);
69  mle-> setMinimumSize ((FIELDWIDTH*c/2), (FIELDHEIGHT/2)*c);
70 
71  // Configure the text box.
72  mle-> setAlignment (Qt::AlignLeft);
73 
74  nhName-> setText (levelName);
75  mle-> setText (levelHint);
76 }
77 
78 KGrNHDialog::~KGrNHDialog()
79 {
80 }
81 
82 /*******************************************************************************
83 *************** DIALOG BOX TO CREATE OR EDIT A GAME (COLLECTION) ***************
84 *******************************************************************************/
85 
86 KGrECDialog::KGrECDialog (int action, int gameIndex,
87  QList<KGrGameData *> & gamesList,
88  QWidget * parent)
89  : KDialog (parent)
90 {
91  myGameList = gamesList;
92  defaultGame = gameIndex;
93 
94  setCaption (i18n ("Edit Game Info"));
95  setButtons (KDialog::Ok | KDialog::Cancel);
96  setDefaultButton (KDialog::Ok);
97  int margin = marginHint();
98  int spacing = spacingHint();
99  QWidget * dad = new QWidget (this);
100  setMainWidget (dad);
101 
102  QVBoxLayout * mainLayout = new QVBoxLayout (dad);
103  mainLayout->setSpacing (spacing);
104  mainLayout->setMargin (margin);
105 
106  QHBoxLayout *hboxLayout5 = new QHBoxLayout();
107  hboxLayout5->setSpacing (spacing);
108  nameL = new QLabel (i18n ("Name of game:"), dad);
109  hboxLayout5->addWidget (nameL);
110  ecName = new QLineEdit (dad);
111  hboxLayout5->addWidget (ecName);
112  mainLayout->addLayout (hboxLayout5);
113 
114  QHBoxLayout *hboxLayout6 = new QHBoxLayout();
115  hboxLayout6->setSpacing (spacing);
116  prefixL = new QLabel (i18n ("File name prefix:"), dad);
117  hboxLayout6->addWidget (prefixL);
118  ecPrefix = new QLineEdit (dad);
119  hboxLayout6->addWidget (ecPrefix);
120  mainLayout->addLayout (hboxLayout6);
121 
122  //In Qt4, QButtonGroup is no longer a widget...
123  ecGrp = new QButtonGroup (dad);
124  ecTradB = new QRadioButton (i18n ("Traditional rules"), dad);
125  ecKGrB = new QRadioButton (i18n ("KGoldrunner rules"), dad);
126  ecGrp->addButton (ecTradB);
127  ecGrp->addButton (ecKGrB);
128 
129  //..so we need to add the radio buttons directly to the layout
130  mainLayout->addWidget (ecTradB);
131  mainLayout->addWidget (ecKGrB);
132 
133 
134  nLevL = new QLabel (i18np ("1 level", "%1 levels", 0), dad);
135  mainLayout->addWidget (nLevL);
136 
137  mleL = new QLabel (i18n ("About this game:"), dad);
138  mainLayout->addWidget (mleL);
139 
140  // Set up a widget to hold the wrapped text, using \n for paragraph breaks.
141  mle = new QTextEdit (dad);
142  mle-> setAcceptRichText (false);
143  mainLayout->addWidget (mle);
144 
145  QPoint p = parent->mapToGlobal (QPoint (0,0));
146 
147  // Base the geometry of the dialog box on the playing area.
148  int cell = qMin(parent->height() / (FIELDHEIGHT + 4), parent->width() / FIELDWIDTH + 4);
149  dad-> move (p.x()+2*cell, p.y()+2*cell);
150  dad-> setMinimumSize ((FIELDWIDTH*cell/2), (FIELDHEIGHT-1)*cell);
151 
152  if (action == SL_CR_GAME) {
153  setCaption (i18n ("Create Game"));
154  }
155  else {
156  setCaption (i18n ("Edit Game Info"));
157  }
158 
159  QString OKText = "";
160  if (action == SL_UPD_GAME) { // Edit existing game.
161  ecName-> setText (myGameList.at (defaultGame)->name);
162  ecPrefix-> setText (myGameList.at (defaultGame)->prefix);
163  if (myGameList.at (defaultGame)->nLevels > 0) {
164  // Game already has some levels, so cannot change the prefix.
165  ecPrefix-> setEnabled (false);
166  }
167  QString s;
168  nLevL-> setText (i18np ("1 level", "%1 levels",
169  myGameList.at (defaultGame)->nLevels));
170  OKText = i18n ("Save Changes");
171  }
172  else { // Create a game.
173  ecName-> setText ("");
174  ecPrefix-> setText ("");
175  nLevL-> setText (i18n ("0 levels"));
176  OKText = i18n ("Save New");
177  }
178  setButtonGuiItem (KDialog::Ok, KGuiItem (OKText));
179 
180  if ((action == SL_CR_GAME) ||
181  (myGameList.at (defaultGame)->rules == 'T')) {
182  ecSetRules ('T'); // Traditional rules.
183  }
184  else {
185  ecSetRules ('K'); // KGoldrunner rules.
186  }
187 
188  // Configure the edit box.
189  mle-> setAlignment (Qt::AlignLeft);
190 
191  if ((action == SL_UPD_GAME) &&
192  (myGameList.at (defaultGame)->about.length() > 0)) {
193  // Display and edit the game description in its original language.
194  mle->setPlainText (QString::fromUtf8
195  (myGameList.at (defaultGame)->about.constData()));
196  }
197  else {
198  mle->setPlainText ("");
199  }
200 
201  connect (ecKGrB, SIGNAL (clicked()), this, SLOT (ecSetKGr()));
202  connect (ecTradB, SIGNAL (clicked()), this, SLOT (ecSetTrad()));
203 }
204 
205 KGrECDialog::~KGrECDialog()
206 {
207 }
208 
209 void KGrECDialog::ecSetRules (const char rules)
210 {
211  ecKGrB-> setChecked (false);
212  ecTradB-> setChecked (false);
213  if (rules == 'K')
214  ecKGrB-> setChecked (true);
215  else
216  ecTradB-> setChecked (true);
217 }
218 
219 void KGrECDialog::ecSetKGr() {ecSetRules ('K');} // Radio button slots.
220 void KGrECDialog::ecSetTrad() {ecSetRules ('T');}
221 
222 /*******************************************************************************
223 *************** DIALOG TO SELECT A SAVED GAME TO BE RE-LOADED ****************
224 *******************************************************************************/
225 
226 KGrLGDialog::KGrLGDialog (QFile * savedGames,
227  QList<KGrGameData *> & gameList,
228  QWidget * parent)
229  : KDialog (parent)
230 {
231  setCaption (i18n ("Select Saved Game"));
232  setButtons (KDialog::Ok | KDialog::Cancel);
233  setDefaultButton (KDialog::Ok);
234  int margin = marginHint();
235  int spacing = spacingHint();
236  QWidget * dad = new QWidget (this);
237  setMainWidget (dad);
238 
239  QVBoxLayout * mainLayout = new QVBoxLayout (dad);
240  mainLayout->setSpacing (spacing);
241  mainLayout->setMargin (margin);
242 
243  QLabel * lgHeader = new QLabel (
244  i18n ("Game Level/Lives/Score "
245  "Day Date Time "), dad);
246 
247  lgList = new QListWidget (dad);
248  QFont f = KGlobalSettings::fixedFont(); // KDE version.
249  f.setFixedPitch (true);
250  lgList-> setFont (f);
251  f.setBold (true);
252  lgHeader-> setFont (f);
253 
254  mainLayout-> addWidget (lgHeader);
255  mainLayout-> addWidget (lgList);
256 
257  // Base the geometry of the list box on the playing area.
258  QPoint p = parent->mapToGlobal (QPoint (0,0));
259  int c = parent->width() / (FIELDWIDTH + 4);
260  dad-> move (p.x()+2*c, p.y()+2*c);
261  lgList-> setMinimumHeight ((FIELDHEIGHT/2)*c);
262 
263  lgHighlight = -1;
264 
265  QTextStream gameText (savedGames);
266  QString s = "";
267  QString pr = "";
268  int i;
269  int imax = gameList.count();
270 
271  // Read the saved games into the list box.
272  while (! gameText.atEnd()) {
273  s = gameText.readLine(); // Read in one saved game.
274  pr = s.left (s.indexOf (" ", 0,
275  Qt::CaseInsensitive)); // Get the game prefix.
276  for (i = 0; i < imax; i++) { // Get the game name.
277  if (gameList.at (i)->prefix == pr) {
278  s = s.insert (0,
279  gameList.at (i)->name.leftJustified (20, ' ', true) + ' ');
280  break;
281  }
282  }
283  lgList-> addItem (s);
284  }
285  savedGames->close();
286 
287  // Mark row 0 (the most recently saved game) as the default selection.
288  lgList-> setSelectionMode (QAbstractItemView::SingleSelection);
289  lgList-> setCurrentRow (0);
290  lgList-> setItemSelected (lgList->currentItem(), true);
291  lgHighlight = 0;
292 
293  connect (lgList, SIGNAL (itemClicked(QListWidgetItem*)),
294  this, SLOT (lgSelect(QListWidgetItem*)));
295 }
296 
297 void KGrLGDialog::lgSelect (QListWidgetItem * item)
298 {
299  lgHighlight = lgList->row (item);
300 }
301 
302 /*******************************************************************************
303 *********************** CENTRALIZED MESSAGE FUNCTIONS ************************
304 *******************************************************************************/
305 
306 void KGrMessage::information (QWidget * parent,
307  const QString & caption, const QString & text,
308  const QString & dontShowAgain)
309 {
310  // KDE does word-wrapping and will observe "\n" line-breaks.
311  KMessageBox::information (parent, text, caption, dontShowAgain);
312 }
313 
314 int KGrMessage::warning (QWidget * parent, const QString &caption,
315  const QString &text, const QString &label0,
316  const QString &label1, const QString &label2)
317 {
318  // KDE does word-wrapping and will observe "\n" line-breaks.
319  int ans = 0;
320  if (label2.isEmpty()) {
321  // Display a box with 2 buttons.
322  ans = KMessageBox::questionYesNo (parent, text, caption,
323  KGuiItem (label0), KGuiItem (label1));
324  ans = (ans == KMessageBox::Yes) ? 0 : 1;
325  }
326  else {
327  // Display a box with 3 buttons.
328  ans = KMessageBox::questionYesNoCancel (parent, text, caption,
329  KGuiItem (label0), KGuiItem (label1),
330  KGuiItem (label2));
331  if (ans == KMessageBox::Cancel)
332  ans = 2;
333  else
334  ans = (ans == KMessageBox::Yes) ? 0 : 1;
335  }
336  return (ans);
337 }
338 
339 #include "kgrdialog.moc"
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QWidget
QListWidget::currentItem
QListWidgetItem * currentItem() const
QTextStream::readLine
QString readLine(qint64 maxlen)
KGrMessage::warning
static int warning(QWidget *parent, const QString &caption, const QString &text, const QString &label0, const QString &label1, const QString &label2="")
Definition: kgrdialog.cpp:314
QButtonGroup::addButton
void addButton(QAbstractButton *button)
SL_UPD_GAME
Definition: kgrglobals.h:61
QFont
QList::at
const T & at(int i) const
QListWidgetItem
QWidget::mapToGlobal
QPoint mapToGlobal(const QPoint &pos) const
QHBoxLayout
KGrNHDialog::KGrNHDialog
KGrNHDialog(const QString &levelName, const QString &levelHint, QWidget *parent=0)
Definition: kgrdialog.cpp:36
QPoint
KDialog
FIELDHEIGHT
const int FIELDHEIGHT
Definition: kgrglobals.h:49
KGrECDialog::~KGrECDialog
~KGrECDialog()
Definition: kgrdialog.cpp:205
QTextEdit::setPlainText
void setPlainText(const QString &text)
QListWidget
KGrLGDialog::KGrLGDialog
KGrLGDialog(QFile *savedGames, QList< KGrGameData * > &gameList, QWidget *parent)
Definition: kgrdialog.cpp:226
KGrNHDialog::~KGrNHDialog
~KGrNHDialog()
Definition: kgrdialog.cpp:78
QFile
QPoint::x
int x() const
QPoint::y
int y() const
QTextStream
QButtonGroup
QWidget::width
width
QFont::setBold
void setBold(bool enable)
kgrdialog.h
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
SL_CR_GAME
Definition: kgrglobals.h:61
QString::fromUtf8
QString fromUtf8(const char *str, int size)
QString::insert
QString & insert(int position, QChar ch)
QTextStream::atEnd
bool atEnd() const
FIELDWIDTH
const int FIELDWIDTH
Definition: kgrglobals.h:48
QString::isEmpty
bool isEmpty() const
KGrMessage::information
static void information(QWidget *parent, const QString &caption, const QString &text, const QString &dontShowAgain=QString())
Definition: kgrdialog.cpp:306
QVBoxLayout
QString
QList< KGrGameData * >
QLayout::setMargin
void setMargin(int margin)
QListWidget::row
int row(const QListWidgetItem *item) const
QFile::close
virtual void close()
KGrECDialog::KGrECDialog
KGrECDialog(int action, int collnIndex, QList< KGrGameData * > &gameList, QWidget *parent=0)
Definition: kgrdialog.cpp:86
kgrglobals.h
QRadioButton
QString::left
QString left(int n) const
QLineEdit
QLabel
QTextEdit
QFont::setFixedPitch
void setFixedPitch(bool enable)
QBoxLayout::setSpacing
void setSpacing(int spacing)
QWidget::height
height
QBoxLayout::addLayout
void addLayout(QLayout *layout, int stretch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:24 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgoldrunner

Skip menu "kgoldrunner"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal