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