1 /***************************************************************************
2  *   This file is part of the Kanagram project                             *
3  *   Copyright 2011 Sebastian Kügler <sebas@kde.org>                       *
4  *   Copyright 2011 Marco Martin <mart@kde.org>                            *
5  *   Copyright 2012 Laszlo Papp <lpapp@kde.org>                            *
6  *   Copyright 2014 Jeremy Whiting <jpwhiting@kde.org>                     *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
22  ***************************************************************************/
23 
24 #include "mainwindow.h"
25 
26 #include "kanagramgame.h"
27 #include "kanagramsettings.h"
28 #include "kanagramconfigdialog.h"
29 
30 #include <QQmlContext>
31 #include <QQmlEngine>
32 
33 #include <KHelpMenu>
34 #include <KLocalizedContext>
35 #include <KLocalizedString>
36 #include <KSharedConfig>
37 
38 #include <QDebug>
39 
MainWindow()40 MainWindow::MainWindow()
41     : m_game(new KanagramGame())
42      ,m_helpMenu(new KHelpMenu(nullptr))
43 {
44     setResizeMode(QQuickView::SizeRootObjectToView);
45 
46     qCDebug(KANAGRAM) << "Created game and engine helper";
47     rootContext()->setContextProperty(QStringLiteral("kanagramGame"), m_game);
48     rootContext()->setContextProperty(QStringLiteral("application"), qApp);
49     rootContext()->setContextProperty(QStringLiteral("mainwindow"), this);
50 
51     // prepare i18n
52     auto context = new KLocalizedContext(this);
53     engine()->rootContext()->setContextObject(context);
54 
55     KConfigGroup windowConfig = config(QStringLiteral("Window"));
56     if (windowConfig.hasKey("geometry")) {
57         setGeometry(windowConfig.readEntry<QRect>("geometry", QRect()));
58         setWindowState(Qt::WindowState(windowConfig.readEntry("windowState").toInt()));
59     }
60 
61     QString location = QStandardPaths::locate(QStandardPaths::DataLocation, QStringLiteral("ui/main.qml"));
62     setSource(QUrl::fromLocalFile(location));
63     qCDebug(KANAGRAM) << "Set qml file location";
64 
65     connect(m_game, &KanagramGame::titleChanged, this, &MainWindow::categoryChanged);
66     categoryChanged();
67 }
68 
~MainWindow()69 MainWindow::~MainWindow()
70 {
71     KConfigGroup windowConfig = config(QStringLiteral("Window"));
72     windowConfig.writeEntry("geometry", geometry());
73     windowConfig.writeEntry("windowState", int(windowState()));
74 
75     delete m_helpMenu;
76     delete m_game;
77 }
78 
config(const QString & group)79 KConfigGroup MainWindow::config(const QString &group)
80 {
81     return KConfigGroup(KSharedConfig::openConfig(qApp->applicationName() + "rc"), group);
82 }
83 
showAboutKanagram()84 void MainWindow::showAboutKanagram()
85 {
86     m_helpMenu->aboutApplication();
87 }
88 
showAboutKDE()89 void MainWindow::showAboutKDE()
90 {
91     m_helpMenu->aboutKDE();
92 }
93 
showHandbook()94 void MainWindow::showHandbook()
95 {
96     m_helpMenu->appHelpActivated();
97 }
98 
showSettings()99 void MainWindow::showSettings()
100 {
101     if (!KConfigDialog::showDialog(QStringLiteral("settings")))
102     {
103         m_configDialog = new KanagramConfigDialog( NULL, QStringLiteral("settings"), KanagramSettings::self() );
104         connect(m_configDialog, &KConfigDialog::settingsChanged, m_game, &KanagramGame::reloadSettings);
105 
106         connect(m_configDialog, &KConfigDialog::accepted, m_game, &KanagramGame::refreshVocabularyList);
107 
108         m_configDialog->resize(600, 500);
109         m_configDialog->show();
110     }
111 }
112 
categoryChanged()113 void MainWindow::categoryChanged()
114 {
115     setTitle(m_game->documentTitle());
116 }
117 
118