Line data Source code
1 : /// Including QT#include <QHeaderView>
2 : #include "dbe/tableselection.hpp"
3 :
4 : #include "dbe/confaccessor.hpp"
5 : #include <QAbstractItemView>
6 : #include <QScrollBar>
7 : #include <QMenu>
8 : #include <QLabel>
9 : #include <QHBoxLayout>
10 : #include <QContextMenuEvent>
11 : #include <QDialog>
12 : #include <QLineEdit>
13 : #include <QVariant>
14 : #include <QHeaderView>
15 :
16 : /// Including DBE
17 : #include "dbe/CustomTableView.hpp"
18 : #include "dbe/ObjectEditor.hpp"
19 : #include "dbe/ObjectCreator.hpp"
20 : #include "dbe/messenger.hpp"
21 :
22 : #include "dbe/MainWindow.hpp"
23 :
24 : //-----------------------------------------------------------------------------------------------------
25 0 : dbe::CustomTableView::CustomTableView ( QWidget * parent )
26 : : QTableView ( parent ),
27 0 : m_context_menu ( nullptr ),
28 0 : FindObject ( nullptr ),
29 0 : editObject ( nullptr ),
30 0 : deleteObjectAc ( nullptr ),
31 0 : refByAc ( nullptr ),
32 0 : refByAcOnlyComp ( nullptr ),
33 0 : copyObjectAc ( nullptr ),
34 0 : m_find_object_dialog ( nullptr ),
35 0 : LineEdit ( nullptr ),
36 0 : NextButton ( nullptr ),
37 0 : GoButton ( nullptr ),
38 0 : ListIndex ( 0 )
39 : {
40 0 : verticalHeader()->setVisible(true);
41 0 : verticalHeader()->setSectionResizeMode ( QHeaderView::Interactive );
42 :
43 0 : horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
44 0 : horizontalHeader()->setMaximumSectionSize(750);
45 0 : setSortingEnabled ( true );
46 0 : setAlternatingRowColors ( true );
47 0 : setSelectionMode ( SelectionMode::SingleSelection );
48 0 : setSelectionBehavior ( QAbstractItemView::SelectionBehavior::SelectRows );
49 0 : setHorizontalScrollMode(ScrollMode::ScrollPerPixel);
50 0 : setVerticalScrollMode(ScrollMode::ScrollPerPixel);
51 0 : setWordWrap(true);
52 0 : setTextElideMode(Qt::ElideRight);
53 :
54 0 : connect ( this, SIGNAL ( activated(const QModelIndex&) ), this, SLOT ( slot_edit_object() ) );
55 :
56 0 : }
57 : //-----------------------------------------------------------------------------------------------------
58 :
59 : //-----------------------------------------------------------------------------------------------------
60 0 : void dbe::CustomTableView::contextMenuEvent ( QContextMenuEvent * Event )
61 : {
62 0 : if ( m_context_menu == nullptr )
63 : {
64 0 : m_context_menu = new QMenu ( this );
65 0 : CreateActions();
66 : }
67 :
68 0 : QModelIndex Index = indexAt ( Event->pos() );
69 :
70 0 : if ( Index.isValid() ) {
71 0 : for (int item=0; item<m_last_object_item; ++item) {
72 0 : m_context_menu->actions().at ( item )->setVisible ( true );
73 : }
74 : }
75 : else {
76 0 : for (int item=0; item<m_last_object_item; ++item) {
77 0 : m_context_menu->actions().at ( item )->setVisible ( false );
78 : }
79 : }
80 0 : m_context_menu->exec ( Event->globalPos() );
81 0 : }
82 : //-----------------------------------------------------------------------------------------------------
83 :
84 : //-----------------------------------------------------------------------------------------------------
85 0 : void dbe::CustomTableView::FindObjectSlot()
86 : {
87 0 : if ( m_find_object_dialog != nullptr )
88 : {
89 0 : delete m_find_object_dialog;
90 0 : m_find_object_dialog = nullptr;
91 : }
92 :
93 0 : m_find_object_dialog = new QDialog ( this );
94 0 : m_find_object_dialog->setSizePolicy ( QSizePolicy::Preferred, QSizePolicy::Preferred );
95 0 : m_find_object_dialog->setToolTip ( "Type string to edit line and press Enter." );
96 0 : m_find_object_dialog->setWindowTitle ( "Search for an object in the table" );
97 :
98 0 : QHBoxLayout * Layout = new QHBoxLayout ( m_find_object_dialog );
99 0 : QLabel * Label = new QLabel ( QString ( "Find Object:" ), m_find_object_dialog );
100 :
101 0 : NextButton = new QPushButton ( "Next" );
102 0 : GoButton = new QPushButton ( "Go !" );
103 :
104 0 : LineEdit = new QLineEdit ( m_find_object_dialog );
105 0 : LineEdit->setToolTip ( "Type string and press Enter" );
106 :
107 0 : Layout->addWidget ( Label );
108 0 : Layout->addWidget ( LineEdit );
109 0 : Layout->addWidget ( GoButton );
110 0 : Layout->addWidget ( NextButton );
111 :
112 0 : m_find_object_dialog->setLayout ( Layout );
113 0 : m_find_object_dialog->show();
114 0 : NextButton->setDisabled ( true );
115 :
116 0 : connect ( LineEdit, SIGNAL ( textEdited ( QString ) ), this,
117 : SLOT ( EditedSearchString ( QString ) ) );
118 0 : connect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( slot_go_to_object() ) );
119 0 : connect ( GoButton, SIGNAL ( clicked() ), this, SLOT ( slot_go_to_object() ) );
120 0 : connect ( NextButton, SIGNAL ( clicked() ), this, SLOT ( GoToNext() ) );
121 0 : }
122 : //-----------------------------------------------------------------------------------------------------
123 :
124 : //-----------------------------------------------------------------------------------------------------
125 0 : void dbe::CustomTableView::slot_go_to_object()
126 : {
127 0 : ListIndex = 0;
128 0 : ListOfMatch.clear();
129 :
130 0 : QString UserType = LineEdit->text();
131 :
132 0 : if ( UserType.isEmpty() )
133 : {
134 0 : return;
135 : }
136 :
137 0 : QAbstractItemModel * Model = model();
138 :
139 0 : if ( Model != nullptr )
140 : {
141 0 : QVariant StringCriterium = QVariant ( UserType );
142 0 : QModelIndex WhereToStartSearch = Model->index ( 0, 0 );
143 0 : ListOfMatch = Model->match ( WhereToStartSearch, Qt::DisplayRole, StringCriterium, 1000,
144 0 : Qt::MatchContains | Qt::MatchWrap );
145 :
146 0 : if ( ListOfMatch.size() > 0 )
147 : {
148 0 : ListIndex = 0;
149 0 : auto val=ListOfMatch.value ( ListIndex );
150 0 : selectRow ( val.row() );
151 : // resizeColumnToContents ( val.row() );
152 0 : scrollTo (val , QAbstractItemView::PositionAtCenter );
153 :
154 0 : GoButton->setDisabled ( true );
155 0 : NextButton->setEnabled ( true );
156 :
157 0 : disconnect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( slot_go_to_object() ) );
158 0 : connect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( GoToNext() ) );
159 : }
160 0 : }
161 0 : }
162 : //-----------------------------------------------------------------------------------------------------
163 :
164 : //-----------------------------------------------------------------------------------------------------
165 0 : void dbe::CustomTableView::GoToNext()
166 : {
167 0 : if ( ( LineEdit->text() ).isEmpty() )
168 : {
169 0 : ListIndex = 0;
170 0 : ListOfMatch.clear();
171 0 : return;
172 : }
173 :
174 0 : if ( ListOfMatch.size() > 0 )
175 : {
176 0 : if ( ( ++ListIndex ) < ListOfMatch.size() )
177 : {
178 0 : scrollTo ( ListOfMatch.value ( ListIndex ), QAbstractItemView::EnsureVisible );
179 0 : selectRow ( ListOfMatch.value ( ListIndex ).row() );
180 0 : resizeColumnToContents ( ListIndex );
181 : }
182 : else
183 : {
184 0 : ListIndex = 0;
185 0 : scrollTo ( ListOfMatch.value ( ListIndex ), QAbstractItemView::EnsureVisible );
186 0 : selectRow ( ListOfMatch.value ( ListIndex ).row() );
187 0 : resizeColumnToContents ( ListIndex );
188 : }
189 : }
190 : }
191 : //-----------------------------------------------------------------------------------------------------
192 :
193 : //-----------------------------------------------------------------------------------------------------
194 0 : void dbe::CustomTableView::EditedSearchString ( QString Text )
195 : {
196 0 : Q_UNUSED ( Text )
197 :
198 0 : connect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( slot_go_to_object() ) );
199 0 : disconnect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( GoToNext() ) );
200 :
201 0 : GoButton->setEnabled ( true );
202 0 : NextButton->setDisabled ( true );
203 0 : }
204 : //-----------------------------------------------------------------------------------------------------
205 :
206 : //-----------------------------------------------------------------------------------------------------
207 0 : void dbe::CustomTableView::slot_edit_object()
208 : {
209 0 : QModelIndex const & index = this->currentIndex();
210 :
211 0 : if ( index.isValid() )
212 : {
213 0 : if ( models::tableselection const * selectionmodel =
214 0 : dynamic_cast<models::tableselection const *> ( index.model() ) )
215 : {
216 0 : if ( models::table const * srcmodel =
217 0 : dynamic_cast<const dbe::models::table *> ( selectionmodel->sourceModel() ) )
218 : {
219 0 : tref obj = srcmodel->GetTableObject ( selectionmodel->mapToSource ( index ).row() );
220 :
221 0 : if ( not obj.is_null() )
222 : {
223 0 : emit OpenEditor ( obj );
224 : }
225 0 : }
226 : }
227 : }
228 0 : }
229 : //-----------------------------------------------------------------------------------------------------
230 :
231 : //-----------------------------------------------------------------------------------------------------
232 0 : void dbe::CustomTableView::referencedBy_OnlyComposite()
233 : {
234 0 : QModelIndex const & Index = this->currentIndex();
235 :
236 0 : const dbe::models::tableselection * Model =
237 0 : dynamic_cast<const dbe::models::tableselection *> ( Index.model() );
238 :
239 0 : const dbe::models::table * SourceModel = dynamic_cast<const dbe::models::table *> ( Model
240 0 : ->sourceModel() );
241 :
242 0 : if ( !Index.isValid() || !Model )
243 : {
244 0 : return;
245 : }
246 :
247 0 : tref obj = SourceModel->GetTableObject ( Model->mapToSource ( Index ).row() );
248 0 : referencedBy ( obj, true );
249 0 : }
250 : //-----------------------------------------------------------------------------------------------------
251 :
252 : //-----------------------------------------------------------------------------------------------------
253 0 : void dbe::CustomTableView::referencedBy_All()
254 : {
255 0 : QModelIndex const & Index = this->currentIndex();
256 0 : const dbe::models::tableselection * Model =
257 0 : dynamic_cast<const dbe::models::tableselection *> ( Index.model() );
258 :
259 0 : dbe::models::table const * SourceModel = dynamic_cast<const dbe::models::table *> ( Model
260 0 : ->sourceModel() );
261 :
262 0 : if ( !Index.isValid() || !Model )
263 : {
264 0 : return;
265 : }
266 :
267 0 : tref obj = SourceModel->GetTableObject ( Model->mapToSource ( Index ).row() );
268 0 : referencedBy ( obj, false );
269 0 : }
270 : //-----------------------------------------------------------------------------------------------------
271 :
272 : //-----------------------------------------------------------------------------------------------------
273 0 : void dbe::CustomTableView::slot_copy_object()
274 : {
275 0 : QModelIndex const & Index = this->currentIndex();
276 :
277 0 : dbe::models::tableselection const * Model =
278 0 : dynamic_cast<const dbe::models::tableselection *> ( Index.model() );
279 :
280 0 : dbe::models::table const * SourceModel = dynamic_cast<const dbe::models::table *> ( Model
281 0 : ->sourceModel() );
282 :
283 0 : if ( Index.isValid() and Model )
284 : {
285 0 : tref obj = SourceModel->GetTableObject ( Model->mapToSource ( Index ).row() );
286 :
287 0 : if ( not obj.is_null() )
288 : {
289 0 : ( new ObjectCreator ( obj ) )->show();
290 : }
291 0 : }
292 0 : }
293 : //-----------------------------------------------------------------------------------------------------
294 :
295 : //-----------------------------------------------------------------------------------------------------
296 0 : void dbe::CustomTableView::slot_create_object()
297 : {
298 : // TODO implement dbe::CustomTableView::slot_create_object
299 0 : throw std::string ( " dbe::CustomTableView::slot_create_object is not yet implemented " );
300 : }
301 : //-----------------------------------------------------------------------------------------------------
302 :
303 : //-----------------------------------------------------------------------------------------------------
304 0 : void dbe::CustomTableView::slot_delete_objects()
305 : {
306 0 : if ( this->model() != nullptr )
307 : {
308 0 : QModelIndexList qindices = this->selectedIndexes();
309 0 : std::vector<QModelIndex> indices;
310 :
311 0 : for ( QModelIndex q : qindices )
312 : {
313 0 : if ( q.isValid() )
314 : {
315 0 : indices.push_back ( q );
316 : }
317 : }
318 :
319 0 : if ( dbe::models::tableselection * casted =
320 0 : dynamic_cast<dbe::models::tableselection *> ( this->model() ) )
321 : {
322 0 : casted->delete_objects ( indices.begin(), indices.end() );
323 : }
324 0 : else if ( dbe::models::table * casted = dynamic_cast<dbe::models::table *>
325 0 : ( this->model() ) )
326 : {
327 0 : casted->delete_objects ( indices.begin(), indices.end() );
328 : }
329 : else
330 : {
331 0 : WARN ( "Object Deleting",
332 : "Object deletion failed due to the internal model being in invalid state",
333 0 : "You are advised to restart the application before proceeding any further" );
334 : }
335 0 : }
336 :
337 0 : }
338 : //-----------------------------------------------------------------------------------------------------
339 :
340 : //-----------------------------------------------------------------------------------------------------
341 0 : void dbe::CustomTableView::CreateActions()
342 : {
343 0 : editObject = new QAction ( tr ( "&Edit Object" ), this );
344 0 : connect ( editObject, SIGNAL ( triggered() ), this, SLOT ( slot_edit_object() ) );
345 0 : m_context_menu->addAction ( editObject );
346 :
347 0 : deleteObjectAc = new QAction ( tr ( "&Delete Object" ), this );
348 0 : connect ( deleteObjectAc, SIGNAL ( triggered() ), this, SLOT ( slot_delete_objects() ) );
349 0 : m_context_menu->addAction ( deleteObjectAc );
350 :
351 0 : refByAc = new QAction ( tr ( "Referenced B&y (All objects)" ), this );
352 0 : refByAc->setToolTip ( "Find all objects which reference the selected object" );
353 0 : refByAc->setStatusTip ( refByAc->toolTip() );
354 0 : connect ( refByAc, SIGNAL ( triggered() ), this, SLOT ( referencedBy_All() ) );
355 0 : m_context_menu->addAction ( refByAc );
356 :
357 0 : refByAcOnlyComp = new QAction ( tr ( "Referenced B&y (Only Composite)" ), this );
358 0 : refByAcOnlyComp->setToolTip (
359 : "Find objects (ONLY Composite ones) which reference the selected object" );
360 0 : refByAcOnlyComp->setStatusTip ( refByAcOnlyComp->toolTip() );
361 0 : connect ( refByAcOnlyComp, SIGNAL ( triggered() ), this,
362 : SLOT ( referencedBy_OnlyComposite() ) );
363 0 : m_context_menu->addAction ( refByAcOnlyComp );
364 :
365 0 : copyObjectAc = new QAction ( tr ( "Copy This Object Into A &New One" ), this );
366 0 : connect ( copyObjectAc, SIGNAL ( triggered() ), this, SLOT ( slot_copy_object() ) );
367 0 : m_context_menu->addAction ( copyObjectAc );
368 :
369 0 : m_context_menu->addSeparator();
370 0 : m_last_object_item = m_context_menu->actions().size();
371 :
372 0 : FindObject = new QAction ( tr ( "&Find Object" ), this );
373 : // FindObject->setShortcut ( QKeySequence ( tr ( "Ctrl+F" ) ) );
374 : // FindObject->setShortcutContext ( Qt::WidgetShortcut );
375 0 : connect ( FindObject, SIGNAL ( triggered() ), this, SLOT ( FindObjectSlot() ) );
376 : // addAction (FindObject);
377 0 : m_context_menu->addAction ( FindObject );
378 :
379 0 : }
380 : //-----------------------------------------------------------------------------------------------------
381 :
382 : //-----------------------------------------------------------------------------------------------------
383 0 : void dbe::CustomTableView::referencedBy ( tref obj, bool onlyComposite )
384 : {
385 0 : if ( not obj.is_null() )
386 : {
387 0 : MainWindow::findthis()->get_view()->referencedBy ( onlyComposite, obj );
388 : }
389 0 : }
390 : //-----------------------------------------------------------------------------------------------------
391 :
|