2010-10-27 17:38:22 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 17:14:20 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "snippetssettingspage.h"
|
|
|
|
|
#include "snippeteditor.h"
|
2010-12-02 17:02:23 +01:00
|
|
|
#include "isnippetprovider.h"
|
2010-10-27 17:38:22 +02:00
|
|
|
#include "snippet.h"
|
|
|
|
|
#include "snippetscollection.h"
|
|
|
|
|
#include "snippetssettings.h"
|
|
|
|
|
#include "reuse.h"
|
|
|
|
|
#include "ui_snippetssettingspage.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
2011-05-31 16:36:58 +02:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
#include <texteditor/fontsettings.h>
|
2010-10-27 17:38:22 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QModelIndex>
|
|
|
|
|
#include <QAbstractTableModel>
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
#include <QHash>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QMainWindow>
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
// SnippetsTableModel
|
|
|
|
|
class SnippetsTableModel : public QAbstractTableModel
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
SnippetsTableModel(QObject *parent);
|
|
|
|
|
virtual ~SnippetsTableModel() {}
|
|
|
|
|
|
|
|
|
|
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
|
|
|
|
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
|
|
|
|
virtual Qt::ItemFlags flags(const QModelIndex &modelIndex) const;
|
|
|
|
|
virtual QVariant data(const QModelIndex &modelIndex, int role = Qt::DisplayRole) const;
|
|
|
|
|
virtual bool setData(const QModelIndex &modelIndex, const QVariant &value,
|
|
|
|
|
int role = Qt::EditRole);
|
|
|
|
|
virtual QVariant headerData(int section, Qt::Orientation orientation,
|
|
|
|
|
int role = Qt::DisplayRole) const;
|
|
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
QList<QString> groupIds() const;
|
|
|
|
|
void load(const QString &groupId);
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
QModelIndex createSnippet();
|
|
|
|
|
QModelIndex insertSnippet(const Snippet &snippet);
|
|
|
|
|
void removeSnippet(const QModelIndex &modelIndex);
|
|
|
|
|
const Snippet &snippetAt(const QModelIndex &modelIndex) const;
|
|
|
|
|
void setSnippetContent(const QModelIndex &modelIndex, const QString &content);
|
2010-11-24 11:04:24 +01:00
|
|
|
void revertBuitInSnippet(const QModelIndex &modelIndex);
|
|
|
|
|
void restoreRemovedBuiltInSnippets();
|
|
|
|
|
void resetSnippets();
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
private:
|
2010-11-24 11:04:24 +01:00
|
|
|
void replaceSnippet(const Snippet &snippet, const QModelIndex &modelIndex);
|
2010-10-27 17:38:22 +02:00
|
|
|
static bool isValidTrigger(const QString &s);
|
|
|
|
|
|
2010-12-08 16:05:48 +01:00
|
|
|
SnippetsCollection* m_collection;
|
2010-12-02 17:02:23 +01:00
|
|
|
QString m_activeGroupId;
|
2010-10-27 17:38:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SnippetsTableModel::SnippetsTableModel(QObject *parent) :
|
|
|
|
|
QAbstractTableModel(parent),
|
2010-12-08 16:05:48 +01:00
|
|
|
m_collection(SnippetsCollection::instance())
|
2010-10-27 17:38:22 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
int SnippetsTableModel::rowCount(const QModelIndex &) const
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
return m_collection->totalActiveSnippets(m_activeGroupId);
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SnippetsTableModel::columnCount(const QModelIndex &) const
|
|
|
|
|
{
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Qt::ItemFlags SnippetsTableModel::flags(const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
Qt::ItemFlags itemFlags = QAbstractTableModel::flags(index);
|
|
|
|
|
if (index.isValid())
|
|
|
|
|
itemFlags |= Qt::ItemIsEditable;
|
|
|
|
|
return itemFlags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant SnippetsTableModel::data(const QModelIndex &modelIndex, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (!modelIndex.isValid())
|
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
2010-12-02 17:02:23 +01:00
|
|
|
const Snippet &snippet = m_collection->snippet(modelIndex.row(), m_activeGroupId);
|
2010-10-27 17:38:22 +02:00
|
|
|
if (modelIndex.column() == 0)
|
|
|
|
|
return snippet.trigger();
|
|
|
|
|
else
|
|
|
|
|
return snippet.complement();
|
|
|
|
|
} else {
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SnippetsTableModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
|
|
|
|
|
{
|
|
|
|
|
if (modelIndex.isValid() && role == Qt::EditRole) {
|
2010-12-02 17:02:23 +01:00
|
|
|
Snippet snippet(m_collection->snippet(modelIndex.row(), m_activeGroupId));
|
2010-10-27 17:38:22 +02:00
|
|
|
if (modelIndex.column() == 0) {
|
|
|
|
|
const QString &s = value.toString();
|
|
|
|
|
if (!isValidTrigger(s)) {
|
|
|
|
|
QMessageBox::critical(0, tr("Error"), tr("Not a valid trigger."));
|
|
|
|
|
if (snippet.trigger().isEmpty())
|
|
|
|
|
removeSnippet(modelIndex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
snippet.setTrigger(s);
|
|
|
|
|
} else {
|
|
|
|
|
snippet.setComplement(value.toString());
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-24 11:04:24 +01:00
|
|
|
replaceSnippet(snippet, modelIndex);
|
2010-10-27 17:38:22 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant SnippetsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
|
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
|
|
if (section == 0)
|
|
|
|
|
return tr("Trigger");
|
|
|
|
|
else
|
2011-02-02 11:07:24 +01:00
|
|
|
return tr("Trigger Variant");
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
void SnippetsTableModel::load(const QString &groupId)
|
2010-10-27 17:38:22 +02:00
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
m_activeGroupId = groupId;
|
2010-10-27 17:38:22 +02:00
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
QList<QString> SnippetsTableModel::groupIds() const
|
|
|
|
|
{
|
|
|
|
|
return m_collection->groupIds();
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-27 17:38:22 +02:00
|
|
|
QModelIndex SnippetsTableModel::createSnippet()
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
Snippet snippet(m_activeGroupId);
|
2010-10-27 17:38:22 +02:00
|
|
|
return insertSnippet(snippet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex SnippetsTableModel::insertSnippet(const Snippet &snippet)
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
const SnippetsCollection::Hint &hint = m_collection->computeInsertionHint(snippet);
|
2010-10-27 17:38:22 +02:00
|
|
|
beginInsertRows(QModelIndex(), hint.index(), hint.index());
|
2010-12-02 17:02:23 +01:00
|
|
|
m_collection->insertSnippet(snippet, hint);
|
2010-10-27 17:38:22 +02:00
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
|
|
return index(hint.index(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsTableModel::removeSnippet(const QModelIndex &modelIndex)
|
|
|
|
|
{
|
|
|
|
|
beginRemoveRows(QModelIndex(), modelIndex.row(), modelIndex.row());
|
2010-12-02 17:02:23 +01:00
|
|
|
m_collection->removeSnippet(modelIndex.row(), m_activeGroupId);
|
2010-10-27 17:38:22 +02:00
|
|
|
endRemoveRows();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Snippet &SnippetsTableModel::snippetAt(const QModelIndex &modelIndex) const
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
return m_collection->snippet(modelIndex.row(), m_activeGroupId);
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsTableModel::setSnippetContent(const QModelIndex &modelIndex, const QString &content)
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
m_collection->setSnippetContent(modelIndex.row(), m_activeGroupId, content);
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-24 11:04:24 +01:00
|
|
|
void SnippetsTableModel::revertBuitInSnippet(const QModelIndex &modelIndex)
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
const Snippet &snippet = m_collection->revertedSnippet(modelIndex.row(), m_activeGroupId);
|
2010-11-24 11:04:24 +01:00
|
|
|
if (snippet.id().isEmpty()) {
|
|
|
|
|
QMessageBox::critical(0, tr("Error"), tr("Error reverting snippet."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
replaceSnippet(snippet, modelIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsTableModel::restoreRemovedBuiltInSnippets()
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
m_collection->restoreRemovedSnippets(m_activeGroupId);
|
2010-11-24 11:04:24 +01:00
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsTableModel::resetSnippets()
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
m_collection->reset(m_activeGroupId);
|
2010-11-24 11:04:24 +01:00
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsTableModel::replaceSnippet(const Snippet &snippet, const QModelIndex &modelIndex)
|
|
|
|
|
{
|
|
|
|
|
const int row = modelIndex.row();
|
|
|
|
|
const SnippetsCollection::Hint &hint =
|
2010-12-02 17:02:23 +01:00
|
|
|
m_collection->computeReplacementHint(row, snippet);
|
2010-11-24 11:04:24 +01:00
|
|
|
if (modelIndex.row() == hint.index()) {
|
2010-12-02 17:02:23 +01:00
|
|
|
m_collection->replaceSnippet(row, snippet, hint);
|
2010-12-02 17:15:35 +01:00
|
|
|
if (modelIndex.column() == 0)
|
|
|
|
|
emit dataChanged(modelIndex, modelIndex.sibling(row, 1));
|
|
|
|
|
else
|
|
|
|
|
emit dataChanged(modelIndex.sibling(row, 0), modelIndex);
|
2010-11-24 11:04:24 +01:00
|
|
|
} 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());
|
2010-12-02 17:02:23 +01:00
|
|
|
m_collection->replaceSnippet(row, snippet, hint);
|
2010-11-24 11:04:24 +01:00
|
|
|
endMoveRows();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-27 17:38:22 +02:00
|
|
|
bool SnippetsTableModel::isValidTrigger(const QString &s)
|
|
|
|
|
{
|
|
|
|
|
if (s.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
for (int i = 0; i < s.length(); ++i)
|
|
|
|
|
if (!s.at(i).isLetter())
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SnippetsSettingsPagePrivate
|
|
|
|
|
class SnippetsSettingsPagePrivate : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
SnippetsSettingsPagePrivate(const QString &id);
|
|
|
|
|
~SnippetsSettingsPagePrivate() { delete m_model; }
|
|
|
|
|
|
|
|
|
|
const QString &id() const { return m_id; }
|
|
|
|
|
const QString &displayName() const { return m_displayName; }
|
|
|
|
|
bool isKeyword(const QString &s) const { return m_keywords.contains(s, Qt::CaseInsensitive); }
|
|
|
|
|
void configureUi(QWidget *parent);
|
|
|
|
|
|
|
|
|
|
void apply();
|
|
|
|
|
void finish();
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
void loadSnippetGroup(int index);
|
|
|
|
|
void markSnippetsCollection();
|
|
|
|
|
void addSnippet();
|
|
|
|
|
void removeSnippet();
|
2010-11-24 11:04:24 +01:00
|
|
|
void revertBuiltInSnippet();
|
|
|
|
|
void restoreRemovedBuiltInSnippets();
|
|
|
|
|
void resetAllSnippets();
|
2010-10-27 17:38:22 +02:00
|
|
|
void selectSnippet(const QModelIndex &parent, int row);
|
|
|
|
|
void selectMovedSnippet(const QModelIndex &, int, int, const QModelIndex &, int row);
|
2010-11-24 11:04:24 +01:00
|
|
|
void setSnippetContent();
|
|
|
|
|
void updateCurrentSnippetDependent(const QModelIndex &modelIndex = QModelIndex());
|
2011-05-31 16:36:58 +02:00
|
|
|
void decorateEditors(const TextEditor::FontSettings &fontSettings);
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
private:
|
2011-02-21 16:02:26 +01:00
|
|
|
SnippetEditorWidget *currentEditor() const;
|
|
|
|
|
SnippetEditorWidget *editorAt(int i) const;
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
void loadSettings();
|
|
|
|
|
bool settingsChanged() const;
|
|
|
|
|
void writeSettings();
|
|
|
|
|
|
|
|
|
|
const QString m_id;
|
|
|
|
|
const QString m_displayName;
|
|
|
|
|
const QString m_settingsPrefix;
|
|
|
|
|
SnippetsTableModel *m_model;
|
|
|
|
|
bool m_snippetsCollectionChanged;
|
|
|
|
|
QString m_keywords;
|
|
|
|
|
SnippetsSettings m_settings;
|
|
|
|
|
Ui::SnippetsSettingsPage m_ui;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SnippetsSettingsPagePrivate::SnippetsSettingsPagePrivate(const QString &id) :
|
|
|
|
|
m_id(id),
|
|
|
|
|
m_displayName(tr("Snippets")),
|
|
|
|
|
m_settingsPrefix(QLatin1String("Text")),
|
|
|
|
|
m_model(new SnippetsTableModel(0)),
|
|
|
|
|
m_snippetsCollectionChanged(false)
|
|
|
|
|
{}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
SnippetEditorWidget *SnippetsSettingsPagePrivate::currentEditor() const
|
2010-10-27 17:38:22 +02:00
|
|
|
{
|
|
|
|
|
return editorAt(m_ui.snippetsEditorStack->currentIndex());
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
SnippetEditorWidget *SnippetsSettingsPagePrivate::editorAt(int i) const
|
2010-10-27 17:38:22 +02:00
|
|
|
{
|
2011-02-21 16:02:26 +01:00
|
|
|
return static_cast<SnippetEditorWidget *>(m_ui.snippetsEditorStack->widget(i));
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::configureUi(QWidget *w)
|
|
|
|
|
{
|
|
|
|
|
m_ui.setupUi(w);
|
|
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
const QList<ISnippetProvider *> &providers =
|
2012-06-18 11:34:15 +02:00
|
|
|
ExtensionSystem::PluginManager::getObjects<ISnippetProvider>();
|
2010-12-02 17:02:23 +01:00
|
|
|
foreach (ISnippetProvider *provider, providers) {
|
|
|
|
|
m_ui.groupCombo->addItem(provider->displayName(), provider->groupId());
|
2011-02-21 16:02:26 +01:00
|
|
|
SnippetEditorWidget *snippetEditor = new SnippetEditorWidget(w);
|
2011-05-31 16:36:58 +02:00
|
|
|
snippetEditor->setFontSettings(TextEditorSettings::instance()->fontSettings());
|
2010-12-02 17:02:23 +01:00
|
|
|
provider->decorateEditor(snippetEditor);
|
|
|
|
|
m_ui.snippetsEditorStack->insertWidget(m_ui.groupCombo->count() - 1, snippetEditor);
|
|
|
|
|
connect(snippetEditor, SIGNAL(snippetContentChanged()), this, SLOT(setSnippetContent()));
|
|
|
|
|
}
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
m_ui.snippetsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
|
m_ui.snippetsTable->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
|
m_ui.snippetsTable->horizontalHeader()->setStretchLastSection(true);
|
|
|
|
|
m_ui.snippetsTable->horizontalHeader()->setHighlightSections(false);
|
|
|
|
|
m_ui.snippetsTable->verticalHeader()->setVisible(false);
|
|
|
|
|
m_ui.snippetsTable->verticalHeader()->setDefaultSectionSize(20);
|
|
|
|
|
m_ui.snippetsTable->setModel(m_model);
|
|
|
|
|
|
2010-11-24 11:04:24 +01:00
|
|
|
m_ui.revertButton->setEnabled(false);
|
|
|
|
|
|
2010-10-27 17:38:22 +02:00
|
|
|
QTextStream(&m_keywords) << m_displayName;
|
|
|
|
|
|
|
|
|
|
loadSettings();
|
|
|
|
|
loadSnippetGroup(m_ui.groupCombo->currentIndex());
|
|
|
|
|
|
2012-03-05 22:30:59 +01:00
|
|
|
connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)),
|
2010-10-27 17:38:22 +02:00
|
|
|
this, SLOT(selectSnippet(QModelIndex,int)));
|
2012-03-05 22:30:59 +01:00
|
|
|
connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)),
|
2010-10-27 17:38:22 +02:00
|
|
|
this, SLOT(markSnippetsCollection()));
|
2012-03-05 22:30:59 +01:00
|
|
|
connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
2010-10-27 17:38:22 +02:00
|
|
|
this, SLOT(markSnippetsCollection()));
|
2012-03-05 22:30:59 +01:00
|
|
|
connect(m_model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
|
2010-10-27 17:38:22 +02:00
|
|
|
this, SLOT(selectMovedSnippet(QModelIndex,int,int,QModelIndex,int)));
|
2012-03-05 22:30:59 +01:00
|
|
|
connect(m_model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
|
2010-10-27 17:38:22 +02:00
|
|
|
this, SLOT(markSnippetsCollection()));
|
|
|
|
|
connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
|
|
|
|
|
this, SLOT(markSnippetsCollection()));
|
2010-11-24 11:04:24 +01:00
|
|
|
connect(m_model, SIGNAL(modelReset()), this, SLOT(updateCurrentSnippetDependent()));
|
|
|
|
|
connect(m_model, SIGNAL(modelReset()), this, SLOT(markSnippetsCollection()));
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
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()));
|
2010-11-24 11:04:24 +01:00
|
|
|
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()));
|
2010-10-27 17:38:22 +02:00
|
|
|
connect(m_ui.snippetsTable->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
|
2010-11-24 11:04:24 +01:00
|
|
|
this, SLOT(updateCurrentSnippetDependent(QModelIndex)));
|
2011-05-31 16:36:58 +02:00
|
|
|
|
|
|
|
|
connect(TextEditorSettings::instance(), SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
|
|
|
|
|
this, SLOT(decorateEditors(TextEditor::FontSettings)));
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::apply()
|
|
|
|
|
{
|
|
|
|
|
if (settingsChanged())
|
|
|
|
|
writeSettings();
|
|
|
|
|
|
2011-01-14 16:07:47 +01:00
|
|
|
if (currentEditor()->document()->isModified())
|
|
|
|
|
setSnippetContent();
|
|
|
|
|
|
2010-11-23 11:31:57 +01:00
|
|
|
if (m_snippetsCollectionChanged) {
|
2011-03-30 15:15:15 +02:00
|
|
|
QString errorString;
|
|
|
|
|
if (SnippetsCollection::instance()->synchronize(&errorString))
|
|
|
|
|
m_snippetsCollectionChanged = false;
|
|
|
|
|
else
|
2012-01-24 15:36:40 +01:00
|
|
|
QMessageBox::critical(Core::ICore::mainWindow(),
|
2011-03-30 15:15:15 +02:00
|
|
|
tr("Error While Saving Snippet Collection"), errorString);
|
2010-11-23 11:31:57 +01:00
|
|
|
}
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::finish()
|
|
|
|
|
{
|
|
|
|
|
if (m_snippetsCollectionChanged) {
|
2010-12-08 16:05:48 +01:00
|
|
|
SnippetsCollection::instance()->reload();
|
2010-10-27 17:38:22 +02:00
|
|
|
m_snippetsCollectionChanged = false;
|
|
|
|
|
}
|
2011-06-20 14:25:55 +02:00
|
|
|
|
|
|
|
|
disconnect(TextEditorSettings::instance(), 0, this, 0);
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::loadSettings()
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
if (m_ui.groupCombo->count() == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-01-24 15:36:40 +01:00
|
|
|
if (QSettings *s = Core::ICore::settings()) {
|
2010-10-27 17:38:22 +02:00
|
|
|
m_settings.fromSettings(m_settingsPrefix, s);
|
2010-12-02 17:02:23 +01:00
|
|
|
const QString &lastGroupName = m_settings.lastUsedSnippetGroup();
|
|
|
|
|
const int index = m_ui.groupCombo->findText(lastGroupName);
|
|
|
|
|
if (index != -1)
|
|
|
|
|
m_ui.groupCombo->setCurrentIndex(index);
|
|
|
|
|
else
|
|
|
|
|
m_ui.groupCombo->setCurrentIndex(0);
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::writeSettings()
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
if (m_ui.groupCombo->count() == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-01-24 15:36:40 +01:00
|
|
|
if (QSettings *s = Core::ICore::settings()) {
|
2010-10-27 17:38:22 +02:00
|
|
|
m_settings.setLastUsedSnippetGroup(m_ui.groupCombo->currentText());
|
|
|
|
|
m_settings.toSettings(m_settingsPrefix, s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SnippetsSettingsPagePrivate::settingsChanged() const
|
|
|
|
|
{
|
|
|
|
|
if (m_settings.lastUsedSnippetGroup() != m_ui.groupCombo->currentText())
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::loadSnippetGroup(int index)
|
|
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
if (index == -1)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-10-27 17:38:22 +02:00
|
|
|
m_ui.snippetsEditorStack->setCurrentIndex(index);
|
|
|
|
|
currentEditor()->clear();
|
2010-12-02 17:02:23 +01:00
|
|
|
m_model->load(m_ui.groupCombo->itemData(index).toString());
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::markSnippetsCollection()
|
|
|
|
|
{
|
|
|
|
|
if (!m_snippetsCollectionChanged)
|
|
|
|
|
m_snippetsCollectionChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::addSnippet()
|
|
|
|
|
{
|
|
|
|
|
const QModelIndex &modelIndex = m_model->createSnippet();
|
|
|
|
|
selectSnippet(QModelIndex(), modelIndex.row());
|
|
|
|
|
m_ui.snippetsTable->edit(modelIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::removeSnippet()
|
|
|
|
|
{
|
|
|
|
|
const QModelIndex &modelIndex = m_ui.snippetsTable->selectionModel()->currentIndex();
|
|
|
|
|
if (!modelIndex.isValid()) {
|
|
|
|
|
QMessageBox::critical(0, tr("Error"), tr("No snippet selected."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_model->removeSnippet(modelIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-24 11:04:24 +01:00
|
|
|
void SnippetsSettingsPagePrivate::restoreRemovedBuiltInSnippets()
|
|
|
|
|
{
|
|
|
|
|
m_model->restoreRemovedBuiltInSnippets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::revertBuiltInSnippet()
|
|
|
|
|
{
|
|
|
|
|
m_model->revertBuitInSnippet(m_ui.snippetsTable->selectionModel()->currentIndex());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::resetAllSnippets()
|
|
|
|
|
{
|
|
|
|
|
m_model->resetSnippets();
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-27 17:38:22 +02:00
|
|
|
void SnippetsSettingsPagePrivate::selectSnippet(const QModelIndex &parent, int row)
|
|
|
|
|
{
|
|
|
|
|
QModelIndex topLeft = m_model->index(row, 0, parent);
|
|
|
|
|
QModelIndex bottomRight = m_model->index(row, 1, parent);
|
|
|
|
|
QItemSelection selection(topLeft, bottomRight);
|
|
|
|
|
m_ui.snippetsTable->selectionModel()->select(selection, QItemSelectionModel::SelectCurrent);
|
|
|
|
|
m_ui.snippetsTable->setCurrentIndex(topLeft);
|
|
|
|
|
m_ui.snippetsTable->scrollTo(topLeft);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPagePrivate::selectMovedSnippet(const QModelIndex &,
|
|
|
|
|
int sourceRow,
|
|
|
|
|
int,
|
|
|
|
|
const QModelIndex &destinationParent,
|
|
|
|
|
int destinationRow)
|
|
|
|
|
{
|
|
|
|
|
QModelIndex modelIndex;
|
|
|
|
|
if (sourceRow < destinationRow)
|
|
|
|
|
modelIndex = m_model->index(destinationRow - 1, 0, destinationParent);
|
|
|
|
|
else
|
|
|
|
|
modelIndex = m_model->index(destinationRow, 0, destinationParent);
|
|
|
|
|
m_ui.snippetsTable->scrollTo(modelIndex);
|
2010-11-24 11:04:24 +01:00
|
|
|
currentEditor()->setPlainText(m_model->snippetAt(modelIndex).content());
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-24 11:04:24 +01:00
|
|
|
void SnippetsSettingsPagePrivate::updateCurrentSnippetDependent(const QModelIndex &modelIndex)
|
2010-10-27 17:38:22 +02:00
|
|
|
{
|
2010-11-24 11:04:24 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-24 11:04:24 +01:00
|
|
|
void SnippetsSettingsPagePrivate::setSnippetContent()
|
2010-10-27 17:38:22 +02:00
|
|
|
{
|
|
|
|
|
const QModelIndex &modelIndex = m_ui.snippetsTable->selectionModel()->currentIndex();
|
|
|
|
|
if (modelIndex.isValid()) {
|
|
|
|
|
m_model->setSnippetContent(modelIndex, currentEditor()->toPlainText());
|
|
|
|
|
markSnippetsCollection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-31 16:36:58 +02:00
|
|
|
void SnippetsSettingsPagePrivate::decorateEditors(const TextEditor::FontSettings &fontSettings)
|
|
|
|
|
{
|
|
|
|
|
const QList<ISnippetProvider *> &providers =
|
2012-06-18 11:34:15 +02:00
|
|
|
ExtensionSystem::PluginManager::getObjects<ISnippetProvider>();
|
2011-05-31 16:36:58 +02:00
|
|
|
for (int i = 0; i < m_ui.groupCombo->count(); ++i) {
|
|
|
|
|
SnippetEditorWidget *snippetEditor = editorAt(i);
|
|
|
|
|
snippetEditor->setFontSettings(fontSettings);
|
|
|
|
|
const QString &id = m_ui.groupCombo->itemData(i).toString();
|
|
|
|
|
// This list should be quite short... Re-iterating over it is ok.
|
|
|
|
|
foreach (const ISnippetProvider *provider, providers) {
|
|
|
|
|
if (provider->groupId() == id)
|
|
|
|
|
provider->decorateEditor(snippetEditor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-27 17:38:22 +02:00
|
|
|
// SnippetsSettingsPage
|
|
|
|
|
SnippetsSettingsPage::SnippetsSettingsPage(const QString &id, QObject *parent) :
|
|
|
|
|
TextEditorOptionsPage(parent),
|
2011-09-16 13:10:06 +02:00
|
|
|
d(new SnippetsSettingsPagePrivate(id))
|
2010-10-27 17:38:22 +02:00
|
|
|
{
|
2012-05-22 11:17:13 +02:00
|
|
|
setId(d->id());
|
|
|
|
|
setDisplayName(d->displayName());
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-22 11:17:13 +02:00
|
|
|
SnippetsSettingsPage::~SnippetsSettingsPage()
|
2010-10-27 17:38:22 +02:00
|
|
|
{
|
2012-05-22 11:17:13 +02:00
|
|
|
delete d;
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SnippetsSettingsPage::matches(const QString &s) const
|
|
|
|
|
{
|
2011-09-16 13:10:06 +02:00
|
|
|
return d->isKeyword(s);
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *SnippetsSettingsPage::createPage(QWidget *parent)
|
|
|
|
|
{
|
|
|
|
|
QWidget *w = new QWidget(parent);
|
2011-09-16 13:10:06 +02:00
|
|
|
d->configureUi(w);
|
2010-10-27 17:38:22 +02:00
|
|
|
return w;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPage::apply()
|
|
|
|
|
{
|
2011-09-16 13:10:06 +02:00
|
|
|
d->apply();
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SnippetsSettingsPage::finish()
|
|
|
|
|
{
|
2011-09-16 13:10:06 +02:00
|
|
|
d->finish();
|
2010-10-27 17:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // Internal
|
|
|
|
|
} // TextEditor
|
|
|
|
|
|
|
|
|
|
#include "snippetssettingspage.moc"
|