void SnippetsCollection::clear()
{
- for (Snippet::Group group = Snippet::Cpp; group < Snippet::GroupSize; ++group) {
- m_snippets[group].clear();
- m_activeSnippetsEnd[group] = m_snippets[group].end();
- }
+ for (Snippet::Group group = Snippet::Cpp; group < Snippet::GroupSize; ++group)
+ clear(group);
+}
+
+void SnippetsCollection::clear(Snippet::Group group)
+{
+ m_snippets[group].clear();
+ m_activeSnippetsEnd[group] = m_snippets[group].end();
}
void SnippetsCollection::updateActiveSnippetsEnd(Snippet::Group group)
removedSnippetPred);
}
+void SnippetsCollection::restoreRemovedSnippets(Snippet::Group group)
+{
+ // The version restored contains the last modifications (if any) by the user.
+ // Reverting the snippet can still bring it to the original version.
+ QVector<Snippet> toRestore(std::distance(m_activeSnippetsEnd[group], m_snippets[group].end()));
+ qCopy(m_activeSnippetsEnd[group], m_snippets[group].end(), toRestore.begin());
+ m_snippets[group].erase(m_activeSnippetsEnd[group], m_snippets[group].end());
+ foreach (Snippet snippet, toRestore) {
+ snippet.setIsRemoved(false);
+ insertSnippet(snippet, group);
+ }
+}
+
+Snippet SnippetsCollection::revertedSnippet(int index, Snippet::Group group) const
+{
+ const Snippet &candidate = snippet(index, group);
+ Q_ASSERT(candidate.isBuiltIn());
+
+ const QList<Snippet> &builtIn =
+ readXML(m_builtInSnippetsPath + m_snippetsFileName, candidate.id());
+ if (builtIn.size() == 1)
+ return builtIn.at(0);
+ return Snippet();
+}
+
+void SnippetsCollection::reset(Snippet::Group group)
+{
+ clear(group);
+
+ const QList<Snippet> &builtInSnippets = readXML(m_builtInSnippetsPath + m_snippetsFileName);
+ foreach (const Snippet &snippet, builtInSnippets)
+ if (group == snippet.group())
+ insertSnippet(snippet, snippet.group());
+}
+
void SnippetsCollection::reload()
{
clear();
const int size = totalSnippets(group);
for (int i = 0; i < size; ++i) {
const Snippet ¤t = snippet(i, group);
- if (!current.isBuiltIn() ||
- (current.isBuiltIn() && (current.isRemoved() || current.isModified()))) {
+ if (!current.isBuiltIn() || current.isRemoved() || current.isModified())
writeSnippetXML(current, &writer);
- }
}
}
writer.writeEndElement();
writer->writeEndElement();
}
-QList<Snippet> SnippetsCollection::readXML(const QString &fileName)
+QList<Snippet> SnippetsCollection::readXML(const QString &fileName, const QString &snippetId)
{
QList<Snippet> snippets;
QFile file(fileName);
while (xml.readNextStartElement()) {
if (xml.name() == kSnippet) {
const QXmlStreamAttributes &atts = xml.attributes();
+ const QString &id = atts.value(kId).toString();
+ if (snippetId.isEmpty() || snippetId == id) {
+ Snippet snippet(id);
+ snippet.setTrigger(atts.value(kTrigger).toString());
+ snippet.setComplement(atts.value(kComplement).toString());
+ snippet.setGroup(toSnippetGroup(atts.value(kGroup).toString()));
+ snippet.setIsRemoved(toBool(atts.value(kRemoved).toString()));
+ snippet.setIsModified(toBool(atts.value(kModified).toString()));
+
+ QString content;
+ while (!xml.atEnd()) {
+ xml.readNext();
+ if (xml.isCharacters()) {
+ content += xml.text();
+ } else if (xml.isEndElement()) {
+ snippet.setContent(content);
+ snippets.append(snippet);
+ break;
+ }
+ }
- Snippet snippet(atts.value(kId).toString());
- snippet.setTrigger(atts.value(kTrigger).toString());
- snippet.setComplement(atts.value(kComplement).toString());
- snippet.setGroup(toSnippetGroup(atts.value(kGroup).toString()));
- snippet.setIsRemoved(toBool(atts.value(kRemoved).toString()));
- snippet.setIsModified(toBool(atts.value(kModified).toString()));
-
- QString content;
- while (!xml.atEnd()) {
- xml.readNext();
- if (xml.isCharacters()) {
- content += xml.text();
- } else if (xml.isEndElement()) {
- snippet.setContent(content);
- snippets.append(snippet);
+ if (!snippetId.isEmpty())
break;
- }
+ } else {
+ xml.skipCurrentElement();
}
} else {
xml.skipCurrentElement();
}
}
- } else {
- xml.skipCurrentElement();
}
}
if (xml.hasError())
void removeSnippet(const QModelIndex &modelIndex);
const Snippet &snippetAt(const QModelIndex &modelIndex) const;
void setSnippetContent(const QModelIndex &modelIndex, const QString &content);
+ void revertBuitInSnippet(const QModelIndex &modelIndex);
+ void restoreRemovedBuiltInSnippets();
+ void resetSnippets();
private:
+ void replaceSnippet(const Snippet &snippet, const QModelIndex &modelIndex);
static bool isValidTrigger(const QString &s);
Snippet::Group m_activeGroup;
bool SnippetsTableModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
{
if (modelIndex.isValid() && role == Qt::EditRole) {
- const int row = modelIndex.row();
- Snippet snippet(m_collection->snippet(row, m_activeGroup));
+ Snippet snippet(m_collection->snippet(modelIndex.row(), m_activeGroup));
if (modelIndex.column() == 0) {
const QString &s = value.toString();
if (!isValidTrigger(s)) {
snippet.setComplement(value.toString());
}
- const SnippetsCollection::Hint &hint =
- m_collection->computeReplacementHint(row, snippet, m_activeGroup);
- if (modelIndex.row() == hint.index()) {
- m_collection->replaceSnippet(row, snippet, m_activeGroup, hint);
- emit dataChanged(modelIndex, modelIndex);
- } else {
- if (row < hint.index())
- // Rows will be moved down.
- beginMoveRows(QModelIndex(), row, row, QModelIndex(), hint.index() + 1);
- else
- beginMoveRows(QModelIndex(), row, row, QModelIndex(), hint.index());
- m_collection->replaceSnippet(row, snippet, m_activeGroup, hint);
- endMoveRows();
- }
-
+ replaceSnippet(snippet, modelIndex);
return true;
}
return false;
m_collection->setSnippetContent(modelIndex.row(), m_activeGroup, content);
}
+void SnippetsTableModel::revertBuitInSnippet(const QModelIndex &modelIndex)
+{
+ const Snippet &snippet = m_collection->revertedSnippet(modelIndex.row(), m_activeGroup);
+ if (snippet.id().isEmpty()) {
+ QMessageBox::critical(0, tr("Error"), tr("Error reverting snippet."));
+ return;
+ }
+ replaceSnippet(snippet, modelIndex);
+}
+
+void SnippetsTableModel::restoreRemovedBuiltInSnippets()
+{
+ m_collection->restoreRemovedSnippets(m_activeGroup);
+ reset();
+}
+
+void SnippetsTableModel::resetSnippets()
+{
+ m_collection->reset(m_activeGroup);
+ reset();
+}
+
+void SnippetsTableModel::replaceSnippet(const Snippet &snippet, const QModelIndex &modelIndex)
+{
+ const int row = modelIndex.row();
+ const SnippetsCollection::Hint &hint =
+ m_collection->computeReplacementHint(row, snippet, m_activeGroup);
+ if (modelIndex.row() == hint.index()) {
+ m_collection->replaceSnippet(row, snippet, m_activeGroup, hint);
+ emit dataChanged(modelIndex, modelIndex);
+ } else {
+ if (row < hint.index())
+ // Rows will be moved down.
+ beginMoveRows(QModelIndex(), row, row, QModelIndex(), hint.index() + 1);
+ else
+ beginMoveRows(QModelIndex(), row, row, QModelIndex(), hint.index());
+ m_collection->replaceSnippet(row, snippet, m_activeGroup, hint);
+ endMoveRows();
+ }
+}
+
bool SnippetsTableModel::isValidTrigger(const QString &s)
{
if (s.isEmpty())
void markSnippetsCollection();
void addSnippet();
void removeSnippet();
+ void revertBuiltInSnippet();
+ void restoreRemovedBuiltInSnippets();
+ void resetAllSnippets();
void selectSnippet(const QModelIndex &parent, int row);
void selectMovedSnippet(const QModelIndex &, int, int, const QModelIndex &, int row);
- void previewSnippet(const QModelIndex &modelIndex);
- void updateSnippetContent();
+ void setSnippetContent();
+ void updateCurrentSnippetDependent(const QModelIndex &modelIndex = QModelIndex());
private:
SnippetEditor *currentEditor() const;
decorateEditor(editorAt(Snippet::Qml), Snippet::Qml);
decorateEditor(editorAt(Snippet::PlainText), Snippet::PlainText);
+ m_ui.revertButton->setEnabled(false);
+
QTextStream(&m_keywords) << m_displayName;
loadSettings();
loadSnippetGroup(m_ui.groupCombo->currentIndex());
connect(editorAt(Snippet::Cpp), SIGNAL(snippetContentChanged()),
- this, SLOT(updateSnippetContent()));
+ this, SLOT(setSnippetContent()));
connect(editorAt(Snippet::Qml), SIGNAL(snippetContentChanged()),
- this, SLOT(updateSnippetContent()));
+ this, SLOT(setSnippetContent()));
connect(editorAt(Snippet::PlainText), SIGNAL(snippetContentChanged()),
- this, SLOT(updateSnippetContent()));
+ this, SLOT(setSnippetContent()));
connect(m_model, SIGNAL(rowsInserted(QModelIndex, int, int)),
this, SLOT(selectSnippet(QModelIndex,int)));
this, SLOT(markSnippetsCollection()));
connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(markSnippetsCollection()));
+ connect(m_model, SIGNAL(modelReset()), this, SLOT(updateCurrentSnippetDependent()));
+ connect(m_model, SIGNAL(modelReset()), this, SLOT(markSnippetsCollection()));
connect(m_ui.groupCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(loadSnippetGroup(int)));
connect(m_ui.addButton, SIGNAL(clicked()), this, SLOT(addSnippet()));
connect(m_ui.removeButton, SIGNAL(clicked()), this, SLOT(removeSnippet()));
+ connect(m_ui.resetAllButton, SIGNAL(clicked()), this, SLOT(resetAllSnippets()));
+ connect(m_ui.restoreRemovedButton, SIGNAL(clicked()),
+ this, SLOT(restoreRemovedBuiltInSnippets()));
+ connect(m_ui.revertButton, SIGNAL(clicked()), this, SLOT(revertBuiltInSnippet()));
connect(m_ui.snippetsTable->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
- this, SLOT(previewSnippet(QModelIndex)));
+ this, SLOT(updateCurrentSnippetDependent(QModelIndex)));
}
void SnippetsSettingsPagePrivate::decorateEditor(SnippetEditor *editor, Snippet::Group group)
m_model->removeSnippet(modelIndex);
}
+void SnippetsSettingsPagePrivate::restoreRemovedBuiltInSnippets()
+{
+ m_model->restoreRemovedBuiltInSnippets();
+}
+
+void SnippetsSettingsPagePrivate::revertBuiltInSnippet()
+{
+ m_model->revertBuitInSnippet(m_ui.snippetsTable->selectionModel()->currentIndex());
+}
+
+void SnippetsSettingsPagePrivate::resetAllSnippets()
+{
+ m_model->resetSnippets();
+}
+
void SnippetsSettingsPagePrivate::selectSnippet(const QModelIndex &parent, int row)
{
QModelIndex topLeft = m_model->index(row, 0, parent);
else
modelIndex = m_model->index(destinationRow, 0, destinationParent);
m_ui.snippetsTable->scrollTo(modelIndex);
+ currentEditor()->setPlainText(m_model->snippetAt(modelIndex).content());
}
-void SnippetsSettingsPagePrivate::previewSnippet(const QModelIndex &modelIndex)
+void SnippetsSettingsPagePrivate::updateCurrentSnippetDependent(const QModelIndex &modelIndex)
{
- currentEditor()->setPlainText(m_model->snippetAt(modelIndex).content());
+ if (modelIndex.isValid()) {
+ const Snippet &snippet = m_model->snippetAt(modelIndex);
+ currentEditor()->setPlainText(snippet.content());
+ m_ui.revertButton->setEnabled(snippet.isBuiltIn());
+ } else {
+ currentEditor()->clear();
+ m_ui.revertButton->setEnabled(false);
+ }
}
-void SnippetsSettingsPagePrivate::updateSnippetContent()
+void SnippetsSettingsPagePrivate::setSnippetContent()
{
const QModelIndex &modelIndex = m_ui.snippetsTable->selectionModel()->currentIndex();
if (modelIndex.isValid()) {