Salome HOME
updated copyright message
[plugins/hexoticplugin.git] / src / GUI / HexoticPluginGUI_TreeWidget.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "HexoticPluginGUI_TreeWidget.h"
21 #include <QKeyEvent>
22
23 namespace
24 {
25   bool isEditable( const QModelIndex& index )
26   {
27     return index.isValid() &&
28       index.flags() & Qt::ItemIsEditable && 
29       index.flags() & Qt::ItemIsEnabled &&
30       ( !index.data( Qt::UserRole + 1 ).isValid() || index.data( Qt::UserRole + 1 ).toInt() != 0 );
31   }
32 }
33
34 HexoticPluginGUI_TreeWidget::HexoticPluginGUI_TreeWidget( QWidget* parent )
35   : QTreeWidget( parent )
36 {
37 }
38
39 QModelIndex HexoticPluginGUI_TreeWidget::moveCursor( CursorAction action, Qt::KeyboardModifiers modifiers )
40 {
41   QModelIndex current = currentIndex();
42   int column = current.column();
43   if ( action == MoveNext ) {
44     if ( column < columnCount()-1 ) {
45       QModelIndex next = current.sibling( current.row(), column+1 );
46       if ( isEditable( next ) )
47         return next;
48     }
49     else {
50       QModelIndex next = current.sibling( current.row()+1, 0 );
51       if ( isEditable( next ) )
52         return next;
53     }
54   }
55   else if ( action == MovePrevious ) {
56     if ( column == 0 ) {
57       QModelIndex next = current.sibling( current.row()-1, columnCount()-1 );
58       if ( isEditable( next ) )
59         return next;
60     }
61     else {
62       QModelIndex next = current.sibling( current.row(), column-1 );
63       if ( isEditable( next ) )
64         return next;
65     }
66   }
67   return QTreeWidget::moveCursor( action, modifiers );
68 }
69
70 void HexoticPluginGUI_TreeWidget::keyPressEvent( QKeyEvent* e )
71 {
72   switch ( e->key() ) {
73   case Qt::Key_F2:
74     {
75       QModelIndex index = currentIndex();
76       if ( !isEditable( index ) ) {
77         for ( int i = 0; i < columnCount(); i++ ) {
78           QModelIndex sibling = index.sibling( index.row(), i );
79           if ( isEditable( sibling ) ) {
80             if ( !edit( sibling, EditKeyPressed, e ) )
81               e->ignore();
82           }
83         }
84       }
85     }
86     break;
87   default:
88     break;
89   }
90   QTreeWidget::keyPressEvent( e );
91 }