void LrcEditor::mergeLyrics() { QByteArray strText = toPlainText().toLocal8Bit(); if (strText.isEmpty()) return ; LrcReader parser(strText, strText.length()); parser.parse(); QStringList newText; const LRC &lrc = parser.getLrc(); generateTags(newText, lrc); struct Line { std::string lyrics; std::vector<int> times; Line(const LRC::Line &line) { lyrics = line.lyrics; times.push_back(line.time); } }; std::vector<Line> sortedLines; const auto &lines = lrc.getLines(); // merge the same lyrics for (const auto &line : lines) { auto it = sortedLines.begin(); for ( ; it != sortedLines.end(); ++it) { if (it->lyrics == line.lyrics) break; } if (it != sortedLines.end()) it->times.push_back(line.time); else sortedLines.push_back(Line(line)); } // sort by time std::sort(sortedLines.begin(), sortedLines.end(), [](Line &rhl, Line &rhs) { Q_ASSERT(rhl.times.size() > 0 && rhs.times.size() > 0); std::sort(rhl.times.begin(), rhl.times.end(), std::greater<int>()); std::sort(rhs.times.begin(), rhs.times.end(), std::greater<int>()); return rhl.times.back() < rhs.times.back(); }); for (const auto &line : sortedLines) { QString strLine; for (auto time : line.times) { strLine += _makeTimeTag(time); } strLine += QString::fromStdString(line.lyrics); newText << strLine; } replaceWholeText(newText.join(m_charNewLine)); }
void PythonTerminalEdit::keyPressEvent(QKeyEvent *event) { if(event->key() == Qt::Key_Up) { if(m_commandStack.size()) { m_current--; if(m_current < 0) { m_current = m_commandStack.size(); } if(m_current == m_commandStack.size()) { // we've reached the first command, display empty prompt setText(toPlainText().left(m_cursorPos)); QTextCursor cursor(textCursor()); cursor.movePosition(QTextCursor::End); setTextCursor(cursor); } else { // display cached command setText(toPlainText().left(m_cursorPos)); QTextCursor cursor(textCursor()); cursor.movePosition(QTextCursor::End); cursor.insertText(m_commandStack.at(m_current)); cursor.movePosition(QTextCursor::End); setTextCursor(cursor); } } event->accept(); return; } else if(event->key() == Qt::Key_Down) { if(m_commandStack.size()) { m_current++; if(m_current > m_commandStack.size()) { m_current = 0; } if(m_current == m_commandStack.size()) { // we've reached the last command, display empty prompt setText(toPlainText().left(m_cursorPos)); QTextCursor cursor(textCursor()); cursor.movePosition(QTextCursor::End); setTextCursor(cursor); } else { // display cached command setText(toPlainText().left(m_cursorPos)); QTextCursor cursor(textCursor()); cursor.movePosition(QTextCursor::End); cursor.insertText(m_commandStack.at(m_current)); cursor.movePosition(QTextCursor::End); setTextCursor(cursor); } } event->accept(); return; } else if(event->key() == Qt::Key_Return) { QString text = toPlainText(); QString t = text.right(text.size() - m_cursorPos); if(!t.isEmpty()) { m_commandStack.append(t); // this limits how many commands we save if(m_commandStack.size() > 100) { m_commandStack.removeFirst(); } // save the commands before we execute, this will allow users to see // what they did before the crash QSettings settings; settings.beginWriteArray("pythonCommands"); for (int i = 0; i < m_commandStack.size(); ++i) { settings.setArrayIndex(i); settings.setValue("command", m_commandStack.at(i)); } settings.endArray(); } m_current = m_commandStack.size(); runCommand(); event->accept(); return; } else if(event->key() == Qt::Key_Backspace) { QTextCursor cursor(textCursor()); if (cursor.position() <= m_cursorPos) { event->accept(); return; } } else if(event->key() == Qt::Key_Home) { QTextCursor cursor(textCursor()); cursor.setPosition(m_cursorPos); setTextCursor(cursor); event->accept(); return; } setTextCursorToEnd(); QTextEdit::keyPressEvent(event); }
void QG_CommandHistory::slotTextChanged() { //only show the selectAll item when there is text m_pSelectAll->setVisible(! toPlainText().isEmpty()); }
void MaxLengthTextEdit::reallyCheckLength() { while (m_maxLength >= 0 && toPlainText().size() > m_maxLength) { textCursor().deletePreviousChar(); } }