dmflagspanel.cpp
1 //------------------------------------------------------------------------------
2 // dmflagspanel.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "dmflagspanel.h"
24 #include "ui_dmflagspanel.h"
25 
26 #include "ini/ini.h"
27 #include "plugins/engineplugin.h"
28 #include "serverapi/gamecreateparams.h"
30 
31 #include <QCheckBox>
32 
33 class DMFlagsTabWidget
34 {
35 public:
36  QWidget *widget;
37  DMFlagsSection section;
38 
42  QList<QCheckBox *> checkBoxes;
43 };
44 
45 
46 DClass<DMFlagsPanel> : public Ui::DMFlagsPanel
47 {
48 public:
49  QList<DMFlagsTabWidget *> dmFlagsTabs;
50 };
51 
52 DPointered(DMFlagsPanel)
53 
54 
55 DMFlagsPanel::DMFlagsPanel(QWidget *parent)
56  : QWidget(parent)
57 {
58  d->setupUi(this);
59 }
60 
61 DMFlagsPanel::~DMFlagsPanel()
62 {
63  removeDMFlagsTabs();
64 }
65 
66 QList<DMFlagsSection> DMFlagsPanel::dmFlags() const
67 {
68  QList<DMFlagsSection> result;
69  for (const DMFlagsTabWidget *p : d->dmFlagsTabs)
70  {
71  DMFlagsSection sec = p->section.copyEmpty();
72  for (int i = 0; i < p->section.count(); ++i)
73  {
74  if (p->checkBoxes[i]->isChecked())
75  sec.add(p->section[i]);
76  }
77  result << sec;
78  }
79  return result;
80 }
81 
82 bool DMFlagsPanel::initDMFlagsTabs(const EnginePlugin *engine)
83 {
84  removeDMFlagsTabs();
85 
86  if (engine->data()->createDMFlagsPagesAutomatic)
87  {
88  QList<DMFlagsSection> dmFlagsSections = engine->dmFlags();
89  if (dmFlagsSections.empty())
90  return false; // Nothing to do
91 
92  for (int i = 0; i < dmFlagsSections.count(); ++i)
93  {
94  auto dmftw = new DMFlagsTabWidget();
95 
96  QWidget *flagsTab = new QWidget(this);
97  dmftw->widget = flagsTab;
98  dmftw->section = dmFlagsSections[i];
99 
100  auto hLayout = new QHBoxLayout(flagsTab);
101 
102  QVBoxLayout *layout = nullptr;
103  for (int j = 0; j < dmFlagsSections[i].count(); ++j)
104  {
105  if ((j % 16) == 0)
106  {
107  if (layout != nullptr)
108  layout->addStretch();
109 
110  layout = new QVBoxLayout();
111  hLayout->addLayout(layout);
112  }
113 
114  auto checkBox = new QCheckBox();
115  checkBox->setText(dmFlagsSections[i][j].name());
116  dmftw->checkBoxes << checkBox;
117  layout->addWidget(checkBox);
118  }
119 
120  if (layout != nullptr)
121  layout->addStretch();
122 
123  d->dmFlagsTabs << dmftw;
124  d->tabWidget->addTab(flagsTab, dmFlagsSections[i].name());
125  }
126  return true;
127  }
128  return false;
129 }
130 
131 void DMFlagsPanel::removeDMFlagsTabs()
132 {
133  for (DMFlagsTabWidget *flags : d->dmFlagsTabs)
134  {
135  int index = d->tabWidget->indexOf(flags->widget);
136  d->tabWidget->removeTab(index);
137  delete flags->widget;
138  delete flags;
139  }
140 
141  d->dmFlagsTabs.clear();
142 }
143 
144 void DMFlagsPanel::fillInParams(GameCreateParams &params)
145 {
146  params.dmFlags() = dmFlags();
147 }
148 
149 void DMFlagsPanel::loadConfig(Ini &config)
150 {
151  IniSection dmflags = config.section("DMFlags");
152  for (DMFlagsTabWidget *p : d->dmFlagsTabs)
153  {
154  const DMFlagsSection &section = p->section;
155  for (int i = 0; i < section.count(); ++i)
156  {
157  QString flagKey = section.internalName() + "/" + section[i].internalName();
158  if (!dmflags.hasSetting(flagKey))
159  {
160  // Fallback to support game setup configs
161  // from old Doomseeker versions.
162  flagKey = section.name() + "/" + section[i].name();
163  }
164  p->checkBoxes[i]->setChecked(dmflags[flagKey]);
165  }
166  }
167 }
168 
169 void DMFlagsPanel::saveConfig(Ini &config)
170 {
171  config.deleteSection("DMFlags");
172  IniSection dmflags = config.section("DMFlags");
173  for (DMFlagsTabWidget *p : d->dmFlagsTabs)
174  {
175  for (int i = 0; i < p->section.count(); ++i)
176  {
177  dmflags[p->section.internalName() + "/" + p->section[i].internalName()]
178  = p->checkBoxes[i]->isChecked();
179  }
180  }
181 }