LayerViewMenuManager.cpp
Go to the documentation of this file.
1 #include "LayerViewMenuManager.h"
2 
3 #include "LayerItemView.h"
4 #include "LayerItem.h"
5 #include "../../../../common/GenericQueue.h"
6 
7 // TerraLib
8 #include "../../../../maptools/AbstractLayer.h"
9 
10 // Qt
11 #include <QContextMenuEvent>
12 #include <QMenu>
13 
14 
16 {
17  if(acts != nullptr && acts->getSize() == 0 && allActs != nullptr && allActs->getSize() == 0)
18  return;
19 
20  unsigned int s = (allActs != nullptr) ? allActs->getSize() : 0;
21 
22  for(unsigned int i = 0; i < s; i++)
23  mnu->addAction(allActs->getValue(i));
24 
25  if(acts == nullptr)
26  return;
27 
28  if(s > 0)
29  mnu->addSeparator();
30 
31  s = acts->getSize();
32 
33  for(unsigned int i = 0; i < s; i++)
34  mnu->addAction(acts->getValue(i));
35 }
36 
38  QObject(view),
39  m_view(view)
40 {
41  m_VL_actions.reset(new QueueAction);
42  m_RL_actions.reset(new QueueAction);
43  m_TL_actions.reset(new QueueAction);
44  m_FL_actions.reset(new QueueAction);
45  m_ML_actions.reset(new QueueAction);
46  m_NL_actions.reset(new QueueAction);
47  m_AL_actions.reset(new QueueAction);
48  m_IL_actions.reset(new QueueAction);
49 }
50 
52 
54 {
55  QueueAction* q;
56 
57  switch(mnu)
58  {
59  case VECTOR_LAYER:
60  q = m_VL_actions.get();
61  break;
62 
63  case RASTER_LAYER:
64  q = m_RL_actions.get();
65  break;
66 
67  case TABULAR_LAYER:
68  q = m_TL_actions.get();
69  break;
70 
71  case FOLDER_LAYER:
72  q = m_FL_actions.get();
73  break;
74 
75  case MULTI_LAYERS:
76  q = m_ML_actions.get();
77  break;
78 
79  case ALL_LAYERS:
80  q = m_AL_actions.get();
81  break;
82 
83  case INVALID_LAYERS:
84  q = m_IL_actions.get();
85  break;
86 
87  default:
88  q = m_NL_actions.get();
89  break;
90  }
91 
92  if(pos == -1)
93  q->add(act);
94  else
95  {
96  try
97  {
98  q->insert(act, (unsigned int) pos);
99  }
100  catch(te::common::Exception&)
101  {
102  q->add(act);
103  }
104  }
105 }
106 
108 {
109  switch(event->type())
110  {
111  case QEvent::ContextMenu:
112  {
113  QContextMenuEvent* evt = static_cast<QContextMenuEvent*>(event);
114  QPoint pos = evt->globalPos();
115 
116  int rows = m_view->model()->rowCount();
117  QModelIndex idx = m_view->indexAt(m_view->viewport()->mapFromGlobal(pos));
118  QModelIndexList ls = m_view->selectionModel()->selectedIndexes();
119  int si = ls.size();
120 
121  if(rows == 0) // List is empty
122  {
123  QMenu mnu;
124  GetMenu(&mnu, m_NL_actions.get(), nullptr);
125 
126  if(!mnu.isEmpty())
127  mnu.exec(pos);
128  }
129  else // List not empty
130  {
131  if(!idx.isValid())
132  {
133  QMenu mnu;
134 
135  GetMenu(&mnu, (si > 1) ? m_ML_actions.get() : m_NL_actions.get(), nullptr);
136  mnu.exec(pos);
137  }
138  else
139  {
140  if(ls.isEmpty())
141  return false;
142 
143  QMenu mnu;
144 
145  if(si > 1)
146  GetMenu(&mnu, m_ML_actions.get(), m_AL_actions.get());
147  else
148  {
149  TreeItem* item = static_cast<TreeItem*>(ls.at(0).internalPointer());
150 
151  if (item->getType() == "FOLDER")
152  {
153  GetMenu(&mnu, m_FL_actions.get(), m_AL_actions.get());
154  }
155  else if (item->getType() == "LAYER")
156  {
157  te::map::AbstractLayerPtr layer = (static_cast<te::qt::widgets::LayerItem*>(item))->getLayer();
158 
159  if (!layer->isValid())
160  {
161  GetMenu(&mnu, m_IL_actions.get(), m_AL_actions.get());
162  }
163  else if (layer->getSchema()->hasRaster())
164  {
165  GetMenu(&mnu, m_RL_actions.get(), m_AL_actions.get());
166  }
167  else if (layer->getSchema()->hasGeom())
168  {
169  GetMenu(&mnu, m_VL_actions.get(), m_AL_actions.get());
170  }
171  else if (layer->getType() == "DATASETADAPTERLAYER"
172  || layer->getType() == "DATASETLAYER"
173  || layer->getType() == "QUERYLAYER")
174  {
175  GetMenu(&mnu, m_TL_actions.get(), m_AL_actions.get());
176  }
177  else
178  {
179  GetMenu(&mnu, nullptr, m_AL_actions.get());
180  }
181  }
182  else
183  {
184  GetMenu(&mnu, nullptr, m_AL_actions.get());
185  }
186  }
187 
188  mnu.exec(pos);
189  }
190  }
191  }
192  break;
193 
194  default:
195  break;
196  }
197 
198  return QObject::eventFilter(watched, event);
199 }
std::unique_ptr< QueueAction > m_IL_actions
Invalid layers action list.
An item that contains a te::map::AbstractLayerPtr.
Definition: LayerItem.h:51
LayerViewMenuManager(LayerItemView *view)
Contructor.
unsigned int getSize() const
Returns the size of the queue.
Definition: GenericQueue.h:287
std::unique_ptr< QueueAction > m_RL_actions
Raster layers action list.
Defines a hierarchical structure.
std::unique_ptr< QueueAction > m_FL_actions
Folder layers action list.
Struct that implements the generic queue.
Definition: GenericQueue.h:85
T getValue(const unsigned int &pos) const
Returns the value stored at pos position of the queue.
Definition: GenericQueue.h:240
std::unique_ptr< QueueAction > m_ML_actions
Multiple selected items action list.
A specialization of QTreeView for manipulate layers.
Definition: LayerItemView.h:78
LayerItemView * m_view
View being inspected.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Defines a layer item view for Qt5.
std::unique_ptr< QueueAction > m_TL_actions
Tabular layers action list.
LMENUACTIONS
Defines the type of actions we can use.
bool eventFilter(QObject *watched, QEvent *event)
Defines a layer item.
void insert(const T &v, const unsigned int &pos)
Inserts a data at a specific position.
Definition: GenericQueue.h:195
void add(const T &v)
Adds an element to the end of the queue.
Definition: GenericQueue.h:115
void addAction(LMENUACTIONS mnu, QAction *act, int pos=-1)
Adds an action to some menu.
TEQTWIDGETSEXPORT QMenu * GetMenu(const QString &mnuText, QMenu *mnu)
Gets a menu or submenu contained in the mnu object.
std::unique_ptr< QueueAction > m_AL_actions
All kinds of items action list.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::unique_ptr< QueueAction > m_VL_actions
Vector layers action list.
void GetMenu(QMenu *mnu, te::qt::widgets::QueueAction *acts, te::qt::widgets::QueueAction *allActs)
std::unique_ptr< QueueAction > m_NL_actions
No selected items action list.
std::string getType() const
Returns the type of the item.
Class for manage the menus of the LayerItemView.