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 : /// Including QT Headers
7 : #include <QFileDialog>
8 : #include <QMessageBox>
9 : /// Including DBE
10 : #include "dbe/CreateDatabaseWidget.hpp"
11 : #include "dbe/StyleUtility.hpp"
12 : /// Including config headers
13 : #include "conffwk/ConfigObject.hpp"
14 : #include "conffwk/Configuration.hpp"
15 : #include "conffwk/Schema.hpp"
16 : #include "dbe/Exceptions.hpp"
17 : #include "dbe/FileInfo.hpp"
18 : #include "dbe/messenger.hpp"
19 :
20 : using namespace dunedaq::conffwk;
21 :
22 0 : dbe::CreateDatabaseWidget::CreateDatabaseWidget(QWidget* parent, bool Include, const QString& CreateDir)
23 : : QWidget(parent)
24 0 : , DirToCreate(CreateDir)
25 0 : , CreateToInclude(Include)
26 : {
27 0 : setupUi(this);
28 :
29 0 : connect(SchemaButton, SIGNAL(clicked()), this, SLOT(DefineSchema()), Qt::UniqueConnection);
30 0 : connect(SelectButton, SIGNAL(clicked()), this, SLOT(DefineDatabaseFile()), Qt::UniqueConnection);
31 :
32 0 : if (!CreateToInclude) {
33 0 : connect(CreateDatabaseButton, SIGNAL(clicked()), this, SLOT(CreateDatabaseFileLoad()), Qt::UniqueConnection);
34 0 : connect(
35 0 : CreateNoLoadDatabaseButton, SIGNAL(clicked()), this, SLOT(CreateDatabaseFileNoLoad()), Qt::UniqueConnection);
36 : } else {
37 0 : connect(CreateDatabaseButton, SIGNAL(clicked()), this, SLOT(CreateDatabaseFileInclude()), Qt::UniqueConnection);
38 0 : CreateDatabaseButton->setText("Create Database File");
39 0 : CreateNoLoadDatabaseButton->hide();
40 : }
41 :
42 0 : m_instructions->setText("Select schema files and define new database file. Database must contain at least one schema "
43 : "file, other includes may be added later via a File info window.");
44 0 : CreateDatabaseButton->setDisabled(true);
45 0 : CreateNoLoadDatabaseButton->setDisabled(true);
46 :
47 0 : DatabaseName->setReadOnly(true);
48 0 : }
49 :
50 : void
51 0 : dbe::CreateDatabaseWidget::DefineSchema()
52 : {
53 0 : auto fd =
54 0 : new QFileDialog(this, tr("Select Schema File"), FileInfo::get_schema_path(), tr("XML schema files (*.schema.xml)"));
55 0 : fd->setFileMode(QFileDialog::ExistingFile);
56 0 : fd->setViewMode(QFileDialog::Detail);
57 0 : fd->setAcceptMode(QFileDialog::AcceptOpen);
58 0 : fd->setSidebarUrls(FileInfo::get_path_urls());
59 0 : if (!(fd->exec() == QDialog::Accepted)) {
60 0 : return;
61 : }
62 0 : FileInfo::set_schema_path(fd->directory().path());
63 :
64 0 : auto files = fd->selectedFiles();
65 0 : for (auto file : files) {
66 0 : QFileInfo SchemaFile = QFileInfo(file);
67 0 : if (!SchemaFile.isFile()) {
68 0 : m_instructions->setPalette(StyleUtility::AlertStatusBarPallete);
69 0 : m_instructions->setText(QString("The file is not accessible. Check before usage"));
70 : } else {
71 0 : if (SchemaFile.fileName().contains("schema")) {
72 0 : schema_list->addItem(FileInfo::prune_path(file));
73 : } else {
74 0 : m_instructions->setPalette(StyleUtility::AlertStatusBarPallete);
75 0 : m_instructions->setText(QString("The file %1 is not a schema file").arg(SchemaFile.absoluteFilePath()));
76 : }
77 : }
78 0 : }
79 :
80 0 : if (schema_list->count() > 0 && !DatabaseName->text().isEmpty()) {
81 0 : CreateDatabaseButton->setEnabled(true);
82 0 : CreateNoLoadDatabaseButton->setEnabled(true);
83 : }
84 0 : }
85 :
86 : void
87 0 : dbe::CreateDatabaseWidget::DefineDatabaseFile()
88 : {
89 0 : QString Dir;
90 :
91 0 : QString path = FileInfo::get_data_path().append("/NewDatabaseFile.data.xml");
92 :
93 0 : auto fd = new QFileDialog(this, tr("Select new DB File"), path, tr("XML data files (*.data.xml)"));
94 0 : fd->setFileMode(QFileDialog::AnyFile);
95 0 : fd->setViewMode(QFileDialog::Detail);
96 0 : fd->setAcceptMode(QFileDialog::AcceptSave);
97 0 : fd->setSidebarUrls(FileInfo::get_path_urls());
98 0 : if (!(fd->exec() == QDialog::Accepted)) {
99 0 : return;
100 : }
101 0 : FileInfo::set_data_path(fd->directory().path());
102 0 : auto files = fd->selectedFiles();
103 :
104 0 : DatabaseFile = QFileInfo(files.at(0));
105 :
106 0 : DatabaseName->setText(DatabaseFile.absoluteFilePath());
107 :
108 0 : if (schema_list->count() > 0) {
109 0 : CreateDatabaseButton->setEnabled(true);
110 0 : CreateNoLoadDatabaseButton->setEnabled(true);
111 : }
112 0 : }
113 :
114 : std::list<std::string>
115 0 : dbe::CreateDatabaseWidget::get_includes()
116 : {
117 0 : std::list<std::string> includes;
118 :
119 0 : for (int row = 0; row < schema_list->count(); ++row) {
120 0 : auto item = schema_list->item(row)->text();
121 0 : includes.push_back(item.toStdString());
122 0 : }
123 0 : return includes;
124 0 : }
125 :
126 : bool
127 0 : dbe::CreateDatabaseWidget::create_database_file(std::string extra_text)
128 : {
129 0 : Configuration db("oksconflibs");
130 0 : try {
131 0 : const std::string DatabaseName = DatabaseFile.absoluteFilePath().toStdString();
132 0 : db.create(DatabaseName, get_includes());
133 0 : db.commit();
134 0 : } catch (dunedaq::conffwk::Exception const& ex) {
135 0 : FAIL("Database creation failure", dbe::config::errors::parse(ex).c_str());
136 0 : return false;
137 0 : }
138 :
139 0 : QMessageBox::information(
140 0 : 0, tr("DBE"), QString::fromStdString("The Database was created.\n" + extra_text), QMessageBox::Ok);
141 0 : return true;
142 0 : }
143 : void
144 0 : dbe::CreateDatabaseWidget::CreateDatabaseFileLoad()
145 : {
146 0 : if (create_database_file("Now the DB will be loaded into the Editor!\n")) {
147 0 : emit CanLoadDatabase(DatabaseFile.absoluteFilePath());
148 0 : close();
149 : }
150 0 : }
151 :
152 : void
153 0 : dbe::CreateDatabaseWidget::CreateDatabaseFileNoLoad()
154 : {
155 0 : if (create_database_file("")) {
156 0 : close();
157 : }
158 0 : }
159 :
160 : void
161 0 : dbe::CreateDatabaseWidget::CreateDatabaseFileInclude()
162 : {
163 0 : if (create_database_file("")) {
164 0 : emit CanIncludeDatabase(DatabaseFile.absoluteFilePath());
165 0 : close();
166 : }
167 0 : }
|