fw4spl
SListView.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2017-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "guiQt/editor/SListView.hpp"
8 
9 #include <fwCom/Signal.hxx>
10 #include <fwCom/Slots.hxx>
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwGuiQt/container/QtContainer.hpp>
15 
16 #include <fwServices/macros.hpp>
17 
18 #include <QEvent>
19 #include <QHBoxLayout>
20 #include <QKeyEvent>
21 
22 namespace guiQt
23 {
24 namespace editor
25 {
26 fwServicesRegisterMacro( ::fwGui::editor::IEditor, ::guiQt::editor::SListView );
27 
28 const ::fwCom::Signals::SignalKeyType SListView::s_ITEM_ADDED_SIG = "itemAdded";
29 const ::fwCom::Signals::SignalKeyType SListView::s_ITEM_REMOVED_SIG = "itemRemoved";
30 const ::fwCom::Signals::SignalKeyType SListView::s_ITEM_DOUBLE_CLICKED_SIG = "itemDoubleClicked";
31 
32 const ::fwCom::Slots::SlotKeyType SListView::s_INSERT_ITEM_SLOT = "insertItem";
33 const ::fwCom::Slots::SlotKeyType SListView::s_REMOVE_ITEM_SLOT = "removeItem";
34 
35 //------------------------------------------------------------------------------
36 
38 {
39  newSignal< ItemAddedSignalType>(s_ITEM_ADDED_SIG);
40  newSignal< ItemRemovedSignalType>(s_ITEM_REMOVED_SIG);
41  newSignal< ItemDoubleClickedSignalType>(s_ITEM_DOUBLE_CLICKED_SIG);
42 
45 }
46 
47 //------------------------------------------------------------------------------
48 
50 {
51 }
52 
53 //------------------------------------------------------------------------------
54 
55 bool SListView::eventFilter(QObject* watched, QEvent* event)
56 {
57  // filter suppr key release event in order to delete the current selected item of the list.
58  if(watched == m_listWidget && event->type() == QEvent::KeyRelease)
59  {
60  QKeyEvent* eventKey = static_cast<QKeyEvent*>(event);
61  if(eventKey->key() == Qt::Key_Delete)
62  {
63  const QList<QListWidgetItem*>& listItem = m_listWidget->selectedItems();
64  if(listItem.size() > 0)
65  {
66  this->removeItem(m_listWidget->row(listItem.first()));
67  return true;
68  }
69  }
70  }
71  return false;
72 }
73 
74 //------------------------------------------------------------------------------
76 {
77  this->initialize();
78 
79 }
80 
81 //------------------------------------------------------------------------------
83 {
84  this->create();
85  ::fwGuiQt::container::QtContainer::sptr qtContainer =
86  ::fwGuiQt::container::QtContainer::dynamicCast(this->getContainer());
87 
88  QPointer<QHBoxLayout> layout = new QHBoxLayout();
89  layout->setObjectName("SListViewLayout");
90  m_listWidget = new(QListWidget);
91  m_listWidget->setObjectName("SListViewWidget");
92  m_listWidget->installEventFilter(this);
93  layout->addWidget( m_listWidget );
94 
95  connect(m_listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this,
96  SLOT(onItemDoubleClicked(QListWidgetItem*)));
97 
98  qtContainer->setLayout(layout);
99 }
100 
101 //------------------------------------------------------------------------------
102 
104 {
105  SLM_TRACE_FUNC();
106 
107  this->destroy();
108 }
109 //------------------------------------------------------------------------------
110 
112 {
113 }
114 
115 //------------------------------------------------------------------------------
116 
118 {
119  this->updating();
120 }
121 
122 //------------------------------------------------------------------------------
123 void SListView::insertItem(int index, std::string value)
124 {
125  // insert item at the index position of the list
126  QListWidgetItem* newItem = new QListWidgetItem(QString::fromStdString(value));
127  m_listWidget->insertItem(index, newItem);
128 
129  // notify
130  this->signal<ItemAddedSignalType>(s_ITEM_ADDED_SIG)->asyncEmit(index);
131 }
132 
133 //------------------------------------------------------------------------------
134 void SListView::removeItem(int index)
135 {
136  // remove item at index position
137  delete m_listWidget->takeItem(index);
138 
139  // notify
140  this->signal<ItemRemovedSignalType>(s_ITEM_REMOVED_SIG)->asyncEmit(index);
141 }
142 
143 //------------------------------------------------------------------------------
144 
145 void SListView::onItemDoubleClicked(QListWidgetItem* item)
146 {
147  const int index = m_listWidget->row(item);
148  this->signal<ItemDoubleClickedSignalType>(s_ITEM_DOUBLE_CLICKED_SIG)->asyncEmit(index);
149 }
150 
151 //------------------------------------------------------------------------------
152 
153 } //namespace editor
154 } //namespace guiQt
virtual void stopping() override
Destroys the layout.
Definition: SListView.cpp:103
void insertItem(int index, std::string value)
SLOT : Called to insert an item at index.
Definition: SListView.cpp:123
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
Defines the service interface managing the editor service for object.
Definition: IEditor.hpp:25
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
virtual void starting() override
Installs the layout.
Definition: SListView.cpp:82
This editor allows to draw a list. The currently selected item can be deleted by pressing on "del" ke...
Definition: SListView.hpp:40
GUIQT_API SListView() noexcept
Constructor. Do nothing.
Definition: SListView.cpp:37
virtual void swapping() override
Does nothing.
Definition: SListView.cpp:117
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
void removeItem(int index)
SLOT : Called to remove the item at the index position.
Definition: SListView.cpp:134
virtual void updating() override
Does nothing.
Definition: SListView.cpp:111
static GUIQT_APIconst::fwCom::Slots::SlotKeyType s_INSERT_ITEM_SLOT
SLOT : Called to insert an item at index.
Definition: SListView.hpp:75
bool eventFilter(QObject *watched, QEvent *event) override
used to catch the del key released event
Definition: SListView.cpp:55
virtual void configuring() override
Configure the service.
Definition: SListView.cpp:75
virtual GUIQT_API ~SListView() noexcept
Destructor. Do nothing.
Definition: SListView.cpp:49
The namespace guiQt contains the basic services to build the application IHM with Qt...
Definition: Code.hpp:21
static GUIQT_APIconst::fwCom::Slots::SlotKeyType s_REMOVE_ITEM_SLOT
SLOT : Called to insert an item at index.
Definition: SListView.hpp:76
FWGUI_API void initialize()
Initialize managers.