23 w_search =
new QLineEdit();
24 w_search->installEventFilter(
this);
25 w_search->activateWindow();
26 w_search->setFocusPolicy(Qt::StrongFocus);
27 w_search->setFocus(Qt::TabFocusReason);
30 connect(w_search, &QLineEdit::textChanged,
this, &G4Commands::filterTreeItems);
33 QSplitter* commands_help_splitter =
new QSplitter(Qt::Horizontal);
34 QVBoxLayout* commands_help_layout =
new QVBoxLayout(commands_help_splitter);
37 create_geant4_commands_widget();
38 commands_help_layout->addWidget(w_commands);
41 w_help =
new QTextEdit(commands_help_splitter);
42 w_help->setReadOnly(
true);
45 w_history =
new QListWidget();
46 w_history->setSelectionMode(QAbstractItemView::SingleSelection);
47 w_history->installEventFilter(
this);
48 connect(w_history, &QListWidget::itemDoubleClicked,
this, &G4Commands::recall_history_item_on_double_click);
50 w_command =
new QLineEdit();
51 w_command->installEventFilter(
this);
52 w_command->activateWindow();
53 w_command->setFocusPolicy(Qt::StrongFocus);
54 w_command->setFocus(Qt::TabFocusReason);
55 connect(w_command, &QLineEdit::returnPressed,
this, &G4Commands::execute_command);
58 commands_help_splitter->setSizes(QList<int>() << 300 << 800);
60 QVBoxLayout* v_layout =
new QVBoxLayout(
this);
61 v_layout->addWidget(
new QLabel(
"Search Commands"));
62 v_layout->addWidget(w_search);
63 v_layout->addWidget(commands_help_splitter, 2);
64 v_layout->addWidget(
new QLabel(
"History"));
65 v_layout->addWidget(w_history);
66 v_layout->addWidget(
new QLabel(
"Enter Command"));
67 v_layout->addWidget(w_command);
70void G4Commands::create_geant4_commands_widget() {
72 QString search_text = w_search->text();
74 G4UImanager* ui_manager = G4UImanager::GetUIpointer();
75 G4UIcommandTree* g4_commands_tree = ui_manager->GetTree();
78 QStandardItemModel* model =
new QStandardItemModel();
79 w_commands =
new QTreeView();
80 w_commands->setModel(model);
81 w_commands->setSelectionMode(QAbstractItemView::SingleSelection);
84 model->setHorizontalHeaderLabels(QStringList() <<
"Commands");
87 G4int g4_commands_tree_size = g4_commands_tree->GetTreeEntry();
88 for (
int a = 0; a < g4_commands_tree_size; a++) {
89 QStandardItem* newItem =
new QStandardItem(
90 QString((
char*)g4_commands_tree->GetTree(a + 1)->GetPathName().data()).trimmed());
91 model->appendRow(newItem);
94 create_child_help_tree(newItem, g4_commands_tree->GetTree(a + 1));
98 w_commands->setEditTriggers(QAbstractItemView::NoEditTriggers);
100 connect(w_commands->selectionModel(), &QItemSelectionModel::selectionChanged,
this,
101 &G4Commands::display_help_from_selection);
102 connect(w_commands, &QTreeView::doubleClicked,
this, &G4Commands::paste_help_selection_item);
106void G4Commands::filterTreeItems() {
107 QString search_text = w_search->text().trimmed();
108 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
111 for (
int i = 0; i < model->rowCount(); ++i) {
112 QStandardItem* item = model->item(i);
113 bool showItem = filterItem(item, search_text);
114 w_commands->setRowHidden(i, QModelIndex(), !showItem);
119bool G4Commands::filterItem(QStandardItem* item,
const QString& search_text) {
120 bool matches = item->text().contains(search_text, Qt::CaseInsensitive);
121 bool childMatches =
false;
124 for (
int i = 0; i < item->rowCount(); ++i) {
125 QStandardItem* childItem = item->child(i);
126 if (filterItem(childItem, search_text)) { childMatches =
true; }
130 bool showItem = matches || childMatches;
131 w_commands->setRowHidden(item->row(), item->index().parent(), !showItem);
137void G4Commands::create_child_help_tree(QStandardItem* parent, G4UIcommandTree* aCommandTree) {
138 if (parent ==
nullptr || aCommandTree ==
nullptr)
return;
141 for (
int a = 0; a < aCommandTree->GetTreeEntry(); a++) {
142 QStandardItem* newItem =
new QStandardItem(
143 QString((
char*)(aCommandTree->GetTree(a + 1)->GetPathName()).data()).trimmed());
144 parent->appendRow(newItem);
145 create_child_help_tree(newItem, aCommandTree->GetTree(a + 1));
149 for (
int a = 0; a < aCommandTree->GetCommandEntry(); a++) {
150 QStandardItem* newItem =
new QStandardItem(
151 QString((
char*)(aCommandTree->GetCommand(a + 1)->GetCommandPath()).data()).trimmed());
152 parent->appendRow(newItem);
157void G4Commands::execute_command() {
158 if (!w_command)
return;
160 QString command = w_command->text().trimmed();
161 if (command.isEmpty())
return;
163 G4UImanager* ui_manager = G4UImanager::GetUIpointer();
164 ui_manager->ApplyCommand(command.toStdString().c_str());
168 for (
int i = 0; i < w_history->count(); ++i) {
169 if (w_history->item(i)->text() == command) {
175 if (!exists) { w_history->addItem(command); }
181void G4Commands::recall_history_item_on_double_click(QListWidgetItem* item) {
185 w_command->setText(item->text());
190void G4Commands::display_help_from_selection() {
191 if (!w_commands || !w_help)
return;
193 QModelIndexList selectedIndexes = w_commands->selectionModel()->selectedIndexes();
194 if (selectedIndexes.isEmpty())
return;
196 QModelIndex index = selectedIndexes.first();
197 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
200 QStandardItem* item = model->itemFromIndex(index);
203 std::string itemText = item->text().toStdString();
204 G4UIcommandTree* treeTop = G4UImanager::GetUIpointer()->GetTree();
205 G4UIcommand* command = treeTop->FindPath(itemText.c_str());
207 if (command) { w_help->setText(get_command_g4help(command)); }
209 G4UIcommandTree* path = treeTop->FindCommandTree(itemText.c_str());
210 if (path) { w_help->setText(QString::fromStdString(path->GetTitle())); }
216void G4Commands::paste_help_selection_item() {
218 display_help_from_selection();
220 if (!w_commands || !w_help) {
return; }
223 QModelIndexList selectedIndexes = w_commands->selectionModel()->selectedIndexes();
224 if (selectedIndexes.isEmpty()) {
return; }
227 QModelIndex index = selectedIndexes.first();
228 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
229 if (!model) {
return; }
232 QStandardItem* item = model->itemFromIndex(index);
233 if (!item) {
return; }
237 w_command->setText(item->text());
241QString G4Commands::get_command_g4help(
const G4UIcommand* aCommand) {
243 if (aCommand ==
nullptr)
246 G4String commandPath = aCommand->GetCommandPath();
247 G4String rangeString = aCommand->GetRange();
248 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
249 G4int n_parameterEntry = aCommand->GetParameterEntries();
251 if ((commandPath ==
"") && (rangeString ==
"") && (n_guidanceEntry == 0) && (n_parameterEntry == 0)) {
return txt; }
253 if ((commandPath.length() - 1) !=
'/') { txt +=
"Command " + QString((
char*)(commandPath).data()) +
"\n"; }
254 txt +=
"Guidance :\n";
256 for (G4int i_thGuidance = 0; i_thGuidance < n_guidanceEntry; i_thGuidance++) { txt += QString((
char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) +
"\n"; }
257 if (rangeString !=
"") { txt +=
" Range of parameters : " + QString((
char*)(rangeString).data()) +
"\n"; }
258 if (n_parameterEntry > 0) {
259 G4UIparameter* param;
262 for (G4int i_thParameter = 0; i_thParameter < n_parameterEntry; i_thParameter++) {
263 param = aCommand->GetParameter(i_thParameter);
264 txt +=
"\nParameter : " + QString((
char*)(param->GetParameterName()).data()) +
"\n";
265 if (param->GetParameterGuidance() !=
"") { txt += QString((
char*)(param->GetParameterGuidance()).data()) +
"\n"; }
266 txt +=
" Parameter type : " + QString(QChar(param->GetParameterType())) +
"\n";
267 if (param->IsOmittable()) { txt +=
" Omittable : True\n"; }
268 else { txt +=
" Omittable : False\n"; }
270 if (param->GetCurrentAsDefault()) { txt +=
" Default value : taken from the current value\n"; }
271 else if (param->GetDefaultValue() !=
"") { txt +=
" Default value : " + QString((
char*)(param->GetDefaultValue()).data()) +
"\n"; }
273 if (param->GetParameterRange() !=
"") { txt +=
" Parameter range : " + QString((
char*)(param->GetParameterRange()).data()) +
"\n"; }
275 if (param->GetParameterCandidates() !=
"") { txt +=
" Candidates : " + QString((
char*)(param->GetParameterCandidates()).data()) +
"\n"; }
G4Commands(QWidget *parent=nullptr)