QRegularExpression SearchOptions::getRegularExpression() const
{
  QRegularExpression regexp = isWildCard() ? StringUtil::wildCardToRegExp(getFindValue()) : QRegularExpression(getFindValue());
  if (!isCaseSensitive()) {
    regexp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
  }
  return regexp;
}
Example #2
0
// display a news
void RSSWidget::handleCurrentArticleChanged(const QModelIndex &currentIndex, const QModelIndex &previousIndex)
{
    m_ui->textBrowser->clear();

    if (previousIndex.isValid()) {
        RSS::Article *article = getArticlePtr(previousIndex);
        Q_ASSERT(article);
        article->markAsRead();
    }

    if (!currentIndex.isValid()) return;

    RSS::Article *article = getArticlePtr(currentIndex);
    Q_ASSERT(article);

    QString html;
    html += "<div style='border: 2px solid red; margin-left: 5px; margin-right: 5px; margin-bottom: 5px;'>";
    html += "<div style='background-color: #678db2; font-weight: bold; color: #fff;'>" + article->title() + "</div>";
    if (article->date().isValid())
        html += "<div style='background-color: #efefef;'><b>" + tr("Date: ") + "</b>" + article->date().toLocalTime().toString(Qt::SystemLocaleLongDate) + "</div>";
    if (!article->author().isEmpty())
        html += "<div style='background-color: #efefef;'><b>" + tr("Author: ") + "</b>" + article->author() + "</div>";
    html += "</div>";
    html += "<div style='margin-left: 5px; margin-right: 5px;'>";
    if (Qt::mightBeRichText(article->description())) {
        html += article->description();
    }
    else {
        QString description = article->description();
        QRegularExpression rx;
        // If description is plain text, replace BBCode tags with HTML and wrap everything in <pre></pre> so it looks nice
        rx.setPatternOptions(QRegularExpression::InvertedGreedinessOption
            | QRegularExpression::CaseInsensitiveOption);

        rx.setPattern("\\[img\\](.+)\\[/img\\]");
        description = description.replace(rx, "<img src=\"\\1\">");

        rx.setPattern("\\[url=(\")?(.+)\\1\\]");
        description = description.replace(rx, "<a href=\"\\2\">");
        description = description.replace("[/url]", "</a>", Qt::CaseInsensitive);

        rx.setPattern("\\[(/)?([bius])\\]");
        description = description.replace(rx, "<\\1\\2>");

        rx.setPattern("\\[color=(\")?(.+)\\1\\]");
        description = description.replace(rx, "<span style=\"color:\\2\">");
        description = description.replace("[/color]", "</span>", Qt::CaseInsensitive);

        rx.setPattern("\\[size=(\")?(.+)\\d\\1\\]");
        description = description.replace(rx, "<span style=\"font-size:\\2px\">");
        description = description.replace("[/size]", "</span>", Qt::CaseInsensitive);

        html += "<pre>" + description + "</pre>";
    }
    html += "</div>";
    m_ui->textBrowser->setHtml(html);
}
Example #3
0
bool Ratings::setRating(const QString& val, RatingAgency ag)
{
    qint32 agencyIndex;

    for (agencyIndex = 0;; ++agencyIndex) {
        Q_ASSERT(agencyIndex <= CountRatingAcencies);
        if (static_cast<qint32>(ag) == (1 << agencyIndex))
            break;
    }
    /////////////////Fix for bloomberg return values in excel//////////////////
    if (val.trimmed().toUpper().left(4) == "#N/A") {
        setRating(RatingValue::NR, ag);
        setWatch(Stable, ag);
        return true;
    }
    //////////////////////////////////////////////////////////////////////////
    const  auto agencySyntax = RatingsPrivate::m_ratingSyntax[agencyIndex];
    QRegularExpression syntaxCheck;
    syntaxCheck.setPatternOptions(QRegularExpression::CaseInsensitiveOption | QRegularExpression::DontCaptureOption);
    for (int i = static_cast<qint16>(RatingValue::AAA); i <= static_cast<qint16>(RatingValue::Dm); ++i){
        if (!agencySyntax[i].isEmpty()) {
            syntaxCheck.setPattern(
                "(^|[^" + QRegularExpression::escape(RatingsPrivate::m_reservedChars[agencyIndex]) + "])"
                + QRegularExpression::escape(agencySyntax[i])
                + "($|[^" + QRegularExpression::escape(RatingsPrivate::m_reservedChars[agencyIndex]) + "])"
                );
            Q_ASSERT(syntaxCheck.isValid());
            if (syntaxCheck.match(val).hasMatch()){
                    setRating(static_cast<RatingValue>(i), ag);
                    break;
            }
        }
        if (i == static_cast<qint16>(RatingValue::Dm)) {
            setRating(RatingValue::NR, ag, Stable);
            return false;
        }
    }
    if (val.indexOf("*-", 0, Qt::CaseInsensitive) >= 0)
        setWatch(Negative, ag);
    else if (val.indexOf("*+", 0, Qt::CaseInsensitive) >= 0)
        setWatch(Positive, ag);
    else
        setWatch(Stable, ag);
    return true;
}
Example #4
0
Post::Post(QObject *parent) :
QObject(parent), d_ptr(new PostPrivate(this))
{
	Q_D(Post);
	ins = this;
	setObjectName("Post");

	auto avProcess = [this](QNetworkReply *reply){
		Q_D(Post);
		Task &task = d->queue.head();
		switch (task.state){
		case None:{
			QString api("http://interface.%1/dmpost");
			api = api.arg(Utils::customUrl(Utils::Bilibili));
			task.request.setUrl(api);
			task.request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
			const Comment &c = task.comment;
			QUrlQuery params;
			params.addQueryItem("cid", QFileInfo(task.target->source).baseName());
			params.addQueryItem("date", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
			params.addQueryItem("pool", "0");
			params.addQueryItem("playTime", QString::number(c.time / 1000.0, 'f', 4));
			params.addQueryItem("color", QString::number(c.color));
			params.addQueryItem("fontsize", QString::number(c.font));
			params.addQueryItem("message", c.string);
			params.addQueryItem("rnd", QString::number(qrand()));
			params.addQueryItem("mode", QString::number(c.mode));
			task.data = QUrl::toPercentEncoding(params.query(QUrl::FullyEncoded), "%=&", "-.~_");
			emit stateChanged(task.state = Code);
			forward();
			break;
		}
		case Code:{
			int code = QString(reply->readAll()).toInt();
			if (code > 0){
				emit stateChanged(task.state = None);
			}
			else{
				emit stateChanged(task.state = code);
			}
			dequeue();
			break;
		}
		}
	};
	auto avRegular = [](QString code){
		static QRegularExpression r("a(v(\\d+([#_])?(\\d+)?)?)?");
		r.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
		return r.match(code).capturedLength() == code.length();
	};
	d->pool.append({ avRegular, 0, avProcess });

	auto ddProcess = [this](QNetworkReply *reply){
		Q_D(Post);
		Task &task = d->queue.head();
		switch (task.state){
		case None:{
			QString api("http://api.%1/api/v1/comment/%2");
			api = api.arg(Utils::customUrl(Utils::AcPlay));
			api = api.arg(QFileInfo(task.target->source).baseName());
			task.request.setUrl(api);
			task.request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
			const Comment &c = task.comment;
			QJsonObject params;
			params["Token"] = 0;
			params["Time"] = c.time / 1000.0;
			params["Mode"] = c.mode;
			params["Color"] = c.color;
			params["TimeStamp"] = QDateTime::currentMSecsSinceEpoch();
			params["Pool"] = 0;
			params["UId"] = 0;
			params["CId"] = 0;
			params["Message"] = c.string;
			task.data= QJsonDocument(params).toJson();
			emit stateChanged(task.state = Code);
			forward();
			break;
		}
		case Code:{
			const QJsonObject &result = QJsonDocument::fromJson(reply->readAll()).object();
			if (result["Success"].toBool()){
				emit stateChanged(task.state = None);
			}
			else{
				emit stateChanged(task.state = QNetworkReply::UnknownNetworkError);
			}
			dequeue();
			break;
		}
		}

	};
	auto ddRegular = [](QString code){
		static QRegularExpression r("d(d\\d*)?");
		r.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
		return r.match(code).capturedLength() == code.length();
	};
	d->pool.append({ ddRegular, 0, ddProcess });

	connect(this, &Post::stateChanged, [this](int code){
		switch (code){
		case None:
		case Code:
			break;
		default:{
			Q_D(Post);
			if (!d->tryNext()){
				emit errorOccured(code);
			}
			break;
		}
		}
	});
}