예제 #1
0
void Transcribe::openTextFile(const QString& path) {
  // Because the way the UI works, we can assume that the text is not dirty
  // So if the file exists, we load the contents into the editor window.
  if (m_text_file) {
    m_text_file->deleteLater();
  }
  m_text_file = new QFile(path);
  if (m_text_file->exists()) {
    if (m_text_file->open(QIODevice::ReadOnly | QIODevice::Text)) {
      QTextStream in_stream(m_text_file);
      QVariant text(in_stream.readAll());
      QQmlProperty::write(m_text_area, "text", text);
      m_text_file->close();
      setTextDirty(false); // QML has signalled text is dirty because we changed
                           // text, so we need reset this.
    } else {
      QString msg = tr("The text file can't be read");
      errorDetected(msg);
      m_text_file = NULL;
      return;
    }
  } else {
    // If the text file doesn't exist, empty the editor and create the file
    // by saving the text to it
    QQmlProperty::write(m_text_area, "text", QVariant(""));
    if (!saveText()) return;
  }

  // Update the gui
  emit textFileNameChanged();
  QQmlProperty::write(m_main_window, "is_editable", QVariant(true));
  setTextDirty(false);
}
예제 #2
0
bool Transcribe::saveText() {
  if (!m_text_file) {
    // This shouldn't happen because of the GUI. We silently ignore it
    return false;
  }

  QSaveFile file(m_text_file->fileName());
  if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
#ifdef Q_OS_ANDROID
    // The on-screen keyboard doesn't actully add the word to the text area
    // until it is committed, normally by a space or period etc. To include it
    // in our saved file, we need to manually commit it.
    qApp->inputMethod()->commit();
#endif
    QTextStream out_stream(&file);
    out_stream << QQmlProperty::read(m_text_area, "text").toString();
    if (file.commit()) {
      setTextDirty(false);
      return true;
    }
  }

  // Notify that the file could not be saved
  QString general_msg =  tr("There was an error saving the text file.\n");
  general_msg         += tr("The latest changes are not saved!");

  errorDetected(general_msg);
  return false;
}
예제 #3
0
  void Transcribe::deleteText() {
    if (m_text_file) {
      // Let's ask for confirmation first
      QMessageBox box;
      box.setText(tr("Do you really want to delete the transcription text?"));
      box.setInformativeText(tr("There's no way to get it back.\nThe audio file will not be deleted."));
      box.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);

      if (box.exec() == QMessageBox::Yes) {
        // Delete the file from our history model
        m_history.del(HistoryModel::TextFileRole, m_text_file);

        // Clear and lock the text area
        QQmlProperty::write(m_text_area,
                            "text",
                            QVariant::fromValue(QString()));
        QQmlProperty::write(m_main_window,
                            "is_editable",
                            QVariant(false));
        setTextDirty(false);

        // Actually remove the file from disk
        m_text_file->remove();

        // Update the text file parameter
        m_text_file->deleteLater();
        m_text_file = NULL;
        emit textFileNameChanged();
      }
    }
  }
예제 #4
0
	void TextField2::setText(const char *str)
	{
		mRawText = str;
		setTextDirty(true);
		mDirty = true;
	}
예제 #5
0
	void TextField2::parseRawText()
	{
		TextTokeniser tokeniser(mRawText.c_str());
		const char *token = tokeniser.nextToken();
		mText = "";
		mNewLinePositions.clear();
		mNewLinePositions.push_back(0);
		mNewLineDirty = true;
		bool firstLine = true;

		enum ParseState {
			BASE, START_FORMAT, END_FORMAT, IN_FORMAT_ATTR, IN_FORMAT_OP, IN_FORMAT_VALUE, POP_FORMAT
		};
		ParseState state = BASE;
		
		Handle<Node> currentNode = mRootNode;
		mRootNode->clear();

		mTextHitboxes->clear();

		string attrName = "";

		while (token != NULL)
		{
			if (state == BASE && token[0] == '<')
			{
				state = START_FORMAT;
			}
			else if (state != BASE && token[0] == '>')
			{
				state = BASE;
			}
			else if (state == START_FORMAT)
			{
				if (token[0] == '/')
				{
					state = POP_FORMAT;
				}
				else 
				{
					string lower = Utils::toLowerCase(token);
					Node *newNode = new Node(lower.c_str());
					currentNode->addChild(newNode);
					currentNode = newNode;
					state = IN_FORMAT_ATTR;
				}
			}
			else if (state == IN_FORMAT_ATTR)
			{
				if (token[0] == '>')
				{
					state = END_FORMAT;
				}
				attrName = Utils::toLowerCase(token);
				state = IN_FORMAT_OP;
			}
			else if (state == IN_FORMAT_OP)
			{
				if (token[0] == '>')
				{
					state = END_FORMAT;
				}
				else if (token[0] != '=')
				{
					currentNode->setAttribute(attrName.c_str(), "true");
					state = IN_FORMAT_ATTR;
				}
				else
				{
					state = IN_FORMAT_VALUE;
				}
			}
			else if (state == IN_FORMAT_VALUE)
			{
				if (token[0] == '>')
				{
					stringstream ss;
					ss << "Error parsing node '" << attrName << "', = operator had not value";
					am_log("TEXT", ss);
				}
				else
				{
					currentNode->setAttribute(attrName.c_str(), token);
					state = IN_FORMAT_ATTR;
				}
			}
			else if (state == END_FORMAT)
			{
				while (token != NULL && token[0] != '>')
				{
					token = tokeniser.nextToken();
				}
				state = BASE;
			}
			else if (state == POP_FORMAT)
			{
				string lower = Utils::toLowerCase(token);
				if (currentNode->getParent() == NULL)
				{
					am_log("NODE", "Pop format to NULL parent");
				}
				else
				{
					currentNode = currentNode->getParent();
				}
				state = END_FORMAT;
			}
			else if (state == BASE)
			{
				currentNode->appendText(token);
				mText += token;
			}
			if (state != END_FORMAT)
			{
				token = tokeniser.nextToken();
			}
		}

		setTextDirty(false);
	}
예제 #6
0
	void TextField2::appendText(const string &str)
	{
		mRawText.append(str);
		mDirty = true;
		setTextDirty(true);
	}
예제 #7
0
	void TextField2::setText(const string &str)
	{
		mRawText = str;
		mDirty = true;
		setTextDirty(true);
	}