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