dmflagspanel.cpp
1 //------------------------------------------------------------------------------
2 // dmflagspanel.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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  foreach(const DMFlagsTabWidget* p, d->dmFlagsTabs)
70  {
71  DMFlagsSection sec(p->section.name());
72  for (int i = 0; i < p->section.count(); ++i)
73  {
74  if (p->checkBoxes[i]->isChecked())
75  {
76  sec.add(p->section[i]);
77  }
78  }
79  result << sec;
80  }
81  return result;
82 }
83 
84 bool DMFlagsPanel::initDMFlagsTabs(const EnginePlugin *engine)
85 {
86  removeDMFlagsTabs();
87 
88  if (engine->data()->createDMFlagsPagesAutomatic)
89  {
90  const QList<DMFlagsSection> &dmFlagsSections = engine->data()->allDMFlags;
91  if (dmFlagsSections.empty())
92  {
93  return false; // Nothing to do
94  }
95 
96  for (int i = 0; i < dmFlagsSections.count(); ++i)
97  {
98  DMFlagsTabWidget* dmftw = new DMFlagsTabWidget();
99 
100  QWidget* flagsTab = new QWidget(this);
101  dmftw->widget = flagsTab;
102  dmftw->section = dmFlagsSections[i];
103 
104  QHBoxLayout* hLayout = new QHBoxLayout(flagsTab);
105 
106  QVBoxLayout* layout = NULL;
107  for (int j = 0; j < dmFlagsSections[i].count(); ++j)
108  {
109  if ((j % 16) == 0)
110  {
111  if (layout != NULL)
112  {
113  layout->addStretch();
114  }
115 
116  layout = new QVBoxLayout();
117  hLayout->addLayout(layout);
118  }
119 
120  QCheckBox* checkBox = new QCheckBox();
121  checkBox->setText(dmFlagsSections[i][j].name());
122  dmftw->checkBoxes << checkBox;
123  layout->addWidget(checkBox);
124  }
125 
126  if (layout != NULL)
127  {
128  layout->addStretch();
129  }
130 
131  d->dmFlagsTabs << dmftw;
132  d->tabWidget->addTab(flagsTab, dmFlagsSections[i].name());
133  }
134  return true;
135  }
136  return false;
137 }
138 
139 void DMFlagsPanel::removeDMFlagsTabs()
140 {
141  foreach (DMFlagsTabWidget* flags, d->dmFlagsTabs)
142  {
143  int index = d->tabWidget->indexOf(flags->widget);
144  d->tabWidget->removeTab(index);
145  delete flags->widget;
146  delete flags;
147  }
148 
149  d->dmFlagsTabs.clear();
150 }
151 
152 void DMFlagsPanel::fillInParams(GameCreateParams &params)
153 {
154  params.dmFlags() = dmFlags();
155 }
156 
157 void DMFlagsPanel::loadConfig(Ini &config)
158 {
159  IniSection dmflags = config.section("DMFlags");
160  foreach(DMFlagsTabWidget* p, d->dmFlagsTabs)
161  {
162  for (int i = 0; i < p->section.count(); ++i)
163  {
164  p->checkBoxes[i]->setChecked(dmflags[p->section.name() + "/" + p->section[i].name()]);
165  }
166  }
167 }
168 
169 void DMFlagsPanel::saveConfig(Ini &config)
170 {
171  config.deleteSection("DMFlags");
172  IniSection dmflags = config.section("DMFlags");
173  foreach(DMFlagsTabWidget* p, d->dmFlagsTabs)
174  {
175  for (int i = 0; i < p->section.count(); ++i)
176  {
177  dmflags[p->section.name() + "/" + p->section[i].name()] = p->checkBoxes[i]->isChecked();
178  }
179  }
180 }
void add(const DMFlag &flag)
Append a new DMFlag to this section.
bool createDMFlagsPagesAutomatic
Controls behavior of "Create Game" dialog.
Definition: engineplugin.h:170
Game parametrization data used when creating new games.
A group of DMFlag objects that can be safely OR'ed together to form a meaningful value.
Definition: serverstructs.h:86
Configuration handler.
Definition: ini.h:69
INI section representation.
Definition: inisection.h:40
IniSection section(const QString &name)
Access configuration file section.
Definition: ini.cpp:91
void deleteSection(const QString &sectionname)
Definition: ini.cpp:60
QList< DMFlagsSection > allDMFlags
List of all engine's DM flags or NULL if none.
Definition: engineplugin.h:125