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