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