1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #ifndef _U2_WORKFLOW_VIEW_CONTROLLER_H_
23 #define _U2_WORKFLOW_VIEW_CONTROLLER_H_
24 
25 #include <QAction>
26 #include <QGraphicsScene>
27 
28 #include <U2Gui/MainWindow.h>
29 
30 #include <U2Lang/ActorModel.h>
31 #include <U2Lang/Aliasing.h>
32 #include <U2Lang/AttributeInfo.h>
33 #include <U2Lang/Schema.h>
34 #include <U2Lang/SchemaConfig.h>
35 #include <U2Lang/WorkflowRunTask.h>
36 
37 class QComboBox;
38 class QSplitter;
39 class QDomDocument;
40 class QListWidget;
41 class QListWidgetItem;
42 class QTabWidget;
43 class QGroupBox;
44 
45 namespace U2 {
46 using namespace Workflow;
47 class DashboardManagerHelper;
48 class SamplesWidget;
49 class WorkflowView;
50 class WorkflowScene;
51 class WorkflowPalette;
52 class WorkflowEditor;
53 class WorkflowGObject;
54 class WorkflowBusItem;
55 class WorkflowPortItem;
56 class WorkflowProcessItem;
57 class WorkflowRunTask;
58 class WorkflowDebugStatus;
59 class BreakpointManagerView;
60 class WorkflowInvestigationWidgetsController;
61 class WorkflowTabView;
62 class ExternalToolLogParser;
63 class LoadWorkflowSceneTask;
64 
65 class WorkflowScene : public QGraphicsScene {
66     Q_OBJECT
67     Q_DISABLE_COPY(WorkflowScene)
68 public:
69     WorkflowScene(WorkflowView *parent = 0);
70     virtual ~WorkflowScene();
isModified()71     bool isModified() const {
72         return modified;
73     }
isLocked()74     bool isLocked() const {
75         return locked;
76     }
77 
getController()78     WorkflowView *getController() const {
79         return controller;
80     }
getRunner()81     WorkflowAbstractRunner *getRunner() const {
82         return runner;
83     }
setRunner(WorkflowAbstractRunner * r)84     void setRunner(WorkflowAbstractRunner *r) {
85         runner = r;
86     }
87 
getLastMousePressPoint()88     const QPointF &getLastMousePressPoint() const {
89         return lastMousePressPoint;
90     }
91 
92     QList<Actor *> getSelectedActors() const;
93 
94     void clearScene();
95     void onModified();
96 
97     void setupLinkCtxMenu(const QString &href, Actor *actor, const QPoint &pos);
98 
99     WorkflowBusItem *addFlow(WorkflowPortItem *from, WorkflowPortItem *to, Link *link);
100 
101 public slots:
102     void sl_deleteItem();
103     void sl_selectAll();
104     void sl_deselectAll();
105     void sl_reset();
setLocked(bool b)106     void setLocked(bool b) {
107         locked = b;
108     }
109     void setModified(bool b);
110     void setModified();
111     void centerView();
setHint(int i)112     void setHint(int i) {
113         hint = i;
114         update();
115     }
116     void sl_openDocuments();
sl_updateDocs()117     void sl_updateDocs() {
118         emit configurationChanged();
119     }
120     void connectConfigurationEditors();
121 
122 signals:
123     void processItemAdded();
124     void processDblClicked();
125     void configurationChanged();
126     void si_itemDeleted(const ActorId &id);
127 
128 protected:
129     void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
130     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent);
131     void contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent);
132 
133     void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
134     void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
135     void dropEvent(QGraphicsSceneDragDropEvent *event);
136     void keyPressEvent(QKeyEvent *event);
137     void keyReleaseEvent(QKeyEvent *event);
138 
139     void drawBackground(QPainter *painter, const QRectF &rect);
140 
141 private:
142     WorkflowView *controller;
143     bool modified, locked;
144 
145     WorkflowAbstractRunner *runner;
146     int hint;
147     QAction *openDocumentsAction;
148 
149     /* Point stores last mouse press position on the scene.  Used to insert new elements from context menu */
150     QPointF lastMousePressPoint;
151 };  // WorkflowScene
152 
153 class WorkflowView : public MWMDIWindow, public SchemaConfig {
154     friend class DashboardManagerHelper;
155     friend class WorkflowScene;
156     Q_OBJECT
157     Q_DISABLE_COPY(WorkflowView)
158 
159     WorkflowView(WorkflowGObject *go);
160     static WorkflowView *createInstance(WorkflowGObject *go);
161 
162 public:
163     static WorkflowView *openWD(WorkflowGObject *go);
164     ~WorkflowView();
165 
getPropertyEditor()166     WorkflowEditor *getPropertyEditor() const {
167         return propertyEditor;
168     }
169 
170     virtual void setupMDIToolbar(QToolBar *tb);
171     virtual void setupViewMenu(QMenu *n);
172     void setupContextMenu(QMenu *menu);
173 
174     void onModified();
175     bool confirmModified();
176 
selectedProto()177     ActorPrototype *selectedProto() const {
178         return currentProto;
179     }
180 
getActor()181     Actor *getActor() {
182         return currentActor;
183     }
184     void addProcess(Actor *proc, const QPointF &pos);
185     void removeProcessItem(WorkflowProcessItem *item);
186     void removeBusItem(WorkflowBusItem *item);
187     void onBusRemoved(Link *link);
188     Actor *createActor(ActorPrototype *proto, const QVariantMap &params) const;
189     WorkflowBusItem *tryBind(WorkflowPortItem *port1, WorkflowPortItem *port2);
190 
getScene()191     WorkflowScene *getScene() const {
192         return scene;
193     }
194     QSharedPointer<Schema> getSchema() const;
195     const Workflow::Metadata &getMeta();
196     const Workflow::Metadata &updateMeta();
197     Workflow::Metadata getMeta(const QList<WorkflowProcessItem *> &items);
198 
refreshView()199     void refreshView() {
200         sl_refreshActorDocs();
201     }
202 
203     // SchemaConfig
204     virtual RunFileSystem *getRFS();
205     virtual QVariant getAttributeValue(const AttributeInfo &info) const;
206     virtual void setAttributeValue(const AttributeInfo &info, const QVariant &value);
207 
208     bool isShowSamplesHint() const;
209 
210 public slots:
211     void sl_updateUi();
212     void sl_rescaleScene(const QString &scale);
213     void sl_loadScene(const QString &url, bool fromDashboard = true);
214 
215 private slots:
216     void sl_editItem();
217     void sl_onSelectionChanged();
218     void sl_showEditor();
219     void sl_selectPrototype(Workflow::ActorPrototype *, bool);
220     void sl_exportScene();
221     void sl_saveScene();
222     void sl_saveSceneAs();
223     void sl_loadScene();
224     void sl_newScene();
225     void sl_updateTitle();
226     void sl_onSceneLoaded();
227     void sl_onSceneSaved();
228 
229     void sl_copyItems();
230     void sl_cutItems();
231     void sl_pasteItems(const QString & = QString(), bool updateSchemaInfo = false);
232     void sl_pasteSample(const QString &);
233 
234     void sl_setStyle();
235     void sl_toggleStyle();
236     void sl_refreshActorDocs();
237 
238     void sl_changeScriptMode();
239 
240     bool sl_validate(bool notify = true);
241     void sl_estimate();
242     void sl_estimationTaskFinished();
243     void sl_pickInfo(QListWidgetItem *);
244     void sl_launch();
245     void sl_stop();
246     void sl_pause(bool isPause = true);
247     void sl_configureParameterAliases();
248     void sl_createGalaxyConfig();
249     void sl_configurePortAliases();
250     void sl_importSchemaToElement();
251 
252     void sl_toggleLock(bool = true);
253     void sl_createScript();
254     void sl_editScript();
255     void sl_createCmdlineBasedWorkerAction();
256     void sl_appendExternalToolWorker();
257     void sl_prototypeIsAboutToBeRemoved(Workflow::ActorPrototype *proto);
258     void sl_xmlSchemaLoaded(Task *);
259     void sl_editExternalTool();
260     void sl_findPrototype();
261     void sl_protoListModified();
262 
263     void sl_updateSchema();
264     void sl_showWizard();
265     void sl_toggleDashboard();
266     void sl_dashboardCountChanged();
267 
268     void sl_toggleBreakpointManager();
269 
270     void sl_breakpointAdded(const ActorId &actor);
271     void sl_breakpointRemoved(const ActorId &actor);
272     void sl_breakpointIsReached(const U2::ActorId &actor);
273     void sl_processOneMessage();
274     void sl_highlightingRequested(const ActorId &actor);
275     void sl_breakpointEnabled(const ActorId &actor);
276     void sl_breakpointDisabled(const ActorId &actor);
277     void sl_convertMessages2Documents(const Workflow::Link *bus, const QString &messageType, int messageNumber);
278 
279 protected:
280     bool onCloseEvent();
281     virtual void paintEvent(QPaintEvent *event);
282 
283 private:
284     void createActions();
285     void saveState();
286     void recreateScene();
287     void localHostLaunch();
288     void rescale(bool updateGui = true);
289 
290     void toggleDebugActionsState(bool enable);
291     void changeBreakpointState(const ActorId &actor, bool isBreakpointBeingAdded, bool isBreakpointStateBeingChanged = false);
292     WorkflowProcessItem *findItemById(ActorId actor) const;
293     void addBottomWidgetsToInfoSplitter();
294     void setInvestigationWidgetsVisible(bool visible);
295     void propagateBreakpointToSceneItem(ActorId actor);
296 
297     void removeWizards();
298     void removeEstimations();
299 
300     // setup ui
301     void setupScene();
302     void setupPalette();
303     void setupPropertyEditor();
304     void setupErrorList();
305     void setupMainSplitter();
306     void loadSceneFromObject();
307     void loadUiSettings();
308 
309     // dashboards
310     void setupActions();
311     void hideDashboards();
312     void showDashboards();
313     void setDashboardActionDecoration(bool isDashboardsViewActive);
314     void setDashboardActionVisible(bool visible);
315     void commitWarningsToMonitor(WorkflowAbstractRunner *t);
316 
317     /** Asynchronously starts wizard dialogs. */
318     void startWizard(Wizard *wizard);
319 
320     /** Runs wizard dialog,waits until it is finished and processes wizard results. */
321     void runWizardAndHandleResult(Wizard *wizard);
322 
323     /** Calls 'startWizard' for the first wizard from schema with AutoRun flag. */
324     void startFirstAutoRunWizard();
325 
326     void loadWizardResult(const QString &result);
327     void procItemAdded();
328 
329 private:
330     bool running;
331     bool sceneRecreation;
332     WorkflowGObject *go;
333     const QSharedPointer<Schema> schema;
334     Workflow::Metadata meta;
335     ActorPrototype *currentProto;
336     Actor *currentActor;
337 
338     QString lastPaste;
339     int pasteCount;
340 
341     bool scriptingMode;
342 
343     QPointer<QMenu> elementsMenu;
344 
345     QAction *deleteAction;
346     QAction *deleteShortcut;
347     QAction *selectAction;
348     QAction *copyAction;
349     QAction *pasteAction;
350     QAction *cutAction;
351     QAction *exportAction;
352     QAction *saveAction;
353     QAction *saveAsAction;
354     QAction *loadAction;
355     QAction *newAction;
356     QAction *createScriptAction;
357     QAction *editScriptAction;
358     QAction *createCmdlineBasedWorkerAction;
359     QAction *appendExternalTool;
360     QAction *editExternalToolAction;
361     QAction *configureParameterAliasesAction;
362     QAction *createGalaxyConfigAction;
363     QAction *configurePortAliasesAction;
364     QAction *importSchemaToElement;
365     QAction *runAction;
366     QAction *stopAction;
367     QAction *validateAction;
368     QAction *pauseAction;
369     QAction *nextStepAction;
370     QAction *toggleBreakpointAction;
371     QAction *tickReadyAction;
372     QAction *estimateAction;
373 
374     QAction *findPrototypeAction;
375     QAction *unlockAction;
376     QAction *showWizard;
377     QAction *toggleDashboard;
378     QAction *loadSep;
379     QAction *runSep;
380     QAction *confSep;
381     QAction *extSep;
382     QAction *scaleSep;
383     QAction *scaleAction;
384     QAction *scriptAction;
385     QAction *dmAction;
386     QList<QAction *> styleActions;
387     QList<QAction *> scriptingActions;
388     QComboBox *scaleComboBox;
389 
390     QAction *toggleBreakpointManager;
391 
392     QSplitter *splitter;
393     WorkflowPalette *palette;
394     WorkflowEditor *propertyEditor;
395     WorkflowTabView *tabView;
396     WorkflowScene *scene;
397     QGraphicsView *sceneView;
398     SamplesWidget *samples;
399     QTabWidget *tabs;
400     QGroupBox *errorList;
401     QListWidget *infoList;
402 
403     QSplitter *infoSplitter;
404 
405     WorkflowDebugStatus *debugInfo;
406     QList<QAction *> debugActions;
407     BreakpointManagerView *breakpointView;
408     QTabWidget *bottomTabs;
409     WorkflowInvestigationWidgetsController *investigationWidgets;
410     QPointer<LoadWorkflowSceneTask> loadWorkflowSceneTask;
411 
412     ExternalToolLogParser *rLogParser;
413 };
414 
415 class SceneCreator {
416 public:
417     SceneCreator(Schema *schema, const Workflow::Metadata &meta);
418     virtual ~SceneCreator();
419 
420     WorkflowScene *createScene(WorkflowView *controller);
421     WorkflowScene *recreateScene(WorkflowScene *scene);
422 
423 private:
424     Schema *schema;
425     Workflow::Metadata meta;
426     WorkflowScene *scene;
427 
428     WorkflowScene *createScene();
429     WorkflowProcessItem *createProcess(Actor *actor);
430     void createBus(const QMap<Port *, WorkflowPortItem *> &ports, Link *link);
431 };
432 
433 class DashboardManagerHelper : public QObject {
434     Q_OBJECT
435 public:
436     DashboardManagerHelper(QAction *dmAction, WorkflowView *parent);
437 
438 private slots:
439     void sl_result(int result);
440     void sl_showDashboardsManagerDialog();
441     void sl_dashboardsScanningStarted();
442     void sl_dashboardsScanningFinished();
443 
444 private:
445     QAction *dmAction;
446     WorkflowView *parent;
447 };
448 
449 }  // namespace U2
450 
451 #endif
452