]> git.cworth.org Git - apitrace/blob - gui/apitracecall.cpp
Switch more places from qlist to qvector.
[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 QList<QVariant> &vals)
392     : m_array(vals)
393 {
394 }
395
396 QList<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 }
429
430 ApiTraceState::ApiTraceState()
431 {
432 }
433
434 ApiTraceState::ApiTraceState(const QVariantMap &parsedJson)
435 {
436     m_parameters = parsedJson[QLatin1String("parameters")].toMap();
437     QVariantMap attachedShaders =
438         parsedJson[QLatin1String("shaders")].toMap();
439     QVariantMap::const_iterator itr;
440
441
442     for (itr = attachedShaders.constBegin(); itr != attachedShaders.constEnd();
443          ++itr) {
444         QString type = itr.key();
445         QString source = itr.value().toString();
446         m_shaderSources[type] = source;
447     }
448
449     m_uniforms = parsedJson[QLatin1String("uniforms")].toMap();
450
451     QVariantMap textures =
452         parsedJson[QLatin1String("textures")].toMap();
453     for (itr = textures.constBegin(); itr != textures.constEnd(); ++itr) {
454         QVariantMap image = itr.value().toMap();
455         QSize size(image[QLatin1String("__width__")].toInt(),
456                    image[QLatin1String("__height__")].toInt());
457         QString cls = image[QLatin1String("__class__")].toString();
458         QString type = image[QLatin1String("__type__")].toString();
459         bool normalized =
460             image[QLatin1String("__normalized__")].toBool();
461         int numChannels =
462             image[QLatin1String("__channels__")].toInt();
463
464         Q_ASSERT(type == QLatin1String("uint8"));
465         Q_ASSERT(normalized == true);
466         Q_UNUSED(normalized);
467
468         QByteArray dataArray =
469             image[QLatin1String("__data__")].toByteArray();
470
471         ApiTexture tex;
472         tex.setSize(size);
473         tex.setNumChannels(numChannels);
474         tex.setLabel(itr.key());
475         tex.contentsFromBase64(dataArray);
476
477         m_textures.append(tex);
478     }
479
480     QVariantMap fbos =
481         parsedJson[QLatin1String("framebuffer")].toMap();
482     for (itr = fbos.constBegin(); itr != fbos.constEnd(); ++itr) {
483         QVariantMap buffer = itr.value().toMap();
484         QSize size(buffer[QLatin1String("__width__")].toInt(),
485                    buffer[QLatin1String("__height__")].toInt());
486         QString cls = buffer[QLatin1String("__class__")].toString();
487         QString type = buffer[QLatin1String("__type__")].toString();
488         bool normalized = buffer[QLatin1String("__normalized__")].toBool();
489         int numChannels = buffer[QLatin1String("__channels__")].toInt();
490
491         Q_ASSERT(type == QLatin1String("uint8"));
492         Q_ASSERT(normalized == true);
493         Q_UNUSED(normalized);
494
495         QByteArray dataArray =
496             buffer[QLatin1String("__data__")].toByteArray();
497
498         ApiFramebuffer fbo;
499         fbo.setSize(size);
500         fbo.setNumChannels(numChannels);
501         fbo.setType(itr.key());
502         fbo.contentsFromBase64(dataArray);
503         m_framebuffers.append(fbo);
504     }
505 }
506
507 const QVariantMap & ApiTraceState::parameters() const
508 {
509     return m_parameters;
510 }
511
512 const QMap<QString, QString> & ApiTraceState::shaderSources() const
513 {
514     return m_shaderSources;
515 }
516
517 const QVariantMap & ApiTraceState::uniforms() const
518 {
519     return m_uniforms;
520 }
521
522 bool ApiTraceState::isEmpty() const
523 {
524     return m_parameters.isEmpty();
525 }
526
527 const QList<ApiTexture> & ApiTraceState::textures() const
528 {
529     return m_textures;
530 }
531
532 const QList<ApiFramebuffer> & ApiTraceState::framebuffers() const
533 {
534     return m_framebuffers;
535 }
536
537 ApiTraceCallSignature::ApiTraceCallSignature(const QString &name,
538                                              const QStringList &argNames)
539     : m_name(name),
540       m_argNames(argNames)
541 {
542 }
543
544 ApiTraceCallSignature::~ApiTraceCallSignature()
545 {
546 }
547
548 QUrl ApiTraceCallSignature::helpUrl() const
549 {
550     return m_helpUrl;
551 }
552
553 void ApiTraceCallSignature::setHelpUrl(const QUrl &url)
554 {
555     m_helpUrl = url;
556 }
557
558 ApiTraceEvent::ApiTraceEvent()
559     : m_type(ApiTraceEvent::None),
560       m_hasBinaryData(false),
561       m_binaryDataIndex(0),
562       m_state(0),
563       m_staticText(0)
564 {
565 }
566
567 ApiTraceEvent::ApiTraceEvent(Type t)
568     : m_type(t),
569       m_hasBinaryData(false),
570       m_binaryDataIndex(0),
571       m_state(0),
572       m_staticText(0)
573 {
574 }
575
576 ApiTraceEvent::~ApiTraceEvent()
577 {
578     delete m_state;
579     delete m_staticText;
580 }
581
582 QVariantMap ApiTraceEvent::stateParameters() const
583 {
584     if (m_state) {
585         return m_state->parameters();
586     } else {
587         return QVariantMap();
588     }
589 }
590
591 ApiTraceState *ApiTraceEvent::state() const
592 {
593     return m_state;
594 }
595
596 void ApiTraceEvent::setState(ApiTraceState *state)
597 {
598     m_state = state;
599 }
600
601 ApiTraceCall::ApiTraceCall(ApiTraceFrame *parentFrame, const Trace::Call *call)
602     : ApiTraceEvent(ApiTraceEvent::Call),
603       m_parentFrame(parentFrame)
604 {
605     ApiTrace *trace = parentTrace();
606
607     Q_ASSERT(trace);
608
609     m_index = call->no;
610
611     m_signature = trace->signature(call->sig->id);
612
613     if (!m_signature) {
614         QString name = QString::fromStdString(call->sig->name);
615         QStringList argNames;
616         argNames.reserve(call->sig->num_args);
617         for (int i = 0; i < call->sig->num_args; ++i) {
618             argNames += QString::fromStdString(call->sig->arg_names[i]);
619         }
620         m_signature = new ApiTraceCallSignature(name, argNames);
621         trace->addSignature(call->sig->id, m_signature);
622     }
623     if (call->ret) {
624         VariantVisitor retVisitor(trace);
625         call->ret->visit(retVisitor);
626         m_returnValue = retVisitor.variant();
627     }
628     m_argValues.reserve(call->args.size());
629     for (int i = 0; i < call->args.size(); ++i) {
630         VariantVisitor argVisitor(trace);
631         call->args[i]->visit(argVisitor);
632         m_argValues.append(argVisitor.variant());
633         if (m_argValues[i].type() == QVariant::ByteArray) {
634             m_hasBinaryData = true;
635             m_binaryDataIndex = i;
636         }
637     }
638     m_argValues.squeeze();
639 }
640
641 ApiTraceCall::~ApiTraceCall()
642 {
643 }
644
645
646 bool ApiTraceCall::hasError() const
647 {
648     return !m_error.isEmpty();
649 }
650
651 QString ApiTraceCall::error() const
652 {
653     return m_error;
654 }
655
656 void ApiTraceCall::setError(const QString &msg)
657 {
658     if (m_error != msg) {
659         ApiTrace *trace = parentTrace();
660         m_error = msg;
661         m_richText = QString();
662         if (trace)
663             trace->callError(this);
664     }
665 }
666
667 ApiTrace * ApiTraceCall::parentTrace() const
668 {
669     if (m_parentFrame)
670         return m_parentFrame->parentTrace();
671     return NULL;
672 }
673
674 QVector<QVariant> ApiTraceCall::originalValues() const
675 {
676     return m_argValues;
677 }
678
679 void ApiTraceCall::setEditedValues(const QVector<QVariant> &lst)
680 {
681     ApiTrace *trace = parentTrace();
682
683     m_editedValues = lst;
684     //lets regenerate data
685     m_richText = QString();
686     m_searchText = QString();
687     delete m_staticText;
688     m_staticText = 0;
689
690     if (trace) {
691         if (!lst.isEmpty()) {
692             trace->callEdited(this);
693         } else {
694             trace->callReverted(this);
695         }
696     }
697 }
698
699 QVector<QVariant> ApiTraceCall::editedValues() const
700 {
701     return m_editedValues;
702 }
703
704 bool ApiTraceCall::edited() const
705 {
706     return !m_editedValues.isEmpty();
707 }
708
709 void ApiTraceCall::revert()
710 {
711     setEditedValues(QVector<QVariant>());
712 }
713
714 void ApiTraceCall::setHelpUrl(const QUrl &url)
715 {
716     m_signature->setHelpUrl(url);
717 }
718
719 void ApiTraceCall::setParentFrame(ApiTraceFrame *frame)
720 {
721     m_parentFrame = frame;
722 }
723
724 ApiTraceFrame * ApiTraceCall::parentFrame()const
725 {
726     return m_parentFrame;
727 }
728
729 int ApiTraceCall::index() const
730 {
731     return m_index;
732 }
733
734 QString ApiTraceCall::name() const
735 {
736     return m_signature->name();
737 }
738
739 QStringList ApiTraceCall::argNames() const
740 {
741     return m_signature->argNames();
742 }
743
744 QVector<QVariant> ApiTraceCall::arguments() const
745 {
746     if (m_editedValues.isEmpty())
747         return m_argValues;
748     else
749         return m_editedValues;
750 }
751
752 QVariant ApiTraceCall::returnValue() const
753 {
754     return m_returnValue;
755 }
756
757 QUrl ApiTraceCall::helpUrl() const
758 {
759     return m_signature->helpUrl();
760 }
761
762 bool ApiTraceCall::hasBinaryData() const
763 {
764     return m_hasBinaryData;
765 }
766
767 int ApiTraceCall::binaryDataIndex() const
768 {
769     return m_binaryDataIndex;
770 }
771
772 QStaticText ApiTraceCall::staticText() const
773 {
774     if (m_staticText && !m_staticText->text().isEmpty())
775         return *m_staticText;
776
777     QVector<QVariant> argValues = arguments();
778
779     QString richText = QString::fromLatin1(
780         "<span style=\"font-weight:bold\">%1</span>(").arg(
781             m_signature->name());
782     QStringList argNames = m_signature->argNames();
783     for (int i = 0; i < argNames.count(); ++i) {
784         richText += QLatin1String("<span style=\"color:#0000ff\">");
785         QString argText = apiVariantToString(argValues[i]);
786
787         //if arguments are really long (e.g. shader text), cut them
788         // and elide it
789         if (argText.length() > 40) {
790             QString shortened = argText.mid(0, 40);
791             shortened[argText.length() - 5] = '.';
792             shortened[argText.length() - 4] = '.';
793             shortened[argText.length() - 3] = '.';
794             shortened[argText.length() - 2] = argText.at(argText.length() - 2);
795             shortened[argText.length() - 1] = argText.at(argText.length() - 1);
796             richText += shortened;
797         } else {
798             richText += argText;
799         }
800         richText += QLatin1String("</span>");
801         if (i < argNames.count() - 1)
802             richText += QLatin1String(", ");
803     }
804     richText += QLatin1String(")");
805     if (m_returnValue.isValid()) {
806         richText +=
807             QLatin1Literal(" = ") %
808             QLatin1Literal("<span style=\"color:#0000ff\">") %
809             apiVariantToString(m_returnValue) %
810             QLatin1Literal("</span>");
811     }
812
813     if (!m_staticText)
814         m_staticText = new QStaticText(richText);
815     else
816         m_staticText->setText(richText);
817     QTextOption opt;
818     opt.setWrapMode(QTextOption::NoWrap);
819     m_staticText->setTextOption(opt);
820     m_staticText->prepare();
821
822     return *m_staticText;
823 }
824
825 QString ApiTraceCall::toHtml() const
826 {
827     if (!m_richText.isEmpty())
828         return m_richText;
829
830     m_richText = QLatin1String("<div class=\"call\">");
831
832     QUrl helpUrl = m_signature->helpUrl();
833     if (helpUrl.isEmpty()) {
834         m_richText += QString::fromLatin1(
835             "%1) <span class=\"callName\">%2</span>(")
836                       .arg(m_index)
837                       .arg(m_signature->name());
838     } else {
839         m_richText += QString::fromLatin1(
840             "%1) <span class=\"callName\"><a href=\"%2\">%3</a></span>(")
841                       .arg(m_index)
842                       .arg(helpUrl.toString())
843                       .arg(m_signature->name());
844     }
845
846     QVector<QVariant> argValues = arguments();
847     QStringList argNames = m_signature->argNames();
848     for (int i = 0; i < argNames.count(); ++i) {
849         m_richText +=
850             QLatin1String("<span class=\"arg-name\">") +
851             argNames[i] +
852             QLatin1String("</span>") +
853             QLatin1Literal(" = ") +
854             QLatin1Literal("<span class=\"arg-value\">") +
855             apiVariantToString(argValues[i], true) +
856             QLatin1Literal("</span>");
857         if (i < argNames.count() - 1)
858             m_richText += QLatin1String(", ");
859     }
860     m_richText += QLatin1String(")");
861
862     if (m_returnValue.isValid()) {
863         m_richText +=
864             QLatin1String(" = ") +
865             QLatin1String("<span style=\"color:#0000ff\">") +
866             apiVariantToString(m_returnValue, true) +
867             QLatin1String("</span>");
868     }
869     m_richText += QLatin1String("</div>");
870
871     if (hasError()) {
872         QString errorStr =
873             QString::fromLatin1(
874                 "<div class=\"error\">%1</div>")
875             .arg(m_error);
876         m_richText += errorStr;
877     }
878
879     m_richText =
880         QString::fromLatin1(
881             "<html><head><style type=\"text/css\" media=\"all\">"
882             "%1</style></head><body>%2</body></html>")
883         .arg(styleSheet)
884         .arg(m_richText);
885     m_richText.squeeze();
886
887     //qDebug()<<m_richText;
888     return m_richText;
889 }
890
891 QString ApiTraceCall::searchText() const
892 {
893     if (!m_searchText.isEmpty())
894         return m_searchText;
895
896     QVector<QVariant> argValues = arguments();
897     m_searchText = m_signature->name() + QLatin1Literal("(");
898     QStringList argNames = m_signature->argNames();
899     for (int i = 0; i < argNames.count(); ++i) {
900         m_searchText += argNames[i] +
901                         QLatin1Literal(" = ") +
902                         apiVariantToString(argValues[i]);
903         if (i < argNames.count() - 1)
904             m_searchText += QLatin1String(", ");
905     }
906     m_searchText += QLatin1String(")");
907
908     if (m_returnValue.isValid()) {
909         m_searchText += QLatin1Literal(" = ") +
910                         apiVariantToString(m_returnValue);
911     }
912     m_searchText.squeeze();
913     return m_searchText;
914 }
915
916 int ApiTraceCall::numChildren() const
917 {
918     return 0;
919 }
920
921 ApiTraceFrame::ApiTraceFrame(ApiTrace *parentTrace)
922     : ApiTraceEvent(ApiTraceEvent::Frame),
923       m_parentTrace(parentTrace),
924       m_binaryDataSize(0)
925 {
926 }
927
928 QStaticText ApiTraceFrame::staticText() const
929 {
930     if (m_staticText && !m_staticText->text().isEmpty())
931         return *m_staticText;
932
933     QString richText;
934
935     //mark the frame if it uploads more than a meg a frame
936     if (m_binaryDataSize > (1024*1024)) {
937         richText =
938             QObject::tr(
939                 "<span style=\"font-weight:bold;\">"
940                 "Frame&nbsp;%1</span>"
941                 "<span style=\"font-style:italic;\">"
942                 "&nbsp;&nbsp;&nbsp;&nbsp;(%2MB)</span>")
943             .arg(number)
944             .arg(double(m_binaryDataSize / (1024.*1024.)), 0, 'g', 2);
945     } else {
946         richText =
947             QObject::tr(
948                 "<span style=\"font-weight:bold\">Frame %1</span>")
949             .arg(number);
950     }
951
952     if (!m_staticText)
953         m_staticText = new QStaticText(richText);
954
955     QTextOption opt;
956     opt.setWrapMode(QTextOption::NoWrap);
957     m_staticText->setTextOption(opt);
958     m_staticText->prepare();
959
960     return *m_staticText;
961 }
962
963 int ApiTraceFrame::numChildren() const
964 {
965     return m_calls.count();
966 }
967
968 ApiTrace * ApiTraceFrame::parentTrace() const
969 {
970     return m_parentTrace;
971 }
972
973 void ApiTraceFrame::addCall(ApiTraceCall *call)
974 {
975     m_calls.append(call);
976     if (call->hasBinaryData()) {
977         QByteArray data =
978             call->arguments()[call->binaryDataIndex()].toByteArray();
979         m_binaryDataSize += data.size();
980     }
981 }
982
983 QVector<ApiTraceCall*> ApiTraceFrame::calls() const
984 {
985     return m_calls;
986 }
987
988 ApiTraceCall * ApiTraceFrame::call(int idx) const
989 {
990     return m_calls.value(idx);
991 }
992
993 int ApiTraceFrame::callIndex(ApiTraceCall *call) const
994 {
995     return m_calls.indexOf(call);
996 }
997
998 bool ApiTraceFrame::isEmpty() const
999 {
1000     return m_calls.isEmpty();
1001 }
1002
1003 int ApiTraceFrame::binaryDataSize() const
1004 {
1005     return m_binaryDataSize;
1006 }
1007
1008 void ApiTraceFrame::setCalls(const QVector<ApiTraceCall*> &calls,
1009                              quint64 binaryDataSize)
1010 {
1011     m_calls = calls;
1012     m_binaryDataSize = binaryDataSize;
1013 }