27 C4ConsoleQtDefinitionFileListModel::DefFileInfo::DefFileInfo(C4ConsoleQtDefinitionFileListModel::DefFileInfo *parent,
const char *filename,
const char *root_path)
28 : parent(parent), filename(filename), root_path(root_path), was_opened(false), is_root(false), user_selected(parent->IsUserSelected()), force_selected(parent->IsForceSelected())
32 if (parent->full_filename.getLength())
33 full_filename = parent->full_filename + R
"(\)" + filename;
35 full_filename = filename;
38 C4ConsoleQtDefinitionFileListModel::DefFileInfo::DefFileInfo()
42 DefFileInfo *main_objects_def =
new DefFileInfo(
this,
C4CFN_Objects,
"");
43 children.emplace_back(main_objects_def);
44 bool has_default_objects_found =
false;
45 for (
auto & root_iter : ::
Reloc)
47 const char *root = root_iter.strBuf.getData();
54 if (!has_default_objects_found && !strcmp(
C4CFN_Objects, def_file))
56 main_objects_def->root_path.Copy(root);
61 for (
auto & child : children)
62 if (!strcmp(child->GetName(), def_file))
67 children.emplace_back(
new DefFileInfo(
this, def_file, root));
73 void C4ConsoleQtDefinitionFileListModel::DefFileInfo::SetSelected(
bool to_val,
bool forced)
76 force_selected = to_val;
78 user_selected = to_val;
80 for (
auto & child : children)
82 child->SetSelected(to_val, forced);
86 bool C4ConsoleQtDefinitionFileListModel::DefFileInfo::OpenGroup()
96 if (!grp.OpenAsChild(&parent->grp, filename.getData()))
return false;
103 children.emplace_back(
new DefFileInfo(
this, child_filename.
getData(),
nullptr));
107 int32_t C4ConsoleQtDefinitionFileListModel::DefFileInfo::GetChildCount()
109 if (!was_opened) OpenGroup();
110 return children.size();
113 C4ConsoleQtDefinitionFileListModel::DefFileInfo *C4ConsoleQtDefinitionFileListModel::DefFileInfo::GetChild(int32_t index)
115 if (!was_opened) OpenGroup();
116 if (index >= children.size())
return nullptr;
117 return children[index].get();
120 int32_t C4ConsoleQtDefinitionFileListModel::DefFileInfo::GetChildIndex(
const DefFileInfo *child)
122 auto iter = std::find_if(children.begin(), children.end(),
123 [child](std::unique_ptr<DefFileInfo> & item)->bool { return item.get() == child; });
124 if (iter == children.end())
return -1;
125 return int32_t(iter - children.begin());
128 void C4ConsoleQtDefinitionFileListModel::DefFileInfo::AddUserSelectedDefinitions(std::list<const char *> *result)
const
133 if (!IsForceSelected())
135 if (IsUserSelected())
136 result->push_back(full_filename.getData());
138 for (
auto &iter : children) iter->AddUserSelectedDefinitions(result);
142 void C4ConsoleQtDefinitionFileListModel::DefFileInfo::AddSelectedDefinitions(std::list<const char *> *result)
const
146 result->push_back(full_filename.getData());
148 for (
auto &iter : children) iter->AddSelectedDefinitions(result);
151 void C4ConsoleQtDefinitionFileListModel::DefFileInfo::SetForcedSelection(
const char *selected_def_filepath)
154 if (
SEqualNoCase(selected_def_filepath, full_filename.getData()))
157 SetSelected(
true,
true);
159 else if (is_root || (
SEqual2NoCase(selected_def_filepath, full_filename.getData()) && selected_def_filepath[full_filename.getLength()] ==
'\\'))
162 if (!was_opened) OpenGroup();
163 for (
auto &iter : children) iter->SetForcedSelection(selected_def_filepath);
167 void C4ConsoleQtDefinitionFileListModel::DefFileInfo::AddExtraDef(
const char *def)
174 for (
auto &iter : children)
186 C4ConsoleQtDefinitionFileListModel::C4ConsoleQtDefinitionFileListModel() =
default;
188 C4ConsoleQtDefinitionFileListModel::~C4ConsoleQtDefinitionFileListModel() =
default;
190 void C4ConsoleQtDefinitionFileListModel::AddExtraDef(
const char *def)
192 root.AddExtraDef(def);
195 std::list<const char *> C4ConsoleQtDefinitionFileListModel::GetUserSelectedDefinitions()
const
197 std::list<const char *> result;
198 root.AddUserSelectedDefinitions(&result);
202 std::list<const char *> C4ConsoleQtDefinitionFileListModel::GetSelectedDefinitions()
const
204 std::list<const char *> result;
205 root.AddSelectedDefinitions(&result);
209 void C4ConsoleQtDefinitionFileListModel::SetForcedSelection(
const std::list<const char *> &defs)
213 root.SetSelected(
false,
true);
215 for (
const char *def : defs)
217 root.SetForcedSelection(def);
222 int C4ConsoleQtDefinitionFileListModel::rowCount(
const QModelIndex & parent)
const
224 if (!parent.isValid())
return root.GetChildCount();
225 DefFileInfo *parent_def =
static_cast<DefFileInfo *
>(parent.internalPointer());
226 if (!parent_def)
return 0;
227 return parent_def->GetChildCount();
230 int C4ConsoleQtDefinitionFileListModel::columnCount(
const QModelIndex & parent)
const
235 QVariant C4ConsoleQtDefinitionFileListModel::data(
const QModelIndex & index,
int role)
const
237 DefFileInfo *def =
static_cast<DefFileInfo *
>(index.internalPointer());
238 if (!def)
return QVariant();
240 if (role == Qt::DisplayRole)
242 return QString(def->GetName());
244 else if (role == Qt::CheckStateRole)
246 return def->IsSelected() ? Qt::Checked : Qt::Unchecked;
252 QModelIndex C4ConsoleQtDefinitionFileListModel::index(
int row,
int column,
const QModelIndex &parent)
const
254 if (column)
return QModelIndex();
255 DefFileInfo *parent_def = &root;
256 if (parent.isValid()) parent_def =
static_cast<DefFileInfo *
>(parent.internalPointer());
257 if (!parent_def)
return QModelIndex();
258 return createIndex(row, column, parent_def->GetChild(row));
261 QModelIndex C4ConsoleQtDefinitionFileListModel::parent(
const QModelIndex &index)
const
263 DefFileInfo *def =
static_cast<DefFileInfo *
>(index.internalPointer());
264 if (!def)
return QModelIndex();
265 DefFileInfo *parent_def = def->GetParent();
266 if (!parent_def)
return QModelIndex();
267 int32_t def_index = parent_def->GetChildIndex(def);
268 if (def_index < 0)
return QModelIndex();
269 return createIndex(def_index, 0, parent_def);
272 Qt::ItemFlags C4ConsoleQtDefinitionFileListModel::flags(
const QModelIndex &index)
const
274 Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
275 DefFileInfo *def =
static_cast<DefFileInfo *
>(index.internalPointer());
276 if (def && !def->IsDisabled()) flags |= Qt::ItemIsEnabled;
280 bool C4ConsoleQtDefinitionFileListModel::setData(
const QModelIndex& index,
const QVariant& value,
int role)
283 if (role == Qt::CheckStateRole)
285 DefFileInfo *def =
static_cast<DefFileInfo *
>(index.internalPointer());
286 if (def && !def->IsDisabled())
288 def->SetSelected(value.toBool(),
false);
290 int32_t child_count = def->GetChildCount();
291 QModelIndex end_changed = index;
292 if (child_count) end_changed = createIndex(child_count - 1, 0, def->GetChild(child_count - 1));
293 emit dataChanged(index, end_changed);
302 C4ConsoleQtNewScenarioDlg::C4ConsoleQtNewScenarioDlg(
class QMainWindow *parent_window)
303 : QDialog(parent_window, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint)
304 , has_custom_filename(false)
308 setMinimumSize(size());
312 QItemSelectionModel *m = ui.definitionTreeView->selectionModel();
313 ui.definitionTreeView->setModel(&def_file_model);
316 InitScenarioTemplateList();
319 void C4ConsoleQtNewScenarioDlg::InitScenarioTemplateList()
323 ui.templateComboBox->clear();
324 C4Group system_templates, user_templates;
327 for (
C4Group *template_group : { &system_templates, &user_templates })
329 if (template_group->IsOpen())
332 template_group->ResetSearch();
337 AddScenarioTemplate(*template_group, template_filename.
getData(), is_default);
344 void C4ConsoleQtNewScenarioDlg::AddScenarioTemplate(
C4Group &parent,
const char *filename,
bool is_default)
350 if (!template_c4s.
Load(grp))
return;
358 ui.templateComboBox->addItem(QString(title.getData()), QString(template_path.getData()));
359 all_template_c4s.push_back(template_c4s);
363 for (
const char *c4s_def : c4s_defs)
367 bool is_extra_def =
false;
378 def_file_model.AddExtraDef(c4s_def);
382 if (is_default) ui.templateComboBox->setCurrentIndex(ui.templateComboBox->count()-1);
385 bool C4ConsoleQtNewScenarioDlg::IsHostAsNetwork()
const
387 return ui.startInNetworkCheckbox->isChecked();
390 void C4ConsoleQtNewScenarioDlg::SelectedTemplateChanged(
int new_selection)
393 if (new_selection >= 0 && new_selection < all_template_c4s.size())
395 const C4Scenario &template_c4s = all_template_c4s[new_selection];
400 def_file_model.SetForcedSelection(std::list<const char *>());
404 bool C4ConsoleQtNewScenarioDlg::CreateScenario()
407 QVariant tmpl_data = ui.templateComboBox->currentData();
408 Log(tmpl_data.toString().toUtf8());
410 template_filename.
Copy(tmpl_data.toString().toUtf8());
430 if (!grp.
Open(filename.getData()))
438 if (!c4s.
Load(grp))
return false;
443 c4s.
Head.
Title = ui.titleEdit->text().toStdString();
444 c4s.
Game.
Mode.
Copy(ui.gameModeComboBox->currentText().toUtf8());
446 filename.Copy(ui.filenameEdit->text().toUtf8());
447 std::list<const char *> definitions = def_file_model.GetUserSelectedDefinitions();
450 const char *forced_definitions_c = forced_definitions.
getData();
451 std::ostringstream definitions_join(forced_definitions_c ? forced_definitions_c :
nullptr, std::ostringstream::ate);
452 if (definitions.size())
458 definitions_join <<
";";
460 auto iter_end = definitions.end();
461 std::copy(definitions.begin(), --iter_end, std::ostream_iterator<std::string>(definitions_join,
";"));
462 definitions_join << *iter_end;
474 void C4ConsoleQtNewScenarioDlg::CreatePressed()
477 if (!ui.titleEdit->text().length())
480 ui.titleEdit->setFocus();
485 DoError(::
LoadResStr(
"IDS_ERR_NEWSCENARIOFILEEXISTS"));
486 ui.titleEdit->setFocus();
489 std::list<const char *> definitions = def_file_model.GetSelectedDefinitions();
493 ui.definitionTreeView->setFocus();
496 if (!CreateScenario())
499 DoError(::
LoadResStr(
"IDS_ERR_CREATESCENARIO"));
500 ui.titleEdit->setFocus();
509 static char ReplaceSpecialFilenameChars(
char c)
511 const char *special_chars = R
"(\/:<>|$?" )";
512 return strchr(special_chars, c) ?
'_' : c;
515 void C4ConsoleQtNewScenarioDlg::TitleChanged(
const QString &new_title)
517 if (!has_custom_filename)
520 std::string filename = new_title.toStdString();
521 std::transform(filename.begin(), filename.end(), filename.begin(), ReplaceSpecialFilenameChars);
524 ui.filenameEdit->setText(filename_full);
525 this->filename.Copy(filename_full);
530 void C4ConsoleQtNewScenarioDlg::DoError(
const char *msg)
532 QMessageBox::critical(
this, ::
LoadResStr(
"IDS_ERR_TITLE"), QString(msg));
535 void C4ConsoleQtNewScenarioDlg::BrowsePressed()
542 if (!new_file.size())
return;
547 QMessageBox::critical(
this, ::
LoadResStr(
"IDS_ERR_TITLE"), ::
LoadResStr(
"IDS_ERR_NEWSCENARIOFILEEXISTS"));
549 filename.Copy(new_file.toUtf8());
550 ui.filenameEdit->setText(filename.getData());
552 has_custom_filename =
true;
#define C4CFN_DefaultScenarioTemplate
#define C4CFN_ScenarioFiles
C4Application Application
bool C4Group_CopyItem(const char *source, const char *target, bool no_sorting, bool reset_attributes)
bool C4Group_UnpackDirectory(const char *filename)
const char * LoadResStr(const char *id)
bool Log(const char *szMessage)
const int32_t C4S_MaxDefinitions
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
StdStrBuf FormatString(const char *szFmt,...)
bool EraseItem(const char *szItemName)
bool CopyDirectory(const char *szSource, const char *szTarget, bool fResetAttributes)
bool DirectoryExists(const char *szFilename)
bool WildcardMatch(const char *szWildcard, const char *szString)
char * GetFilename(char *szPath)
bool ItemExists(const char *szItemName)
bool GetLanguageString(const char *szLanguage, StdStrBuf &rTarget)
char LanguageEx[CFG_MaxString+1]
char UserDataPath[CFG_MaxString+1]
const char * AtUserDataPath(const char *filename)
bool DeleteEntry(const char *filename, bool do_recycle=false)
StdStrBuf GetFullName() const
bool OpenAsChild(C4Group *mother, const char *entry_name, bool is_exclusive=false, bool do_create=false)
bool Open(const char *group_name, bool do_create=false)
static bool LoadComponentHost(C4ComponentHost *host, C4Group &hGroup, const char *szFilename, const char *szLanguage)
void SetModules(const char *szList, const char *szRelativeToPath=nullptr, const char *szRelativeToPath2=nullptr)
std::list< const char * > GetModulesAsList() const
bool GetModules(StdStrBuf *psOutModules) const
void SetConstant(int32_t val)
bool Save(C4Group &hGroup, bool fSaveSection=false)
C4SDefinitions Definitions
bool Load(C4Group &hGroup, bool fLoadSection=false, bool suppress_errors=false)
const char * getData() const