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_CFG_EDITOR_H_
23 #define _U2_WORKFLOW_CFG_EDITOR_H_
24 
25 #include <QItemDelegate>
26 
27 #include <U2Core/U2OpStatus.h>
28 
29 #include <U2Lang/Attribute.h>
30 #include <U2Lang/SchemaConfig.h>
31 
32 class QWidget;
33 
34 namespace U2 {
35 
36 class ConfigurationEditor;
37 class DelegateTags;
38 class PropertyDelegate;
39 
40 /**
41  * base class for controller of configuration editor
42  * editing comes from delegates (see PropertyDelegate)
43  */
44 class U2LANG_EXPORT ConfigurationEditor : public QObject {
45     Q_OBJECT
46 public:
47     enum ItemValueRole {
48         ItemValueRole = Qt::UserRole + 2,
49         ItemListValueRole
50     };  // ItemValueRole
51 
52 public:
ConfigurationEditor()53     ConfigurationEditor() {
54     }
ConfigurationEditor(const ConfigurationEditor &)55     ConfigurationEditor(const ConfigurationEditor &)
56         : QObject() {
57     }
~ConfigurationEditor()58     virtual ~ConfigurationEditor() {
59     }
60 
61     // editing widget
getWidget()62     virtual QWidget *getWidget() {
63         return nullptr;
64     }
65 
66     // this controller is a container of delegates
getDelegate(const QString &)67     virtual PropertyDelegate *getDelegate(const QString &) {
68         return nullptr;
69     }
removeDelegate(const QString &)70     virtual PropertyDelegate *removeDelegate(const QString &) {
71         return nullptr;
72     }
updateDelegates()73     virtual void updateDelegates() {
74     }
updateDelegate(const QString &)75     virtual void updateDelegate(const QString &) {
76     }
addDelegate(PropertyDelegate *,const QString &)77     virtual void addDelegate(PropertyDelegate *, const QString &) {
78     }
79 
80     // commit data to model
commit()81     virtual void commit() {
82     }
83 
84     // make another editor
clone()85     virtual ConfigurationEditor *clone() {
86         return new ConfigurationEditor(*this);
87     }
88 
isEmpty()89     virtual bool isEmpty() const {
90         return false;
91     }
92 
93 signals:
94     void si_configurationChanged();
95 
96 };  // ConfigurationEditor
97 
98 /**
99  * Abstract class for property widgets: spin box, url line...
100  */
101 class U2LANG_EXPORT PropertyWidget : public QWidget {
102     Q_OBJECT
103 public:
104     PropertyWidget(QWidget *parent = nullptr, DelegateTags *tags = nullptr);
105     virtual ~PropertyWidget();
106 
107     virtual QVariant value() = 0;
108     virtual void setRequired();
109     virtual void activate();
110 
111     /**
112      * Returns the widget that can be registered as a field of wizard pages.
113      * Returns NULL if there is no such widget.
114      */
115     virtual QWidget *getField();
116 
117     void setDelegateTags(const DelegateTags *value);
118     const DelegateTags *tags() const;
119     void setSchemaConfig(SchemaConfig *value);
120 
121 public slots:
122     virtual void setValue(const QVariant &value) = 0;
processDelegateTags()123     virtual void processDelegateTags() {
124     }
125 
126 signals:
127     void si_valueChanged(const QVariant &value);
128 
129 protected:
130     void addMainWidget(QWidget *w);
131 
132 protected:
133     const DelegateTags *_tags;
134     SchemaConfig *schemaConfig;
135 };
136 
137 /**
138  * provides display and editing facilities for data items
139  * in our case, inheritors will provide this facilities for attributes of configuration
140  */
141 class U2LANG_EXPORT PropertyDelegate : public QItemDelegate {
142     Q_OBJECT
143 public:
144     enum Type {
145         NO_TYPE,
146         INPUT_FILE,
147         INPUT_DIR,
148         OUTPUT_FILE,
149         OUTPUT_DIR,
150         SHARED_DB_URL
151     };
152     PropertyDelegate(QObject *parent = 0);
153     virtual ~PropertyDelegate();
154     virtual QVariant getDisplayValue(const QVariant &v) const;
155     virtual PropertyDelegate *clone();
156     virtual PropertyWidget *createWizardWidget(U2OpStatus &os, QWidget *parent) const;
getItems(QVariantMap &)157     virtual void getItems(QVariantMap &) const {
158     }
159     virtual Type type() const;
update()160     virtual void update() {
161     }
162 
163     DelegateTags *tags() const;
164     void setSchemaConfig(SchemaConfig *value);
165 
166 protected:
167     DelegateTags *_tags;
168     SchemaConfig *schemaConfig;
169 };  // PropertyDelegate
170 
171 class U2LANG_EXPORT DelegateTags : public QObject {
172 public:
173     DelegateTags(QObject *parent = nullptr);
174     DelegateTags(const DelegateTags &other);
175 
176     QStringList names() const;
177     QVariant get(const QString &name) const;
178     void set(const QString &name, const QVariant &value);
179     void set(const DelegateTags &other);
180 
181     static QString getString(const DelegateTags *tags, const QString &name);
182     static QStringList getStringList(const DelegateTags *tags, const QString &name);
183 
184     DelegateTags &operator=(const DelegateTags &other);
185 
186     static const QString PLACEHOLDER_TEXT;  // placeholder (like in QLineEdit)
187     static const QString FILTER;  // file filter (like in QFileDialog)
188     static const QString FORMAT;  // document format ID
189 
190 private:
191     QVariantMap tags;
192 };
193 
194 }  // namespace U2
195 
196 #endif
197