File indexing completed on 2024-05-12 04:06:27

0001 /*
0002     SPDX-FileCopyrightText: 2009-2011 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "mainwindow.h"
0008 
0009 #include "../engine/gameplay.h"
0010 #include "palapeli_debug.h"
0011 
0012 #include "puzzletablewidget.h"
0013 #include "../file-io/collection-view.h"
0014 #include "settings.h"
0015 #include <QAction>
0016 
0017 #include <KActionCollection>
0018 #include <KLocalizedString>
0019 #include <KGameStandardAction>
0020 #include <KStandardAction>
0021 #include <KStandardShortcut>
0022 #include <KToggleAction>
0023 #include <KMessageBox>
0024 
0025 Palapeli::MainWindow::MainWindow(const QString &path)
0026     : m_game(new Palapeli::GamePlay(this))
0027 {
0028     setupActions();
0029     //setup GUI
0030     KXmlGuiWindow::StandardWindowOptions guiOptions = KXmlGuiWindow::Default;
0031     guiOptions &= ~KXmlGuiWindow::StatusBar; //no statusbar
0032     setupGUI(QSize(500, 500), guiOptions);
0033     m_game->init();
0034     //start a puzzle if a puzzle URL has been given
0035     if (!path.isEmpty())
0036         m_game->playPuzzleFile(path);
0037 }
0038 
0039 bool Palapeli::MainWindow::queryClose()
0040 {
0041     // Terminate cleanly if the user Quits when playing a puzzle.
0042     m_game->shutdown();
0043     return true;
0044 }
0045 
0046 void Palapeli::MainWindow::setupActions()
0047 {
0048     // Standard stuff.
0049     KStandardAction::preferences(m_game, &GamePlay::configure,
0050                         actionCollection());
0051     QAction * statusBarAct = KStandardAction::showStatusbar
0052     (m_game->puzzleTable(), &PuzzleTableWidget::showStatusBar, actionCollection());
0053     statusBarAct->setChecked(Settings::showStatusBar());
0054     statusBarAct->setText(i18n("Show Statusbar of Puzzle Table"));
0055 
0056     // Back to collection.
0057     QAction * goCollAct = new QAction(QIcon::fromTheme(QStringLiteral("go-previous")), i18n("Back to &Collection"), this);
0058     goCollAct->setToolTip(i18n("Go back to the collection to choose another puzzle"));
0059     goCollAct->setEnabled(false); //because the collection is initially shown
0060     actionCollection()->addAction(QStringLiteral("view_collection"), goCollAct);
0061     connect(goCollAct, &QAction::triggered, m_game, &GamePlay::actionGoCollection);
0062 
0063     // Create new puzzle (FIXME: action should have a custom icon).
0064     QAction * createAct = new QAction(QIcon::fromTheme(QStringLiteral("tools-wizard")), i18n("Create &New Puzzle..."), this);
0065     createAct->setToolTip(i18n("Create a new puzzle using an image file from your disk"));
0066     KActionCollection::setDefaultShortcuts(createAct, KStandardShortcut::openNew());
0067     actionCollection()->addAction(QStringLiteral("game_new"), createAct);
0068     connect(createAct, &QAction::triggered, m_game, &GamePlay::actionCreate);
0069 
0070     // Delete a puzzle.
0071     QAction * deleteAct = new QAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("&Delete Puzzle"), this);
0072     deleteAct->setEnabled(false); //will be enabled when something is selected
0073     deleteAct->setToolTip(i18n("Delete the selected puzzle from your collection"));
0074     actionCollection()->addAction(QStringLiteral("game_delete"), deleteAct);
0075     connect(m_game->collectionView(), &CollectionView::canDeleteChanged, deleteAct, &QAction::setEnabled);
0076     connect(deleteAct, &QAction::triggered, m_game, &GamePlay::actionDelete);
0077 
0078     // Import from file...
0079     QAction * importAct = new QAction(QIcon::fromTheme(QStringLiteral("document-import")), i18n("&Import from File..."), this);
0080     importAct->setToolTip(i18n("Import a new puzzle from a file into your collection"));
0081     actionCollection()->addAction(QStringLiteral("game_import"), importAct);
0082     connect(importAct, &QAction::triggered, m_game, &GamePlay::actionImport);
0083 
0084     // Export to file...
0085     QAction * exportAct = new QAction(QIcon::fromTheme(QStringLiteral("document-export")), i18n("&Export to File..."), this);
0086     exportAct->setEnabled(false); //will be enabled when something is selected
0087     exportAct->setToolTip(i18n("Export the selected puzzle from your collection into a file"));
0088     actionCollection()->addAction(QStringLiteral("game_export"), exportAct);
0089     connect(m_game->collectionView(), &CollectionView::canExportChanged, exportAct, &QAction::setEnabled);
0090     connect(exportAct, &QAction::triggered, m_game, &GamePlay::actionExport);
0091 
0092     //Reshuffle and restart puzzle
0093     QAction * restartPuzzleAct = new QAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("&Restart Puzzle..."), this);
0094     restartPuzzleAct->setToolTip(i18n("Delete the saved progress and reshuffle the pieces"));
0095     restartPuzzleAct->setEnabled(false); //no puzzle in progress initially
0096     actionCollection()->addAction(QStringLiteral("game_restart"), restartPuzzleAct);
0097     connect(restartPuzzleAct, &QAction::triggered, m_game, &GamePlay::restartPuzzle);
0098     // Quit.
0099     KGameStandardAction::quit (this, &QWidget::close, actionCollection());
0100     // Create piece-holder.
0101     QAction * createHolderAct = new QAction(i18n("&Create Piece Holder..."), this);
0102     createHolderAct->setToolTip(i18n("Create a temporary holder for sorting pieces"));
0103     KActionCollection::setDefaultShortcut(createHolderAct, QKeySequence(Qt::Key_C));
0104     actionCollection()->addAction(QStringLiteral("move_create_holder"), createHolderAct);
0105         connect(createHolderAct, &QAction::triggered, m_game,
0106                 qOverload<>(&GamePlay::createHolder));
0107 
0108         // Delete piece-holder.
0109     QAction * deleteHolderAct = new QAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("&Delete Piece Holder"), this);
0110     deleteHolderAct->setToolTip(i18n("Delete a selected temporary holder when it is empty"));
0111     KActionCollection::setDefaultShortcut(deleteHolderAct, QKeySequence(Qt::Key_D));
0112     actionCollection()->addAction(QStringLiteral("move_delete_holder"), deleteHolderAct);
0113     connect(deleteHolderAct, &QAction::triggered, m_game, &GamePlay::deleteHolder);
0114 
0115     // Select all pieces in a piece-holder.
0116     QAction * selectAllAct = new QAction(QIcon::fromTheme(QStringLiteral("edit-select-all")), i18n("&Select All in Holder"), this);
0117     selectAllAct->setToolTip(i18n("Select all pieces in a selected piece holder"));
0118     KActionCollection::setDefaultShortcut(selectAllAct, QKeySequence(Qt::Key_A));
0119     actionCollection()->addAction(QStringLiteral("move_select_all"), selectAllAct);
0120     connect(selectAllAct, &QAction::triggered, m_game, &GamePlay::selectAll);
0121 
0122     // Rearrange a selected piece-holder or selected pieces in any view.
0123     QAction * rearrangeAct = new QAction(i18n("&Rearrange Pieces"), this);
0124     rearrangeAct->setToolTip(i18n("Rearrange all pieces in a selected piece holder or selected pieces in any window"));
0125     KActionCollection::setDefaultShortcut(rearrangeAct, QKeySequence(Qt::Key_R));
0126     actionCollection()->addAction(QStringLiteral("move_rearrange"), rearrangeAct);
0127     connect(rearrangeAct, &QAction::triggered, m_game, &GamePlay::rearrangePieces);
0128 
0129     // Toggle puzzle-preview.
0130     bool  isVisible = Settings::puzzlePreviewVisible();
0131     const QString text = i18nc("Preview is a noun here", "&Preview");
0132     KToggleAction* togglePreviewAct = new KToggleAction(QIcon::fromTheme(QStringLiteral("view-preview")), text, this);
0133     togglePreviewAct->setIconText(i18nc("Preview is a noun here", "Preview"));
0134     togglePreviewAct->setToolTip(i18n("Show or hide the image of the completed puzzle"));
0135     actionCollection()->addAction(QStringLiteral("view_preview"), togglePreviewAct);
0136     togglePreviewAct->setEnabled(false);
0137     togglePreviewAct->setChecked(isVisible);
0138     connect(togglePreviewAct, &QAction::triggered, m_game, &GamePlay::actionTogglePreview);
0139 
0140     // View zoom in.
0141     KStandardAction::zoomIn(m_game, &GamePlay::actionZoomIn,
0142                         actionCollection());
0143 
0144     // View zoom out.
0145     KStandardAction::zoomOut(m_game, &GamePlay::actionZoomOut,
0146                         actionCollection());
0147     // Settings: enable messages that the user marked "Do not show again".
0148     QAction * enableMessagesAct = new QAction(i18n("Enable All Messages"), this);
0149     actionCollection()->addAction(QStringLiteral("enable_messages"), enableMessagesAct);
0150     connect(enableMessagesAct, &QAction::triggered, this, &MainWindow::enableMessages);
0151 }
0152 
0153 void Palapeli::MainWindow::enableMessages()
0154 {
0155     // Enable all messages that the user has marked "Do not show again".
0156     int result = KMessageBox::questionTwoActions(this,
0157                         i18n("Enable all messages again?"), {},
0158                         KGuiItem(i18nc("@action:button", "Enable"), QStringLiteral("dialog-ok")),
0159                         KStandardGuiItem::cancel());
0160     if (result == KMessageBox::PrimaryAction) {
0161         qCDebug(PALAPELI_LOG) << "ENABLE ALL MESSAGES";
0162         KMessageBox::enableAllMessages();
0163         KSharedConfig::openConfig()->sync();    // Save the changes to disk.
0164     }
0165 }
0166 
0167 #include "moc_mainwindow.cpp"