void TagConverter::applyPatternToColumns() { QList<int> columns; //QRegularExpression re = this->generatePattern2(fileToTagLineEdit); /// QString text = fileToTagLineEdit->text(); text = text.replace(':', '_'); // Remove and convert spaces according to columns for (int i = fileToTagLineEdit->tags().count() - 1; i >= 0; i--) { TagButton *tag = fileToTagLineEdit->tags().at(i); QString substitution = ':' + QString::number(tag->column()); columns.prepend(tag->column()); text.replace(tag->position(), tag->spaceCount(), substitution); } QString pattern = "^"; QString characterClass; // Depending on which detected columns, choose a sub-regex for (int i = 0; i < text.size(); i++) { QChar c = text.at(i); if (c == ':') { c = text.at(++i); switch (c.digitValue()) { case Miam::COL_Track: case Miam::COL_Year: case Miam::COL_Disc: characterClass = "[\\d]+"; // Digits break; default: characterClass = "[\\w\\W]+"; break; } pattern += "(" + characterClass + ")"; } else { pattern += QRegularExpression::escape(text.at(i)); } } pattern += "$"; QRegularExpression re(pattern, QRegularExpression::UseUnicodePropertiesOption); for (QModelIndex index : _tagEditor->selectionModel()->selectedRows()) { QString filename = index.data().toString(); filename = filename.left(filename.lastIndexOf(".")); QRegularExpressionMatch m = re.match(filename); if (!m.hasMatch()) { continue; } // The implicit capturing group with index 0 captures the result of the whole match. for (int i = 0; i < re.captureCount(); i++) { _tagEditor->updateCellData(index.row(), columns.at(i), m.captured(i + 1)); } } this->setVisible(false); _convertButton->setChecked(false); }
/** Redefined to be able to move tag buttons. * Backspace method is not virtual in QLineEdit, therefore keyPressEvent must be intercepted and eaten. */ void TagLineEdit::backspace() { bool oneTagNeedToBeRemoved = false; TagButton *tag = nullptr; QPoint cursorCenter; cursorBackward(false); int dx = fontMetrics().width(text().at(cursorPosition())); for (TagButton *button : _tags) { cursorCenter = cursorRect().center(); // One tag button need to be removed if (button->frameGeometry().contains(cursorCenter)) { oneTagNeedToBeRemoved = true; tag = button; } // Tags need to be moved to the left if (button->x() > cursorCenter.x()) { button->move(button->x() - dx, 0); button->setPosition(cursorPosition()); } } cursorForward(false); if (oneTagNeedToBeRemoved) { for (int i = 0; i < tag->spaceCount(); i++) { LineEdit::backspace(); } dx = fontMetrics().width(" ") * tag->spaceCount(); for (TagButton *button : _tags) { if (button != tag && button->x() > cursorCenter.x()) { button->move(button->x() - dx, 0); button->setPosition(cursorPosition()); } } _tags.removeOne(tag); delete tag; /// FIXME //emit taglistHasChanged(this->toStringList()); } else { LineEdit::backspace(); } }
QString TagConverter::generatePattern(TagLineEdit *lineEdit) const { QString pattern = lineEdit->text(); pattern = pattern.replace(':', '_'); /// XXX code review needed // Proceed to substitutions in reverse order for (int i = lineEdit->tags().count() - 1; i >= 0; i--) { TagButton *tag = lineEdit->tags().at(i); QString substitution = ':' + QString::number(tag->column()); pattern.replace(tag->position(), tag->spaceCount(), substitution); } return pattern; }