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