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

kanagram

  • sources
  • kde-4.12
  • kdeedu
  • kanagram
  • src
  • desktop
kanagram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Joshua Keel <joshuakeel@gmail.com> *
3  * (C) 2007 by Jeremy Whiting <jpwhiting@kde.org> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include "kanagram.h"
22 
23 #include <QMouseEvent>
24 #include <QPainter>
25 #include <QPaintEvent>
26 #include <QPixmap>
27 #include <QSvgRenderer>
28 #include <QTimer>
29 
30 #include <Phonon/MediaObject>
31 
32 #include <KAction>
33 #include <KActionCollection>
34 #include <KConfigDialog>
35 #include <KFontUtils>
36 #include <KGlobalSettings>
37 #include <KHelpMenu>
38 #include <KLineEdit>
39 #include <KMessageBox>
40 #include <KShortcutsEditor>
41 #include <KStandardAction>
42 #include <KStandardDirs>
43 
44 #include <sharedkvtmlfiles.h>
45 
46 #include <kanagramgame.h>
47 #include <kanagramsettings.h>
48 #include "mainsettings.h"
49 #include "vocabsettings.h"
50 
51 static const char* m_textRevealWord = I18N_NOOP("reveal word");
52 static const char* m_textHint = I18N_NOOP("hint");
53 static const char* m_textPicHint = I18N_NOOP("picture hint");
54 static const char* m_nextText = I18N_NOOP("Next Anagram");
55 
56 double kWindowWidth = 1000.0;
57 double kWindowHeight = 725.0;
58 
59 double xEyesScale = 270.437 / kWindowWidth;
60 double yEyesScale = 195.176 / kWindowHeight;
61 
62 double xScale57Buttons = 57.5 / kWindowWidth;
63 double yScale57Buttons = 57.5 / kWindowHeight;
64 
65 double xScale55Buttons = 55.0 / kWindowWidth;
66 double yScale55Buttons = 55.0 / kWindowHeight;
67 
68 double xTranslateButtons = 810.053 / kWindowWidth;
69 
70 double yTranslateNextButton = 59.588 / kWindowHeight;
71 double yTranslateConfigButton = 199.85 / kWindowHeight;
72 double yTranslateHelpButton = 337.487 / kWindowHeight;
73 
74 double xScaleQuitButton = 77.484 / kWindowWidth;
75 double yScaleQuitButton = 77.5 / kWindowHeight;
76 
77 Kanagram::Kanagram()
78 : KMainWindow(), m_game(NULL), m_overNext(false), m_overConfig(false),
79  m_overHelp(false), m_overQuit(false), m_overReveal(false), m_overHint(false),m_overPicHint(false),
80  m_overUp(false), m_overAboutKDE(false), m_overAboutApp(false),
81  m_overHandbook(false), m_overSwitcher(false), m_overLogo(false),
82  m_overHintBox(false), m_showHint(false), m_showPicHint(false), m_player(NULL), m_wordRevealed(false),
83  m_actionCollection(NULL)
84 {
85  setAttribute(Qt::WA_StaticContents);
86  m_renderer = new QSvgRenderer(KStandardDirs::locate("appdata", "images/kanagram.svg"));
87 
88  m_helpMenu = new KHelpMenu(this, KGlobal::mainComponent().aboutData());
89 
90  setupActions();
91 
92  loadSettings();
93 
94  m_game = new KanagramGame();
95  if (!m_game->picHint().isEmpty())
96  {
97  m_pictureHint.load(m_game->picHint().pathOrUrl());
98  }
99 
100  setMouseTracking(true);
101  m_chalkColor = QColor(155, 155, 155);
102  m_chalkHighlightColor = QColor(255, 255, 255);
103  m_fillColor = QColor(45, 45, 45);
104  m_fontColor = QColor(55, 55, 55);
105  m_fontHighlightColor = QColor(99, 99, 99);
106 
107  m_hintTimer = new QTimer(this);
108  m_hintTimer->setSingleShot(true);
109 
110  m_resolveTimer = new QTimer(this);
111  m_resolveTimer->setSingleShot(true);
112 
113  m_inputBox = new KLineEdit(this);
114  m_inputBox->setFrame(false);
115 
116  connect(m_inputBox, SIGNAL(returnPressed()), SLOT(checkWord()));
117  connect(m_hintTimer, SIGNAL(timeout()), SLOT(hideHint()));
118  connect(m_resolveTimer, SIGNAL(timeout()), SLOT(slotRevealWord()));
119  connect(m_inputBox, SIGNAL(textChanged(QString)), SLOT(update()));
120  connect(m_game, SIGNAL(fileError(QString)), SLOT(slotFileError(QString)));
121 
122  QFont f = QFont();
123  f.setPointSize(17);
124  m_inputBox->setFont(f);
125  m_inputBox->show();
126 
127  setAutoSaveSettings();
128 
129  setMinimumSize(650, 471);
130 
131  if (m_resolveTime != 0)
132  {
133  m_resolveTimer->start(m_resolveTime * 1000);
134  }
135 }
136 
137 Kanagram::~Kanagram()
138 {
139  delete m_player;
140  m_player = NULL;
141 
142  delete m_game;
143  m_game = NULL;
144 
145  delete m_renderer;
146  m_renderer = NULL;
147 }
148 
149 QSize Kanagram::sizeHint() const
150 {
151  return QSize(650, 471);
152 }
153 
154 void Kanagram::loadSettings()
155 {
156  QString hideTime = KanagramSettings::hintHideTime();
157 
158  if (hideTime.at(0).isDigit())
159  {
160  // because the choices are 0, 3, 5, 7, 9
161  m_hintHideTime = (hideTime.at(0).digitValue() * 2) + 1;
162 
163  // reset to 0 if it's 1 to allow for the don't hide option
164  if (m_hintHideTime == 1)
165  {
166  m_hintHideTime = 0;
167  }
168  }
169  else
170  {
171  m_hintHideTime = 0;
172  }
173 
174  QString resolveTime = KanagramSettings::resolveTime();
175 
176  if (resolveTime.at(0).isDigit())
177  {
178  // because the choices are 0, 15, 30, 45, 60 seconds
179  m_resolveTime = (resolveTime.at(0).digitValue()) * 15;
180  }
181  else
182  {
183  m_resolveTime = 0;
184  }
185 
186  if (KanagramSettings::dataLanguage().isEmpty())
187  {
188  QStringList userLanguagesCode = KGlobal::locale()->languageList();
189 
190  QStringList sharedKvtmlFilesLanguages = SharedKvtmlFiles::languages();
191  QString foundLanguage;
192  foreach (const QString &userLanguageCode, userLanguagesCode)
193  {
194  if (sharedKvtmlFilesLanguages.contains(userLanguageCode))
195  {
196  foundLanguage = userLanguageCode;
197  break;
198  }
199  }
200 
201  KanagramSettings::setDataLanguage(!foundLanguage.isEmpty() ? foundLanguage : "en");
202  }
203 
204  m_useSounds = KanagramSettings::useSounds();
205  m_arrowName = "basicarrow";
206 }
207 
208 void Kanagram::reloadSettings()
209 {
210  loadSettings();
211  refreshVocabularies();
212 }
213 
214 void Kanagram::setupActions()
215 {
216  m_actionCollection = new KActionCollection(this);
217 
218  // next anagram action
219  KAction *nextAnagramAction = new KAction(i18n(m_nextText), m_actionCollection);
220  nextAnagramAction->setShortcut(Qt::CTRL+Qt::Key_N);
221  connect(nextAnagramAction, SIGNAL(triggered(bool)), this, SLOT(slotNextAnagram()));
222  m_actionCollection->addAction("nextanagram", nextAnagramAction);
223 
224  // hint action needs to not be help, as that conflicts with helpContents for shortcut key
225  KAction *showHintAction = new KAction(i18n("Show Hint"), m_actionCollection);
226  showHintAction->setShortcut(Qt::CTRL+Qt::Key_H);
227  connect(showHintAction, SIGNAL(triggered(bool)), this, SLOT(slotToggleHint()));
228  m_actionCollection->addAction("showhint", showHintAction);
229 
230  // reveal word action
231  KAction *revealWordAction = new KAction(i18n("Reveal Anagram"), m_actionCollection);
232  revealWordAction->setShortcut(Qt::CTRL+Qt::Key_R);
233  connect(revealWordAction, SIGNAL(triggered(bool)), this, SLOT(slotRevealWord()));
234  m_actionCollection->addAction("revealword", revealWordAction);
235 
236  // vocabulary actions
237  KAction *priorVocabularyAction = new KAction(i18n("Previous Vocabulary"), m_actionCollection);
238  priorVocabularyAction->setShortcut(Qt::Key_PageUp);
239  connect(priorVocabularyAction, SIGNAL(triggered(bool)), this, SLOT(slotPrevVocabulary()));
240  m_actionCollection->addAction("priorvocabulary", priorVocabularyAction);
241 
242  KAction *nextVocabularyAction = new KAction(i18n("Next Vocabulary"), m_actionCollection);
243  nextVocabularyAction->setShortcut(Qt::Key_PageDown);
244  connect(nextVocabularyAction, SIGNAL(triggered(bool)), this, SLOT(slotNextVocabulary()));
245  m_actionCollection->addAction("nextvocabulary", nextVocabularyAction);
246 
247  // help actions
248  KStandardAction::aboutApp(m_helpMenu, SLOT(aboutApplication()), m_actionCollection);
249  KStandardAction::aboutKDE(m_helpMenu, SLOT(aboutKDE()), m_actionCollection);
250  KStandardAction::helpContents(m_helpMenu, SLOT(appHelpActivated()), m_actionCollection);
251 
252  // standard actions
253  KStandardAction::preferences(this, SLOT(slotShowSettings()), m_actionCollection);
254  KStandardAction::quit(this, SLOT(close()), m_actionCollection);
255 
256 
257  // load any user-defined changes to shortcuts
258  m_actionCollection->readSettings();
259 
260  m_actionCollection->addAssociatedWidget(this);
261  foreach (QAction* action, m_actionCollection->actions())
262  action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
263 }
264 
265 void Kanagram::paintEvent(QPaintEvent *)
266 {
267  QPixmap buf(width(), height());
268  QPainter p(&buf);
269  p.setRenderHint(QPainter::Antialiasing);
270 
271  m_renderer->render(&p, "background");
272 
273  m_xRatio = width() / kWindowWidth;
274  m_yRatio = height() / kWindowHeight;
275 
276  if (m_overLogo)
277  {
278  p.translate(int(112.188 * m_xRatio), int(32.375 * m_yRatio));
279  p.scale(466.981 / kWindowWidth, 91.407 / kWindowHeight);
280  m_renderer->render(&p, "logo_hover");
281  p.resetMatrix();
282  }
283  else
284  {
285  p.translate(int(112.188 * m_xRatio), int(32.375 * m_yRatio));
286  p.scale(466.981 / kWindowWidth, 91.407 / kWindowHeight);
287  m_renderer->render(&p, "logo");
288  p.resetMatrix();
289  }
290 
291  if (m_overNext)
292  {
293  p.translate(xTranslateButtons * width(), yTranslateNextButton * height());
294  p.scale(xScale55Buttons, yScale55Buttons);
295  m_renderer->render(&p, "next_hover");
296  p.resetMatrix();
297  }
298 
299  if (m_overConfig)
300  {
301  p.translate(xTranslateButtons * width(), yTranslateConfigButton * height());
302  p.scale(xScale55Buttons, yScale55Buttons);
303  m_renderer->render(&p, "config_hover");
304  p.resetMatrix();
305  }
306 
307  if (m_overHelp)
308  {
309  p.translate(xTranslateButtons * width(), yTranslateHelpButton * height());
310  p.scale(xScale55Buttons, yScale55Buttons);
311  m_renderer->render(&p, "help_hover");
312  p.resetMatrix();
313  }
314 
315  if (m_overQuit)
316  {
317  p.translate(798.811 * m_xRatio, 556.803 * m_yRatio);
318  p.scale(xScaleQuitButton, yScaleQuitButton);
319  m_renderer->render(&p, "quit_hover");
320  p.resetMatrix();
321  }
322 
323  QString anagram = m_game->anagram();
324  int afontSize = KFontUtils::adaptFontSize(p, anagram, m_blackboardRect.width(), m_blackboardRect.height() / 5);
325  FixFontSize(afontSize);
326  drawTextNew(p, anagram, Qt::AlignCenter, 10, 10, m_blackboardRect, true, afontSize);
327 
328  QString reveal = i18n(m_textRevealWord);
329  m_cornerFontSize = KFontUtils::adaptFontSize(p, reveal, m_blackboardRect.width() / 3, m_blackboardRect.height() / 5);
330  FixFontSize(m_cornerFontSize);
331  if(!m_wordRevealed)
332  {
333  drawTextNew(p, reveal, Qt::AlignBottom | Qt::AlignRight, 6, 0, m_blackboardRect, m_overReveal, m_cornerFontSize);
334  }
335 
336  drawTextNew(p, i18n(m_textHint), Qt::AlignBottom | Qt::AlignLeft, 6, 0, m_blackboardRect, m_overHint, m_cornerFontSize);
337 
338  if(!m_game->picHint().isEmpty())
339  {
340  drawTextNew(p, i18n(m_textPicHint), Qt::AlignTop | Qt::AlignLeft, 6, 8, m_blackboardRect, m_overPicHint, m_cornerFontSize);
341  }
342 
343  // update these rects because we have access to the painter and thus the fontsize here
344  QFont font = KGlobalSettings::generalFont();
345  font.setPointSize(m_cornerFontSize);
346  font.setBold(true);
347  QFontMetrics fm(font);
348  QRect r = innerRect(m_blackboardRect, 6, 0);
349  m_hintRect = fm.boundingRect(r, Qt::AlignBottom|Qt::AlignLeft, i18n(m_textHint));
350  m_picHintRect = fm.boundingRect(r, Qt::AlignTop|Qt::AlignLeft, i18n(m_textPicHint));
351  m_hintBoxRect = QRect(int(684.813 * m_xRatio), int(319.896 * m_yRatio), int(xEyesScale * width()), int(yEyesScale * height()));
352  r = innerRect(m_blackboardRect, 6, 0);
353  m_revealRect = fm.boundingRect(r, Qt::AlignBottom|Qt::AlignRight, reveal);
354 
355  drawSwitcher(p, 9, 8);
356 
357  p.setPen(QPen(Qt::black, 3));
358 
359  //Draw the border of the input box
360  QRect borderRect = m_inputBox->geometry();
361  p.drawRoundRect(borderRect, 10, 5);
362  int inputBoxFontSize = KFontUtils::adaptFontSize(p, "A", borderRect.width(), borderRect.height()) - 1;
363  FixFontSize(inputBoxFontSize);
364  QFont f = QFont();
365  f.setPointSize(inputBoxFontSize);
366  m_inputBox->setFont(f);
367 
368  //Draw the border of the Up arrow
369  borderRect = m_upRect;
370  p.fillRect(borderRect, m_fillColor);
371  p.drawRoundRect(borderRect, 10, 5);
372 
373  QString upArrow = "up";
374  if (m_overUp && !m_inputBox->text().isEmpty())
375  {
376  upArrow = "up_hover";
377  }
378  else if (m_inputBox->text().isEmpty())
379  {
380  upArrow = "up_disabled";
381  }
382 
383  p.translate(m_inputBox->x() + m_inputBox->width() + 26 * m_xRatio, m_inputBox->y() + 12 * m_yRatio);
384  p.scale(38 / kWindowWidth, 20 / kWindowHeight);
385  m_renderer->render(&p, upArrow);
386  p.resetMatrix();
387 
388  if (m_showHint)
389  {
390  p.translate(684.813 * m_xRatio, 319.896 * m_yRatio);
391  p.scale(xEyesScale, yEyesScale);
392  m_renderer->render(&p, m_hintOverlayName);
393  p.resetMatrix();
394 
395  // do drawText with svg position and size
396  QFont f = KGlobalSettings::generalFont();
397  f.setWeight(QFont::Bold);
398  p.setFont(f);
399  QString hint = m_game->hint();
400  int fontSize = KFontUtils::adaptFontSize(p, hint, int(250 * m_xRatio), int(110 * m_yRatio));
401  FixFontSize(fontSize);
402  f.setPointSize(fontSize);
403  p.setFont(f);
404  p.drawText(int(694 * m_xRatio), int(330 * m_yRatio), int(250 * m_xRatio), int(110 * m_yRatio),
405  Qt::TextWordWrap | Qt::AlignCenter, hint);
406  }
407 
408  if (m_showPicHint)
409  {
410  p.drawImage(m_picHintRect.topLeft(),m_pictureHint);
411  }
412 
413  if (m_overHelp && !m_showHint && !m_showPicHint)
414  {
415  if (m_overAboutApp)
416  {
417  p.translate(808.377 * m_xRatio, 335.352 * m_yRatio);
418  p.scale(xScale57Buttons, yScale57Buttons);
419  m_renderer->render(&p, "appicon_hover");
420  p.resetMatrix();
421  drawHelpText(p, i18n("About Kanagram"));
422  }
423  else
424  {
425  p.translate(808.377 * m_xRatio, 335.352 * m_yRatio);
426  p.scale(xScale57Buttons, yScale57Buttons);
427  m_renderer->render(&p, "appicon");
428  p.resetMatrix();
429  }
430  if (m_overAboutKDE)
431  {
432  p.translate(865.877 * m_xRatio, 335.352 * m_yRatio);
433  p.scale(xScale57Buttons, yScale57Buttons);
434  m_renderer->render(&p, "kicon_hover");
435  p.resetMatrix();
436  drawHelpText(p, i18n("About KDE"));
437  }
438  else
439  {
440  p.translate(865.877 * m_xRatio, 335.352 * m_yRatio);
441  p.scale(xScale57Buttons, yScale57Buttons);
442  m_renderer->render(&p, "kicon");
443  p.resetMatrix();
444  }
445  if (m_overHandbook)
446  {
447  p.translate(750.877 * m_xRatio, 335.352 * m_yRatio);
448  p.scale(xScale57Buttons, yScale57Buttons);
449  m_renderer->render(&p, "handbook_hover");
450  p.resetMatrix();
451  drawHelpText(p, i18n("Kanagram Handbook"));
452  }
453  else
454  {
455  p.translate(750.877 * m_xRatio, 335.352 * m_yRatio);
456  p.scale(xScale57Buttons, yScale57Buttons);
457  m_renderer->render(&p, "handbook");
458  p.resetMatrix();
459  }
460  }
461  else if (m_overNext)
462  {
463  drawHelpText(p, i18n(m_nextText));
464  }
465  else if (m_overConfig)
466  {
467  drawHelpText(p, i18n("Configure Kanagram"));
468  }
469  else if (m_overQuit)
470  {
471  drawHelpText(p, i18n("Quit Kanagram"));
472  }
473 
474  QPainter p2(this);
475  p2.drawPixmap(0, 0, buf);
476 }
477 
479 void Kanagram::FixFontSize(int &fontSize)
480 {
481  if (fontSize <= 0)
482  {
483  fontSize = 8;
484  }
485 }
486 
487 void Kanagram::resizeEvent(QResizeEvent *)
488 {
489  m_xRatio = width() / kWindowWidth;
490  m_yRatio = height() / kWindowHeight;
491 
492  m_blackboardRect = QRect(int(63.657 * m_xRatio), int(182.397 * m_yRatio), int(563.273 * m_xRatio), int(380.735 * m_yRatio));
493  m_inputBox->setGeometry(QRect(int(80 * m_xRatio), int(657.272 * m_yRatio), int(420 * m_xRatio), int(44.639 * m_yRatio)));
494 
495  m_upRect = QRect(int(m_inputBox->x() + m_inputBox->width() + 20 * m_xRatio), m_inputBox->y(), int(50 * m_xRatio), m_inputBox->height());
496  m_arrowRect = QRect(m_switcherRect.right() + 5, m_switcherRect.top(), int(16.250 * m_xRatio), int(25.0 * m_yRatio));
497  m_logoRect = QRect(int(112.188 * m_xRatio), int(32.375 * m_yRatio), int(466.981 * m_xRatio), int(91.407 * m_yRatio));
498 
499  m_aboutAppRect = QRect(int(xTranslateButtons * width()), int(yTranslateHelpButton * height()),
500  int(xScale57Buttons * width()), int(yScale57Buttons * height()));
501  m_aboutKDERect = QRect(int(867 * m_xRatio), int(335.352 * m_yRatio),
502  int(xScale57Buttons * width()), int(yScale57Buttons * height()));
503  m_handbookRect = QRect(int(753 * m_xRatio), int(335.352 * m_yRatio),
504  int(xScale57Buttons * width()), int(yScale57Buttons * height()));
505 
506  m_nextRect = QRect(int(735.448 * m_xRatio), int(49.028 * m_yRatio), int(206.142 * m_xRatio), int(117.537 * m_yRatio));
507  m_configRect = QRect(int(735.448 * m_xRatio), int(188.264 * m_yRatio), int(206.142 * m_xRatio), int(117.537 * m_yRatio));
508  m_helpRect = QRect(int(735.448 * m_xRatio), int(327.5 * m_yRatio), int(206.142 * m_xRatio), int(117.537 * m_yRatio));
509  m_quitRect = QRect(int(697.549 * m_xRatio), int(542.337 * m_yRatio), int(279.935 * m_xRatio), int(160.68 * m_yRatio));
510 
511  update();
512 }
513 
514 void Kanagram::drawHelpText(QPainter &p, const QString &text)
515 {
516  p.translate(700.582 * m_xRatio, 424.176 * m_yRatio);
517  p.scale(256.582 / kWindowWidth, 101.150 / kWindowHeight);
518  m_renderer->render(&p, "card");
519  p.resetMatrix();
520 
521  p.save();
522  QFont font = KGlobalSettings::generalFont();
523  font.setPointSize(m_cornerFontSize);
524  p.setFont(font);
525  // translate to the same place as the folder plus a margin
526  p.translate(720.582 * m_xRatio, 480 * m_yRatio);
527  p.rotate(-3.29);
528  p.setPen(Qt::black);
529 
530  // draw the first word
531  p.drawText(0, 0, text.section(' ', 0, 0));
532  // draw the second word under the first
533  p.drawText(0, int(30 * m_yRatio), text.section(' ', 1));
534  p.restore();
535 }
536 
537 void Kanagram::drawSwitcher(QPainter &p, const int xMargin, const int yMargin)
538 {
539  const int padding = 5;
540  QString text = m_game->documentTitle();
541  QFont font = KGlobalSettings::generalFont();
542  font.setPointSize(m_cornerFontSize);
543  QFontMetrics fm(font);
544  QRect r = innerRect(m_blackboardRect, xMargin, yMargin);
545  r = r.normalized();
546  r.translate(- padding - int(16.250 * m_xRatio), yMargin);
547  r.setHeight(int(25.0 * m_yRatio));
548  m_switcherRect = p.boundingRect(r, Qt::AlignVCenter|Qt::AlignRight, text);
549  p.setFont(font);
550  QString arrow = m_arrowName;
551  if (m_overSwitcher)
552  {
553  p.setPen(m_chalkHighlightColor);
554  arrow = m_arrowName + "_hover";
555  }
556  else
557  {
558  p.setPen(m_chalkColor);
559  }
560  p.translate(m_switcherRect.right() + padding, m_switcherRect.top());
561  p.scale(16.250 / kWindowWidth, 25.0 / kWindowHeight);
562  m_renderer->render(&p, arrow);
563  p.resetMatrix();
564 
565  m_switcherRect.translate(0, -2);
566  p.drawText(m_switcherRect, Qt::AlignVCenter|Qt::AlignRight, text);
567 }
568 
569 QRect Kanagram::innerRect(const QRect &rect, const int xMargin, const int yMargin)
570 {
571  QRect r = rect;
572 
573  if (xMargin>0)
574  {
575  r.setWidth(r.width() - 2 * xMargin);
576  r.translate(xMargin, 0);
577  }
578  if (yMargin>0)
579  {
580  r.setHeight(r.height() - 2 * yMargin);
581  r.translate(0, yMargin);
582  }
583 
584  return r;
585 }
586 
588 void Kanagram::slotNextAnagram()
589 {
590  m_wordRevealed = false;
591  hideHint();
592  hidePicHint();
593  m_game->nextAnagram();
594  if (!m_game->picHint().isEmpty())
595  {
596  m_pictureHint.load(m_game->picHint().pathOrUrl());
597  }
598 
599  if (m_useSounds)
600  {
601  play("chalk.ogg");
602  }
603  m_inputBox->setPalette(QPalette());
604  update();
605 
606  if (m_resolveTime != 0)
607  {
608  m_resolveTimer->start(m_resolveTime * 1000);
609  }
610 }
611 
612 void Kanagram::slotRevealWord()
613 {
614  m_wordRevealed = true;
615  m_game->restoreWord();
616  update();
617 }
618 
620 void Kanagram::slotNextVocabulary()
621 {
622  m_game->nextVocabulary();
623  hideHint();
624  m_game->nextAnagram();
625 
626  if (m_useSounds)
627  {
628  play("chalk.ogg");
629  }
630 
631  KanagramSettings::setDefaultVocabulary(m_game->filename());
632  KanagramSettings::self()->writeConfig();
633  update();
634 }
635 
637 void Kanagram::slotPrevVocabulary()
638 {
639  m_game->previousVocabulary();
640  hideHint();
641  m_game->nextAnagram();
642 
643  if (m_useSounds)
644  {
645  play("chalk.ogg");
646  }
647 
648  KanagramSettings::setDefaultVocabulary(m_game->filename());
649  KanagramSettings::self()->writeConfig();
650  update();
651 }
652 
653 void Kanagram::slotToggleHint()
654 {
655  if (m_showHint)
656  {
657  m_showHint = false;
658  }
659  else
660  {
661  if (m_hintHideTime)
662  {
663  m_hintTimer->start(m_hintHideTime * 1000);
664  }
665  m_showHint = true;
666  randomHintImage();
667  }
668  update();
669 }
670 
671 void Kanagram::slotTogglePicHint()
672 {
673  if (m_showPicHint)
674  {
675  m_showPicHint = false;
676  }
677  else
678  {
679  if (m_hintHideTime)
680  {
681  m_hintTimer->start(m_hintHideTime * 1000);
682  }
683  m_showPicHint = true;
684  // randomHintImage();
685  }
686  update();
687 
688 
689 }
690 
691 void Kanagram::mousePressEvent(QMouseEvent *e)
692 {
693  if (m_showPicHint)
694  {
695  hidePicHint();
696  }
697 
698  if (m_nextRect.contains(e->pos()))
699  {
700  slotNextAnagram();
701  }
702 
703  if (m_configRect.contains(e->pos()))
704  {
705  slotShowSettings();
706  }
707 
708  if (m_quitRect.contains(e->pos()))
709  {
710  close();
711  }
712 
713  if (m_revealRect.contains(e->pos()))
714  {
715  slotRevealWord();
716  }
717 
718  if (m_logoRect.contains(e->pos()))
719  {
720  m_helpMenu->aboutApplication();
721  }
722 
723  if (!m_showHint && m_overHelp && !m_showPicHint)
724  {
725  if (m_handbookRect.contains(e->pos()))
726  {
727  m_helpMenu->appHelpActivated();
728  }
729 
730  if (m_aboutKDERect.contains(e->pos()))
731  {
732  m_helpMenu->aboutKDE();
733  }
734 
735  if (m_aboutAppRect.contains(e->pos()))
736  {
737  m_helpMenu->aboutApplication();
738  }
739  }
740 
741  if (m_hintBoxRect.contains(e->pos()))
742  {
743  hideHint();
744  }
745 
746  if (m_switcherRect.contains(e->pos()) || m_arrowRect.contains(e->pos()))
747  {
748  if (!(e->button() == Qt::RightButton))
749  {
750  slotNextVocabulary();
751  }
752  else
753  {
754  slotPrevVocabulary();
755  }
756  }
757 
758  if (m_hintRect.contains(e->pos()))
759  {
760  slotToggleHint();
761  }
762 
763  if (m_picHintRect.contains(e->pos()))
764  {
765  slotTogglePicHint();
766  }
767 
768  if (m_upRect.contains(e->pos()) && !m_inputBox->text().isEmpty())
769  {
770  checkWord();
771  }
772 }
773 
775 void Kanagram::CheckRect(const QRect &rect, const QPoint &p, bool &flag, bool &changed)
776 {
777  if (rect.contains(p))
778  {
779  changed = !flag;
780  flag = true;
781  }
782  else if (flag)
783  {
784  flag = false;
785  changed = true;
786  }
787 }
788 
789 void Kanagram::mouseMoveEvent(QMouseEvent *e)
790 {
791  QPoint p = e->pos();
792  bool haveToUpdate = false;
793 
794  CheckRect(m_nextRect, p, m_overNext, haveToUpdate);
795  CheckRect(m_configRect, p, m_overConfig, haveToUpdate);
796  CheckRect(m_logoRect, p, m_overLogo, haveToUpdate);
797  CheckRect(m_helpRect, p, m_overHelp, haveToUpdate);
798  CheckRect(m_quitRect, p, m_overQuit, haveToUpdate);
799  CheckRect(m_hintRect, p, m_overHint, haveToUpdate);
800  if (!m_game->picHint().isEmpty())
801  {
802  CheckRect(m_picHintRect, p, m_overPicHint, haveToUpdate);
803  }
804  CheckRect(m_hintBoxRect, p, m_overHintBox, haveToUpdate);
805  CheckRect(m_revealRect, p, m_overReveal, haveToUpdate);
806  CheckRect(m_upRect, p, m_overUp, haveToUpdate);
807  CheckRect(m_aboutAppRect, p, m_overAboutApp, haveToUpdate);
808 
809  if (m_switcherRect.contains(p) || m_arrowRect.contains(p))
810  {
811  haveToUpdate = !m_overSwitcher;
812  m_overSwitcher = true;
813  }
814  else if (m_overSwitcher)
815  {
816  m_overSwitcher = false;
817  haveToUpdate = true;
818  }
819 
820  if (!m_showHint ||!m_showPicHint)
821  {
822  CheckRect(m_handbookRect, p, m_overHandbook, haveToUpdate);
823  CheckRect(m_aboutKDERect, p, m_overAboutKDE, haveToUpdate);
824  }
825 
826  if (m_overAboutKDE || m_overHandbook || m_overSwitcher || m_overNext || m_overQuit
827  || m_overConfig || (m_overReveal && !m_wordRevealed) || m_overHint || m_overPicHint || (m_overUp && !m_inputBox->text().isEmpty())
828  || m_overAboutApp || m_overHintBox || m_overLogo)
829  {
830  this->setCursor(Qt::PointingHandCursor);
831  }
832  else
833  {
834  this->unsetCursor();
835  }
836 
837  if (haveToUpdate)
838  {
839  update();
840  }
841 }
842 
843 void Kanagram::drawTextNew(QPainter &p, const QString &text, int textAlign, int xMargin, int yMargin, const QRect &rect, bool highlight, int fontSize)
844 {
845  QRect r = innerRect(rect, xMargin, yMargin);
846  QFont font = KGlobalSettings::generalFont();
847  font.setPointSize(fontSize);
848  font.setBold(true);
849  p.setFont(font);
850 
851  const bool withMargin = false;
852  if (withMargin)
853  {
854  p.fillRect(r, m_fillColor);
855  p.setPen(QPen(Qt::black, 3));
856  p.drawRoundRect(r.left(), r.top(), r.width(), r.height(), 15, 15);
857  }
858 
859  if (highlight)
860  p.setPen(m_chalkHighlightColor);
861  else
862  p.setPen(m_chalkColor);
863  p.drawText(r, textAlign, text);
864 }
865 
866 void Kanagram::checkWord()
867 {
868  QPalette palette;
869  QString enteredWord = m_inputBox->text().toLower().trimmed();
870  QString word = m_game->word().toLower().trimmed();
871  if (!enteredWord.isEmpty())
872  {
873  if (enteredWord == word || stripAccents(enteredWord) == stripAccents(word))
874  {
875  if (m_useSounds) play("right.ogg");
876  palette.setColor(m_inputBox->backgroundRole(), QColor(0, 255, 0));
877  QTimer::singleShot(1000, this, SLOT(resetInputBox()));
878  m_inputBox->clear();
879  m_wordRevealed = false;
880  hideHint();
881  m_game->nextAnagram();
882  }
883  else
884  {
885  if (m_useSounds) play("wrong.ogg");
886  palette.setColor(m_inputBox->backgroundRole(), QColor(255, 0, 0));
887  QTimer::singleShot(1000, this, SLOT(resetInputBox()));
888  m_inputBox->clear();
889  }
890  m_inputBox->setPalette(palette);
891  update();
892  }
893 }
894 
895 QString Kanagram::stripAccents(const QString & original)
896 {
897  QString noAccents;
898  QString decomposed = original.normalized(QString::NormalizationForm_D);
899  for (int i = 0; i < decomposed.length(); ++i) {
900  if ( decomposed[i].category() != QChar::Mark_NonSpacing ) {
901  noAccents.append(decomposed[i]);
902  }
903  }
904  return noAccents;
905 }
906 
907 void Kanagram::randomHintImage()
908 {
909  unsigned long imageNum = m_randomImage.getLong(8);
910  m_hintOverlayName = "eyes" + QString::number(imageNum + 1);
911 }
912 
913 void Kanagram::slotShowSettings()
914 {
915  if (!KConfigDialog::showDialog("settings"))
916  {
917  m_configDialog = new KConfigDialog( this, "settings", KanagramSettings::self() );
918  //m_configDialog->setAttribute(Qt::WA_DeleteOnClose);
919  connect(m_configDialog, SIGNAL(finished()), this, SLOT(reloadSettings()));
920 
921  // add the main settings page
922  MainSettings * mainSettingsPage = new MainSettings( m_configDialog );
923  connect (mainSettingsPage, SIGNAL(settingsChanged()), this, SLOT(reloadSettings()));
924  m_configDialog->addPage(mainSettingsPage , i18nc("@title:group main settings page name", "General" ), "preferences-other" );
925 
926  // create and add the vocabsettings page
927  m_vocabSettings = new VocabSettings( m_configDialog );
928  m_configDialog->addPage(m_vocabSettings, i18n("Vocabularies"), "document-properties" );
929 
930  // now make and add the shortcuts page
931  m_shortcutsEditor = new KShortcutsEditor(m_actionCollection, m_configDialog);
932  m_configDialog->addPage(m_shortcutsEditor, i18n("Shortcuts"), "preferences-desktop-keyboard");
933  connect(m_configDialog, SIGNAL(accepted()), this, SLOT(slotSaveSettings()));
934  connect(m_configDialog, SIGNAL(rejected()), this, SLOT(slotSettingsCancelled()));
935  connect(m_shortcutsEditor, SIGNAL(keyChange()), this, SLOT(slotEnableApplyButton()));
936 
937  m_configDialog->setHelp("kanagram/index.html");
938  m_configDialog->resize(600, 500);
939  m_configDialog->show();
940  }
941 }
942 
943 void Kanagram::slotSaveSettings()
944 {
945  m_shortcutsEditor->save();
946 }
947 
948 void Kanagram::slotSettingsCancelled()
949 {
950  m_shortcutsEditor->undoChanges();
951 }
952 
953 void Kanagram::slotEnableApplyButton()
954 {
955  m_configDialog->enableButtonApply(true);
956 }
957 
958 void Kanagram::hideHint()
959 {
960  if (m_showHint == true )
961  {
962  m_showHint = false;
963  }
964  update();
965 }
966 
967 void Kanagram::hidePicHint()
968 {
969  if (m_showPicHint == true || !m_game->picHint().isEmpty())
970  {
971  m_showPicHint = false;
972  }
973  update();
974 }
975 
976 void Kanagram::resetInputBox()
977 {
978  m_inputBox->setPalette(QPalette());
979 }
980 
981 void Kanagram::refreshVocabularies()
982 {
983  if (m_game->refreshVocabularyList())
984  {
985  // vocab/word are no longer valid, so get new ones and hide the hint
986  m_game->nextVocabulary();
987  m_game->nextAnagram();
988  hideHint();
989 
990  if (m_useSounds)
991  {
992  play("chalk.ogg");
993  }
994 
995  // save the default vocab now that it's changed
996  KanagramSettings::setDefaultVocabulary(m_game->filename());
997  KanagramSettings::self()->writeConfig();
998  m_vocabSettings->refreshView();
999  }
1000 }
1001 
1002 void Kanagram::play(const QString &filename)
1003 {
1004  if (!filename.isEmpty())
1005  {
1006  QString soundFile = KStandardDirs::locate("appdata", "sounds/" + filename);
1007  if (!soundFile.isEmpty())
1008  {
1009  if (!m_player)
1010  {
1011  m_player = Phonon::createPlayer(Phonon::GameCategory, soundFile);
1012  }
1013  else
1014  {
1015  m_player->setCurrentSource(soundFile);
1016  }
1017  m_player->play();
1018  }
1019  }
1020 }
1021 
1022 void Kanagram::slotFileError(const QString &filename)
1023 {
1024  QString msg = i18n("File %1 cannot be found.\n Please ensure that Kanagram is properly installed.", filename);
1025  KMessageBox::sorry(this, msg, i18n("Error"));
1026  exit(0);
1027 }
1028 
1030 void Kanagram::slotChooseVocabulary()
1031 {
1032 }
1033 
1034 #include "kanagram.moc"
kanagramgame.h
Kanagram::Kanagram
Kanagram()
default constructor
Definition: kanagram.cpp:77
KanagramGame::picHint
KUrl picHint()
Get this anagram's picture hint URL.
Definition: kanagramgame.cpp:346
yScaleQuitButton
double yScaleQuitButton
Definition: kanagram.cpp:75
yScale57Buttons
double yScale57Buttons
Definition: kanagram.cpp:63
KanagramSettings::resolveTime
static QString resolveTime()
Get This setting allows you to set in seconds how much time is available for resolving the anagram...
Definition: kanagramsettings.h:70
KanagramGame::anagram
QString anagram() const
Get the anagram to show.
Definition: kanagramgame.cpp:232
KanagramGame::refreshVocabularyList
bool refreshVocabularyList()
Refresh the list of vocabulary files available from what we find on disk.
Definition: kanagramgame.cpp:105
kWindowWidth
double kWindowWidth
Definition: kanagram.cpp:56
KanagramGame::hint
Q_INVOKABLE QString hint() const
Get this anagram's hint.
Definition: kanagramgame.cpp:237
VocabSettings::refreshView
void refreshView()
reload the list of vocabularies from what's on disk
Definition: vocabsettings.cpp:53
KanagramGame::word
QString word() const
Get this anagram's answer.
Definition: kanagramgame.cpp:242
KanagramSettings::useSounds
static bool useSounds()
Get Turns sounds on/off.
Definition: kanagramsettings.h:89
Kanagram::sizeHint
virtual QSize sizeHint() const
sizehint method for initial size
Definition: kanagram.cpp:149
xTranslateButtons
double xTranslateButtons
Definition: kanagram.cpp:68
xEyesScale
double xEyesScale
Definition: kanagram.cpp:59
xScaleQuitButton
double xScaleQuitButton
Definition: kanagram.cpp:74
KanagramSettings::self
static KanagramSettings * self()
Definition: kanagramsettings.cpp:17
kanagramsettings.h
KMainWindow
KanagramGame::previousVocabulary
void previousVocabulary()
Use the previous vocabulary file in the list.
Definition: kanagramgame.cpp:146
m_textHint
static const char * m_textHint
Definition: kanagram.cpp:52
yScale55Buttons
double yScale55Buttons
Definition: kanagram.cpp:66
KanagramSettings::setDataLanguage
static void setDataLanguage(const QString &v)
Set Set the default translation.
Definition: kanagramsettings.h:117
m_textPicHint
static const char * m_textPicHint
Definition: kanagram.cpp:53
yEyesScale
double yEyesScale
Definition: kanagram.cpp:60
VocabSettings
Vocabulary Settings class.
Definition: vocabsettings.h:34
vocabsettings.h
m_textRevealWord
static const char * m_textRevealWord
Definition: kanagram.cpp:51
yTranslateConfigButton
double yTranslateConfigButton
Definition: kanagram.cpp:71
KanagramGame::nextVocabulary
void nextVocabulary()
Use the next vocabulary file in the list.
Definition: kanagramgame.cpp:162
KanagramGame
game api
Definition: kanagramgame.h:41
m_nextText
static const char * m_nextText
Definition: kanagram.cpp:54
kWindowHeight
double kWindowHeight
Definition: kanagram.cpp:57
KanagramGame::nextAnagram
void nextAnagram()
Set the index to the next word.
Definition: kanagramgame.cpp:181
Kanagram::~Kanagram
~Kanagram()
default destructor
Definition: kanagram.cpp:137
KanagramSettings::dataLanguage
static QString dataLanguage()
Get Set the default translation.
Definition: kanagramsettings.h:127
yTranslateNextButton
double yTranslateNextButton
Definition: kanagram.cpp:70
xScale57Buttons
double xScale57Buttons
Definition: kanagram.cpp:62
KanagramGame::restoreWord
void restoreWord()
Restore the word, set the anagram to the answer.
Definition: kanagramgame.cpp:247
KanagramGame::documentTitle
QString documentTitle() const
Get the current vocabulary file's title.
Definition: kanagramgame.cpp:271
KanagramGame::filename
QString filename() const
Get the current vocabulary file's filename.
Definition: kanagramgame.cpp:227
MainSettings
user preferences page of the config dialog allows choosing a hint hide time, the language, and the sound settings
Definition: mainsettings.h:33
kanagram.h
header file for kanagram class
yTranslateHelpButton
double yTranslateHelpButton
Definition: kanagram.cpp:72
xScale55Buttons
double xScale55Buttons
Definition: kanagram.cpp:65
mainsettings.h
KanagramSettings::setDefaultVocabulary
static void setDefaultVocabulary(const QString &v)
Set Set the default vocabulary.
Definition: kanagramsettings.h:98
KanagramSettings::hintHideTime
static QString hintHideTime()
Get This setting allows you to set in seconds how long Kanagram's hint bubble is shown.
Definition: kanagramsettings.h:51
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:34 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kanagram

Skip menu "kanagram"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

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