]> git.cworth.org Git - apitrace/blob - gui/apitracecall.cpp
Change apiarray to use a qvector instead of a qlist
[apitrace] / gui / apitracecall.cpp
1 #include "apitracecall.h"
2
3 #include "apitrace.h"
4 #include "trace_model.hpp"
5
6 #include <QDebug>
7 #include <QLocale>
8 #include <QObject>
9 #define QT_USE_FAST_OPERATOR_PLUS
10 #include <QStringBuilder>
11 #include <QTextDocument>
12
13 const char * const styleSheet =
14     ".call {\n"
15     "    font-weight:bold;\n"
16     // text shadow looks great but doesn't work well in qtwebkit 4.7
17     "    /*text-shadow: 0px 2px 3px #555;*/\n"
18     "    font-size: 1.2em;\n"
19     "}\n"
20     ".arg-name {\n"
21     "    border: 1px solid rgb(238,206,0);\n"
22     "    border-radius: 4px;\n"
23     "    background: yellow;\n"
24     "    padding: 2px;\n"
25     "    box-shadow: 0px 1px 3px dimgrey;\n"
26     "    -webkit-transition: background 1s linear;\n"
27     "}\n"
28     ".arg-name:hover {\n"
29     "    background: white;\n"
30     "}\n"
31     ".arg-value {\n"
32     "    color: #0000ff;\n"
33     "}\n"
34     ".error {\n"
35     "    border: 1px solid rgb(255,0,0);\n"
36     "    margin: 10px;\n"
37     "    padding: 1;\n"
38     "    border-radius: 4px;\n"
39     // also looks great but qtwebkit doesn't support it
40     //"    background: #6fb2e5;\n"
41     //"    box-shadow: 0 1px 5px #0061aa, inset 0 10px 20px #b6f9ff;\n"
42     //"    -o-box-shadow: 0 1px 5px #0061aa, inset 0 10px 20px #b6f9ff;\n"
43     //"    -webkit-box-shadow: 0 1px 5px #0061aa, inset 0 10px 20px #b6f9ff;\n"
44     //"    -moz-box-shadow: 0 1px 5px #0061aa, inset 0 10px 20px #b6f9ff;\n"
45     "}\n";
46
47
48 // Qt::convertFromPlainText doesn't do precisely what we want
49 static QString
50 plainTextToHTML(const QString & plain, bool multiLine)
51 {
52     int col = 0;
53     bool quote = false;
54     QString rich;
55     for (int i = 0; i < plain.length(); ++i) {
56         if (plain[i] == QLatin1Char('\n')){
57             if (multiLine) {
58                 rich += QLatin1String("<br>\n");
59             } else {
60                 rich += QLatin1String("\\n");
61             }
62             col = 0;
63             quote = true;
64         } else {
65             if (plain[i] == QLatin1Char('\t')){
66                 if (multiLine) {
67                     rich += QChar(0x00a0U);
68                     ++col;
69                     while (col % 8) {
70                         rich += QChar(0x00a0U);
71                         ++col;
72                     }
73                 } else {
74                     rich += QLatin1String("\\t");
75                 }
76                 quote = true;
77             } else if (plain[i].isSpace()) {
78                 rich += QChar(0x00a0U);
79                 quote = true;
80             } else if (plain[i] == QLatin1Char('<')) {
81                 rich += QLatin1String("&lt;");
82             } else if (plain[i] == QLatin1Char('>')) {
83                 rich += QLatin1String("&gt;");
84             } else if (plain[i] == QLatin1Char('&')) {
85                 rich += QLatin1String("&amp;");
86             } else {
87                 rich += plain[i];
88             }
89             ++col;
90         }
91     }
92
93     if (quote) {
94         return QLatin1Literal("\"") + rich + QLatin1Literal("\"");
95     }
96
97     return rich;
98 }
99
100 QString
101 apiVariantToString(const QVariant &variant, bool multiLine)
102 {
103     if (variant.userType() == QVariant::Double) {
104         return QString::number(variant.toFloat());
105     }
106     if (variant.userType() == QVariant::ByteArray) {
107         if (variant.toByteArray().size() < 1024) {
108             int bytes = variant.toByteArray().size();
109             return QObject::tr("[binary data, size = %1 bytes]").arg(bytes);
110         } else {
111             float kb = variant.toByteArray().size()/1024.;
112             return QObject::tr("[binary data, size = %1 kb]").arg(kb);
113         }
114     }
115
116     if (variant.userType() == QVariant::String) {
117         return plainTextToHTML(variant.toString(), multiLine);
118     }
119
120     if (variant.userType() < QVariant::UserType) {
121         return variant.toString();
122     }
123
124     if (variant.canConvert<ApiPointer>()) {
125         return variant.value<ApiPointer>().toString();
126     }
127     if (variant.canConvert<ApiBitmask>()) {
128         return variant.value<ApiBitmask>().toString();
129     }
130     if (variant.canConvert<ApiStruct>()) {
131         return variant.value<ApiStruct>().toString();
132     }
133     if (variant.canConvert<ApiArray>()) {
134         return variant.value<ApiArray>().toString();
135     }
136     if (variant.canConvert<ApiEnum>()) {
137         return variant.value<ApiEnum>().toString();
138     }
139
140     return QString();
141 }
142
143
144 void VariantVisitor::visit(Trace::Null *)
145 {
146     m_variant = QVariant::fromValue(ApiPointer(0));
147 }
148
149 void VariantVisitor::visit(Trace::Bool *node)
150 {
151     m_variant = QVariant(node->value);
152 }
153
154 void VariantVisitor::visit(Trace::SInt *node)
155 {
156     m_variant = QVariant(node->value);
157 }
158
159 void VariantVisitor::visit(Trace::UInt *node)
160 {
161     m_variant = QVariant(node->value);
162 }
163
164 void VariantVisitor::visit(Trace::Float *node)
165 {
166     m_variant = QVariant(node->value);
167 }
168
169 void VariantVisitor::visit(Trace::String *node)
170 {
171     m_variant = QVariant(QString::fromStdString(node->value));
172 }
173
174 void VariantVisitor::visit(Trace::Enum *e)
175 {
176     ApiTraceEnumSignature *sig = 0;
177
178     if (m_trace) {
179         sig = m_trace->enumSignature(e->sig->id);
180     }
181     if (!sig) {
182         sig = new ApiTraceEnumSignature(
183             QString::fromStdString(e->sig->name),
184             QVariant(e->sig->value));
185         if (m_trace) {
186             m_trace->addEnumSignature(e->sig->id, sig);
187         }
188     }
189
190     m_variant = QVariant::fromValue(ApiEnum(sig));
191 }
192
193 void VariantVisitor::visit(Trace::Bitmask *bitmask)
194 {
195     m_variant = QVariant::fromValue(ApiBitmask(bitmask));
196 }
197
198 void VariantVisitor::visit(Trace::Struct *str)
199 {
200     m_variant = QVariant::fromValue(ApiStruct(str));
201 }
202
203 void VariantVisitor::visit(Trace::Array *array)
204 {
205     m_variant = QVariant::fromValue(ApiArray(array));
206 }
207
208 void VariantVisitor::visit(Trace::Blob *blob)
209 {
210     //XXX
211     //FIXME: this is a nasty hack. Trace::Blob's can't
212     //   delete the contents in the destructor because
213     //   the data is being used by other calls. We piggy back
214     //   on that assumption and don't deep copy the data. If
215     //   Blob's will start deleting the data we will need to
216     //   start deep copying it or switch to using something like
217     //   Boost's shared_ptr or Qt's QSharedPointer to handle it
218     QByteArray barray = QByteArray::fromRawData(blob->buf, blob->size);
219     m_variant = QVariant(barray);
220 }
221
222 void VariantVisitor::visit(Trace::Pointer *ptr)
223 {
224     m_variant = QVariant::fromValue(ApiPointer(ptr->value));
225 }
226
227
228 ApiEnum::ApiEnum(ApiTraceEnumSignature *sig)
229     : m_sig(sig)
230 {
231 }
232
233 QString ApiEnum::toString() const
234 {
235     if (m_sig) {
236         return m_sig->name();
237     }
238     Q_ASSERT(!"should never happen");
239     return QString();
240 }
241
242 QVariant ApiEnum::value() const
243 {
244     if (m_sig) {
245         return m_sig->value();
246     }
247     Q_ASSERT(!"should never happen");
248     return QVariant();
249 }
250
251 QString ApiEnum::name() const
252 {
253     if (m_sig) {
254         return m_sig->name();
255     }
256     Q_ASSERT(!"should never happen");
257     return QString();
258 }
259
260 unsigned long long ApiBitmask::value() const
261 {
262     return m_value;
263 }
264
265 ApiBitmask::Signature ApiBitmask::signature() const
266 {
267     return m_sig;
268 }
269
270 ApiStruct::Signature ApiStruct::signature() const
271 {
272     return m_sig;
273 }
274
275 QList<QVariant> ApiStruct::values() const
276 {
277     return m_members;
278 }
279
280 ApiPointer::ApiPointer(unsigned long long val)
281     : m_value(val)
282 {
283 }
284
285
286 unsigned long long ApiPointer::value() const
287 {
288     return m_value;
289 }
290
291 QString ApiPointer::toString() const
292 {
293     if (m_value)
294         return QString("0x%1").arg(m_value, 0, 16);
295     else
296         return QLatin1String("NULL");
297 }
298
299 ApiBitmask::ApiBitmask(const Trace::Bitmask *bitmask)
300     : m_value(0)
301 {
302     init(bitmask);
303 }
304
305 void ApiBitmask::init(const Trace::Bitmask *bitmask)
306 {
307     if (!bitmask)
308         return;
309
310     m_value = bitmask->value;
311     for (const Trace::BitmaskFlag *it = bitmask->sig->flags;
312          it != bitmask->sig->flags + bitmask->sig->num_flags; ++it) {
313         assert(it->value);
314         QPair<QString, unsigned long long> pair;
315
316         pair.first = QString::fromStdString(it->name);
317         pair.second = it->value;
318
319         m_sig.append(pair);
320     }
321 }
322
323 QString ApiBitmask::toString() const
324 {
325     QString str;
326     unsigned long long value = m_value;
327     bool first = true;
328     for (Signature::const_iterator it = m_sig.begin();
329          value != 0 && it != m_sig.end(); ++it) {
330         Q_ASSERT(it->second);
331         if ((value & it->second) == it->second) {
332             if (!first) {
333                 str += QLatin1String(" | ");
334             }
335             str += it->first;
336             value &= ~it->second;
337             first = false;
338         }
339     }
340     if (value || first) {
341         if (!first) {
342             str += QLatin1String(" | ");
343         }
344         str += QString::fromLatin1("0x%1").arg(value, 0, 16);
345     }
346     return str;
347 }
348
349 ApiStruct::ApiStruct(const Trace::Struct *s)
350 {
351     init(s);
352 }
353
354 QString ApiStruct::toString() const
355 {
356     QString str;
357
358     str += QLatin1String("{");
359     for (unsigned i = 0; i < m_members.count(); ++i) {
360         str += m_sig.memberNames[i] %
361                QLatin1Literal(" = ") %
362                apiVariantToString(m_members[i]);
363         if (i < m_members.count() - 1)
364             str += QLatin1String(", ");
365     }
366     str += QLatin1String("}");
367
368     return str;
369 }
370
371 void ApiStruct::init(const Trace::Struct *s)
372 {
373     if (!s)
374         return;
375
376     m_sig.name = QString::fromStdString(s->sig->name);
377     for (unsigned i = 0; i < s->sig->num_members; ++i) {
378         VariantVisitor vis(0);
379         m_sig.memberNames.append(
380             QString::fromStdString(s->sig->member_names[i]));
381         s->members[i]->visit(vis);
382         m_members.append(vis.variant());
383     }
384 }
385
386 ApiArray::ApiArray(const Trace::Array *arr)
387 {
388     init(arr);
389 }
390
391 ApiArray::ApiArray(const QVector<QVariant> &vals)
392     : m_array(vals)
393 {
394 }
395
396 QVector<QVariant> ApiArray::values() const
397 {
398     return m_array;
399 }
400
401 QString ApiArray::toString() const
402 {
403     QString str;
404     str += QLatin1String("[");
405     for(int i = 0; i < m_array.count(); ++i) {
406         const QVariant &var = m_array[i];
407         str += apiVariantToString(var);
408         if (i < m_array.count() - 1)
409             str += QLatin1String(", ");
410     }
411     str += QLatin1String("]");
412
413     return str;
414 }
415
416 void ApiArray::init(const Trace::Array *arr)
417 {
418     if (!arr)
419         return;
420
421     m_array.reserve(arr->values.size());
422     for (int i = 0; i < arr->values.size(); ++i) {
423         VariantVisitor vis(0);
424         arr->values[i]->visit(vis);
425
426         m_array.append(vis.variant());
427     }
428     m_array.squeeze();
429 }
430
431 ApiTraceState::ApiTraceState()
432 {
433 }
434
435 ApiTraceState::ApiTraceState(const QVariantMap &parsedJson)
436 {
437     m_parameters = parsedJson[QLatin1String("parameters")].toMap();
438     QVariantMap attachedShaders =
439         parsedJson[QLatin1String("shaders")].toMap();
440     QVariantMap::const_iterator itr;
441
442
443     for (itr = attachedShaders.constBegin(); itr != attachedShaders.constEnd();
444          ++itr) {
445         QString type = itr.key();
446         QString source = itr.value().toString();
447         m_shaderSources[type] = source;
448     }
449
450     m_uniforms = parsedJson[QLatin1String("uniforms")].toMap();
451
452     QVariantMap textures =
453         parsedJson[QLatin1String("textures")].toMap();
454     for (itr = textures.constBegin(); itr != textures.constEnd(); ++itr) {
455         QVariantMap image = itr.value().toMap();
456         QSize size(image[QLatin1String("__width__")].toInt(),
457                    image[QLatin1String("__height__")].toInt());
458         QString cls = image[QLatin1String("__class__")].toString();
459         QString type = image[QLatin1String("__type__")].toString();
460         bool normalized =
461             image[QLatin1String("__normalized__")].toBool();
462         int numChannels =
463             image[QLatin1String("__channels__")].toInt();
464
465         Q_ASSERT(type == QLatin1String("uint8"));
466         Q_ASSERT(normalized == true);
467         Q_UNUSED(normalized);
468
469         QByteArray dataArray =
470             image[QLatin1String("__data__")].toByteArray();
471
472         ApiTexture tex;
473         tex.setSize(size);
474         tex.setNumChannels(numChannels);
475         tex.setLabel(itr.key());
476         tex.contentsFromBase64(dataArray);
477
478         m_textures.append(tex);
479     }
480
481     QVariantMap fbos =
482         parsedJson[QLatin1String("framebuffer")].toMap();
483     for (itr = fbos.constBegin(); itr != fbos.constEnd(); ++itr) {
484         QVariantMap buffer = itr.value().toMap();
485         QSize size(buffer[QLatin1String("__width__")].toInt(),
486                    buffer[QLatin1String("__height__")].toInt());
487         QString cls = buffer[QLatin1String("__class__")].toString();
488         QString type = buffer[QLatin1String("__type__")].toString();
489         bool normalized = buffer[QLatin1String("__normalized__")].toBool();
490         int numChannels = buffer[QLatin1String("__channels__")].toInt();
491
492         Q_ASSERT(type == QLatin1String("uint8"));
493         Q_ASSERT(normalized == true);
494         Q_UNUSED(normalized);
495
496         QByteArray dataArray =
497             buffer[QLatin1String("__data__")].toByteArray();
498
499         ApiFramebuffer fbo;
500         fbo.setSize(size);
501         fbo.setNumChannels(numChannels);
502         fbo.setType(itr.key());
503         fbo.contentsFromBase64(dataArray);
504         m_framebuffers.append(fbo);
505     }
506 }
507
508 const QVariantMap & ApiTraceState::parameters() const
509 {
510     return m_parameters;
511 }
512
513 const QMap<QString, QString> & ApiTraceState::shaderSources() const
514 {
515     return m_shaderSources;
516 }
517
518 const QVariantMap & ApiTraceState::uniforms() const
519 {
520     return m_uniforms;
521 }
522
523 bool ApiTraceState::isEmpty() const
524 {
525     return m_parameters.isEmpty();
526 }
527
528 const QList<ApiTexture> & ApiTraceState::textures() const
529 {
530     return m_textures;
531 }
532
533 const QList<ApiFramebuffer> & ApiTraceState::framebuffers() const
534 {
535     return m_framebuffers;
536 }
537
538 ApiTraceCallSignature::ApiTraceCallSignature(const QString &name,
539                                              const QStringList &argNames)
540     : m_name(name),
541       m_argNames(argNames)
542 {
543 }
544
545 ApiTraceCallSignature::~ApiTraceCallSignature()
546 {
547 }
548
549 QUrl ApiTraceCallSignature::helpUrl() const
550 {
551     return m_helpUrl;
552 }
553
554 void ApiTraceCallSignature::setHelpUrl(const QUrl &url)
555 {
556     m_helpUrl = url;
557 }
558
559 ApiTraceEvent::ApiTraceEvent()
560     : m_type(ApiTraceEvent::None),
561       m_hasBinaryData(false),
562       m_binaryDataIndex(0),
563       m_state(0),
564       m_staticText(0)
565 {
566 }
567
568 ApiTraceEvent::ApiTraceEvent(Type t)
569     : m_type(t),
570       m_hasBinaryData(false),
571       m_binaryDataIndex(0),
572       m_state(0),
573       m_staticText(0)
574 {
575 }
576
577 ApiTraceEvent::~ApiTraceEvent()
578 {
579     delete m_state;
580     delete m_staticText;
581 }
582
583 QVariantMap ApiTraceEvent::stateParameters() const
584 {
585     if (m_state) {
586         return m_state->parameters();
587     } else {
588         return QVariantMap();
589     }
590 }
591
592 ApiTraceState *ApiTraceEvent::state() const
593 {
594     return m_state;
595 }
596
597 void ApiTraceEvent::setState(ApiTraceState *state)
598 {
599     m_state = state;
600 }
601
602 ApiTraceCall::ApiTraceCall(ApiTraceFrame *parentFrame, const Trace::Call *call)
603     : ApiTraceEvent(ApiTraceEvent::Call),
604       m_parentFrame(parentFrame)
605 {
606     ApiTrace *trace = parentTrace();
607
608     Q_ASSERT(trace);
609
610     m_index = call->no;
611
612     m_signature = trace->signature(call->sig->id);
613
614     if (!m_signature) {
615         QString name = QString::fromStdString(call->sig->name);
616         QStringList argNames;
617         argNames.reserve(call->sig->num_args);
618         for (int i = 0; i < call->sig->num_args; ++i) {
619             argNames += QString::fromStdString(call->sig->arg_names[i]);
620         }
621         m_signature = new ApiTraceCallSignature(name, argNames);
622         trace->addSignature(call->sig->id, m_signature);
623     }
624     if (call->ret) {
625         VariantVisitor retVisitor(trace);
626         call->ret->visit(retVisitor);
627         m_returnValue = retVisitor.variant();
628     }
629     m_argValues.reserve(call->args.size());
630     for (int i = 0; i < call->args.size(); ++i) {
631         VariantVisitor argVisitor(trace);
632         call->args[i]->visit(argVisitor);
633         m_argValues.append(argVisitor.variant());
634         if (m_argValues[i].type() == QVariant::ByteArray) {
635             m_hasBinaryData = true;
636             m_binaryDataIndex = i;
637         }
638     }
639     m_argValues.squeeze();
640 }
641
642 ApiTraceCall::~ApiTraceCall()
643 {
644 }
645
646
647 bool ApiTraceCall::hasError() const
648 {
649     return !m_error.isEmpty();
650 }
651
652 QString ApiTraceCall::error() const
653 {
654     return m_error;
655 }
656
657 void ApiTraceCall::setError(const QString &msg)
658 {
659     if (m_error != msg) {
660         ApiTrace *trace = parentTrace();
661         m_error = msg;
662         m_richText = QString();
663         if (trace)
664             trace->callError(this);
665     }
666 }
667
668 ApiTrace * ApiTraceCall::parentTrace() const
669 {
670     if (m_parentFrame)
671         return m_parentFrame->parentTrace();
672     return NULL;
673 }
674
675 QVector<QVariant> ApiTraceCall::originalValues() const
676 {
677     return m_argValues;
678 }
679
680 void ApiTraceCall::setEditedValues(const QVector<QVariant> &lst)
681 {
682     ApiTrace *trace = parentTrace();
683
684     m_editedValues = lst;
685     //lets regenerate data
686     m_richText = QString();
687     m_searchText = QString();
688     delete m_staticText;
689     m_staticText = 0;
690
691     if (trace) {
692         if (!lst.isEmpty()) {
693             trace->callEdited(this);
694         } else {
695             trace->callReverted(this);
696         }
697     }
698 }
699
700 QVector<QVariant> ApiTraceCall::editedValues() const
701 {
702     return m_editedValues;
703 }
704
705 bool ApiTraceCall::edited() const
706 {
707     return !m_editedValues.isEmpty();
708 }
709
710 void ApiTraceCall::revert()
711 {
712     setEditedValues(QVector<QVariant>());
713 }
714
715 void ApiTraceCall::setHelpUrl(const QUrl &url)
716 {
717     m_signature->setHelpUrl(url);
718 }
719
720 void ApiTraceCall::setParentFrame(ApiTraceFrame *frame)
721 {
722     m_parentFrame = frame;
723 }
724
725 ApiTraceFrame * ApiTraceCall::parentFrame()const
726 {
727     return m_parentFrame;
728 }
729
730 int ApiTraceCall::index() const
731 {
732     return m_index;
733 }
734
735 QString ApiTraceCall::name() const
736 {
737     return m_signature->name();
738 }
739
740 QStringList ApiTraceCall::argNames() const
741 {
742     return m_signature->argNames();
743 }
744
745 QVector<QVariant> ApiTraceCall::arguments() const
746 {
747     if (m_editedValues.isEmpty())
748         return m_argValues;
749     else
750         return m_editedValues;
751 }
752
753 QVariant ApiTraceCall::returnValue() const
754 {
755     return m_returnValue;
756 }
757
758 QUrl ApiTraceCall::helpUrl() const
759 {
760     return m_signature->helpUrl();
761 }
762
763 bool ApiTraceCall::hasBinaryData() const
764 {
765     return m_hasBinaryData;
766 }
767
768 int ApiTraceCall::binaryDataIndex() const
769 {
770     return m_binaryDataIndex;
771 }
772
773 QStaticText ApiTraceCall::staticText() const
774 {
775     if (m_staticText && !m_staticText->text().isEmpty())
776         return *m_staticText;
777
778     QVector<QVariant> argValues = arguments();
779
780     QString richText = QString::fromLatin1(
781         "<span style=\"font-weight:bold\">%1</span>(").arg(
782             m_signature->name());
783     QStringList argNames = m_signature->argNames();
784     for (int i = 0; i < argNames.count(); ++i) {
785         richText += QLatin1String("<span style=\"color:#0000ff\">");
786         QString argText = apiVariantToString(argValues[i]);
787
788         //if arguments are really long (e.g. shader text), cut them
789         // and elide it
790         if (argText.length() > 40) {
791             QString shortened = argText.mid(0, 40);
792             shortened[argText.length() - 5] = '.';
793             shortened[argText.length() - 4] = '.';
794             shortened[argText.length() - 3] = '.';
795             shortened[argText.length() - 2] = argText.at(argText.length() - 2);
796             shortened[argText.length() - 1] = argText.at(argText.length() - 1);
797             richText += shortened;
798         } else {
799             richText += argText;
800         }
801         richText += QLatin1String("</span>");
802         if (i < argNames.count() - 1)
803             richText += QLatin1String(", ");
804     }
805     richText += QLatin1String(")");
806     if (m_returnValue.isValid()) {
807         richText +=
808             QLatin1Literal(" = ") %
809             QLatin1Literal("<span style=\"color:#0000ff\">") %
810             apiVariantToString(m_returnValue) %
811             QLatin1Literal("</span>");
812     }
813
814     if (!m_staticText)
815         m_staticText = new QStaticText(richText);
816     else
817         m_staticText->setText(richText);
818     QTextOption opt;
819     opt.setWrapMode(QTextOption::NoWrap);
820     m_staticText->setTextOption(opt);
821     m_staticText->prepare();
822
823     return *m_staticText;
824 }
825
826 QString ApiTraceCall::toHtml() const
827 {
828     if (!m_richText.isEmpty())
829         return m_richText;
830
831     m_richText = QLatin1String("<div class=\"call\">");
832
833     QUrl helpUrl = m_signature->helpUrl();
834     if (helpUrl.isEmpty()) {
835         m_richText += QString::fromLatin1(
836             "%1) <span class=\"callName\">%2</span>(")
837                       .arg(m_index)
838                       .arg(m_signature->name());
839     } else {
840         m_richText += QString::fromLatin1(
841             "%1) <span class=\"callName\"><a href=\"%2\">%3</a></span>(")
842                       .arg(m_index)
843                       .arg(helpUrl.toString())
844                       .arg(m_signature->name());
845     }
846
847     QVector<QVariant> argValues = arguments();
848     QStringList argNames = m_signature->argNames();
849     for (int i = 0; i < argNames.count(); ++i) {
850         m_richText +=
851             QLatin1String("<span class=\"arg-name\">") +
852             argNames[i] +
853             QLatin1String("</span>") +
854             QLatin1Literal(" = ") +
855             QLatin1Literal("<span class=\"arg-value\">") +
856             apiVariantToString(argValues[i], true) +
857             QLatin1Literal("</span>");
858         if (i < argNames.count() - 1)
859             m_richText += QLatin1String(", ");
860     }
861     m_richText += QLatin1String(")");
862
863     if (m_returnValue.isValid()) {
864         m_richText +=
865             QLatin1String(" = ") +
866             QLatin1String("<span style=\"color:#0000ff\">") +
867             apiVariantToString(m_returnValue, true) +
868             QLatin1String("</span>");
869     }
870     m_richText += QLatin1String("</div>");
871
872     if (hasError()) {
873         QString errorStr =
874             QString::fromLatin1(
875                 "<div class=\"error\">%1</div>")
876             .arg(m_error);
877         m_richText += errorStr;
878     }
879
880     m_richText =
881         QString::fromLatin1(
882             "<html><head><style type=\"text/css\" media=\"all\">"
883             "%1</style></head><body>%2</body></html>")
884         .arg(styleSheet)
885         .arg(m_richText);
886     m_richText.squeeze();
887
888     //qDebug()<<m_richText;
889     return m_richText;
890 }
891
892 QString ApiTraceCall::searchText() const
893 {
894     if (!m_searchText.isEmpty())
895         return m_searchText;
896
897     QVector<QVariant> argValues = arguments();
898     m_searchText = m_signature->name() + QLatin1Literal("(");
899     QStringList argNames = m_signature->argNames();
900     for (int i = 0; i < argNames.count(); ++i) {
901         m_searchText += argNames[i] +
902                         QLatin1Literal(" = ") +
903                         apiVariantToString(argValues[i]);
904         if (i < argNames.count() - 1)
905             m_searchText += QLatin1String(", ");
906     }
907     m_searchText += QLatin1String(")");
908
909     if (m_returnValue.isValid()) {
910         m_searchText += QLatin1Literal(" = ") +
911                         apiVariantToString(m_returnValue);
912     }
913     m_searchText.squeeze();
914     return m_searchText;
915 }
916
917 int ApiTraceCall::numChildren() const
918 {
919     return 0;
920 }
921
922 ApiTraceFrame::ApiTraceFrame(ApiTrace *parentTrace)
923     : ApiTraceEvent(ApiTraceEvent::Frame),
924       m_parentTrace(parentTrace),
925       m_binaryDataSize(0)
926 {
927 }
928
929 QStaticText ApiTraceFrame::staticText() const
930 {
931     if (m_staticText && !m_staticText->text().isEmpty())
932         return *m_staticText;
933
934     QString richText;
935
936     //mark the frame if it uploads more than a meg a frame
937     if (m_binaryDataSize > (1024*1024)) {
938         richText =
939             QObject::tr(
940                 "<span style=\"font-weight:bold;\">"
941                 "Frame&nbsp;%1</span>"
942                 "<span style=\"font-style:italic;\">"
943                 "&nbsp;&nbsp;&nbsp;&nbsp;(%2MB)</span>")
944             .arg(number)
945             .arg(double(m_binaryDataSize / (1024.*1024.)), 0, 'g', 2);
946     } else {
947         richText =
948             QObject::tr(
949                 "<span style=\"font-weight:bold\">Frame %1</span>")
950             .arg(number);
951     }
952
953     if (!m_staticText)
954         m_staticText = new QStaticText(richText);
955
956     QTextOption opt;
957     opt.setWrapMode(QTextOption::NoWrap);
958     m_staticText->setTextOption(opt);
959     m_staticText->prepare();
960
961     return *m_staticText;
962 }
963
964 int ApiTraceFrame::numChildren() const
965 {
966     return m_calls.count();
967 }
968
969 ApiTrace * ApiTraceFrame::parentTrace() const
970 {
971     return m_parentTrace;
972 }
973
974 void ApiTraceFrame::addCall(ApiTraceCall *call)
975 {
976     m_calls.append(call);
977     if (call->hasBinaryData()) {
978         QByteArray data =
979             call->arguments()[call->binaryDataIndex()].toByteArray();
980         m_binaryDataSize += data.size();
981     }
982 }
983
984 QVector<ApiTraceCall*> ApiTraceFrame::calls() const
985 {
986     return m_calls;
987 }
988
989 ApiTraceCall * ApiTraceFrame::call(int idx) const
990 {
991     return m_calls.value(idx);
992 }
993
994 int ApiTraceFrame::callIndex(ApiTraceCall *call) const
995 {
996     return m_calls.indexOf(call);
997 }
998
999 bool ApiTraceFrame::isEmpty() const
1000 {
1001     return m_calls.isEmpty();
1002 }
1003
1004 int ApiTraceFrame::binaryDataSize() const
1005 {
1006     return m_binaryDataSize;
1007 }
1008
1009 void ApiTraceFrame::setCalls(const QVector<ApiTraceCall*> &calls,
1010                              quint64 binaryDataSize)
1011 {
1012     m_calls = calls;
1013     m_binaryDataSize = binaryDataSize;
1014 }