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 "dbe/FileInfo.hpp"
8 : #include "dbe/ui_constants.hpp"
9 :
10 : #include <QAction>
11 : #include <QMenu>
12 : #include <QMessageBox>
13 : #include <QContextMenuEvent>
14 : #include <QHBoxLayout>
15 : #include <QHeaderView>
16 : #include <QLabel>
17 : #include <QShortcut>
18 : /// Including DBE
19 : #include "dbe/CustomFileView.hpp"
20 : #include "dbe/IncludeFileWidget.hpp"
21 :
22 0 : dbe::CustomFileView::CustomFileView ( QWidget * parent )
23 : : QTableView ( parent ),
24 0 : ContextMenu ( nullptr ),
25 0 : LaunchIncludeEditor ( nullptr ),
26 0 : HideReadOnlyFiles ( nullptr ),
27 0 : FindFile ( nullptr ),
28 0 : FindFileDialog ( nullptr ),
29 0 : LineEdit ( nullptr ),
30 0 : NextButton ( nullptr ),
31 0 : GoButton ( nullptr )
32 : {
33 0 : ConnectActions();
34 0 : CreateContextMenu();
35 0 : setSelectionBehavior ( SelectionBehavior::SelectRows );
36 0 : horizontalHeader()->setSectionResizeMode ( QHeaderView::ResizeMode::Stretch );
37 0 : setSortingEnabled ( true );
38 :
39 0 : connect ( this, SIGNAL(activated(QModelIndex)),
40 : this, SLOT(file_info_slot(QModelIndex)) );
41 0 : }
42 :
43 0 : void dbe::CustomFileView::CreateActions()
44 : {
45 0 : LaunchIncludeEditor = new QAction ( tr ( "Add/Remove Files" ), this );
46 0 : LaunchIncludeEditor->setShortcutContext ( Qt::WidgetShortcut );
47 0 : connect ( LaunchIncludeEditor, SIGNAL ( triggered() ), this,
48 : SLOT ( LaunchIncludeEditorSlot() ) );
49 0 : ContextMenu->addAction ( LaunchIncludeEditor );
50 :
51 0 : m_file_info_action = new QAction (tr ("File &information"));
52 0 : connect ( m_file_info_action, SIGNAL(triggered()),
53 : this, SLOT(file_info_slot()) );
54 0 : ContextMenu->addAction ( m_file_info_action );
55 :
56 0 : HideReadOnlyFiles = new QAction ( tr ( "Hide Read Only Files" ), this );
57 0 : HideReadOnlyFiles->setShortcutContext ( Qt::WidgetShortcut );
58 0 : HideReadOnlyFiles->setCheckable ( true );
59 0 : HideReadOnlyFiles->setChecked ( false );
60 0 : connect ( HideReadOnlyFiles, SIGNAL ( triggered ( bool ) ), this,
61 : SLOT ( HideReadOnlyFilesSlot ( bool ) ) );
62 0 : ContextMenu->addAction ( HideReadOnlyFiles );
63 :
64 :
65 0 : FindFile = new QAction ( tr ( "Find File" ), this );
66 0 : FindFile->setShortcut ( QKeySequence ( tr ( "Ctrl+F" ) ) );
67 0 : FindFile->setShortcutContext ( Qt::WidgetShortcut );
68 0 : connect ( FindFile, SIGNAL ( triggered() ), this, SLOT ( FindFileSlot() ) );
69 : // connect ( Shortcut, SIGNAL ( activated() ), this, SLOT ( FindFileSlot() ) );
70 0 : ContextMenu->addAction ( FindFile );
71 0 : }
72 :
73 0 : void dbe::CustomFileView::ConnectActions()
74 : {
75 0 : connect ( this, SIGNAL ( clicked ( QModelIndex ) ), this,
76 : SLOT ( ChangeSelection ( QModelIndex ) ) );
77 0 : }
78 :
79 0 : void dbe::CustomFileView::CreateContextMenu()
80 : {
81 0 : if ( ContextMenu == nullptr )
82 : {
83 0 : ContextMenu = new QMenu ( this );
84 0 : CreateActions();
85 : }
86 0 : }
87 :
88 0 : void dbe::CustomFileView::contextMenuEvent ( QContextMenuEvent * Event )
89 : {
90 0 : QModelIndex index = indexAt ( Event->pos() );
91 :
92 0 : if ( index.isValid() )
93 : {
94 0 : if ( model()->data ( model()->index ( index.row(), 2 ) ).toString() == "RO" ) {
95 0 : ContextMenu->actions().at ( 0 )->setVisible ( false );
96 : }
97 : else {
98 0 : ContextMenu->actions().at ( 0 )->setVisible ( true );
99 : }
100 0 : ContextMenu->actions().at ( 1 )->setVisible ( true );
101 :
102 0 : setCurrentIndex ( index );
103 0 : selectionModel()->setCurrentIndex ( index, QItemSelectionModel::NoUpdate );
104 : }
105 : else {
106 0 : ContextMenu->actions().at(0)->setVisible(false);
107 0 : ContextMenu->actions().at(1)->setVisible(false);
108 : }
109 0 : ContextMenu->exec ( Event->globalPos() );
110 0 : }
111 :
112 0 : void dbe::CustomFileView::GoToFile()
113 : {
114 0 : ListIndex = 0;
115 0 : ListOfMatch.clear();
116 :
117 0 : QString UserType = LineEdit->text();
118 :
119 0 : if ( UserType.isEmpty() )
120 : {
121 0 : return;
122 : }
123 :
124 0 : QAbstractItemModel * Model = model();
125 :
126 0 : if ( Model != nullptr )
127 : {
128 0 : QVariant StringCriterium = QVariant ( UserType );
129 :
130 0 : QModelIndex find_by_name = Model->index ( 0, 0 );
131 0 : QModelIndex find_by_path = Model->index ( 0, 1 );
132 :
133 0 : Qt::MatchFlags options = Qt::MatchContains;
134 :
135 0 : if ( CaseSensitiveCheckBox->isChecked() )
136 : {
137 0 : options = options | Qt::MatchCaseSensitive;
138 : }
139 :
140 0 : if ( WholeWordCheckBox->isChecked() )
141 : {
142 0 : options = Qt::MatchExactly;
143 : }
144 :
145 0 : ListOfMatch = Model->match ( find_by_name, Qt::DisplayRole, StringCriterium, 1000,
146 0 : options );
147 :
148 0 : ListOfMatch.append (
149 0 : Model->match ( find_by_path, Qt::DisplayRole, StringCriterium, 1000, options ) );
150 :
151 0 : if ( ListOfMatch.size() > 0 )
152 : {
153 0 : ListIndex = 0;
154 0 : scrollTo ( ListOfMatch.value ( ListIndex ), QAbstractItemView::EnsureVisible );
155 0 : selectRow ( ListOfMatch.value ( ListIndex ).row() );
156 0 : resizeColumnToContents ( ListIndex );
157 :
158 0 : GoButton->setDisabled ( true );
159 0 : NextButton->setEnabled ( true );
160 :
161 0 : disconnect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( GoToFile() ) );
162 0 : connect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( GoToNext() ) );
163 0 : ChangeSelection ( ListOfMatch.at ( ListIndex ) );
164 : }
165 0 : }
166 0 : }
167 :
168 0 : void dbe::CustomFileView::GoToNext()
169 : {
170 0 : if ( ( LineEdit->text() ).isEmpty() )
171 : {
172 0 : ListIndex = 0;
173 0 : ListOfMatch.clear();
174 0 : return;
175 : }
176 :
177 0 : if ( ListOfMatch.size() > 0 )
178 : {
179 0 : if ( ( ++ListIndex ) < ListOfMatch.size() )
180 : {
181 0 : scrollTo ( ListOfMatch.value ( ListIndex ), QAbstractItemView::EnsureVisible );
182 0 : selectRow ( ListOfMatch.value ( ListIndex ).row() );
183 0 : resizeColumnToContents ( ListIndex );
184 0 : ChangeSelection ( ListOfMatch.at ( ListIndex ) );
185 : }
186 : else
187 : {
188 0 : ListIndex = 0;
189 0 : scrollTo ( ListOfMatch.value ( ListIndex ), QAbstractItemView::EnsureVisible );
190 0 : selectRow ( ListOfMatch.value ( ListIndex ).row() );
191 0 : resizeColumnToContents ( ListIndex );
192 0 : ChangeSelection ( ListOfMatch.at ( ListIndex ) );
193 : }
194 : }
195 : }
196 :
197 0 : void dbe::CustomFileView::FindFileSlot()
198 : {
199 0 : if ( FindFileDialog != nullptr )
200 : {
201 0 : delete FindFileDialog;
202 0 : FindFileDialog = nullptr;
203 : }
204 :
205 0 : FindFileDialog = new QDialog ( this );
206 0 : FindFileDialog->setSizePolicy ( QSizePolicy::Preferred, QSizePolicy::Preferred );
207 0 : FindFileDialog->setToolTip ( "Type string to edit line and press Enter." );
208 0 : FindFileDialog->setWindowTitle ( "Search for a file name in the table" );
209 :
210 0 : QHBoxLayout * FileLayout = new QHBoxLayout();
211 0 : QLabel * Label = new QLabel ( QString ( "Find File:" ) );
212 0 : NextButton = new QPushButton ( "Next" );
213 0 : GoButton = new QPushButton ( "Go !" );
214 0 : LineEdit = new QLineEdit();
215 0 : LineEdit->setToolTip ( "Type string and press Enter" );
216 :
217 0 : FileLayout->addWidget ( Label );
218 0 : FileLayout->addWidget ( LineEdit );
219 0 : FileLayout->addWidget ( GoButton );
220 0 : FileLayout->addWidget ( NextButton );
221 :
222 0 : QHBoxLayout * ButtonsLayout = new QHBoxLayout();
223 0 : WholeWordCheckBox = new QCheckBox ( "Whole String" );
224 0 : CaseSensitiveCheckBox = new QCheckBox ( "Case Sensitive" );
225 :
226 0 : ButtonsLayout->addWidget ( WholeWordCheckBox );
227 0 : ButtonsLayout->addWidget ( CaseSensitiveCheckBox );
228 :
229 0 : QVBoxLayout * FindDialogLayout = new QVBoxLayout();
230 0 : FindDialogLayout->addItem ( FileLayout );
231 0 : FindDialogLayout->addItem ( ButtonsLayout );
232 :
233 0 : FindFileDialog->setLayout ( FindDialogLayout );
234 0 : FindFileDialog->show();
235 : //NextButton->setDisabled(true);
236 :
237 0 : connect ( LineEdit, SIGNAL ( textEdited ( QString ) ), this,
238 : SLOT ( EditedSearchString ( QString ) ) );
239 0 : connect ( WholeWordCheckBox, SIGNAL ( clicked() ), this, SLOT ( EditedSearchString() ) );
240 0 : connect ( CaseSensitiveCheckBox, SIGNAL ( clicked() ), this,
241 : SLOT ( EditedSearchString() ) );
242 :
243 0 : connect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( GoToFile() ) );
244 0 : connect ( GoButton, SIGNAL ( clicked() ), this, SLOT ( GoToFile() ) );
245 0 : connect ( NextButton, SIGNAL ( clicked() ), this, SLOT ( GoToNext() ) );
246 0 : }
247 :
248 0 : void dbe::CustomFileView::file_info_slot() {
249 0 : file_info_slot(currentIndex());
250 0 : }
251 :
252 0 : void dbe::CustomFileView::file_info_slot(QModelIndex index) {
253 0 : QString file = model()->data ( model()->index ( index.row(), 0,
254 0 : index.parent() ) ).toString();
255 0 : QString path = model()->data ( model()->index ( index.row(), 1,
256 0 : index.parent() ) ).toString();
257 0 : auto full_name = path + "/" + file;
258 :
259 0 : FileInfo::show_file_info(full_name);
260 0 : }
261 :
262 0 : void dbe::CustomFileView::LaunchIncludeEditorSlot()
263 : {
264 0 : QModelIndex index = currentIndex();
265 0 : QString file = model()->data ( model()->index ( index.row(), 0,
266 0 : index.parent() ) ).toString();
267 0 : QString path = model()->data ( model()->index ( index.row(), 1,
268 0 : index.parent() ) ).toString();
269 0 : auto full_name = path + "/" + file;
270 0 : auto * file_widget = new IncludeFileWidget ( full_name );
271 :
272 0 : file_widget->show();
273 0 : }
274 :
275 0 : void dbe::CustomFileView::HideReadOnlyFilesSlot ( bool Hide )
276 : {
277 0 : for ( int i = 0; i < model()->rowCount(); ++i )
278 : {
279 0 : if ( model()->data ( model()->index ( i, 2 ) ).toString() == "RO" )
280 : {
281 0 : if ( Hide )
282 : {
283 0 : hideRow ( i );
284 : }
285 : else
286 : {
287 0 : showRow ( i );
288 : }
289 : }
290 : }
291 0 : }
292 :
293 0 : void dbe::CustomFileView::EditedSearchString ( QString Text )
294 : {
295 0 : Q_UNUSED ( Text )
296 :
297 0 : connect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( GoToFile() ) );
298 0 : disconnect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( GoToNext() ) );
299 :
300 0 : GoButton->setEnabled ( true );
301 0 : NextButton->setDisabled ( true );
302 0 : }
303 :
304 0 : void dbe::CustomFileView::EditedSearchString()
305 : {
306 0 : QString Dummy ( "Dummy" );
307 0 : EditedSearchString ( Dummy );
308 0 : }
309 :
310 0 : void dbe::CustomFileView::ChangeSelection ( QModelIndex index )
311 : {
312 0 : QString file = model()->data ( model()->index ( index.row(), 0,
313 0 : index.parent() ) ).toString();
314 0 : QString path = model()->data ( model()->index ( index.row(), 1,
315 0 : index.parent() ) ).toString();
316 0 : auto full_name = path + "/" + file;
317 0 : emit stateChanged ( full_name );
318 0 : }
|