Пример #1
0
void WordChecker::checkWord(const QString currentLine)
{
    QString compareLine = mSpeedText.section('\n', mLine, mLine);
    QString compareString = compareLine.left(currentLine.length());

    if (currentLine.compare(compareString) == 0) {

        // Update the number of correct characters entered.
        mNbrOfCharacters = mEnteredLines.length() + currentLine.length();
        emit nbrOfCharactersChanged(mNbrOfCharacters);

        // Update the number of characters that is left to enter.
        mRemainingTextLength = mSpeedTextLength - mNbrOfCharacters;

        // Updates the Qstring holding the text that is not yet typed.
        mRemainingText = mSpeedText.right(mRemainingTextLength);
        emit remainingTextChanged(mRemainingText);

        if (compareLine.length() == currentLine.length()) {
            // If at the end of a line, update the line and enteredLines
            // property with the number of lines and the text entered so far
            mLine++;

            // Update the entered lines.
            mEnteredLines = mSpeedText.section('\n', 0, mLine - 1) + "\n";
            emit enteredLinesChanged(mEnteredLines);

            // lineChanged signal needs to be emitted after enteredLinesChanged. If not, the
            // speedtext will be updated with the just entered line instead of a new one.
            emit lineChanged(mLine);

            // Start a new line by setting the current correct line to an empty string.
            mCurrentCorrectLine = "";
            emit currentCorrectLineChanged(mCurrentCorrectLine);

            if (mNbrOfCharacters >= mSpeedTextLength) {
                // When the entire text has been correctly entered return end.
                emit ended();
            }

        } else {
            mCurrentCorrectLine = currentLine;
            emit currentCorrectLineChanged(mCurrentCorrectLine);
        }
        setValid(true);
    } else {
        setValid(false);
    }
}
Пример #2
0
WordResult WordChecker::checkWord(const QString currentLine)
{
  QString compareLine = mSpeedText.section('\n', mLine, mLine);
  QString compareString = compareLine.left(currentLine.length());

  if (currentLine.compare(compareString) == 0) {

    // Update the number of correct characters entered.
    mNbrOfCharacters = mEnteredLines.length() + currentLine.length();
    emit nbrOfCharactersChanged(mNbrOfCharacters);

    if (mNbrOfCharacters >= mSpeedTextLength) {
      emit ended();

      // When the entire text has been correctly entered return end.
      return WordResult_End;
    }

    if (compareLine.length() == currentLine.length()) {
      // If at the end of a line, update the line and enteredLines
      // property with the number of lines and the text entered so far
      mLine++;
      emit lineChanged(mLine);

      // Update the entered lines.
      mEnteredLines = mSpeedText.section('\n', 0, mLine - 1) + "\n";
      emit enteredLinesChanged(mEnteredLines);

      // Start a new line by setting the current correct line to an empty string.
      mCurrentCorrectLine = "";
      emit currentCorrectLineChanged(mCurrentCorrectLine);

      return WordResult_NewLine;
    } else {
      mCurrentCorrectLine = currentLine;
      emit currentCorrectLineChanged(mCurrentCorrectLine);
    }

    return WordResult_Correct;
  }

  return WordResult_Wrong;
}