Salome HOME
Merge modifications for HYDRO project (origin/hydro/imps_2017_salome_83 branch)
[modules/gui.git] / tools / RemoteFileBrowser / QRemoteFileBrowser
1 // Copyright (C) 2017  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 // Author : Anthony GEAY (EDF R&D)
20
21 #ifndef __QREMOTEFILEBROWSER__
22 #define __QREMOTEFILEBROWSER__
23
24 #include "QWidget"
25 #include "QTreeView"
26 #include "QMimeData"
27 #include "QThread"
28
29 class AnotherTreeView;
30 class QMachineBrowser;
31 class QRemoteFileSystemModel;
32 class TopDirDataStructure;
33
34 class LoadingThread : public QThread
35 {
36   Q_OBJECT
37 public:
38   LoadingThread(QThread *th, QMachineBrowser *mb):_fatherThread(th),_mb(mb),_model(nullptr) { }
39   void setGeneratedModel(QRemoteFileSystemModel *model) { _model=model; }
40   QRemoteFileSystemModel *generatedModel() const { return _model; }
41 signals:
42   void letsGenerateModel(TopDirDataStructure *fds);
43 protected:
44   void run();
45 private:
46   QThread *_fatherThread;
47   QMachineBrowser *_mb;
48   QRemoteFileSystemModel *_model;
49 };
50
51 class QRemoteFileBrowser : public QWidget
52 {
53   Q_OBJECT
54 public:
55   QRemoteFileBrowser(QWidget *parent);
56   QMachineBrowser *machineBrower() const { return _mb; }
57 public slots:
58   void onLocationChanged();
59   void locationHasBeenChanged();
60 private:
61   AnotherTreeView *_treeView;
62   QMachineBrowser *_mb;
63 };
64
65 class QRemoteFileTransfer : public QWidget
66 {
67 public:
68   QRemoteFileTransfer(QWidget *parent=0);
69 private:
70   QRemoteFileBrowser *_left;
71   QRemoteFileBrowser *_right;
72 };
73
74 class DirDataStructure;
75 class TopDirDataStructure;
76
77 class DataStructure : public QObject
78 {
79   Q_OBJECT
80 public:
81   DataStructure(QObject *parent, const QString& name):QObject(parent),_name(name),_selected(false) { }
82   void select() { _selected=true; }
83   void unselect() { _selected=false; }
84   bool isSelected() const { return _selected; }
85   virtual bool isFile() const = 0;
86   virtual QString nameOnDrop() const = 0;
87   const DirDataStructure *getDirParent() const;
88   bool isRoot() const { return getDirParent()==NULL; }
89   const TopDirDataStructure *getRoot() const;
90   std::vector<const DataStructure *> getItermediateElts(const TopDirDataStructure *tpds) const;
91   virtual int size() const = 0;
92   QString entryForRSyncSrc() const;
93   virtual QString entryForRSyncDest() const = 0;
94   QString name() const;
95   const QString& fullName() const { return _name; }
96   void removeFileArgs(QString& prg, QStringList& args) const;
97 private:
98   QString _name;
99   bool _selected;
100 };
101
102 class FileDataStructure : public DataStructure
103 {
104 public:
105   FileDataStructure(DirDataStructure *dds, const QString& name);
106   bool isFile() const { return true; }
107   int size() const { return 0; }
108   QString entryForRSyncDest() const;
109   QString nameOnDrop() const;
110 };
111
112 class DirDataStructure : public DataStructure
113 {
114 public:
115   DirDataStructure(DirDataStructure *dds, const QString& name):DataStructure(dds,name),_is_loaded(false),_is_expanded(false) { }
116   DirDataStructure(QObject *dds, const QString& name):DataStructure(dds,name),_is_loaded(false) { }
117   bool isFile() const { return false; }
118   int size() const { load(); return children().size(); }
119   QString entryForRSyncDest() const;
120   QString nameOnDrop() const { return name(); }
121   const DataStructure *operator[](int pos) const;
122   int posOf(const DataStructure *ds) const;
123   bool load() const;
124   void markAsLoaded() const { _is_loaded=true; }
125   void setExpanded(bool status) { _is_expanded=status; }
126   bool isExpanded() const { return _is_expanded; }
127 private:
128   mutable bool _is_loaded;
129   mutable bool _is_expanded;
130 };
131
132 class FileLoader;
133
134 class TopDirDataStructure : public DirDataStructure
135 {
136 public:
137   TopDirDataStructure(QObject *dds, FileLoader *fl);
138   virtual ~TopDirDataStructure();
139   FileLoader *getLoader() const { return _fl; }
140   bool isOK() const { return _isOK; }
141   QString entryForRSync(const QString& fn) const;
142   QString getMachine() const;
143   void removeFileArgsImpl(const QString& filePath, QString& prg, QStringList& args) const;
144 private:
145   FileLoader *_fl;
146   bool _isOK;
147 };
148
149 class QRemoteFileSystemModel;
150
151 class MyTreeView : public QTreeView
152 {
153   Q_OBJECT
154 public:
155   MyTreeView(QWidget *parent);
156   void mousePressEvent(QMouseEvent *event);
157   void mouseReleaseEvent(QMouseEvent *event);
158   void mouseMoveEvent(QMouseEvent *event);
159   void keyPressEvent(QKeyEvent *event);
160   void dragEnterEvent(QDragEnterEvent *event);
161   void dragMoveEvent(QDragMoveEvent *event);
162   void dragLeaveEvent(QDragLeaveEvent *event);
163   void dropEvent(QDropEvent *event);
164   QRemoteFileSystemModel *zeModel();
165   void emitResetModel();
166 public slots:
167   void itemExpanded(const QModelIndex &index);
168   void itemCollapsed(const QModelIndex &index);
169 signals:
170   void somethingChangedDueToFileModif();
171 private:
172   void itemExpandedStatus(const QModelIndex &index, bool status);
173   void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const;
174   void paintEvent(QPaintEvent *event);
175 private:
176   QPoint _start_pos;
177   // during drag drop _sel is the element under the mouse on the drop site
178   DataStructure *_sel;
179   // during drag drop _slider_pos is the pos of vertical slider on drop site
180   int _slider_pos;
181 };
182
183 class AnotherTreeView;
184
185 class AnotherTreeViewPainter
186 {
187 public:
188   virtual void paint(AnotherTreeView *atv, QPaintEvent *event) const = 0;
189 };
190
191 class AnotherTreeViewWaitPainter : public AnotherTreeViewPainter
192 {
193 public:
194   void paint(AnotherTreeView *atv, QPaintEvent *event) const;
195 };
196
197 class AnotherTreeViewNothingPainter : public AnotherTreeViewPainter
198 {
199 public:
200   void paint(AnotherTreeView *atv, QPaintEvent *event) const;
201 };
202
203 class AnotherTreeView : public QWidget
204 {
205   Q_OBJECT
206 public:
207   AnotherTreeView(QWidget *parent);
208   void generateModel(QMachineBrowser *mb);
209   QSize sizeHint() const;
210   QSize minimumSizeHint() const;
211   int getAngle() const { return _angle; }
212   ~AnotherTreeView();
213 public slots:
214   void goGenerate(TopDirDataStructure *fds);
215   void modelHasBeenGenerated();
216 signals:
217   void modelHasBeenGeneratedSignal(bool isOK);
218   void somethingChangedDueToFileModif();
219 protected:
220   void paintEvent(QPaintEvent *event);
221   void timerEvent(QTimerEvent *e);
222 private:
223   int _timerId;
224   int _angle;
225   AnotherTreeViewPainter *_painter;
226   MyTreeView *_tw;
227   LoadingThread *_th;
228 };
229
230 class FileLoader
231 {
232 protected:
233   FileLoader(const QString& dirName):_dirName(dirName) { }
234 public:
235   QString getDirName() const { return _dirName; }
236   virtual bool load(DirDataStructure *parent) const = 0;
237   virtual QString prettyPrint() const = 0;
238   virtual QString entryForRSync(const QString& fn) const = 0;
239   virtual QString getMachine() const = 0;
240   virtual void removeFileArgs(const QString& filePath, QString& prg, QStringList& args) const = 0;
241   virtual ~FileLoader() { }
242 private:
243   QString _dirName;
244 };
245
246 class LocalFileLoader : public FileLoader
247 {
248 public:
249   LocalFileLoader(const QString& dirName):FileLoader(dirName) { }
250   bool load(DirDataStructure *parent) const;
251   void fillArgs(const QString& dn, QString& prg, QStringList& args) const;
252   QString prettyPrint() const;
253   QString entryForRSync(const QString& fn) const;
254   QString getMachine() const;
255   void removeFileArgs(const QString& filePath, QString& prg, QStringList& args) const;
256 };
257
258 class RemoteFileLoader : public FileLoader
259 {
260 public:
261   RemoteFileLoader(const QString& machine,const QString& dirName):FileLoader(dirName),_machine(machine) { }
262   bool load(DirDataStructure *parent) const;
263   void fillArgs(const QString& dn, QString& prg, QStringList& args) const;
264   QString prettyPrint() const;
265   QString entryForRSync(const QString& fn) const;
266   QString getMachine() const;
267   void removeFileArgs(const QString& filePath, QString& prg, QStringList& args) const;
268 private:
269   QString _machine;
270 };
271
272 class SelectionMimeData : public QMimeData
273 {
274   Q_OBJECT
275 public:
276   SelectionMimeData(const QModelIndexList& los):_los(los) { }
277   const QModelIndexList& getSelection() const { return _los; }
278 private:
279   QModelIndexList _los;
280 };
281
282 class QRemoteFileSystemModel : public QAbstractItemModel
283 {
284   Q_OBJECT
285 public:
286   QRemoteFileSystemModel(QObject *parent, FileLoader *fl);
287   QRemoteFileSystemModel(QObject *parent, TopDirDataStructure *fds);
288   QVariant headerData(int section, Qt::Orientation orientation, int role) const;
289   QModelIndex parent(const QModelIndex&) const;
290   QModelIndex index(int, int, const QModelIndex&) const;
291   int rowCount(const QModelIndex&) const;
292   int columnCount(const QModelIndex&) const;
293   QVariant data(const QModelIndex&, int) const;
294   void emitResetModel();
295   FileLoader *getLoader() const { return _fds->getLoader(); }
296   bool isOK() const { return _fds->isOK(); }
297   QString getMachine() const { return _fds->getMachine(); }
298 private:
299   TopDirDataStructure *_fds;
300 };
301
302 #endif