File indexing completed on 2024-05-19 04:07:20

0001 /***************************************************************************
0002  *   Copyright 2007      Francesco Rossi <redsh@email.it>                  *
0003  *   Copyright 2006-2007 Mick Kappenburg <ksudoku@kappendburg.net>         *
0004  *   Copyright 2006-2008 Johannes Bergmeier <johannes.bergmeier@gmx.net>   *
0005  *   Copyright 2012      Ian Wadham <iandw.au@gmail.com>                   *
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or modify  *
0008  *   it under the terms of the GNU General Public License as published by  *
0009  *   the Free Software Foundation; either version 2 of the License, or     *
0010  *   (at your option) any later version.                                   *
0011  *                                                                         *
0012  *   This program is distributed in the hope that it will be useful,       *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0015  *   GNU General Public License for more details.                          *
0016  *                                                                         *
0017  *   You should have received a copy of the GNU General Public License     *
0018  *   along with this program; if not, write to the                         *
0019  *   Free Software Foundation, Inc.,                                       *
0020  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0021  ***************************************************************************/
0022 
0023 #include "ksview.h"
0024 
0025 #include "settings.h"
0026 
0027 
0028 
0029 #include "puzzle.h"
0030 
0031 #ifdef OPENGL_SUPPORT
0032 #include "roxdokuview.h"
0033 #endif
0034 
0035 #include "view2d.h"
0036 
0037 namespace ksudoku{
0038 
0039 KsView::KsView(const Game& game, GameActions* gameActions, QObject* parent)
0040     : QObject(parent), m_game(game), m_gameActions(gameActions), m_viewWidget(nullptr)
0041 {
0042     m_symbolTable = nullptr;
0043     m_currentValue = 1;
0044     m_valueListWidget = nullptr;
0045 }
0046 
0047 KsView::~KsView()
0048 {
0049     delete m_viewWidget;
0050 }
0051 
0052 void KsView::createView() {
0053     GameType type = m_game.puzzle()->gameType();
0054     switch(type) {
0055         case TypeSudoku: {
0056             setWidget(new View2D(nullptr, m_game, m_gameActions));
0057             break;
0058         }
0059         case TypeCustom: {
0060 #ifdef OPENGL_SUPPORT
0061             if(m_game.puzzle()->graph()->sizeZ() > 1) {
0062                 // TODO - IDW - Add a parent widget. Memory leak?
0063                 setWidget(new RoxdokuView(m_game, m_gameActions, nullptr));
0064             }
0065             else {
0066                 setWidget(new View2D(nullptr, m_game, m_gameActions));
0067             }
0068 #else
0069             setWidget(new View2D(0, m_game, m_gameActions));
0070 #endif
0071             break;
0072         }
0073 #ifdef OPENGL_SUPPORT
0074         case TypeRoxdoku: {
0075             // TODO - IDW - Add a parent widget. Memory leak?
0076             setWidget(new RoxdokuView(m_game, m_gameActions, nullptr));
0077             break;
0078         }
0079 #endif
0080         default:
0081             // TODO this will not work
0082             break;
0083     }
0084     
0085     QObject* view = m_view->widget();
0086     connect(view, SIGNAL(valueSelected(int)), SLOT(selectValue(int)));
0087     
0088     connect(this, SIGNAL(valueSelected(int)), view, SLOT(selectValue(int)));
0089 
0090     connect(parent(), SIGNAL(settingsChanged()), view, SLOT(settingsChanged()));
0091     
0092     settingsChanged();
0093 }
0094 
0095 void KsView::selectValue(int value) {
0096     if(value == m_currentValue) return;
0097     m_currentValue = value;
0098     Q_EMIT valueSelected(value);
0099 }
0100 
0101 SymbolTable* KsView::symbolTable() const {
0102     return m_symbolTable;
0103 }
0104 
0105 void KsView::setSymbolTable(SymbolTable* table) {
0106     m_symbolTable = table;
0107     Q_EMIT symbolsChanged(table);
0108 }
0109 
0110 void KsView::setWidget(QWidget* widget) {
0111     m_viewWidget = widget;
0112     Q_EMIT flagsChanged(m_flags);
0113     Q_EMIT symbolsChanged(m_symbolTable);
0114     
0115     m_view = dynamic_cast<ViewInterface*>(widget);
0116 }
0117 
0118 void KsView::settingsChanged() {
0119     ViewFlags flags;
0120     if(Settings::showErrors()) flags |= ShowErrors;
0121     if(Settings::showHighlights()) flags |= ShowHighlights;
0122     setFlags(flags);
0123 }
0124 
0125 }
0126 
0127 #include "moc_ksview.cpp"