itom  4.1.0
workspaceWidget.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 *********************************************************************** */
22 
23 #ifndef WORKSPACEWIDGET_H
24 #define WORKSPACEWIDGET_H
25 
26 #ifndef Q_MOC_RUN
27  //python
28  // see http://vtk.org/gitweb?p=VTK.git;a=commitdiff;h=7f3f750596a105d48ea84ebfe1b1c4ca03e0bab3
29  #if (defined _DEBUG) && (defined WIN32)
30  #undef _DEBUG
31  #include "python/pythonWrapper.h"
32  #define _DEBUG
33  #else
34  #include "python/pythonWrapper.h"
35  #endif
36 #endif
37 
38 #include "../global.h"
39 #include "../common/sharedStructures.h"
40 #include "../common/sharedStructuresQt.h"
41 
42 #include "../python/pythonWorkspace.h"
43 
44 #include <qtreewidget.h>
45 #include <qmimedata.h>
46 #include <qpixmap.h>
47 #include <qhash.h>
48 #include <qset.h>
49 
50 namespace ito
51 {
52 
54 /*
55 The variable names of a WorkspaceWidget are usually strings, however these strings can
56 also be numbers (e.g. children of a list or tuple). In this case, it is desired that numbers
57 are compared to numbers based on their number value and not the text, such that 1 < 2 < 10 instead of "1" < "10" < "2".
58 */
59 class WorkspaceTreeItem : public QTreeWidgetItem
60 {
61 public:
62  explicit WorkspaceTreeItem(int type = Type) : QTreeWidgetItem(type) {}
63  explicit WorkspaceTreeItem(const QStringList &strings, int type = Type) : QTreeWidgetItem(strings, type) {}
64  explicit WorkspaceTreeItem(QTreeWidget *view, int type = Type) : QTreeWidgetItem(view, type) {}
65  WorkspaceTreeItem(QTreeWidget *view, const QStringList &strings, int type = Type) : QTreeWidgetItem(strings, type) {}
66  WorkspaceTreeItem(QTreeWidget *view, QTreeWidgetItem *after, int type = Type) : QTreeWidgetItem(view, after, type) {}
67  explicit WorkspaceTreeItem(QTreeWidgetItem *parent, int type = Type) : QTreeWidgetItem(parent, type) {}
68  WorkspaceTreeItem(QTreeWidgetItem *parent, const QStringList &strings, int type = Type) : QTreeWidgetItem(parent, strings, type) {}
69  WorkspaceTreeItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type = Type) : QTreeWidgetItem(parent, after, type) {}
70  WorkspaceTreeItem(const QTreeWidgetItem &other) : QTreeWidgetItem(other) {}
71 
72  virtual ~WorkspaceTreeItem() {}
73 
75  virtual bool operator<(const QTreeWidgetItem &other) const
76  {
77  int column = treeWidget()->sortColumn();
78  QString thisText = text(column);
79  QString otherText = other.text(column);
80 
81  bool ok;
82  float a = thisText.toFloat(&ok);
83 
84  if (ok)
85  {
86  float b = otherText.toFloat(&ok);
87  if (ok)
88  {
89  return a < b;
90  }
91 
92  }
93 
94  return thisText.localeAwareCompare(otherText) < 0;
95  }
96 
97 };
98 
100 class WorkspaceWidget : public QTreeWidget
101 {
102  Q_OBJECT
103 public:
104  WorkspaceWidget(bool globalNotLocal, QWidget* parent = NULL);
106 
107  int numberOfSelectedItems(bool ableToBeRenamed = false) const;
108  int numberOfSelectedMainItems() const;
109  inline ito::PyWorkspaceContainer* getWorkspaceContainer() { return m_workspaceContainer; }
110 
112  {
113  RoleFullName = Qt::UserRole + 1,
114  RoleCompatibleTypes = Qt::UserRole + 2,
115  RoleType = Qt::UserRole + 3
116  };
117 
118  QString getPythonReadableName(const QTreeWidgetItem *item) const;
119 
120 protected:
121  QStringList mimeTypes() const;
122  QMimeData * mimeData(const QList<QTreeWidgetItem *> items) const;
123  void startDrag(Qt::DropActions supportedActions);
124 
125 private:
126  void updateView(QHash<QString,ito::PyWorkspaceItem*> items, QString baseName, QTreeWidgetItem *parent = NULL);
127  void recursivelyDeleteHash(QTreeWidgetItem *item);
128  void recursivelyDeleteHash(const QString &fullBaseName);
129 
131  QHash<QString,QTreeWidgetItem*> m_itemHash;
132  ito::PyWorkspaceContainer *m_workspaceContainer;
133 
134  QPixmap m_dragPixmap;
135  Qt::DropActions supportedDragActions() const;
136 
137 signals:
138 
139 public slots:
140  void workspaceContainerUpdated(PyWorkspaceItem *rootItem, QString fullNameRoot, QStringList recentlyDeletedFullNames);
141 
142 private slots:
143  void itemDoubleClicked(QTreeWidgetItem* item, int column);
144  void itemExpanded(QTreeWidgetItem* item);
145  void itemCollapsed(QTreeWidgetItem* item);
146 };
147 
148 } //end namespace ito
149 
150 #endif
WorkspaceWidget(bool globalNotLocal, QWidget *parent=NULL)
constructor
Definition: workspaceWidget.cpp:47
bool m_globalNotLocal
Definition: workspaceWidget.h:130
Definition: apiFunctionsGraph.cpp:39
major class WorkspaceWidget to show a tree widget for the global and local workspace toolbox ...
Definition: workspaceWidget.h:100
tiny derivative of QTreeWidgetItem, that overwrites the comparison operator
Definition: workspaceWidget.h:59
WorkspaceRole
Definition: workspaceWidget.h:111
Definition: workspaceWidget.h:113
every item in the workspace is represented by one PyWorkspaceItem
Definition: pythonWorkspace.h:71
void itemDoubleClicked(QTreeWidgetItem *item, int column)
slot, invoked if item is double-clicked
Definition: workspaceWidget.cpp:409
Definition: pythonWorkspace.h:99
~WorkspaceWidget()
destructor
Definition: workspaceWidget.cpp:117
virtual bool operator<(const QTreeWidgetItem &other) const
overwritten operator for better number comparison
Definition: workspaceWidget.h:75