Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3 : // Fork baseline commit: dbe-02-12-17 (2022-05-12).
4 : // Renamed since fork: no.
5 :
6 : #include "dbe/IncludeFileWidget.hpp"
7 : #include "dbe/Exceptions.hpp"
8 : #include "dbe/MainWindow.hpp"
9 : #include "dbe/StyleUtility.hpp"
10 : #include "dbe/confaccessor.hpp"
11 : #include "dbe/config_api_commands.hpp"
12 : #include "dbe/config_api_get.hpp"
13 : #include "dbe/messenger.hpp"
14 : #include "ui_IncludeFileWidget.h"
15 :
16 : #include <QApplication>
17 : #include <QUrl>
18 :
19 : #include <boost/scope_exit.hpp>
20 : #include <cstdlib>
21 :
22 0 : dbe::IncludeFileWidget::~IncludeFileWidget() = default;
23 :
24 0 : dbe::IncludeFileWidget::IncludeFileWidget(QString FilePath, QWidget* parent)
25 : : QWidget(parent)
26 0 : , ui(new Ui::IncludeFileWidget)
27 0 : , CreateWidget(nullptr)
28 0 : , DatabasePath(FilePath)
29 0 : , Removed(false)
30 0 : , StatusBar(nullptr)
31 0 : , SelectFile(nullptr)
32 : {
33 0 : ui->setupUi(this);
34 :
35 : /// Visual
36 0 : ui->AddLabel->setStyleSheet("QLabel{background-color:#8DAA2E; color:#facd64; font:bold14px; border-style:outset; "
37 : "border-width:2px; border-radius:10px;"
38 : "border-color:beige; font:bold14px; min-width:10em; padding: 6px;}");
39 0 : ui->RemoveLabel->setStyleSheet(
40 : "QLabel{background-color:#b5351b; color:#facd64; font:bold14px; border-style:outset;"
41 : "border-width:2px; border-radius:10px; border-color:beige; font:bold14px; min-width:10em; padding:6px;}");
42 0 : setWindowTitle(QString("Edit Included Files for Database : %1").arg(QFileInfo(FilePath).fileName()));
43 :
44 0 : StatusBar = new QStatusBar(ui->StatusFrame);
45 0 : StatusBar->setSizeGripEnabled(false);
46 0 : ui->StatusFrame->setFrameStyle(QFrame::NoFrame);
47 0 : ui->StatusLayout->addWidget(StatusBar);
48 :
49 0 : StatusBar->setAutoFillBackground(true);
50 0 : StatusBar->showMessage("Select files to include or to remove from include!");
51 :
52 0 : SetRemoveComboBox();
53 :
54 0 : SelectFile = new QFileDialog(this, tr("Open File"), ".", tr("XML files (*.xml)"));
55 0 : SelectFile->setFileMode(QFileDialog::ExistingFiles);
56 0 : SelectFile->setViewMode(QFileDialog::Detail);
57 0 : SelectFile->setAcceptMode(QFileDialog::AcceptOpen);
58 :
59 0 : QString TDAQ_DB_REPOSITORY = getenv("TDAQ_DB_REPOSITORY");
60 0 : if (TDAQ_DB_REPOSITORY.isEmpty() == false) {
61 0 : QString TDAQ_DB_USER_REPOSITORY = getenv("TDAQ_DB_USER_REPOSITORY");
62 0 : FolderPathList = TDAQ_DB_USER_REPOSITORY.split(":", Qt::SkipEmptyParts);
63 :
64 0 : FolderPathList.append(dbe::MainWindow::findthis()->find_db_repository_dir());
65 0 : } else {
66 0 : QString DUNEDAQ_DB_PATH = getenv("DUNEDAQ_DB_PATH");
67 0 : FolderPathList = DUNEDAQ_DB_PATH.split(":", Qt::SkipEmptyParts);
68 0 : }
69 :
70 0 : for (QString& PathName : FolderPathList) {
71 0 : if (!PathName.endsWith("/")) {
72 0 : PathName.append("/");
73 : }
74 : }
75 :
76 0 : QList<QUrl> List = SelectFile->sidebarUrls();
77 :
78 0 : if (!FolderPathList.isEmpty()) {
79 0 : dbPath = FolderPathList;
80 :
81 0 : for (const QString& Dir : dbPath) {
82 0 : ui->DirectoryCombo->addItem(Dir);
83 0 : QString TDAqDir = QString("file://").append(Dir);
84 0 : QUrl URL = QUrl(TDAqDir);
85 0 : List << URL;
86 0 : }
87 : }
88 :
89 0 : SelectFile->setSidebarUrls(List);
90 0 : SelectFile->setOption(QFileDialog::DontResolveSymlinks, true);
91 0 : ui->AddToIncludeButton->setDisabled(true);
92 0 : ui->RemoveButton->setDisabled(true);
93 :
94 0 : ui->AddFileLine->setSelectionMode(QAbstractItemView::NoSelection);
95 :
96 0 : SetController();
97 0 : }
98 :
99 : void
100 0 : dbe::IncludeFileWidget::keyPressEvent(QKeyEvent* event)
101 : {
102 0 : if (event->key() == Qt::Key_Escape) {
103 0 : close();
104 : }
105 0 : QWidget::keyPressEvent(event);
106 0 : }
107 :
108 : void
109 0 : dbe::IncludeFileWidget::SetRemoveComboBox()
110 : {
111 0 : QStringList IncludeList(dbe::config::api::get::file::inclusions_singlefile(DatabasePath));
112 :
113 0 : if (!IncludeList.isEmpty()) {
114 0 : if (IncludeList.contains(DatabasePath)) {
115 0 : IncludeList.removeOne(DatabasePath);
116 : }
117 :
118 0 : ui->RemoveCombo->clear();
119 0 : ui->RemoveCombo->addItems(IncludeList);
120 : }
121 0 : }
122 :
123 : void
124 0 : dbe::IncludeFileWidget::SetController()
125 : {
126 0 : connect(ui->SelectFileButton, SIGNAL(clicked()), this, SLOT(SelectFileToInclude()), Qt::UniqueConnection);
127 0 : connect(ui->CreateFileButton, SIGNAL(clicked()), this, SLOT(CreateFileToInclude()), Qt::UniqueConnection);
128 0 : connect(ui->AddToIncludeButton, SIGNAL(clicked()), this, SLOT(AddFileToInclude()), Qt::UniqueConnection);
129 0 : connect(ui->RemoveButton, SIGNAL(clicked()), this, SLOT(RemoveFileFromInclude()), Qt::UniqueConnection);
130 0 : connect(ui->RemoveCombo, SIGNAL(activated(int)), this, SLOT(RemoveFileFromInclude(int)), Qt::UniqueConnection);
131 0 : connect(ui->DirectoryCombo,
132 : SIGNAL(activated(const QString&)),
133 : this,
134 : SLOT(SetDirectory(const QString&)),
135 : Qt::UniqueConnection);
136 0 : connect(ui->ExitButton, SIGNAL(clicked()), this, SLOT(close()));
137 0 : }
138 :
139 : void
140 0 : dbe::IncludeFileWidget::SelectFileToInclude()
141 : {
142 0 : ui->AddFileLine->clear();
143 0 : QStringList Files;
144 :
145 0 : if (!Directory.isEmpty()) {
146 0 : SelectFile->setDirectory(Directory);
147 : }
148 :
149 0 : if (SelectFile->exec()) {
150 0 : Files = SelectFile->selectedFiles();
151 : }
152 :
153 0 : if (Files.isEmpty()) {
154 0 : return;
155 : }
156 :
157 0 : for (const QString& FileToInclude : Files) {
158 0 : QFileInfo f(FileToInclude);
159 :
160 0 : if (Directory.isEmpty()) {
161 0 : ui->AddFileLine->addItem(f.absoluteFilePath());
162 : } else {
163 0 : ui->AddFileLine->addItem(f.absoluteFilePath().remove(Directory + QString('/'), Qt::CaseSensitive));
164 : }
165 0 : }
166 :
167 0 : CheckInclude();
168 0 : }
169 :
170 : void
171 0 : dbe::IncludeFileWidget::AddFileToInclude()
172 : {
173 0 : BOOST_SCOPE_EXIT(void)
174 : {
175 0 : QApplication::restoreOverrideCursor();
176 0 : }
177 0 : BOOST_SCOPE_EXIT_END
178 :
179 0 : QApplication::setOverrideCursor(Qt::WaitCursor);
180 :
181 0 : for (int i = 0; i < ui->AddFileLine->count(); ++i) {
182 0 : QString File = ui->AddFileLine->item(i)->text();
183 :
184 0 : if (!File.isEmpty()) {
185 : /// Need to strip file....
186 0 : for (QString& PathFile : FolderPathList) {
187 0 : if (File.startsWith(PathFile)) {
188 0 : File = File.replace(0, PathFile.size(), "");
189 0 : break;
190 : }
191 : }
192 :
193 0 : config::api::commands::file::add(DatabasePath, File);
194 :
195 0 : StatusBar->setPalette(QApplication::palette(this));
196 0 : StatusBar->showMessage(QString("The file %1 was added to the included files").arg(File));
197 : }
198 0 : }
199 :
200 0 : SetRemoveComboBox();
201 :
202 0 : ui->AddFileLine->clear();
203 0 : ui->AddToIncludeButton->setDisabled(true);
204 0 : }
205 :
206 : void
207 0 : dbe::IncludeFileWidget::AddNewFileToInclude(const QString& File)
208 : {
209 0 : ui->AddFileLine->clear();
210 :
211 0 : QString includeFilePath;
212 0 : includeFilePath = File;
213 :
214 0 : if (includeFilePath.isEmpty()) {
215 0 : return;
216 : }
217 :
218 0 : QFileInfo includeFile = QFileInfo(includeFilePath);
219 :
220 0 : if (!includeFile.isFile()) {
221 0 : StatusBar->setPalette(StyleUtility::AlertStatusBarPallete);
222 0 : StatusBar->showMessage(QString("The file is NOT accessible. Check before usage"));
223 : } else {
224 0 : QString fileToInclude;
225 :
226 0 : if (Directory.isEmpty()) {
227 0 : fileToInclude = includeFile.absoluteFilePath();
228 0 : } else if ((includeFile.absoluteDir().canonicalPath()).compare(Directory) == 0)
229 0 : fileToInclude = includeFile.absoluteFilePath().remove(Directory + QString('/'), Qt::CaseSensitive);
230 : else {
231 0 : fileToInclude = includeFile.absoluteFilePath();
232 : }
233 :
234 0 : ui->AddFileLine->addItem(fileToInclude);
235 0 : ui->AddToIncludeButton->setDisabled(false);
236 0 : }
237 0 : }
238 :
239 : void
240 0 : dbe::IncludeFileWidget::RemoveFileFromInclude()
241 : {
242 0 : if (ui->RemoveCombo->currentIndex() != -1) {
243 0 : BOOST_SCOPE_EXIT(void)
244 : {
245 0 : QApplication::restoreOverrideCursor();
246 0 : }
247 0 : BOOST_SCOPE_EXIT_END
248 :
249 0 : QApplication::setOverrideCursor(Qt::WaitCursor);
250 :
251 0 : QString File = ui->RemoveCombo->currentText();
252 :
253 0 : config::api::commands::file::remove(DatabasePath, File);
254 0 : Removed = true;
255 0 : StatusBar->setPalette(QApplication::palette(this));
256 0 : StatusBar->showMessage(QString("The file %1 was removed from the included files").arg(File));
257 0 : SetRemoveComboBox();
258 0 : } else {
259 0 : StatusBar->setPalette(StyleUtility::AlertStatusBarPallete);
260 0 : StatusBar->showMessage(QString("The file is NOT selected. Use combo box to select one."));
261 : }
262 0 : }
263 :
264 : void
265 0 : dbe::IncludeFileWidget::RemoveFileFromInclude(int)
266 : {
267 0 : if (ui->RemoveCombo->currentIndex() != -1) {
268 0 : ui->RemoveButton->setEnabled(true);
269 0 : StatusBar->setPalette(QApplication::palette(this));
270 0 : StatusBar->showMessage(QString("The file %1 will be removed from the included files of %2")
271 0 : .arg(QFileInfo(ui->RemoveCombo->currentText()).fileName())
272 0 : .arg(QFileInfo(DatabasePath).fileName()));
273 : }
274 0 : }
275 :
276 : void
277 0 : dbe::IncludeFileWidget::SetDirectory(const QString& Dir)
278 : {
279 0 : if (dbPath.contains(Dir, Qt::CaseSensitivity::CaseSensitive) && QDir(Dir).exists()) {
280 0 : Directory = Dir;
281 0 : SelectFile->setDirectory(Directory);
282 : } else {
283 0 : Directory.clear();
284 0 : SelectFile->setDirectory(QDir("."));
285 : }
286 0 : }
287 :
288 : void
289 0 : dbe::IncludeFileWidget::CheckInclude()
290 : {
291 0 : SetRemoveComboBox();
292 :
293 0 : if (ui->AddFileLine->count() == 0) {
294 0 : StatusBar->setPalette(StyleUtility::AlertStatusBarPallete);
295 0 : StatusBar->showMessage(QString("Select existing file!"));
296 0 : ui->AddToIncludeButton->setDisabled(true);
297 :
298 0 : return;
299 : }
300 :
301 0 : std::vector<int> removedItems;
302 0 : for (int i = 0; i < ui->AddFileLine->count(); ++i) {
303 0 : const QString inc = ui->AddFileLine->item(i)->text();
304 :
305 0 : if (inc == DatabasePath) {
306 0 : removedItems.push_back(i);
307 : } else {
308 0 : for (int j = 0; j < ui->RemoveCombo->count(); ++j) {
309 0 : if (ui->RemoveCombo->itemText(j).compare(inc) == 0) {
310 0 : removedItems.push_back(i);
311 : }
312 : }
313 : }
314 0 : }
315 :
316 0 : if (removedItems.empty() == false) {
317 0 : QString message = "The following files will not be included because already included:";
318 :
319 0 : for (int i : removedItems) {
320 0 : QListWidgetItem* item = ui->AddFileLine->takeItem(i);
321 0 : message += "\n- " + item->text();
322 0 : delete item;
323 : }
324 :
325 0 : WARN("File inclusion issue", message.toStdString());
326 0 : }
327 :
328 0 : ui->AddToIncludeButton->setDisabled((ui->AddFileLine->count() == 0) ? true : false);
329 0 : }
330 :
331 : void
332 0 : dbe::IncludeFileWidget::CreateFileToInclude()
333 : {
334 0 : CreateWidget = new CreateDatabaseWidget(nullptr, true, Directory);
335 0 : connect(CreateWidget,
336 : SIGNAL(CanIncludeDatabase(const QString&)),
337 : this,
338 : SLOT(AddNewFileToInclude(const QString&)),
339 : Qt::UniqueConnection);
340 0 : CreateWidget->show();
341 0 : }
|