File indexing completed on 2024-04-14 03:59:52
0001 /* 0002 SPDX-FileCopyrightText: 2007 Mauricio Piacentini <mauricio@tabuleiro.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 // own 0008 #include "kmahjongglayoutselector.h" 0009 0010 // Qt 0011 #include <QDir> 0012 0013 // KMahjongg 0014 #include "gamedata.h" 0015 #include "gamescene.h" 0016 #include "gameview.h" 0017 #include "kmahjongglayout.h" 0018 #include "prefs.h" 0019 0020 KMahjonggLayoutSelector::KMahjonggLayoutSelector(QWidget * parent, KConfigSkeleton * aconfig) 0021 : QWidget(parent) 0022 , m_gameData(nullptr) 0023 { 0024 setupUi(this); 0025 m_gameScene = new GameScene(this); 0026 m_gameView = new GameView(m_gameScene, nullptr, layoutPreview); 0027 m_gameView->resize(layoutPreview->size()); 0028 m_gameView->setInteractive(false); 0029 setupData(aconfig); 0030 } 0031 0032 KMahjonggLayoutSelector::~KMahjonggLayoutSelector() 0033 { 0034 qDeleteAll(layoutMap); 0035 } 0036 0037 void KMahjonggLayoutSelector::setupData(KConfigSkeleton * aconfig) 0038 { 0039 //Get our currently configured Layout entry 0040 KConfig * config = aconfig->config(); 0041 KConfigGroup group = config->group(QStringLiteral("General")); 0042 QString initialGroup = group.readEntry("Layout_file"); 0043 0044 //The lineEdit widget holds our layout path, but the user does not manipulate it directly 0045 kcfg_Layout->hide(); 0046 0047 //No new stuff yet 0048 getNewButton->hide(); 0049 0050 //Now get our layouts into a list 0051 QStringList layoutsAvailable; 0052 0053 const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, QStringLiteral("layouts"), QStandardPaths::LocateDirectory); 0054 for (const QString & dir : dirs) { 0055 const QStringList fileNames = QDir(dir).entryList({QStringLiteral("*.desktop")}); 0056 layoutsAvailable.reserve(layoutsAvailable.size() + fileNames.size()); 0057 for (const QString & file : fileNames) { 0058 layoutsAvailable.append(dir + QLatin1Char('/') + file); 0059 } 0060 } 0061 layoutsAvailable.sort(); 0062 0063 int numvalidentries = 0; 0064 for (const QString &layoutPath : std::as_const(layoutsAvailable)) { 0065 auto *alayout = new KMahjonggLayout(); 0066 if (alayout->load(layoutPath)) { 0067 const QString name = alayout->name(); 0068 layoutMap.insert(name, alayout); 0069 layoutList->addItem(name); 0070 //Find if this is our currently configured layout 0071 if (layoutPath == initialGroup) { 0072 //Select current entry 0073 layoutList->setCurrentRow(numvalidentries); 0074 layoutChanged(); 0075 } 0076 ++numvalidentries; 0077 } else { 0078 delete alayout; 0079 } 0080 } 0081 0082 connect(layoutList, &QListWidget::currentItemChanged, this, &KMahjonggLayoutSelector::layoutChanged); 0083 } 0084 0085 void KMahjonggLayoutSelector::layoutChanged() 0086 { 0087 KMahjonggLayout * selLayout = layoutMap.value(layoutList->currentItem()->text()); 0088 //Sanity checkings. Should not happen. 0089 if (!selLayout) { 0090 return; 0091 } 0092 if (selLayout->path() == kcfg_Layout->text()) { 0093 return; 0094 } 0095 0096 kcfg_Layout->setText(selLayout->path()); 0097 layoutAuthor->setText(selLayout->authorName()); 0098 layoutContact->setText(selLayout->authorEmailAddress()); 0099 layoutDescription->setText(selLayout->description()); 0100 0101 //If settings for tiles/background have been applied, update our preview 0102 if (m_gameView->getTilesetPath() != Prefs::tileSet()) { 0103 m_gameView->setTilesetPath(Prefs::tileSet()); 0104 } 0105 if (m_gameView->getBackgroundPath() != Prefs::background()) { 0106 m_gameView->setBackgroundPath(Prefs::background()); 0107 } 0108 0109 //Now load the boardLayout 0110 delete m_gameData; 0111 m_gameData = new GameData(selLayout->board()); 0112 m_gameView->setGameData(m_gameData); 0113 0114 m_gameView->createNewGame(); 0115 } 0116 0117 void KMahjonggLayoutSelector::useRandomLayoutToggled(bool active) 0118 { 0119 widgetNoRandom->setEnabled(!active); 0120 m_gameView->setVisible(!active); 0121 } 0122 0123 #include "moc_kmahjongglayoutselector.cpp"