Beispiel #1
0
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);
}
Beispiel #2
0
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;
}