]> git.cworth.org Git - apitrace/blob - gui/apitracecall.cpp
bd30aaaa89aff9a333ea84f13dc534e2b62d4a42
[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     blob->toPointer(true);
219     QByteArray barray = QByteArray::fromRawData(blob->buf, blob->size);
220     m_variant = QVariant(barray);
221 }
222
223 void VariantVisitor::visit(Trace::Pointer *ptr)
224 {
225     m_variant = QVariant::fromValue(ApiPointer(ptr->value));
226 }
227
228
229 ApiEnum::ApiEnum(ApiTraceEnumSignature *sig)
230     : m_sig(sig)
231 {
232 }
233
234 QString ApiEnum::toString() const
235 {
236     if (m_sig) {
237         return m_sig->name();
238     }
239     Q_ASSERT(!"should never happen");
240     return QString();
241 }
242
243 QVariant ApiEnum::value() const
244 {
245     if (m_sig) {
246         return m_sig->value();
247     }
248     Q_ASSERT(!"should never happen");
249     return QVariant();
250 }
251
252 QString ApiEnum::name() const
253 {
254     if (m_sig) {
255         return m_sig->name();
256     }
257     Q_ASSERT(!"should never happen");
258     return QString();
259 }
260
261 unsigned long long ApiBitmask::value() const
262 {
263     return m_value;
264 }
265
266 ApiBitmask::Signature ApiBitmask::signature() const
267 {
268     return m_sig;
269 }
270
271 ApiStruct::Signature ApiStruct::signature() const
272 {
273     return m_sig;
274 }
275
276 QList<QVariant> ApiStruct::values() const
277 {
278     return m_members;
279 }
280
281 ApiPointer::ApiPointer(unsigned long long val)
282     : m_value(val)
283 {
284 }
285
286
287 unsigned long long ApiPointer::value() const
288 {
289     return m_value;
290 }
291
292 QString ApiPointer::toString() const
293 {
294     if (m_value)
295         return QString("0x%1").arg(m_value, 0, 16);
296     else
297         return QLatin1String("NULL");
298 }
299
300 ApiBitmask::ApiBitmask(const Trace::Bitmask *bitmask)
301     : m_value(0)
302 {
303     init(bitmask);
304 }
305
306 void ApiBitmask::init(const Trace::Bitmask *bitmask)
307 {
308     if (!bitmask)
309         return;
310
311     m_value = bitmask->value;
312     for (const Trace::BitmaskFlag *it = bitmask->sig->flags;
313          it != bitmask->sig->flags + bitmask->sig->num_flags; ++it) {
314         assert(it->value);
315         QPair<QString, unsigned long long> pair;
316
317         pair.first = QString::fromStdString(it->name);
318         pair.second = it->value;
319
320         m_sig.append(pair);
321     }
322 }
323
324 QString ApiBitmask::toString() const
325 {
326     QString str;
327     unsigned long long value = m_value;
328     bool first = true;
329     for (Signature::const_iterator it = m_sig.begin();
330          value != 0 && it != m_sig.end(); ++it) {
331         Q_ASSERT(it->second);
332         if ((value & it->second) == it->second) {
333             if (!first) {
334                 str += QLatin1String(" | ");
335             }
336             str += it->first;
337             value &= ~it->second;
338             first = false;
339         }
340     }
341     if (value || first) {
342         if (!first) {
343             str += QLatin1String(" | ");
344         }
345         str += QString::fromLatin1("0x%1").arg(value, 0, 16);
346     }
347     return str;
348 }
349
350 ApiStruct::ApiStruct(const Trace::Struct *s)
351 {
352     init(s);
353 }
354
355 QString ApiStruct::toString() const
356 {
357     QString str;
358
359     str += QLatin1String("{");
360     for (unsigned i = 0; i < m_members.count(); ++i) {
361         str += m_sig.memberNames[i] %
362                QLatin1Literal(" = ") %
363                apiVariantToString(m_members[i]);
364         if (i < m_members.count() - 1)
365             str += QLatin1String(", ");
366     }
367     str += QLatin1String("}");
368
369     return str;
370 }
371
372 void ApiStruct::init(const Trace::Struct *s)
373 {
374     if (!s)
375         return;
376
377     m_sig.name = QString::fromStdString(s->sig->name);
378     for (unsigned i = 0; i < s->sig->num_members; ++i) {
379         VariantVisitor vis(0);
380         m_sig.memberNames.append(
381             QString::fromStdString(s->sig->member_names[i]));
382         s->members[i]->visit(vis);
383         m_members.append(vis.variant());
384     }
385 }
386
387 ApiArray::ApiArray(const Trace::Array *arr)
388 {
389     init(arr);
390 }
391
392 ApiArray::ApiArray(const QVector<QVariant> &vals)
393     : m_array(vals)
394 {
395 }
396
397 QVector<QVariant> ApiArray::values() const
398 {
399     return m_array;
400 }
401
402 QString ApiArray::toString() const
403 {
404     QString str;
405     str += QLatin1String("[");
406     for(int i = 0; i < m_array.count(); ++i) {
407         const QVariant &var = m_array[i];
408         str += apiVariantToString(var);
409         if (i < m_array.count() - 1)
410             str += QLatin1String(", ");
411     }
412     str += QLatin1String("]");
413
414     return str;
415 }
416
417 void ApiArray::init(const Trace::Array *arr)
418 {
419     if (!arr)
420         return;
421
422     m_array.reserve(arr->values.size());
423     for (int i = 0; i < arr->values.size(); ++i) {
424         VariantVisitor vis(0);
425         arr->values[i]->visit(vis);
426
427         m_array.append(vis.variant());
428     }
429     m_array.squeeze();
430 }
431
432 ApiTraceState::ApiTraceState()
433 {
434 }
435
436 ApiTraceState::ApiTraceState(const QVariantMap &parsedJson)
437 {
438     m_parameters = parsedJson[QLatin1String("parameters")].toMap();
439     QVariantMap attachedShaders =
440         parsedJson[QLatin1String("shaders")].toMap();
441     QVariantMap::const_iterator itr;
442
443
444     for (itr = attachedShaders.constBegin(); itr != attachedShaders.constEnd();
445          ++itr) {
446         QString type = itr.key();
447         QString source = itr.value().toString();
448         m_shaderSources[type] = source;
449     }
450
451     m_uniforms = parsedJson[QLatin1String("uniforms")].toMap();
452
453     QVariantMap textures =
454         parsedJson[QLatin1String("textures")].toMap();
455     for (itr = textures.constBegin(); itr != textures.constEnd(); ++itr) {
456         QVariantMap image = itr.value().toMap();
457         QSize size(image[QLatin1String("__width__")].toInt(),
458                    image[QLatin1String("__height__")].toInt());
459         QString cls = image[QLatin1String("__class__")].toString();
460         QString type = image[QLatin1String("__type__")].toString();
461         bool normalized =
462             image[QLatin1String("__normalized__")].toBool();
463         int numChannels =
464             image[QLatin1String("__channels__")].toInt();
465
466         Q_ASSERT(type == QLatin1String("uint8"));
467         Q_ASSERT(normalized == true);
468         Q_UNUSED(normalized);
469
470         QByteArray dataArray =
471             image[QLatin1String("__data__")].toByteArray();
472
473         ApiTexture tex;
474         tex.setSize(size);
475         tex.setNumChannels(numChannels);
476         tex.setLabel(itr.key());
477         tex.contentsFromBase64(dataArray);
478
479         m_textures.append(tex);
480     }
481
482     QVariantMap fbos =
483         parsedJson[QLatin1String("framebuffer")].toMap();
484     for (itr = fbos.constBegin(); itr != fbos.constEnd(); ++itr) {
485         QVariantMap buffer = itr.value().toMap();
486         QSize size(buffer[QLatin1String("__width__")].toInt(),
487                    buffer[QLatin1String("__height__")].toInt());
488         QString cls = buffer[QLatin1String("__class__")].toString();
489         QString type = buffer[QLatin1String("__type__")].toString();
490         bool normalized = buffer[QLatin1String("__normalized__")].toBool();
491         int numChannels = buffer[QLatin1String("__channels__")].toInt();
492
493         Q_ASSERT(type == QLatin1String("uint8"));
494         Q_ASSERT(normalized == true);
495         Q_UNUSED(normalized);
496
497         QByteArray dataArray =
498             buffer[QLatin1String("__data__")].toByteArray();
499
500         ApiFramebuffer fbo;
501         fbo.setSize(size);
502         fbo.setNumChannels(numChannels);
503         fbo.setType(itr.key());
504         fbo.contentsFromBase64(dataArray);
505         m_framebuffers.append(fbo);
506     }
507 }
508
509 const QVariantMap & ApiTraceState::parameters() const
510 {
511     return m_parameters;
512 }
513
514 const QMap<QString, QString> & ApiTraceState::shaderSources() const
515 {
516     return m_shaderSources;
517 }
518
519 const QVariantMap & ApiTraceState::uniforms() const
520 {
521     return m_uniforms;
522 }
523
524 bool ApiTraceState::isEmpty() const
525 {
526     return m_parameters.isEmpty();
527 }
528
529 const QList<ApiTexture> & ApiTraceState::textures() const
530 {
531     return m_textures;
532 }
533
534 const QList<ApiFramebuffer> & ApiTraceState::framebuffers() const
535 {
536     return m_framebuffers;
537 }
538
539 ApiTraceCallSignature::ApiTraceCallSignature(const QString &name,
540                                              const QStringList &argNames)
541     : m_name(name),
542       m_argNames(argNames)
543 {
544 }
545
546 ApiTraceCallSignature::~ApiTraceCallSignature()
547 {
548 }
549
550 QUrl ApiTraceCallSignature::helpUrl() const
551 {
552     return m_helpUrl;
553 }
554
555 void ApiTraceCallSignature::setHelpUrl(const QUrl &url)
556 {
557     m_helpUrl = url;
558 }
559
560 ApiTraceEvent::ApiTraceEvent()
561     : m_type(ApiTraceEvent::None),
562       m_hasBinaryData(false),
563       m_binaryDataIndex(0),
564       m_state(0),
565       m_staticText(0)
566 {
567 }
568
569 ApiTraceEvent::ApiTraceEvent(Type t)
570     : m_type(t),
571       m_hasBinaryData(false),
572       m_binaryDataIndex(0),
573       m_state(0),
574       m_staticText(0)
575 {
576 }
577
578 ApiTraceEvent::~ApiTraceEvent()
579 {
580     delete m_state;
581     delete m_staticText;
582 }
583
584 QVariantMap ApiTraceEvent::stateParameters() const
585 {
586     if (m_state) {
587         return m_state->parameters();
588     } else {
589         return QVariantMap();
590     }
591 }
592
593 ApiTraceState *ApiTraceEvent::state() const
594 {
595     return m_state;
596 }
597
598 void ApiTraceEvent::setState(ApiTraceState *state)
599 {
600     m_state = state;
601 }
602
603 ApiTraceCall::ApiTraceCall(ApiTraceFrame *parentFrame, const Trace::Call *call)
604     : ApiTraceEvent(ApiTraceEvent::Call),
605       m_parentFrame(parentFrame)
606 {
607     ApiTrace *trace = parentTrace();
608
609     Q_ASSERT(trace);
610
611     m_index = call->no;
612
613     m_signature = trace->signature(call->sig->id);
614
615     if (!m_signature) {
616         QString name = QString::fromStdString(call->sig->name);
617         QStringList argNames;
618         argNames.reserve(call->sig->num_args);
619         for (int i = 0; i < call->sig->num_args; ++i) {
620             argNames += QString::fromStdString(call->sig->arg_names[i]);
621         }
622         m_signature = new ApiTraceCallSignature(name, argNames);
623         trace->addSignature(call->sig->id, m_signature);
624     }
625     if (call->ret) {
626         VariantVisitor retVisitor(trace);
627         call->ret->visit(retVisitor);
628         m_returnValue = retVisitor.variant();
629     }
630     m_argValues.reserve(call->args.size());
631     for (int i = 0; i < call->args.size(); ++i) {
632         VariantVisitor argVisitor(trace);
633         call->args[i]->visit(argVisitor);
634         m_argValues.append(argVisitor.variant());
635         if (m_argValues[i].type() == QVariant::ByteArray) {
636             m_hasBinaryData = true;
637             m_binaryDataIndex = i;
638         }
639     }
640     m_argValues.squeeze();
641 }
642
643 ApiTraceCall::~ApiTraceCall()
644 {
645 }
646
647
648 bool ApiTraceCall::hasError() const
649 {
650     return !m_error.isEmpty();
651 }
652
653 QString ApiTraceCall::error() const
654 {
655     return m_error;
656 }
657
658 void ApiTraceCall::setError(const QString &msg)
659 {
660     if (m_error != msg) {
661         ApiTrace *trace = parentTrace();
662         m_error = msg;
663         m_richText = QString();
664         if (trace)
665             trace->callError(this);
666     }
667 }
668
669 ApiTrace * ApiTraceCall::parentTrace() const
670 {
671     if (m_parentFrame)
672         return m_parentFrame->parentTrace();
673     return NULL;
674 }
675
676 QVector<QVariant> ApiTraceCall::originalValues() const
677 {
678     return m_argValues;
679 }
680
681 void ApiTraceCall::setEditedValues(const QVector<QVariant> &lst)
682 {
683     ApiTrace *trace = parentTrace();
684
685     m_editedValues = lst;
686     //lets regenerate data
687     m_richText = QString();
688     m_searchText = QString();
689     delete m_staticText;
690     m_staticText = 0;
691
692     if (trace) {
693         if (!lst.isEmpty()) {
694             trace->callEdited(this);
695         } else {
696             trace->callReverted(this);
697         }
698     }
699 }
700
701 QVector<QVariant> ApiTraceCall::editedValues() const
702 {
703     return m_editedValues;
704 }
705
706 bool ApiTraceCall::edited() const
707 {
708     return !m_editedValues.isEmpty();
709 }
710
711 void ApiTraceCall::revert()
712 {
713     setEditedValues(QVector<QVariant>());
714 }
715
716 void ApiTraceCall::setHelpUrl(const QUrl &url)
717 {
718     m_signature->setHelpUrl(url);
719 }
720
721 void ApiTraceCall::setParentFrame(ApiTraceFrame *frame)
722 {
723     m_parentFrame = frame;
724 }
725
726 ApiTraceFrame * ApiTraceCall::parentFrame()const
727 {
728     return m_parentFrame;
729 }
730
731 int ApiTraceCall::index() const
732 {
733     return m_index;
734 }
735
736 QString ApiTraceCall::name() const
737 {
738     return m_signature->name();
739 }
740
741 QStringList ApiTraceCall::argNames() const
742 {
743     return m_signature->argNames();
744 }
745
746 QVector<QVariant> ApiTraceCall::arguments() const
747 {
748     if (m_editedValues.isEmpty())
749         return m_argValues;
750     else
751         return m_editedValues;
752 }
753
754 QVariant ApiTraceCall::returnValue() const
755 {
756     return m_returnValue;
757 }
758
759 QUrl ApiTraceCall::helpUrl() const
760 {
761     return m_signature->helpUrl();
762 }
763
764 bool ApiTraceCall::hasBinaryData() const
765 {
766     return m_hasBinaryData;
767 }
768
769 int ApiTraceCall::binaryDataIndex() const
770 {
771     return m_binaryDataIndex;
772 }
773
774 QStaticText ApiTraceCall::staticText() const
775 {
776     if (m_staticText && !m_staticText->text().isEmpty())
777         return *m_staticText;
778
779     QVector<QVariant> argValues = arguments();
780
781     QString richText = QString::fromLatin1(
782         "<span style=\"font-weight:bold\">%1</span>(").arg(
783             m_signature->name());
784     QStringList argNames = m_signature->argNames();
785     for (int i = 0; i < argNames.count(); ++i) {
786         richText += QLatin1String("<span style=\"color:#0000ff\">");
787         QString argText = apiVariantToString(argValues[i]);
788
789         //if arguments are really long (e.g. shader text), cut them
790         // and elide it
791         if (argText.length() > 40) {
792             QString shortened = argText.mid(0, 40);
793             shortened[argText.length() - 5] = '.';
794             shortened[argText.length() - 4] = '.';
795             shortened[argText.length() - 3] = '.';
796             shortened[argText.length() - 2] = argText.at(argText.length() - 2);
797             shortened[argText.length() - 1] = argText.at(argText.length() - 1);
798             richText += shortened;
799         } else {
800             richText += argText;
801         }
802         richText += QLatin1String("</span>");
803         if (i < argNames.count() - 1)
804             richText += QLatin1String(", ");
805     }
806     richText += QLatin1String(")");
807     if (m_returnValue.isValid()) {
808         richText +=
809             QLatin1Literal(" = ") %
810             QLatin1Literal("<span style=\"color:#0000ff\">") %
811             apiVariantToString(m_returnValue) %
812             QLatin1Literal("</span>");
813     }
814
815     if (!m_staticText)
816         m_staticText = new QStaticText(richText);
817     else
818         m_staticText->setText(richText);
819     QTextOption opt;
820     opt.setWrapMode(QTextOption::NoWrap);
821     m_staticText->setTextOption(opt);
822     m_staticText->prepare();
823
824     return *m_staticText;
825 }
826
827 QString ApiTraceCall::toHtml() const
828 {
829     if (!m_richText.isEmpty())
830         return m_richText;
831
832     m_richText = QLatin1String("<div class=\"call\">");
833
834     QUrl helpUrl = m_signature->helpUrl();
835     if (helpUrl.isEmpty()) {
836         m_richText += QString::fromLatin1(
837             "%1) <span class=\"callName\">%2</span>(")
838                       .arg(m_index)
839                       .arg(m_signature->name());
840     } else {
841         m_richText += QString::fromLatin1(
842             "%1) <span class=\"callName\"><a href=\"%2\">%3</a></span>(")
843                       .arg(m_index)
844                       .arg(helpUrl.toString())
845                       .arg(m_signature->name());
846     }
847
848     QVector<QVariant> argValues = arguments();
849     QStringList argNames = m_signature->argNames();
850     for (int i = 0; i < argNames.count(); ++i) {
851         m_richText +=
852             QLatin1String("<span class=\"arg-name\">") +
853             argNames[i] +
854             QLatin1String("</span>") +
855             QLatin1Literal(" = ") +
856             QLatin1Literal("<span class=\"arg-value\">") +
857             apiVariantToString(argValues[i], true) +
858             QLatin1Literal("</span>");
859         if (i < argNames.count() - 1)
860             m_richText += QLatin1String(", ");
861     }
862     m_richText += QLatin1String(")");
863
864     if (m_returnValue.isValid()) {
865         m_richText +=
866             QLatin1String(" = ") +
867             QLatin1String("<span style=\"color:#0000ff\">") +
868             apiVariantToString(m_returnValue, true) +
869             QLatin1String("</span>");
870     }
871     m_richText += QLatin1String("</div>");
872
873     if (hasError()) {
874         QString errorStr =
875             QString::fromLatin1(
876                 "<div class=\"error\">%1</div>")
877             .arg(m_error);
878         m_richText += errorStr;
879     }
880
881     m_richText =
882         QString::fromLatin1(
883             "<html><head><style type=\"text/css\" media=\"all\">"
884             "%1</style></head><body>%2</body></html>")
885         .arg(styleSheet)
886         .arg(m_richText);
887     m_richText.squeeze();
888
889     //qDebug()<<m_richText;
890     return m_richText;
891 }
892
893 QString ApiTraceCall::searchText() const
894 {
895     if (!m_searchText.isEmpty())
896         return m_searchText;
897
898     QVector<QVariant> argValues = arguments();
899     m_searchText = m_signature->name() + QLatin1Literal("(");
900     QStringList argNames = m_signature->argNames();
901     for (int i = 0; i < argNames.count(); ++i) {
902         m_searchText += argNames[i] +
903                         QLatin1Literal(" = ") +
904                         apiVariantToString(argValues[i]);
905         if (i < argNames.count() - 1)
906             m_searchText += QLatin1String(", ");
907     }
908     m_searchText += QLatin1String(")");
909
910     if (m_returnValue.isValid()) {
911         m_searchText += QLatin1Literal(" = ") +
912                         apiVariantToString(m_returnValue);
913     }
914     m_searchText.squeeze();
915     return m_searchText;
916 }
917
918 int ApiTraceCall::numChildren() const
919 {
920     return 0;
921 }
922
923 ApiTraceFrame::ApiTraceFrame(ApiTrace *parentTrace)
924     : ApiTraceEvent(ApiTraceEvent::Frame),
925       m_parentTrace(parentTrace),
926       m_binaryDataSize(0)
927 {
928 }
929
930 QStaticText ApiTraceFrame::staticText() const
931 {
932     if (m_staticText && !m_staticText->text().isEmpty())
933         return *m_staticText;
934
935     QString richText;
936
937     //mark the frame if it uploads more than a meg a frame
938     if (m_binaryDataSize > (1024*1024)) {
939         richText =
940             QObject::tr(
941                 "<span style=\"font-weight:bold;\">"
942                 "Frame&nbsp;%1</span>"
943                 "<span style=\"font-style:italic;\">"
944                 "&nbsp;&nbsp;&nbsp;&nbsp;(%2MB)</span>")
945             .arg(number)
946             .arg(double(m_binaryDataSize / (1024.*1024.)), 0, 'g', 2);
947     } else {
948         richText =
949             QObject::tr(
950                 "<span style=\"font-weight:bold\">Frame %1</span>")
951             .arg(number);
952     }
953
954     if (!m_staticText)
955         m_staticText = new QStaticText(richText);
956
957     QTextOption opt;
958     opt.setWrapMode(QTextOption::NoWrap);
959     m_staticText->setTextOption(opt);
960     m_staticText->prepare();
961
962     return *m_staticText;
963 }
964
965 int ApiTraceFrame::numChildren() const
966 {
967     return m_calls.count();
968 }
969
970 ApiTrace * ApiTraceFrame::parentTrace() const
971 {
972     return m_parentTrace;
973 }
974
975 void ApiTraceFrame::addCall(ApiTraceCall *call)
976 {
977     m_calls.append(call);
978     if (call->hasBinaryData()) {
979         QByteArray data =
980             call->arguments()[call->binaryDataIndex()].toByteArray();
981         m_binaryDataSize += data.size();
982     }
983 }
984
985 QVector<ApiTraceCall*> ApiTraceFrame::calls() const
986 {
987     return m_calls;
988 }
989
990 ApiTraceCall * ApiTraceFrame::call(int idx) const
991 {
992     return m_calls.value(idx);
993 }
994
995 int ApiTraceFrame::callIndex(ApiTraceCall *call) const
996 {
997     return m_calls.indexOf(call);
998 }
999
1000 bool ApiTraceFrame::isEmpty() const
1001 {
1002     return m_calls.isEmpty();
1003 }
1004
1005 int ApiTraceFrame::binaryDataSize() const
1006 {
1007     return m_binaryDataSize;
1008 }
1009
1010 void ApiTraceFrame::setCalls(const QVector<ApiTraceCall*> &calls,
1011                              quint64 binaryDataSize)
1012 {
1013     m_calls = calls;
1014     m_binaryDataSize = binaryDataSize;
1015 }