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