TreeFrogFramework  2.8
tactionview.h
Go to the documentation of this file.
1 #pragma once
2 #include <QObject>
3 #include <QTextStream>
4 #include <QVariant>
5 #include <TActionHelper>
6 #include <TGlobal>
7 #include <THttpRequest>
8 #include <THttpUtility>
9 #include <TViewHelper>
10 
12 
13 
14 class T_CORE_EXPORT TActionView : public QObject, public TActionHelper, public TViewHelper {
15 public:
16  TActionView();
17  virtual ~TActionView() { }
18 
19  virtual QString toString() = 0;
20  QString yield() const;
21  QString renderPartial(const QString &templateName, const QVariantMap &vars = QVariantMap()) const;
22  QString authenticityToken() const;
23  QVariant variant(const QString &name) const;
24  bool hasVariant(const QString &name) const;
25  const QVariantMap &allVariants() const;
26  const TAbstractController *controller() const override;
27  const THttpRequest &httpRequest() const;
28  void reset();
29 
30 protected:
31  QString echo(const QString &str);
32  QString echo(const char *str);
33  QString echo(const QByteArray &str);
34  QString echo(int n, int base = 10);
35  QString echo(long n, int base = 10);
36  QString echo(ulong n, int base = 10);
37  QString echo(qlonglong n, int base = 10);
38  QString echo(qulonglong n, int base = 10);
39  QString echo(double d, char format = 'g', int precision = 6);
40  QString echo(const QJsonObject &object);
41  QString echo(const QJsonArray &array);
42  QString echo(const QJsonDocument &doc);
43  QString echo(const THtmlAttribute &attr);
44  QString echo(const QVariant &var);
45  QString echo(const QVariantMap &map);
46  QString eh(const QString &str);
47  QString eh(const char *str);
48  QString eh(const QByteArray &str);
49  QString eh(int n, int base = 10);
50  QString eh(long n, int base = 10);
51  QString eh(ulong n, int base = 10);
52  QString eh(qlonglong n, int base = 10);
53  QString eh(qulonglong n, int base = 10);
54  QString eh(double d, char format = 'g', int precision = 6);
55  QString eh(const QJsonObject &object);
56  QString eh(const QJsonArray &array);
57  QString eh(const QJsonDocument &doc);
58  QString eh(const THtmlAttribute &attr);
59  QString eh(const QVariant &var);
60  QString eh(const QVariantMap &map);
61  QString renderReact(const QString &component);
62 
63  QString responsebody;
64 
65 private:
68 
69  void setVariantMap(const QVariantMap &vars);
70  void setController(TAbstractController *controller);
71  void setSubActionView(TActionView *actionView);
72  virtual const TActionView *actionView() const override { return this; }
73 
74  TAbstractController *actionController {nullptr};
75  TActionView *subView {nullptr};
76  QVariantMap variantMap;
77 
78  friend class TActionController;
79  friend class TActionMailer;
80  friend class TDirectView;
81 };
82 
83 
84 inline void TActionView::setSubActionView(TActionView *actionView)
85 {
86  subView = actionView;
87 }
88 
90 {
91  return actionController;
92 }
93 
94 inline void TActionView::setVariantMap(const QVariantMap &vars)
95 {
96  variantMap = vars;
97 }
98 
99 inline QVariant TActionView::variant(const QString &name) const
100 {
101  return variantMap.value(name);
102 }
103 
104 inline bool TActionView::hasVariant(const QString &name) const
105 {
106  return variantMap.contains(name);
107 }
108 
109 inline const QVariantMap &TActionView::allVariants() const
110 {
111  return variantMap;
112 }
113 
114 inline QString TActionView::echo(const QString &str)
115 {
116  responsebody += str;
117  return QString();
118 }
119 
120 inline QString TActionView::echo(const char *str)
121 {
122  responsebody += QString(str); // using codecForCStrings()
123  return QString();
124 }
125 
126 inline QString TActionView::echo(const QByteArray &str)
127 {
128  responsebody += QString(str); // using codecForCStrings()
129  return QString();
130 }
131 
132 inline QString TActionView::echo(int n, int base)
133 {
134  responsebody += QString::number(n, base);
135  return QString();
136 }
137 
138 inline QString TActionView::echo(long n, int base)
139 {
140  responsebody += QString::number(n, base);
141  return QString();
142 }
143 
144 inline QString TActionView::echo(ulong n, int base)
145 {
146  responsebody += QString::number(n, base);
147  return QString();
148 }
149 
150 inline QString TActionView::echo(qlonglong n, int base)
151 {
152  responsebody += QString::number(n, base);
153  return QString();
154 }
155 
156 inline QString TActionView::echo(qulonglong n, int base)
157 {
158  responsebody += QString::number(n, base);
159  return QString();
160 }
161 
162 inline QString TActionView::echo(double d, char format, int precision)
163 {
164  responsebody += QString::number(d, format, precision);
165  return QString();
166 }
167 
168 inline QString TActionView::echo(const QJsonObject &object)
169 {
170  return echo(QJsonDocument(object));
171 }
172 
173 inline QString TActionView::echo(const QJsonArray &array)
174 {
175  return echo(QJsonDocument(array));
176 }
177 
178 inline QString TActionView::echo(const QJsonDocument &doc)
179 {
180  responsebody += doc.toJson(QJsonDocument::Compact);
181  return QString();
182 }
183 
184 inline QString TActionView::eh(const QString &str)
185 {
186  return echo(THttpUtility::htmlEscape(str));
187 }
188 
189 inline QString TActionView::eh(const char *str)
190 {
191  return echo(THttpUtility::htmlEscape(str));
192 }
193 
194 inline QString TActionView::eh(const QByteArray &str)
195 {
196  return echo(THttpUtility::htmlEscape(str));
197 }
198 
199 inline QString TActionView::eh(int n, int base)
200 {
201  return echo(THttpUtility::htmlEscape(QString::number(n, base)));
202 }
203 
204 inline QString TActionView::eh(long n, int base)
205 {
206  return echo(THttpUtility::htmlEscape(QString::number(n, base)));
207 }
208 
209 inline QString TActionView::eh(ulong n, int base)
210 {
211  return echo(THttpUtility::htmlEscape(QString::number(n, base)));
212 }
213 
214 inline QString TActionView::eh(qlonglong n, int base)
215 {
216  return echo(THttpUtility::htmlEscape(QString::number(n, base)));
217 }
218 
219 inline QString TActionView::eh(qulonglong n, int base)
220 {
221  return echo(THttpUtility::htmlEscape(QString::number(n, base)));
222 }
223 
224 inline QString TActionView::eh(double d, char format, int precision)
225 {
226  return echo(THttpUtility::htmlEscape(QString::number(d, format, precision)));
227 }
228 
229 inline QString TActionView::eh(const QJsonObject &object)
230 {
231  return eh(QJsonDocument(object));
232 }
233 
234 inline QString TActionView::eh(const QJsonArray &array)
235 {
236  return eh(QJsonDocument(array));
237 }
238 
239 inline QString TActionView::eh(const QJsonDocument &doc)
240 {
241  return echo(THttpUtility::htmlEscape(doc.toJson(QJsonDocument::Compact)));
242 }
243 
244 inline void TActionView::setController(TAbstractController *controller)
245 {
246  actionController = controller;
247 }
248 
The TAbstractController class is the abstract base class of controllers, providing functionality comm...
Definition: tabstractcontroller.h:13
The TActionController class is the base class of all action controllers.
Definition: tactioncontroller.h:21
The TActionHelper class is the base class of all helpers.
Definition: tactionhelper.h:12
virtual const TAbstractController * controller() const =0
The TActionMailer class provides a mail client on action controller.
Definition: tactionmailer.h:6
The TActionView class is the abstract base class of views, providing functionality common to view.
Definition: tactionview.h:14
QString echo(const QString &str)
Outputs the string str to a view template.
Definition: tactionview.h:114
virtual QString toString()=0
This function is reimplemented in subclasses to return a string to render a view.
const TAbstractController * controller() const override
Definition: tactionview.h:89
virtual ~TActionView()
Definition: tactionview.h:17
const QVariantMap & allVariants() const
Definition: tactionview.h:109
QString eh(const QString &str)
Outputs a escaped string of the str to a view template.
Definition: tactionview.h:184
QString responsebody
Definition: tactionview.h:63
bool hasVariant(const QString &name) const
Returns true if the QVariantMap variable for a view contains an item with the name; otherwise returns...
Definition: tactionview.h:104
QVariant variant(const QString &name) const
Returns the value associated with the name in the QVariantMap variable for a view.
Definition: tactionview.h:99
The THtmlAttribute class represents HTML attributes for customizing web elements.
Definition: thtmlattribute.h:7
The THttpRequest class contains request information for HTTP.
Definition: thttprequest.h:34
static QString htmlEscape(const QString &input, Tf::EscapeFlag flag=Tf::Quotes)
Returns a converted copy of input.
Definition: thttputility.cpp:140
The TViewHelper class provides some functionality for views.
Definition: tviewhelper.h:15
virtual const TActionView * actionView() const =0
Must be overridden by subclasses to return the current action view.
#define T_CORE_EXPORT
Definition: tdeclexport.h:28
#define T_DISABLE_COPY(Class)
Definition: tdeclexport.h:37
#define T_DISABLE_MOVE(Class)
Definition: tdeclexport.h:41