char * newtReflowText(char * text, int width, int flexDown, int flexUp,
                      int * actualWidth, int * actualHeight) {
    int min, max;
    int i;
    char * result;
    int minbad, minbadwidth, howbad;
    char * expandedText;

    expandedText = expandTabs(text);

    if (flexDown || flexUp) {
        min = width - flexDown;
        max = width + flexUp;

        minbad = -1;
        minbadwidth = width;

        for (i = min; i <= max; i++) {
            doReflow(expandedText, NULL, i, &howbad, NULL);

            if (minbad == -1 || howbad < minbad) {
                minbad = howbad;
                minbadwidth = i;
            }
        }

        width = minbadwidth;
    }

    doReflow(expandedText, &result, width, NULL, actualHeight);
    free(expandedText);
    if (actualWidth) *actualWidth = width;
    return result;
}
void GTUtilsWorkflowDesigner::addSample(HI::GUITestOpStatus &os, const QString &sampName) {
    expandTabs(os);
    QTabWidget *tabs = qobject_cast<QTabWidget *>(GTWidget::findWidget(os, "tabs"));
    GT_CHECK(tabs != NULL, "tabs widget not found");

    GTTabWidget::setCurrentIndex(os, tabs, 1);

    QTreeWidgetItem *samp = findTreeItem(os, sampName, samples);
    GTGlobals::sleep(100);
    GT_CHECK(samp != NULL,"sample is NULL");

    selectSample(os, samp);
    GTGlobals::sleep(500);
}
void newtTextboxSetText(newtComponent co, const char * text) {
    const char * start, * end;
    struct textbox * tb = co->data;
    char * reflowed, * expanded;
    int badness, height;

    if (tb->lines) {
        free(tb->lines);
        tb->linesAlloced = tb->numLines = 0;
    }

    expanded = expandTabs(text);

    if (tb->doWrap) {
        doReflow(expanded, &reflowed, tb->textWidth, &badness, &height);
        free(expanded);
        expanded = reflowed;
    }

    for (start = expanded; *start; start++)
        if (*start == '\n') tb->linesAlloced++;

    /* This ++ leaves room for an ending line w/o a \n */
    tb->linesAlloced++;
    tb->lines = malloc(sizeof(char *) * tb->linesAlloced);

    start = expanded;
    while ((end = strchr(start, '\n'))) {
        addLine(co, start, end - start);
        start = end + 1;
    }

    if (*start)
        addLine(co, start, strlen(start));

    free(expanded);

    newtTrashScreen();
}
void GTUtilsWorkflowDesigner::addAlgorithm(HI::GUITestOpStatus &os, QString algName, bool exactMatch, bool useDragAndDrop){
    expandTabs(os);
    QTabWidget* tabs = qobject_cast<QTabWidget*>(GTWidget::findWidget(os,"tabs"));
    GT_CHECK(tabs!=NULL, "tabs widget not found");

    GTTabWidget::setCurrentIndex(os,tabs,0);
    GTGlobals::sleep(500);

    QTreeWidgetItem *alg = findTreeItem(os, algName, algoriths, exactMatch);
    GTGlobals::sleep(100);
    GT_CHECK(alg!=NULL,"algorithm is NULL");

    selectAlgorithm(os,alg);
    QWidget* w = GTWidget::findWidget(os,"sceneView");

    int workerNum = getWorkers(os).size();
    QPoint p(w->rect().topLeft() + QPoint(100+300*(workerNum-(workerNum/2)*2),100 + 200*(workerNum/2)));//shifting workers position
    if(useDragAndDrop){
        GTMouseDriver::dragAndDrop(os, GTMouseDriver::getMousePosition(), w->mapToGlobal(p));
    }else{
        GTWidget::click(os, w,Qt::LeftButton, p);
    }
    GTGlobals::sleep(1000);
}