TPolygonBasedParamsGuiComp.h
Go to the documentation of this file.
1 #ifndef iqt2d_TPolygonBasedParamsGuiComp_included
2 #define iqt2d_TPolygonBasedParamsGuiComp_included
3 
4 
5 // Qt includes
6 #include <QtCore/QMimeData>
7 #include <QtCore/QByteArray>
8 #include <QtCore/QAbstractTableModel>
9 #include <QtGui/QDoubleValidator>
10 #include <QtGui/QClipboard>
11 #if QT_VERSION >= 0x050000
12 #include <QtWidgets/QLineEdit>
13 #else
14 #include <QtGui/QLineEdit>
15 #endif
16 
17 // ACF includes
18 #include <istd/CChangeNotifier.h>
21 #include <i2d/CPolygon.h>
22 #include <i2d/CPolyline.h>
23 #include <iqt/CSignalBlocker.h>
25 
26 #include <GeneratedFiles/iqt2d/ui_CPolygonParamsGuiComp.h>
27 
28 
29 namespace iqt2d
30 {
31 
32 
33 template <class PolygonBasedShape, class PolygonBasedModel>
36  Ui::CPolygonParamsGuiComp,
37  PolygonBasedShape,
38  PolygonBasedModel>
39 {
40 public:
44  enum CellIndex
45  {
50 
55 
60  };
61 
63  Ui::CPolygonParamsGuiComp,
64  PolygonBasedShape,
65  PolygonBasedModel> BaseClass;
66 
67  I_BEGIN_COMPONENT(TPolygonBasedParamsGuiComp);
68  I_ASSIGN(m_nodeListSizeAttrPtr, "NodeListSize", "Fixed height of the node list if set (0 - hide)", false, 0);
69  I_END_COMPONENT;
70 
72 
73 protected:
74  class TableModel: public QAbstractTableModel
75  {
76  public:
77  typedef QAbstractTableModel BaseClass;
78 
80 
81  // reimplemented (QAbstractTableModel)
82  virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
83  virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
84  virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
85  virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
86  virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
87  virtual Qt::ItemFlags flags(const QModelIndex &index) const;
88 
89  using QAbstractTableModel::dataChanged;
90  using QAbstractTableModel::layoutChanged;
91 
92  private:
94  };
95 
99  QTableView* GetNodeTable();
100 
101  virtual void OnInsertNode();
102  virtual void OnRemoveNode();
103  virtual void OnCopyData();
104  virtual void OnPasteData();
105 
106  // delegated from QAbstractTableModel
107  virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
108  virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
109  virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
110  virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
111  virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
112  virtual Qt::ItemFlags flags(const QModelIndex &index) const;
113 
114  // reimplemented (iqtgui::CGuiComponentBase)
115  virtual void OnGuiCreated();
116  virtual void OnGuiRetranslate();
117 
118  // reimplemented (iqt2d::TShapeParamsGuiCompBase)
119  virtual bool PopulateActions(QWidget& host, imod::IModel* modelPtr);
120  virtual void OnActionTriggered(QAction* actionPtr);
121 
122  // reimplemented (iqtgui::TGuiObserverWrap)
123  virtual void UpdateGui(const istd::IChangeable::ChangeSet& changeSet);
124 
125 protected:
130  using BaseClass::NodeParamsTable;
131  using BaseClass::tr;
133  using BaseClass::RemoveButton;
134  using BaseClass::InsertButton;
135  using BaseClass::ToolsButton;
136 
137 protected:
138  I_ATTR(int, m_nodeListSizeAttrPtr);
139 
145 
146 private:
147  TableModel m_tableModel;
148 };
149 
150 
151 // public methods
152 
153 template <class PolygonBasedShape, class PolygonBasedModel>
155 : m_flipHorizontalAction(QIcon(":/Icons/FlipHorizontal"), "", this),
156  m_flipVerticalAction(QIcon(":/Icons/FlipVertical"), "", this),
157  m_rotateCwAction(QIcon(":/Icons/RotateRight"), "", this),
158  m_rotateCcwAction(QIcon(":/Icons/RotateLeft"), "", this),
159  m_reversePolarityAction(QIcon(":/Icons/Reverse"), "", this),
160  m_tableModel(this)
161 {
162 }
163 
164 
165 // protected methods
166 
167 template <class PolygonBasedShape, class PolygonBasedModel>
169 {
170  return NodeParamsTable;
171 }
172 
173 
174 template <class PolygonBasedShape, class PolygonBasedModel>
176 {
177  i2d::CPolygon* objectPtr = GetObservedObject();
178  if (objectPtr != NULL){
179  QItemSelectionModel *select = NodeParamsTable->selectionModel();
180  QModelIndexList selected = select->selectedRows();
181 
182  int row = selected.isEmpty()? -1: selected.front().row();
183  if (row >= 0){
184  objectPtr->InsertNode(row, i2d::CVector2d::GetZero());
185  }
186  else{
187  objectPtr->InsertNode(i2d::CVector2d::GetZero());
188  }
189  }
190 }
191 
192 
193 template <class PolygonBasedShape, class PolygonBasedModel>
195 {
196  PolygonBasedModel* objectPtr = GetObservedObject();
197  if (objectPtr == NULL){
198  return;
199  }
200 
202 
203  if (objectPtr->Serialize(archive)){
204  QByteArray data((const char*)archive.GetBuffer(), archive.GetBufferSize());
205 
206  QMimeData* dataPtr = new QMimeData();
207  dataPtr->setData(typeid (PolygonBasedModel).name(), data);
208 
209  QApplication::clipboard()->setMimeData(dataPtr);
210  }
211 }
212 
213 
214 template <class PolygonBasedShape, class PolygonBasedModel>
216 {
217  PolygonBasedModel* objectPtr = GetObservedObject();
218  if (objectPtr == NULL){
219  return;
220  }
221 
222  const QMimeData* dataPtr = QApplication::clipboard()->mimeData();
223  if (dataPtr != NULL){
224  if (dataPtr->hasFormat(typeid (PolygonBasedModel).name())){
225  QByteArray data(dataPtr->data(typeid (PolygonBasedModel).name()));
226 
227  iser::CMemoryReadArchive archive(data.data(), data.size());
228 
229  objectPtr->Serialize(archive);
230  }
231  }
232 }
233 
234 
235 template <class PolygonBasedShape, class PolygonBasedModel>
237 {
238  i2d::CPolygon* objectPtr = GetObservedObject();
239  if (objectPtr != NULL){
240  QItemSelectionModel *select = NodeParamsTable->selectionModel();
241  QModelIndexList selected = select->selectedRows();
242 
243  int row = selected.isEmpty()? -1: selected.front().row();
244  if (row >= 0){
245  objectPtr->RemoveNode(row);
246  }
247  }
248 }
249 
250 
251 // delegated from QAbstractTableModel
252 
253 template <class PolygonBasedShape, class PolygonBasedModel>
255 {
256  int retVal = 0;
257 
258  i2d::CPolygon* objectPtr = GetObservedObject();
259  if (objectPtr != NULL){
260  retVal = objectPtr->GetNodesCount();
261  }
262 
263  return retVal;
264 }
265 
266 
267 template <class PolygonBasedShape, class PolygonBasedModel>
269 {
270  return 2;
271 }
272 
273 
274 template <class PolygonBasedShape, class PolygonBasedModel>
275 QVariant TPolygonBasedParamsGuiComp<PolygonBasedShape, PolygonBasedModel>::data(const QModelIndex& index, int role) const
276 {
277  if ((role == Qt::DisplayRole) || (role == Qt::EditRole)){
278  i2d::CVector2d retVal(0, 0);
279  i2d::CPolygon* objectPtr = GetObservedObject();
280  if ((objectPtr != NULL) && (index.column() <= 1) && (index.column() >= 0)){
281  retVal = objectPtr->GetNodePos(index.row());
282 
283  return retVal[index.column()];
284  }
285  }
286 
287  return QVariant();
288 }
289 
290 
291 template <class PolygonBasedShape, class PolygonBasedModel>
292 bool TPolygonBasedParamsGuiComp<PolygonBasedShape, PolygonBasedModel>::setData(const QModelIndex& index, const QVariant& value, int role)
293 {
294  if (role == Qt::EditRole){
295  i2d::CPolygon* objectPtr = GetObservedObject();
296  if ((objectPtr != NULL) && (index.column() <= 1) && (index.column() >= 0)){
297  i2d::CVector2d pos = objectPtr->GetNodePos(index.row());
298  bool isOk = false;
299  pos[index.column()] = value.toDouble(&isOk);
300  if (isOk){
301  objectPtr->SetNodePos(index.row(), pos);
302 
303  return true;
304  }
305  }
306  }
307 
308  return false;
309 }
310 
311 
312 template <class PolygonBasedShape, class PolygonBasedModel>
313 QVariant TPolygonBasedParamsGuiComp<PolygonBasedShape, PolygonBasedModel>::headerData(int section, Qt::Orientation orientation, int role) const
314 {
315  if (orientation == Qt::Horizontal){
316  if (role == Qt::DisplayRole){
317  switch (section){
318  case 0:
319  return "X";
320 
321  case 1:
322  return "Y";
323  }
324  }
325  }
326 
327  return QVariant();
328 }
329 
330 
331 template <class PolygonBasedShape, class PolygonBasedModel>
332 Qt::ItemFlags TPolygonBasedParamsGuiComp<PolygonBasedShape, PolygonBasedModel>::flags(const QModelIndex& /*index*/) const
333 {
334  return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
335 }
336 
337 
338 // reimplemented (iqtgui::CGuiComponentBase)
339 
340 template <class PolygonBasedShape, class PolygonBasedModel>
342 {
343  BaseClass::OnGuiCreated();
344 
345  NodeParamsTable->setModel(&m_tableModel);
346 
347  bool isNodeTable = true;
348  if (m_nodeListSizeAttrPtr.IsValid()){
349  if (*m_nodeListSizeAttrPtr > 0){
350  NodeParamsTable->setFixedHeight(qMax(40, *m_nodeListSizeAttrPtr)); // 40 is minimum visible size for 1 row
351  }
352  else{
353  InsertButton->hide();
354  RemoveButton->hide();
355  NodeParamsTable->hide();
356  isNodeTable = false;
357  }
358 
359  GetQtWidget()->adjustSize();
360  }
361 
362  if (isNodeTable){
363 #if QT_VERSION < 0x050000
364  NodeParamsTable->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
365 #else
366  NodeParamsTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
367 #endif
368  }
369 
370  // tools actions
371  CreateToolsMenu(ToolsButton);
372 }
373 
374 
375 template <class PolygonBasedShape, class PolygonBasedModel>
377 {
378  BaseClass::OnGuiRetranslate();
379 
380  m_flipHorizontalAction.setText(QCoreApplication::translate("iqt2d", "Flip horizontally"));
381  m_flipVerticalAction.setText(QCoreApplication::translate("iqt2d", "Flip vertically"));
382  m_rotateCwAction.setText(QCoreApplication::translate("iqt2d", "Rotate clockwise"));
383  m_rotateCcwAction.setText(QCoreApplication::translate("iqt2d", "Rotate counterclockwise"));
384  m_reversePolarityAction.setText(QCoreApplication::translate("iqt2d", "Reverse line"));
385 }
386 
387 
388 // reimplemented (iqt2d::TShapeParamsGuiCompBase)
389 
390 template <class PolygonBasedShape, class PolygonBasedModel>
392 {
393  if (!BaseClass::PopulateActions(host, modelPtr)){
394  return false;
395  }
396 
397  i2d::CPolyline* polylinePtr = dynamic_cast<i2d::CPolyline*>(modelPtr);
398  i2d::CPolygon* polygonPtr = dynamic_cast<i2d::CPolygon*>(modelPtr);
399  if (polygonPtr == NULL && polylinePtr == NULL){
400  return false;
401  }
402 
403  host.addAction(&m_flipHorizontalAction);
404  host.addAction(&m_flipVerticalAction);
405  host.addAction(&m_rotateCwAction);
406  host.addAction(&m_rotateCcwAction);
407 
408  if (polylinePtr != NULL){
409  host.addAction(&m_reversePolarityAction);
410  }
411 
412  return true;
413 }
414 
415 
416 template <class PolygonBasedShape, class PolygonBasedModel>
418 {
419  Q_ASSERT(actionPtr);
420 
421  PolygonBasedModel* modelPtr = dynamic_cast<PolygonBasedModel*>(BaseClass::GetObservedModel());
422  Q_ASSERT(modelPtr);
423 
424  if (actionPtr == NULL || modelPtr == NULL){
425  return;
426  }
427 
428  if (actionPtr == &m_flipHorizontalAction){
429  modelPtr->FlipByX();
430  }
431  else if (actionPtr == &m_flipVerticalAction){
432  modelPtr->FlipByY();
433  }
434  else if (actionPtr == &m_rotateCwAction){
435  modelPtr->Rotate(M_PI/2);
436  }
437  else if (actionPtr == &m_rotateCcwAction){
438  modelPtr->Rotate(-M_PI/2);
439  }
440  else if (actionPtr == &m_reversePolarityAction){
441  modelPtr->ReverseNodes();
442  }
443 }
444 
445 
446 // reimplemented (iqtgui::TGuiObserverWrap)
447 
448 template <class PolygonBasedShape, class PolygonBasedModel>
450 {
451  QModelIndex leftTop = m_tableModel.index(0, 0);
452  QModelIndex rightBottom = m_tableModel.index(rowCount() - 1, columnCount() - 1);
453 
454  Q_EMIT m_tableModel.dataChanged(leftTop, rightBottom);
455  Q_EMIT m_tableModel.layoutChanged();
456 
457  BaseClass::UpdateGui(changeSet);
458 }
459 
460 
461 // public methods of embedded class TableModel
462 
463 template <class PolygonBasedShape, class PolygonBasedModel>
465 : m_parentPtr(parentPtr)
466 {
467 }
468 
469 
470 // reimplemented (QAbstractTableModel)
471 
472 template <class PolygonBasedShape, class PolygonBasedModel>
474 {
475  if (m_parentPtr != NULL){
476  return m_parentPtr->rowCount(parent);
477  }
478 
479  return 0;
480 }
481 
482 
483 template <class PolygonBasedShape, class PolygonBasedModel>
485 {
486  if (m_parentPtr != NULL){
487  return m_parentPtr->columnCount(parent);
488  }
489 
490  return 0;
491 }
492 
493 
494 template <class PolygonBasedShape, class PolygonBasedModel>
496 {
497  if (m_parentPtr != NULL){
498  return m_parentPtr->data(index, role);
499  }
500 
501  return QVariant();
502 }
503 
504 
505 template <class PolygonBasedShape, class PolygonBasedModel>
506 bool TPolygonBasedParamsGuiComp<PolygonBasedShape, PolygonBasedModel>::TableModel::setData(const QModelIndex& index, const QVariant& value, int role)
507 {
508  if (m_parentPtr != NULL){
509  return m_parentPtr->setData(index, value, role);
510  }
511 
512  return false;
513 }
514 
515 
516 template <class PolygonBasedShape, class PolygonBasedModel>
517 QVariant TPolygonBasedParamsGuiComp<PolygonBasedShape, PolygonBasedModel>::TableModel::headerData(int section, Qt::Orientation orientation, int role) const
518 {
519  if (m_parentPtr != NULL){
520  return m_parentPtr->headerData(section, orientation, role);
521  }
522 
523  return QVariant();
524 }
525 
526 
527 template <class PolygonBasedShape, class PolygonBasedModel>
529 {
530  if (m_parentPtr != NULL){
531  return m_parentPtr->flags(index);
532  }
533 
534  return Qt::ItemIsSelectable;
535 }
536 
537 
538 } // namespace iqt2d
539 
540 
541 #endif // !iqt2d_TPolygonBasedParamsGuiComp_included
542 
543 
Definition of the data model for a polygon.
Definition: CPolygon.h:16
virtual void OnGuiRetranslate()
Called from widget event filter when GUI should be retranslated.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
iqt2d::TShapeParamsGuiCompBase< Ui::CPolygonParamsGuiComp, PolygonBasedShape, PolygonBasedModel > BaseClass
PolygonBasedModel * GetObservedObject() const
Get instance of observed object.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
virtual bool RemoveNode(int index)
Remove a node at specified index.
virtual void UpdateGui(const istd::IChangeable::ChangeSet &changeSet)
Do update of the GUI to reflect the state of model.
virtual void OnGuiCreated()
Called just after GUI is initialized.
Implementation of archive using memory buffer to read the persistent objects.
virtual const i2d::CVector2d & GetNodePos(int index) const
Return position of node at specified index.
Definition: CPolypoint.h:149
imod::IModel * GetObservedModel() const
Get access to observed model.
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition of position or mathematical vector on 2D plane.
Definition: CVector2d.h:28
virtual bool PopulateActions(QWidget &host, imod::IModel *modelPtr)
virtual bool InsertNode(const i2d::CVector2d &position)
Insert a node at the end of node table.
virtual void SetNodePos(int index, const i2d::CVector2d &position)
Set node at specified index.
Definition: CPolypoint.h:165
#define NULL
Definition: istd.h:64
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
QTableView * GetNodeTable()
Get the table with the node data.
virtual void CreateToolsMenu(QAbstractButton *buttonPtr)
Maintenance of the tools actions.
Index of the table cell for the X coordinate of the polygon node.
virtual void OnActionTriggered(QAction *actionPtr)
static const CVector2d & GetZero()
Definition: CVector2d.h:306
Index of the table cell for the X coordinate of the polygon node.
virtual int GetNodesCount() const
Return size of node table.
Definition: CPolypoint.h:143
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Common interface for model objects, that supports Model/Observer design pattern.
Definition: IModel.h:23
Set of change flags (its IDs).
Definition: IChangeable.h:30
Implementation of archive using memory buffer to store the persistent objects.
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
const void * GetBuffer() const
virtual QWidget * GetQtWidget() const
TableModel(TPolygonBasedParamsGuiComp< PolygonBasedShape, PolygonBasedModel > *parentPtr)
2D-object representing a polyline.
Definition: CPolyline.h:18
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)

© 2007-2017 Witold Gantzke and Kirill Lepskiy