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(tr(
"Search Commands")));
66 v_layout->addWidget(w_search);
67 v_layout->addWidget(commands_help_splitter, 2);
68 v_layout->addWidget(
new QLabel(tr(
"History")));
69 v_layout->addWidget(w_history);
70 v_layout->addWidget(
new QLabel(tr(
"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() << tr(
"Commands"));
93 G4int g4_commands_tree_size = g4_commands_tree->GetTreeEntry();
94 for (
int a = 0; a < g4_commands_tree_size; a++) {
96 QStandardItem* newItem =
new QStandardItem(
97 QString((
char*)g4_commands_tree->GetTree(a + 1)->GetPathName().data()).trimmed());
98 model->appendRow(newItem);
101 create_child_help_tree(newItem, g4_commands_tree->GetTree(a + 1));
105 w_commands->setEditTriggers(QAbstractItemView::NoEditTriggers);
108 connect(w_commands->selectionModel(), &QItemSelectionModel::selectionChanged,
this,
109 &G4Commands::display_help_from_selection);
110 connect(w_commands, &QTreeView::doubleClicked,
this, &G4Commands::paste_help_selection_item);
113void G4Commands::filterTreeItems() {
116 QString search_text = w_search->text().trimmed();
117 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
120 for (
int i = 0; i < model->rowCount(); ++i) {
121 QStandardItem* item = model->item(i);
122 bool showItem = filterItem(item, search_text);
125 w_commands->setRowHidden(i, QModelIndex(), !showItem);
129bool G4Commands::filterItem(QStandardItem* item,
const QString& search_text) {
133 bool matches = item->text().contains(search_text, Qt::CaseInsensitive);
134 bool childMatches =
false;
137 for (
int i = 0; i < item->rowCount(); ++i) {
138 QStandardItem* childItem = item->child(i);
139 if (filterItem(childItem, search_text)) { childMatches =
true; }
143 bool showItem = matches || childMatches;
146 w_commands->setRowHidden(item->row(), item->index().parent(), !showItem);
151void G4Commands::create_child_help_tree(QStandardItem* parent, G4UIcommandTree* aCommandTree) {
154 if (parent ==
nullptr || aCommandTree ==
nullptr)
return;
157 for (
int a = 0; a < aCommandTree->GetTreeEntry(); a++) {
158 QStandardItem* newItem =
new QStandardItem(
159 QString((
char*)(aCommandTree->GetTree(a + 1)->GetPathName()).data()).trimmed());
160 parent->appendRow(newItem);
161 create_child_help_tree(newItem, aCommandTree->GetTree(a + 1));
165 for (
int a = 0; a < aCommandTree->GetCommandEntry(); a++) {
166 QStandardItem* newItem =
new QStandardItem(
167 QString((
char*)(aCommandTree->GetCommand(a + 1)->GetCommandPath()).data()).trimmed());
168 parent->appendRow(newItem);
172void G4Commands::execute_command() {
175 if (!w_command)
return;
177 QString command = w_command->text().trimmed();
178 if (command.isEmpty())
return;
180 G4UImanager* ui_manager = G4UImanager::GetUIpointer();
181 ui_manager->ApplyCommand(command.toStdString().c_str());
185 for (
int i = 0; i < w_history->count(); ++i) {
186 if (w_history->item(i)->text() == command) {
192 if (!exists) { w_history->addItem(command); }
198void G4Commands::recall_history_item_on_double_click(QListWidgetItem* item) {
204 w_command->setText(item->text());
208void G4Commands::display_help_from_selection() {
211 if (!w_commands || !w_help)
return;
213 QModelIndexList selectedIndexes = w_commands->selectionModel()->selectedIndexes();
214 if (selectedIndexes.isEmpty())
return;
216 QModelIndex index = selectedIndexes.first();
217 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
220 QStandardItem* item = model->itemFromIndex(index);
223 std::string itemText = item->text().toStdString();
224 G4UIcommandTree* treeTop = G4UImanager::GetUIpointer()->GetTree();
225 G4UIcommand* command = treeTop->FindPath(itemText.c_str());
229 w_help->setText(get_command_g4help(command));
233 G4UIcommandTree* path = treeTop->FindCommandTree(itemText.c_str());
234 if (path) { w_help->setText(QString::fromStdString(path->GetTitle())); }
239void G4Commands::paste_help_selection_item() {
243 display_help_from_selection();
245 if (!w_commands || !w_help) {
return; }
248 QModelIndexList selectedIndexes = w_commands->selectionModel()->selectedIndexes();
249 if (selectedIndexes.isEmpty()) {
return; }
252 QModelIndex index = selectedIndexes.first();
253 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
254 if (!model) {
return; }
257 QStandardItem* item = model->itemFromIndex(index);
258 if (!item) {
return; }
262 w_command->setText(item->text());
265QString G4Commands::get_command_g4help(
const G4UIcommand* aCommand) {
269 if (aCommand ==
nullptr)
272 G4String commandPath = aCommand->GetCommandPath();
273 G4String rangeString = aCommand->GetRange();
274 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
275 G4int n_parameterEntry = aCommand->GetParameterEntries();
278 if ((commandPath ==
"") && (rangeString ==
"") && (n_guidanceEntry == 0) && (n_parameterEntry == 0)) {
return txt; }
281 if ((commandPath.length() - 1) !=
'/') { txt += tr(
"Command %1\n").arg(QString::fromStdString(commandPath)); }
282 txt += tr(
"Guidance :\n");
283 for (G4int i_thGuidance = 0; i_thGuidance < n_guidanceEntry; i_thGuidance++) {
284 txt += QString((
char*)(aCommand->GetGuidanceLine(i_thGuidance)).data()) +
"\n";
286 if (rangeString !=
"") { txt += tr(
" Range of parameters : %1\n").arg(QString::fromStdString(rangeString)); }
287 if (n_parameterEntry > 0) {
288 G4UIparameter* param;
291 for (G4int i_thParameter = 0; i_thParameter < n_parameterEntry; i_thParameter++) {
292 param = aCommand->GetParameter(i_thParameter);
293 txt += tr(
"\nParameter : %1\n").arg(QString::fromStdString(param->GetParameterName()));
294 if (param->GetParameterGuidance() !=
"") {
295 txt += QString((
char*)(param->GetParameterGuidance()).data()) +
"\n";
297 txt += tr(
" Parameter type : %1\n").arg(QChar(param->GetParameterType()));
298 if (param->IsOmittable()) { txt += tr(
" Omittable : True\n"); }
299 else { txt += tr(
" Omittable : False\n"); }
300 if (param->GetCurrentAsDefault()) { txt += tr(
" Default value : taken from the current value\n"); }
301 else if (param->GetDefaultValue() !=
"") {
302 txt += tr(
" Default value : %1\n").arg(QString::fromStdString(param->GetDefaultValue()));
305 if (param->GetParameterRange() !=
"") {
306 txt += tr(
" Parameter range : %1\n").arg(QString::fromStdString(param->GetParameterRange()));
309 if (param->GetParameterCandidates() !=
"") {
310 txt += tr(
" Candidates : %1\n").arg(QString::fromStdString(param->GetParameterCandidates()));
G4Commands(QWidget *parent=nullptr)
Construct the commands widget.