File indexing completed on 2024-04-28 07:29:04

0001 /***************************************************************************
0002  *   This file is part of the Kanagram project                             *
0003  *   Copyright 2011 Sebastian Kügler <sebas@kde.org>                       *
0004  *   Copyright 2011 Marco Martin <mart@kde.org>                            *
0005  *   Copyright 2012 Laszlo Papp <lpapp@kde.org>                            *
0006  *   Copyright 2014 Jeremy Whiting <jpwhiting@kde.org>                     *
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or modify  *
0009  *   it under the terms of the GNU General Public License as published by  *
0010  *   the Free Software Foundation; either version 2 of the License, or     *
0011  *   (at your option) any later version.                                   *
0012  *                                                                         *
0013  *   This program is distributed in the hope that it will be useful,       *
0014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0016  *   GNU General Public License for more details.                          *
0017  *                                                                         *
0018  *   You should have received a copy of the GNU General Public License     *
0019  *   along with this program; if not, write to the                         *
0020  *   Free Software Foundation, Inc.,                                       *
0021  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0022  ***************************************************************************/
0023 
0024 #include "mainwindow.h"
0025 
0026 #include "kanagramgame.h"
0027 #include "kanagramsettings.h"
0028 #include "kanagramconfigdialog.h"
0029 
0030 #include <QQmlContext>
0031 #include <QQmlEngine>
0032 
0033 #include <KHelpMenu>
0034 #include <KLocalizedContext>
0035 #include <KLocalizedString>
0036 #include <KSharedConfig>
0037 
0038 #include <QDebug>
0039 
0040 MainWindow::MainWindow()
0041     : m_game(new KanagramGame())
0042      ,m_helpMenu(new KHelpMenu(nullptr))
0043 {
0044     setResizeMode(QQuickView::SizeRootObjectToView);
0045 
0046     qCDebug(KANAGRAM) << "Created game and engine helper";
0047     rootContext()->setContextProperty(QStringLiteral("kanagramGame"), m_game);
0048     rootContext()->setContextProperty(QStringLiteral("application"), qApp);
0049     rootContext()->setContextProperty(QStringLiteral("mainwindow"), this);
0050 
0051     // prepare i18n
0052     auto context = new KLocalizedContext(this);
0053     engine()->rootContext()->setContextObject(context);
0054 
0055     KConfigGroup windowConfig = config(QStringLiteral("Window"));
0056     if (windowConfig.hasKey("geometry")) {
0057         setGeometry(windowConfig.readEntry<QRect>("geometry", QRect()));
0058         setWindowState(Qt::WindowState(windowConfig.readEntry("windowState").toInt()));
0059     }
0060 
0061     QString location = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("ui/main.qml"));
0062     setSource(QUrl::fromLocalFile(location));
0063     qCDebug(KANAGRAM) << "Set qml file location";
0064 
0065     connect(m_game, &KanagramGame::titleChanged, this, &MainWindow::categoryChanged);
0066     categoryChanged();
0067 }
0068 
0069 MainWindow::~MainWindow()
0070 {
0071     KConfigGroup windowConfig = config(QStringLiteral("Window"));
0072     windowConfig.writeEntry("geometry", geometry());
0073     windowConfig.writeEntry("windowState", int(windowState()));
0074 
0075     delete m_helpMenu;
0076     delete m_game;
0077 }
0078 
0079 KConfigGroup MainWindow::config(const QString &group)
0080 {
0081     return KConfigGroup(KSharedConfig::openConfig(qApp->applicationName() + "rc"), group);
0082 }
0083 
0084 void MainWindow::showAboutKanagram()
0085 {
0086     m_helpMenu->aboutApplication();
0087 }
0088 
0089 void MainWindow::showAboutKDE()
0090 {
0091     m_helpMenu->aboutKDE();
0092 }
0093 
0094 void MainWindow::showHandbook()
0095 {
0096     m_helpMenu->appHelpActivated();
0097 }
0098 
0099 void MainWindow::showSettings()
0100 {
0101     if (!KConfigDialog::showDialog(QStringLiteral("settings")))
0102     {
0103         m_configDialog = new KanagramConfigDialog( nullptr, QStringLiteral("settings"), KanagramSettings::self() );
0104         connect(m_configDialog, &KConfigDialog::settingsChanged, m_game, &KanagramGame::reloadSettings);
0105 
0106         connect(m_configDialog, &KConfigDialog::accepted, m_game, &KanagramGame::refreshVocabularyList);
0107 
0108         m_configDialog->resize(600, 500);
0109         m_configDialog->show();
0110     }
0111 }
0112 
0113 void MainWindow::categoryChanged()
0114 {
0115     setTitle(m_game->documentTitle());
0116 }
0117 
0118 #include "moc_mainwindow.cpp"