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