g4dialog
Loading...
Searching...
No Matches
gcommands.cc
Go to the documentation of this file.
1// G4Dialog
2#include "gcommands.h"
3
4G4Commands::G4Commands(QWidget* parent) : QWidget(parent) {
5 // + +-------------------+ +
6 // | | > Search | |
7 // + +-------------------+ +
8 // | | | | |
9 // | | Tree | Help | |
10 // | | | | |
11 // | +-------------------+ |
12 // | +-------------------+ |
13 // | | | |
14 // | | History | |
15 // | | | |
16 // | +-------------------+ |
17 // | +-------------------+ |
18 // | | > Prompt | |
19 // | +-------------------+ |
20 // +-----------------------+
21
22 // search
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);
28
29 // every time w_search is changed, filter the tree
30 connect(w_search, &QLineEdit::textChanged, this, &G4Commands::filterTreeItems);
31
32 // commands tree and help
33 QSplitter* commands_help_splitter = new QSplitter(Qt::Horizontal);
34 QVBoxLayout* commands_help_layout = new QVBoxLayout(commands_help_splitter);
35
36 // Left: the commands tree
37 create_geant4_commands_widget();
38 commands_help_layout->addWidget(w_commands);
39
40 // Right: the help on individual commands
41 w_help = new QTextEdit(commands_help_splitter);
42 w_help->setReadOnly(true);
43
44 // history area
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);
49
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);
56
57 // putting all together
58 commands_help_splitter->setSizes(QList<int>() << 300 << 800);
59
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, /* stretch factor */ 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);
68}
69
70void G4Commands::create_geant4_commands_widget() {
71 // Print search text on screen for debugging
72 QString search_text = w_search->text();
73
74 G4UImanager* ui_manager = G4UImanager::GetUIpointer();
75 G4UIcommandTree* g4_commands_tree = ui_manager->GetTree();
76
77 // Create model for QTreeView
78 QStandardItemModel* model = new QStandardItemModel();
79 w_commands = new QTreeView();
80 w_commands->setModel(model);
81 w_commands->setSelectionMode(QAbstractItemView::SingleSelection);
82
83 // Set header label for the QTreeView model
84 model->setHorizontalHeaderLabels(QStringList() << "Commands");
85
86 // Add commands to the model
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);
92
93 // Add child commands
94 create_child_help_tree(newItem, g4_commands_tree->GetTree(a + 1));
95 }
96
97 // w_commands is read only
98 w_commands->setEditTriggers(QAbstractItemView::NoEditTriggers);
99
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);
103}
104
105
106void G4Commands::filterTreeItems() {
107 QString search_text = w_search->text().trimmed();
108 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
109 if (!model) return;
110
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);
115 }
116}
117
118
119bool G4Commands::filterItem(QStandardItem* item, const QString& search_text) {
120 bool matches = item->text().contains(search_text, Qt::CaseInsensitive);
121 bool childMatches = false;
122
123 // Check child items recursively
124 for (int i = 0; i < item->rowCount(); ++i) {
125 QStandardItem* childItem = item->child(i);
126 if (filterItem(childItem, search_text)) { childMatches = true; }
127 }
128
129 // Show this item if it matches or has a matching child
130 bool showItem = matches || childMatches;
131 w_commands->setRowHidden(item->row(), item->index().parent(), !showItem);
132
133 return showItem;
134}
135
136
137void G4Commands::create_child_help_tree(QStandardItem* parent, G4UIcommandTree* aCommandTree) {
138 if (parent == nullptr || aCommandTree == nullptr) return;
139
140 // Add child directories
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));
146 }
147
148 // Add commands
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);
153 }
154}
155
156
157void G4Commands::execute_command() {
158 if (!w_command) return;
159
160 QString command = w_command->text().trimmed();
161 if (command.isEmpty()) return;
162
163 G4UImanager* ui_manager = G4UImanager::GetUIpointer();
164 ui_manager->ApplyCommand(command.toStdString().c_str());
165
166 // Avoid duplicate history entries
167 bool exists = false;
168 for (int i = 0; i < w_history->count(); ++i) {
169 if (w_history->item(i)->text() == command) {
170 exists = true;
171 break;
172 }
173 }
174
175 if (!exists) { w_history->addItem(command); }
176
177 w_command->clear();
178}
179
180
181void G4Commands::recall_history_item_on_double_click(QListWidgetItem* item) {
182 if (!item) return;
183
184 // Set the command line input to the selected history item
185 w_command->setText(item->text());
186}
187
188
189// paste history item onto command line
190void G4Commands::display_help_from_selection() {
191 if (!w_commands || !w_help) return;
192
193 QModelIndexList selectedIndexes = w_commands->selectionModel()->selectedIndexes();
194 if (selectedIndexes.isEmpty()) return;
195
196 QModelIndex index = selectedIndexes.first();
197 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
198 if (!model) return;
199
200 QStandardItem* item = model->itemFromIndex(index);
201 if (!item) return;
202
203 std::string itemText = item->text().toStdString();
204 G4UIcommandTree* treeTop = G4UImanager::GetUIpointer()->GetTree();
205 G4UIcommand* command = treeTop->FindPath(itemText.c_str());
206
207 if (command) { w_help->setText(get_command_g4help(command)); }
208 else {
209 G4UIcommandTree* path = treeTop->FindCommandTree(itemText.c_str());
210 if (path) { w_help->setText(QString::fromStdString(path->GetTitle())); }
211 }
212}
213
214
215// display help on selected item
216void G4Commands::paste_help_selection_item() {
217 // Display help from the selection
218 display_help_from_selection();
219
220 if (!w_commands || !w_help) { return; }
221
222 // Get the selected items from the selection model of QTreeView
223 QModelIndexList selectedIndexes = w_commands->selectionModel()->selectedIndexes();
224 if (selectedIndexes.isEmpty()) { return; }
225
226 // Assuming the first selected item is the one to retrieve
227 QModelIndex index = selectedIndexes.first();
228 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(w_commands->model());
229 if (!model) { return; }
230
231 // Retrieve the item at the given index
232 QStandardItem* item = model->itemFromIndex(index);
233 if (!item) { return; }
234
235 // Clear the command input field and set the selected item's text
236 w_command->clear();
237 w_command->setText(item->text());
238}
239
240
241QString G4Commands::get_command_g4help(const G4UIcommand* aCommand) {
242 QString txt = "";
243 if (aCommand == nullptr)
244 return txt;
245
246 G4String commandPath = aCommand->GetCommandPath();
247 G4String rangeString = aCommand->GetRange();
248 G4int n_guidanceEntry = aCommand->GetGuidanceEntries();
249 G4int n_parameterEntry = aCommand->GetParameterEntries();
250
251 if ((commandPath == "") && (rangeString == "") && (n_guidanceEntry == 0) && (n_parameterEntry == 0)) { return txt; }
252
253 if ((commandPath.length() - 1) != '/') { txt += "Command " + QString((char*)(commandPath).data()) + "\n"; }
254 txt += "Guidance :\n";
255
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;
260
261 // Re-implementation from G4UIparameter.cc
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"; }
269
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"; }
272
273 if (param->GetParameterRange() != "") { txt += " Parameter range : " + QString((char*)(param->GetParameterRange()).data()) + "\n"; }
274
275 if (param->GetParameterCandidates() != "") { txt += " Candidates : " + QString((char*)(param->GetParameterCandidates()).data()) + "\n"; }
276 }
277 }
278 return txt;
279}
G4Commands(QWidget *parent=nullptr)
Definition gcommands.cc:4