void QSnippetEdit::on_leSnippetScope_editingFinished() { if ( m_editedSnippet < 0 ) return; QSnippet *snip = m_manager->snippet(m_editedSnippet); snip->setContexts(leSnippetScope->text().split(_cxt_splitter)); }
bool QSnippetManager::loadSnippetFromFile(const QString& file, const QString& type) { QFile f(file); if ( !f.open(QFile::ReadOnly | QFile::Text) ) { qWarning("Unable to load snippet from %s", qPrintable(file)); return false; } //qDebug("loading from : %s", qPrintable(file)); QString s = QString::fromLocal8Bit(f.readAll()); static const QRegExp meta("# name:(\\S+) context:(\\S*)[^\n]*\n"); int idx = meta.indexIn(s); bool metaMatch = idx != -1; if ( metaMatch ) { //qDebug("meta! : %i => %s", idx, qPrintable(meta.cap(0))); s.remove(idx, meta.matchedLength()); } if ( s.endsWith('\n') ) s.chop(1); bool ok = loadSnippetFromString(metaMatch ? meta.cap(1) : QFileInfo(file).baseName(), s, type); if ( ok ) { QSnippet *snip = m_snippets.last(); snip->setContexts(metaMatch ? meta.cap(2).split(",") : QStringList("all")); } return ok; }
void QSnippetEdit::on_tbCreateSnippet_clicked() { QString name = leSnippetName->text(); QString pattern = eSnippet->text(); QStringList contexts = leSnippetScope->text().split(_cxt_splitter); if ( pattern.endsWith('\n') ) pattern.chop(1); if ( name.isEmpty() || pattern.isEmpty() ) { QMessageBox::information(0, tr("Missing data"), tr("Please provide a name and a content to create a new snippet")); return; } // TODO : allow pattern loader choice... bool ok = m_manager->loadSnippetFromString(name, pattern, "Simple"); if ( !ok ) { QMessageBox::warning(0, tr("Error"), tr("Invalid snippet pattern.")); return; } eSnippet->setText(QString()); leSnippetScope->clear(); leSnippetName->clear(); QSnippet *snip = m_manager->snippet(m_manager->snippetCount() - 1); //snip->setName(name); snip->setContexts(contexts); lwSnippets->setCurrentRow(0); }