tests/benchmarks/gui/text/qtext/main.cpp
changeset 33 3e2da88830cd
parent 18 2f34d5167611
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    80     void odfWriting_images();
    80     void odfWriting_images();
    81 
    81 
    82     void constructControl();
    82     void constructControl();
    83     void constructDocument();
    83     void constructDocument();
    84 
    84 
       
    85     void newLineReplacement();
       
    86     void formatManipulation();
       
    87     void fontResolution();
       
    88 
       
    89     void layout_data();
    85     void layout();
    90     void layout();
       
    91     void formattedLayout();
    86     void paintLayoutToPixmap();
    92     void paintLayoutToPixmap();
    87     void paintLayoutToPixmap_painterFill();
    93     void paintLayoutToPixmap_painterFill();
    88 
    94 
    89     void document();
    95     void document();
    90     void paintDocToPixmap();
    96     void paintDocToPixmap();
    93     void control();
    99     void control();
    94     void paintControlToPixmap();
   100     void paintControlToPixmap();
    95     void paintControlToPixmap_painterFill();
   101     void paintControlToPixmap_painterFill();
    96 
   102 
    97 private:
   103 private:
    98     QSize setupTextLayout(QTextLayout *layout);
   104     QSize setupTextLayout(QTextLayout *layout, bool wrap = true, int wrapWidth = 100);
    99 
   105 
   100     QString m_lorem;
   106     QString m_lorem;
   101     QString m_shortLorem;
   107     QString m_shortLorem;
   102 };
   108 };
   103 
   109 
   225         writer.write(doc);
   231         writer.write(doc);
   226     }
   232     }
   227     delete doc;
   233     delete doc;
   228 }
   234 }
   229 
   235 
   230 QSize tst_QText::setupTextLayout(QTextLayout *layout)
   236 QSize tst_QText::setupTextLayout(QTextLayout *layout, bool wrap, int wrapWidth)
   231 {
   237 {
   232     bool wrap = true;
       
   233     int wrapWidth = 300;
       
   234     layout->setCacheEnabled(true);
   238     layout->setCacheEnabled(true);
   235 
   239 
   236     int height = 0;
   240     int height = 0;
   237     qreal widthUsed = 0;
   241     qreal widthUsed = 0;
   238     qreal lineWidth = 0;
   242     qreal lineWidth = 0;
   240     //set manual width
   244     //set manual width
   241     if (wrap)
   245     if (wrap)
   242         lineWidth = wrapWidth;
   246         lineWidth = wrapWidth;
   243 
   247 
   244     layout->beginLayout();
   248     layout->beginLayout();
   245 
       
   246     while (1) {
   249     while (1) {
   247         QTextLine line = layout->createLine();
   250         QTextLine line = layout->createLine();
   248         if (!line.isValid())
   251         if (!line.isValid())
   249             break;
   252             break;
   250 
   253 
   282         QTextDocument *doc = new QTextDocument;
   285         QTextDocument *doc = new QTextDocument;
   283         delete doc;
   286         delete doc;
   284     }
   287     }
   285 }
   288 }
   286 
   289 
       
   290 //this step is needed before giving the string to a QTextLayout
       
   291 void tst_QText::newLineReplacement()
       
   292 {
       
   293     QString text = QString::fromLatin1("H\ne\nl\nl\no\n\nW\no\nr\nl\nd");
       
   294 
       
   295     QBENCHMARK {
       
   296         QString tmp = text;
       
   297         tmp.replace(QLatin1Char('\n'), QChar::LineSeparator);
       
   298     }
       
   299 }
       
   300 
       
   301 void tst_QText::formatManipulation()
       
   302 {
       
   303     QFont font;
       
   304 
       
   305     QBENCHMARK {
       
   306         QTextCharFormat format;
       
   307         format.setFont(font);
       
   308     }
       
   309 }
       
   310 
       
   311 void tst_QText::fontResolution()
       
   312 {
       
   313     QFont font;
       
   314     QFont font2;
       
   315     font.setFamily("DejaVu");
       
   316     font2.setBold(true);
       
   317 
       
   318     QBENCHMARK {    
       
   319         QFont res = font.resolve(font2);
       
   320     }
       
   321 }
       
   322 
       
   323 void tst_QText::layout_data()
       
   324 {
       
   325     QTest::addColumn<bool>("wrap");
       
   326     QTest::newRow("wrap") << true;
       
   327     QTest::newRow("nowrap") << false;
       
   328 }
       
   329 
   287 void tst_QText::layout()
   330 void tst_QText::layout()
   288 {
   331 {
       
   332     QFETCH(bool,wrap);
   289     QTextLayout layout(m_shortLorem);
   333     QTextLayout layout(m_shortLorem);
       
   334     setupTextLayout(&layout, wrap);
       
   335 
       
   336     QBENCHMARK {
       
   337         QTextLayout layout(m_shortLorem);
       
   338         setupTextLayout(&layout, wrap);
       
   339     }
       
   340 }
       
   341 
       
   342 //### requires tst_QText to be a friend of QTextLayout
       
   343 /*void tst_QText::stackTextLayout()
       
   344 {
       
   345     QStackTextEngine engine(m_shortLorem, qApp->font());
       
   346     QTextLayout layout(&engine);
   290     setupTextLayout(&layout);
   347     setupTextLayout(&layout);
   291 
   348 
   292     QBENCHMARK {
   349     QBENCHMARK {
       
   350         QStackTextEngine engine(m_shortLorem, qApp->font());
       
   351         QTextLayout layout(&engine);
       
   352         setupTextLayout(&layout);
       
   353     }
       
   354 }*/
       
   355 
       
   356 void tst_QText::formattedLayout()
       
   357 {
       
   358     //set up formatting
       
   359     QList<QTextLayout::FormatRange> ranges;
       
   360     {
       
   361         QTextCharFormat format;
       
   362         format.setForeground(QColor("steelblue"));
       
   363 
       
   364         QTextLayout::FormatRange formatRange;
       
   365         formatRange.format = format;
       
   366         formatRange.start = 0;
       
   367         formatRange.length = 50;
       
   368 
       
   369         ranges.append(formatRange);
       
   370     }
       
   371 
       
   372     QTextLayout layout(m_shortLorem);
       
   373     layout.setAdditionalFormats(ranges);
       
   374     setupTextLayout(&layout);
       
   375 
       
   376     QBENCHMARK {
   293         QTextLayout layout(m_shortLorem);
   377         QTextLayout layout(m_shortLorem);
       
   378         layout.setAdditionalFormats(ranges);
   294         setupTextLayout(&layout);
   379         setupTextLayout(&layout);
   295     }
   380     }
   296 }
   381 }
   297 
   382 
   298 void tst_QText::paintLayoutToPixmap()
   383 void tst_QText::paintLayoutToPixmap()