static QStringList parseDefinition(const QString &definition) { QStringList result; QString word; bool ignoreWord = false; QVector<QChar> braceStack; foreach (const QChar &c, definition) { if (c == '[' || c == '<' || c == '(') { braceStack.append(c); ignoreWord = false; } else if (c == ']' || c == '>' || c == ')') { if (braceStack.isEmpty() || braceStack.takeLast() == '<') ignoreWord = true; } if (c == ' ' || c == '[' || c == '<' || c == '(' || c == ']' || c == '>' || c == ')') { if (!ignoreWord && !word.isEmpty()) { if (result.isEmpty() || Utils::allOf(word, [](const QChar &c) { return c.isUpper() || c == '_'; })) result.append(word); } word.clear(); ignoreWord = false; } else { word.append(c); } } return result; }
void QCanvas::fill_algorithm(QPointF start, QColor color,QColor border, bool time_sleep) { QVector<QPointF> stack; QImage im(pix->toImage()); im.setPixel(start.toPoint(),color.rgb()); stack.push_back(start); while(!stack.isEmpty()) { QPoint tmp_pix = stack.takeLast().toPoint(); im.setPixel(tmp_pix,color.rgb()); size_t tmp_x = tmp_pix.x(); tmp_pix.setX(tmp_x+1); while(im.pixel(tmp_pix) != border.rgb()) { im.setPixel(tmp_pix,color.rgb()); tmp_pix.setX(tmp_pix.x()+1); } size_t x_r = tmp_pix.x() - 1; tmp_pix.setX(tmp_x-1); while(im.pixel(tmp_pix) != border.rgb()) { im.setPixel(tmp_pix,color.rgb()); tmp_pix.setX(tmp_pix.x()-1); } size_t x_l = tmp_pix.x() + 1; tmp_pix.setX(x_l); tmp_pix.setY(tmp_pix.y()+1); while(tmp_pix.x() <= x_r) { bool flag = false; while(im.pixel(tmp_pix) != border.rgb() && im.pixel(tmp_pix) != color.rgb() && tmp_pix.x() <= x_r) { if(!flag) flag = true; tmp_pix.setX(tmp_pix.x()+1); } if(flag) { if (tmp_pix.x() == x_r && im.pixel(tmp_pix) != border.rgb() && im.pixel(tmp_pix) != color.rgb()) stack.push_back(tmp_pix); else stack.push_back(QPointF(tmp_pix.x()-1,tmp_pix.y())); flag = false; } size_t x_st = tmp_pix.x(); while(im.pixel(tmp_pix) == border.rgb() || im.pixel(tmp_pix) == color.rgb() && tmp_pix.x() <= x_r) tmp_pix.setX(tmp_pix.x()+1); if(tmp_pix.x() == x_st) tmp_pix.setX(tmp_pix.x()+1); } if(time_sleep) { pix->convertFromImage(im); this->setPixmap(*pix); QTime dieTime= QTime::currentTime().addMSecs(10); while (QTime::currentTime() < dieTime) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } tmp_pix.setX(x_l); tmp_pix.setY(tmp_pix.y()-2); while(tmp_pix.x() <= x_r) { bool flag = false; while(im.pixel(tmp_pix) != border.rgb() && im.pixel(tmp_pix) != color.rgb() && tmp_pix.x() <= x_r) { if(!flag) flag = true; tmp_pix.setX(tmp_pix.x()+1); } if(flag) { if (tmp_pix.x() == x_r && im.pixel(tmp_pix) != border.rgb() && im.pixel(tmp_pix) != color.rgb()) stack.push_back(tmp_pix); else stack.push_back(QPointF(tmp_pix.x()-1,tmp_pix.y())); flag = false; } size_t x_st = tmp_pix.x(); while(im.pixel(tmp_pix) == border.rgb() || im.pixel(tmp_pix) == color.rgb() && tmp_pix.x() <= x_r) tmp_pix.setX(tmp_pix.x()+1); if(tmp_pix.x() == x_st) tmp_pix.setX(tmp_pix.x()+1); } if(time_sleep) { pix->convertFromImage(im); this->setPixmap(*pix); QTime dieTime= QTime::currentTime().addMSecs(10); while (QTime::currentTime() < dieTime) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } } pix->convertFromImage(im); this->setPixmap(*pix); }