25 w_search =
new QLineEdit();
26 w_search->installEventFilter(
this);
27 w_search->activateWindow();
28 w_search->setFocusPolicy(Qt::StrongFocus);
29 w_search->setFocus(Qt::TabFocusReason);
32 connect(w_search, &QLineEdit::textChanged,
this, &G4Commands::filterTreeItems);
35 QSplitter* commands_help_splitter =
new QSplitter(Qt::Horizontal);
36 QVBoxLayout* commands_help_layout =
new QVBoxLayout(commands_help_splitter);
39 create_geant4_commands_widget();
40 commands_help_layout->addWidget(w_commands);
43 w_help =
new QTextEdit(commands_help_splitter);
44 w_help->setReadOnly(
true);
47 w_history =
new QListWidget();
48 w_history->setSelectionMode(QAbstractItemView::SingleSelection);
49 w_history->installEventFilter(
this);
50 connect(w_history, &QListWidget::itemDoubleClicked,
this, &G4Commands::recall_history_item_on_double_click);
53 w_command =
new QLineEdit();
54 w_command->installEventFilter(
this);
55 w_command->activateWindow();
56 w_command->setFocusPolicy(Qt::StrongFocus);
57 w_command->setFocus(Qt::TabFocusReason);
58 connect(w_command, &QLineEdit::returnPressed,
this, &G4Commands::execute_command);
61 commands_help_splitter->setSizes(QList<int>() << 300 << 800);
64 QVBoxLayout* v_layout =
new QVBoxLayout(
this);
65 v_layout->addWidget(
new QLabel(
"Search Commands"));
66 v_layout->addWidget(w_search);
67 v_layout->addWidget(commands_help_splitter, 2);
68 v_layout->addWidget(
new QLabel(
"History"));
69 v_layout->addWidget(w_history);
70 v_layout->addWidget(
new QLabel(
"Enter Command"));
71 v_layout->addWidget(w_command);
74void G4Commands::create_geant4_commands_widget() {
78 QString search_text = w_search->text();
81 G4UImanager* ui_manager = G4UImanager::GetUIpointer();
82 G4UIcommandTree* g4_commands_tree = ui_manager->GetTree();
85 QStandardItemModel* model =
new QStandardItemModel();
86 w_commands =
new QTreeView();
87 w_commands->setModel(model);
88 w_commands->setSelectionMode(QAbstractItemView::SingleSelection);
91 model->setHorizontalHeaderLabels(QStringList() <<
"Commands");
94 G4int g4_commands_tree_size = g4_commands_tree->GetTreeEntry();
95 for (
int a = 0; a < g4_commands_tree_size; a++) {
97 QStandardItem* newItem =
new QStandardItem(
98 QString((
char*)g4_commands_tree->GetTree(a + 1)->GetPathName().data()).trimmed());
99 model->appendRow(newItem);
102 create_child_help_tree(newItem, g4_commands_tree->GetTree(a + 1));
106 w_commands->setEditTriggers(QAbstractItemView::NoEditTriggers);
109 connect(w_commands->selectionModel(), &QItemSelectionModel::selectionChanged,
this,
110 &G4Commands::display_help_from_selection);
111 connect(w_commands, &QTreeView::doubleClicked,
this, &G4Commands::paste_help_selection_item);
114void G4Commands::filterTreeItems() {
117 QString search_text = w_search->text().trimmed();
118 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
121 for (
int i = 0; i < model->rowCount(); ++i) {
122 QStandardItem* item = model->item(i);
123 bool showItem = filterItem(item, search_text);
126 w_commands->setRowHidden(i, QModelIndex(), !showItem);
130bool G4Commands::filterItem(QStandardItem* item,
const QString& search_text) {
134 bool matches = item->text().contains(search_text, Qt::CaseInsensitive);
135 bool childMatches =
false;
138 for (
int i = 0; i < item->rowCount(); ++i) {
139 QStandardItem* childItem = item->child(i);
140 if (filterItem(childItem, search_text)) { childMatches =
true; }
144 bool showItem = matches || childMatches;
147 w_commands->setRowHidden(item->row(), item->index().parent(), !showItem);
152void G4Commands::create_child_help_tree(QStandardItem* parent, G4UIcommandTree* aCommandTree) {
155 if (parent ==
nullptr || aCommandTree ==
nullptr)
return;
158 for (
int a = 0; a < aCommandTree->GetTreeEntry(); a++) {
159 QStandardItem* newItem =
new QStandardItem(
160 QString((
char*)(aCommandTree->GetTree(a + 1)->GetPathName()).data()).trimmed());
161 parent->appendRow(newItem);
162 create_child_help_tree(newItem, aCommandTree->GetTree(a + 1));
166 for (
int a = 0; a < aCommandTree->GetCommandEntry(); a++) {
167 QStandardItem* newItem =
new QStandardItem(
168 QString((
char*)(aCommandTree->GetCommand(a + 1)->GetCommandPath()).data()).trimmed());
169 parent->appendRow(newItem);
173void G4Commands::execute_command() {
176 if (!w_command)
return;
178 QString command = w_command->text().trimmed();
179 if (command.isEmpty())
return;
181 G4UImanager* ui_manager = G4UImanager::GetUIpointer();
182 ui_manager->ApplyCommand(command.toStdString().c_str());
186 for (
int i = 0; i < w_history->count(); ++i) {
187 if (w_history->item(i)->text() == command) {
193 if (!exists) { w_history->addItem(command); }
199void G4Commands::recall_history_item_on_double_click(QListWidgetItem* item) {
205 w_command->setText(item->text());
209void G4Commands::display_help_from_selection() {
212 if (!w_commands || !w_help)
return;
214 QModelIndexList selectedIndexes = w_commands->selectionModel()->selectedIndexes();
215 if (selectedIndexes.isEmpty())
return;
217 QModelIndex index = selectedIndexes.first();
218 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
221 QStandardItem* item = model->itemFromIndex(index);
224 std::string itemText = item->text().toStdString();
225 G4UIcommandTree* treeTop = G4UImanager::GetUIpointer()->GetTree();
226 G4UIcommand* command = treeTop->FindPath(itemText.c_str());
230 w_help->setText(get_command_g4help(command));
234 G4UIcommandTree* path = treeTop->FindCommandTree(itemText.c_str());
235 if (path) { w_help->setText(QString::fromStdString(path->GetTitle())); }
240void G4Commands::paste_help_selection_item() {
244 display_help_from_selection();
246 if (!w_commands || !w_help) {
return; }
249 QModelIndexList selectedIndexes = w_commands->selectionModel()->selectedIndexes();
250 if (selectedIndexes.isEmpty()) {
return; }
253 QModelIndex index = selectedIndexes.first();
254 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
255 if (!model) {
return; }
258 QStandardItem* item = model->itemFromIndex(index);
259 if (!item) {
return; }
263 w_command->setText(item->text());
266QString G4Commands::get_command_g4help(
const G4UIcommand* aCommand) {
270 if (aCommand ==
nullptr)
273 G4String commandPath = aCommand->GetCommandPath();
274 G4String rangeString = aCommand->GetRange();
275 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
276 G4int n_parameterEntry = aCommand->GetParameterEntries();
279 if ((commandPath ==
"") && (rangeString ==
"") && (n_guidanceEntry == 0) && (n_parameterEntry == 0)) {
return txt; }
282 if ((commandPath.length() - 1) !=
'/') { txt +=
"Command " + QString((
char*)(commandPath).data()) +
"\n"; }
283 txt +=
"Guidance :\n";
285 for (G4int i_thGuidance = 0; i_thGuidance < n_guidanceEntry; i_thGuidance++) {
286 txt += QString((
char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) +
"\n";
289 if (rangeString !=
"") { txt +=
" Range of parameters : " + QString((
char*)(rangeString).data()) +
"\n"; }
291 if (n_parameterEntry > 0) {
292 G4UIparameter* param;
295 for (G4int i_thParameter = 0; i_thParameter < n_parameterEntry; i_thParameter++) {
296 param = aCommand->GetParameter(i_thParameter);
297 txt +=
"\nParameter : " + QString((
char*)(param->GetParameterName()).data()) +
"\n";
298 if (param->GetParameterGuidance() !=
"") {
299 txt += QString((
char*)(param->GetParameterGuidance()).data()) +
"\n";
301 txt +=
" Parameter type : " + QString(QChar(param->GetParameterType())) +
"\n";
303 if (param->IsOmittable()) { txt +=
" Omittable : True\n"; }
304 else { txt +=
" Omittable : False\n"; }
306 if (param->GetCurrentAsDefault()) { txt +=
" Default value : taken from the current value\n"; }
307 else if (param->GetDefaultValue() !=
"") {
308 txt +=
" Default value : " + QString((
char*)(param->GetDefaultValue()).data()) +
"\n";
311 if (param->GetParameterRange() !=
"") {
312 txt +=
" Parameter range : " + QString((
char*)(param->GetParameterRange()).data()) +
"\n";
315 if (param->GetParameterCandidates() !=
"") {
316 txt +=
" Candidates : " + QString((
char*)(param->GetParameterCandidates()).data()) +
"\n";
G4Commands(QWidget *parent=nullptr)
Construct the commands widget.