void LevelDesignerController::clear(void) {
  _model->clear();
  _undoStack->clear();
  emit updateReset();
  emitUpdate(0);

  _levelInfo->updateLevelReadyToBeTested(_model->getPolygonList());
}
Beispiel #2
0
bool Media::updateIfChanged(T& variable, const T& newValue)
{
    if (variable != newValue) {
        variable = newValue;
        emitUpdate();
        return true;
    }
    return false;
}
void LevelDesignerController::moveVertex(int polygonRow, int vertexRow, int oldX, int oldY, int newX, int newY, bool pushToStack) {
  if (pushToStack) {
    QUndoCommand* moverVertexCommand = new MoveVertexCommand(_model, polygonRow, vertexRow, oldX, oldY, newX, newY, polygonRow, vertexRow);
    _undoStack->push(moverVertexCommand);
  } else {
    _model->replaceVertex(polygonRow, vertexRow, Point2d(newX, newY));

    emitUpdate(0);
  }
}
void LevelDesignerController::movePolygon(int polygonRow, int oldX, int oldY, int newX, int newY, bool pushToStack) {
  if (pushToStack) {
    QUndoCommand* movePolygonCommand = new MovePolygonCommand(_model, polygonRow, oldX, oldY, newX, newY, polygonRow, -1);
    _undoStack->push(movePolygonCommand);
  } else {
    poc::Polygon polygon(_model->polygonFromIndex(_model->index(polygonRow, 0)));
    polygon.translate(Vector2d(newX-oldX, newY-oldY));
    _model->replacePolygon(polygonRow, polygon);

    emitUpdate(0);
  }
}
void LevelDesignerController::openLevel(const QString& fileName) {
  clear();

  ParserXML parser(fileName);

  PolygonList polygonList(parser.createPolygonList());

  _model->setPolygonList(polygonList);
  _model->setLinesCount(parser.getLinesCount());
  _model->setPartsCount(parser.getPartsCount());
  _model->setMaxGapToWin(parser.getMaxGapToWin());
  emit updateStats();
  emitUpdate(0);

  _levelInfo->updateLevelReadyToBeTested(_model->getPolygonList());
}
void CashPointInRadius::fetchIds(ServerApiPtr api, quint32 leftAttempts)
{
    if (data.isEmpty()) {
        return;
    }

    mId = api->sendRequest("/nearby/cashpoints", data,
    [this, api, leftAttempts](ServerApi::RequestStatusCode reqCode, ServerApi::HttpStatusCode httpCode, const QByteArray &data) {
        if (!isRunning()) {
            if (isDisposing()) {
                deleteLater();
            }
            return;
        }
        const int step = 0;

        if (reqCode == ServerApi::RSC_Timeout) {
            emitUpdate(leftAttempts - 1, step);
            return;
        }

        const QString errText = trUtf8("Cannot receive list of nearby cashpoints");
        if (reqCode != ServerApi::RSC_Ok) {
            emitError(ServerApi::requestStatusCodeText(reqCode));
            emitStepFinished(*api, step, false, errText);
            return;
        }

        if (httpCode != ServerApi::HSC_Ok) {
            qWarning() << "Server request http code: " << httpCode;
            emitUpdate(leftAttempts - 1, step);
            return;
        }

        QJsonParseError err;
        const QJsonDocument json = QJsonDocument::fromJson(data, &err);
        if (err.error != QJsonParseError::NoError) {
            emitError("CashPointInRadius: server response json parse error: " + err.errorString());
            emitStepFinished(*api, step, false, errText);
            return;
        }

        if (!json.isArray()) {
            emitError("CashPointInRadius: response is not an array!");
            emitStepFinished(*api, step, false, errText);
            return;
        }

        mCashpointsToProcess.clear();
        mResponse = new CashPointResponse;
        mResponse->type = CashPointResponse::CashpointData;

        const QJsonArray arr = json.array();
        const auto end = arr.constEnd();
        for (auto it = arr.constBegin(); it != end; it++) {
            const QJsonValue &val = *it;
            const int id = val.toInt();
            if (id > 0) {
                mCashpointsToProcess.append(id);
//                qDebug() << id;
            }
        }

        // no cashpoints in radius => send response now
        if (mCashpointsToProcess.empty()) {
            emitResponseReady(true);
            emitStepFinished(*api, getStepHandlers().count() - 1, true, trUtf8("There is no nearby cashpoints"));
        }

        const int timestamp = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000;
        const int outdateTimestamp = timestamp - 300;

        const QMap<quint32, int> cachedCashpoints = getModel()->getCachedCashpoints();
        const auto cacheEnd = cachedCashpoints.end();

        auto it = mCashpointsToProcess.begin();
        while (it != mCashpointsToProcess.end()) {
            const quint32 id = *it;
            mResponse->addVisiableCashpoint(id);

            const auto cit = cachedCashpoints.find(id);
            if (cit != cacheEnd) {
                const int cachedTimestamp = cit.value();

                // cache is uptodate, so we do not need to fetch this id
                if (outdateTimestamp < cachedTimestamp) {
                    QJsonObject obj = getModel()->getCachedCashpointData(id);
                    if (!obj.isEmpty()) {
                        it = mCashpointsToProcess.erase(it);
                        mResponse->addCashPointData(obj);
                        continue;
                    }
                }
            }
            it++;
        }

        /// TODO: We have not to clear model but may be only remove invisiable points
//        getModel()->clear();
        emitStepFinished(*api, step, true, trUtf8("List of nearby cashpoints received"));
    });
}
void CashPointInRadius::fetchCashpoints(ServerApiPtr api, quint32 leftAttempts)
{
    const int step = 1;
    qDebug() << "fetchCashpoints";

    if (mCashpointsToProcess.empty()) {
        return;
    }

    const quint32 cashpointsToProcess = qMin(getModel()->getRequestBatchSize(), (quint32)mCashpointsToProcess.size());
    if (cashpointsToProcess == 0) {
        emitStepFinished(*api, step, true, trUtf8("Data of nearby cashpoints received"));
        return;
    }

    QList<quint32> cachedCashpoints = getModel()->getCachedCashpoints().keys();

    QJsonArray requestCashpoints;
    QJsonArray requestCachedCashpoints;

    for (quint32 i = 0; i < cashpointsToProcess; ++i) {
        const quint32 id = mCashpointsToProcess.front();
        mCashpointsToProcess.removeFirst();

        if (cachedCashpoints.contains(id)) {
            requestCachedCashpoints.append(QJsonValue((int)id));
        } else {
            requestCashpoints.append(QJsonValue((int)id));
        }
    }

    const QJsonObject json
    {
        { "cashpoints", requestCashpoints },
        { "cached", requestCachedCashpoints }
    };

    const bool lastRequest = mCashpointsToProcess.empty();
    const quint32 totalAttempts = getModel()->getAttemptsCount();

    /// Get cashpoints data from list
    mId = api->sendRequest("/cashpoints", json,
    [this, api, leftAttempts, step, lastRequest, totalAttempts]
    (ServerApi::RequestStatusCode reqCode, ServerApi::HttpStatusCode httpCode, const QByteArray &data) {
        if (!isRunning()) {
            if (isDisposing()) {
                deleteLater();
            }
            return;
        }

        const QString errText = trUtf8("Cannot receive data of nearby cashpoints");
        if (reqCode == ServerApi::RSC_Timeout) {
            emitUpdate(leftAttempts - 1, step);
            return;
        }

        if (reqCode != ServerApi::RSC_Ok) {
            emitError(ServerApi::requestStatusCodeText(reqCode));
            emitStepFinished(*api, step, false, errText);
            return;
        }

        if (httpCode != ServerApi::HSC_Ok) {
            qWarning() << "Server request http code: " << httpCode;
            emitUpdate(leftAttempts - 1, step);
            return;
        }

        QJsonParseError err;
        const QJsonDocument json = QJsonDocument::fromJson(data, &err);
        if (err.error != QJsonParseError::NoError) {
            emitError("CashPointInRadius: server response json parse error: " + err.errorString());
            emitStepFinished(*api, step, false, errText);
            return;
        }

        if (!json.isArray()) {
            emitError("CashPointInRadius: server response json array expected. Dump: " +
                      QString::fromUtf8(json.toJson(QJsonDocument::Compact)));
            emitStepFinished(*api, step, false, errText);
            return;
        }
        const QJsonArray cashpoints = json.array();

        if (!mResponse) {
            mResponse = new CashPointResponse;
        }

        const auto cpEnd = cashpoints.end();
        for (auto it = cashpoints.begin(); it != cpEnd; it++) {
            mResponse->addCashPointData(it->toObject());
            //qDebug() << "added cashpoint: " << it->toObject()["id"].toInt();
        }

        emitResponseReady(lastRequest);
        if (lastRequest) {
            emitStepFinished(*api, step, true, trUtf8("Data of nearby cashpoints received"));
        } else {
            emitUpdate(totalAttempts, step);
        }
    });
}
ColorPalette& ColorPalette::operator=(ColorPalette&& other)
{
    std::swap(p, other.p);
    emitUpdate();
    return *this;
}
ColorPalette& ColorPalette::operator=(const ColorPalette& other)
{
    *p = *other.p;
    emitUpdate();
    return *this;
}
bool ColorPalette::load(const QString& name)
{
    p->fileName = name;
    p->colors.clear();
    p->columns = 0;
    p->dirty = false;
    p->name = QFileInfo(name).baseName();

    QFile file(name);

    if ( !file.open(QFile::ReadOnly|QFile::Text) )
    {
        emitUpdate();
        return false;
    }

    QTextStream stream( &file );

    if ( stream.readLine() != "GIMP Palette" )
    {
        emitUpdate();
        return false;
    }

    QString line;

    // parse properties
    QHash<QString,QString> properties;
    while( !stream.atEnd() )
    {
        line = stream.readLine();
        if ( line.isEmpty() )
            continue;
        if ( line[0] == '#' )
            break;
        int colon = line.indexOf(':');
        if ( colon == -1 )
            break;
        properties[line.left(colon).toLower()] =
            line.right(line.size() - colon - 1).trimmed();
    }
    /// \todo Store extra properties in the palette object
    setName(properties["name"]);
    setColumns(properties["columns"].toInt());

    // Skip comments
    if ( !stream.atEnd() && line[0] == '#' )
        while( !stream.atEnd() )
        {
            qint64 pos = stream.pos();
            line = stream.readLine();
            if ( !line.isEmpty() && line[0] != '#' )
            {
                stream.seek(pos);
                break;
            }
        }

    while( !stream.atEnd() )
    {
        int r = 0, g = 0, b = 0;
        stream >> r >> g >> b;
        line = stream.readLine().trimmed();
        p->colors.push_back(qMakePair(QColor(r, g, b), line));
    }

    colorsChanged(p->colors);
    setDirty(false);

    return true;
}