26 #include <QContextMenuEvent>
28 #include <KMessageBox>
40 #include <KGlobalSettings>
42 #include <KNS3/DownloadDialog>
43 #include <knewstuff3/uploaddialog.h>
46 :
QWidget(parent), Ui::SnippetViewBase(), m_plugin(plugin)
48 Ui::SnippetViewBase::setupUi(
this);
50 setWindowTitle(
i18n(
"Snippets"));
51 setWindowIcon (
KIcon(
"document-new"));
53 connect(filterText, SIGNAL(clearButtonClicked()),
54 this, SLOT(slotFilterChanged()));
55 connect(filterText, SIGNAL(textChanged(
QString)),
56 this, SLOT(slotFilterChanged()));
58 snippetTree->setContextMenuPolicy( Qt::CustomContextMenu );
59 snippetTree->viewport()->installEventFilter(
this );
60 connect(snippetTree, SIGNAL(customContextMenuRequested(
QPoint)),
61 this, SLOT(contextMenu(
QPoint)));
67 snippetTree->setModel( m_proxy );
70 snippetTree->header()->hide();
72 m_addRepoAction =
new KAction(
KIcon(
"folder-new"),
i18n(
"Add Repository"),
this);
73 connect(m_addRepoAction, SIGNAL(triggered()),
this, SLOT(slotAddRepo()));
74 addAction(m_addRepoAction);
75 m_editRepoAction =
new KAction(
KIcon(
"folder-txt"),
i18n(
"Edit Repository"),
this);
76 connect(m_editRepoAction, SIGNAL(triggered()),
this, SLOT(slotEditRepo()));
77 addAction(m_editRepoAction);
78 m_removeRepoAction =
new KAction(
KIcon(
"edit-delete"),
i18n(
"Remove Repository"),
this);
79 connect(m_removeRepoAction, SIGNAL(triggered()),
this, SLOT(slotRemoveRepo()));
80 addAction(m_removeRepoAction);
82 m_putNewStuffAction =
new KAction(
KIcon(
"get-hot-new-stuff"),
i18n(
"Publish Repository"),
this);
83 connect(m_putNewStuffAction, SIGNAL(triggered()),
this, SLOT(slotSnippetToGHNS()));
84 addAction(m_putNewStuffAction);
87 separator->setSeparator(
true);
90 m_addSnippetAction =
new KAction(
KIcon(
"document-new"),
i18n(
"Add Snippet"),
this);
91 connect(m_addSnippetAction, SIGNAL(triggered()),
this, SLOT(slotAddSnippet()));
92 addAction(m_addSnippetAction);
93 m_editSnippetAction =
new KAction(
KIcon(
"document-edit"),
i18n(
"Edit Snippet"),
this);
94 connect(m_editSnippetAction, SIGNAL(triggered()),
this, SLOT(slotEditSnippet()));
95 addAction(m_editSnippetAction);
96 m_removeSnippetAction =
new KAction(
KIcon(
"document-close"),
i18n(
"Remove Snippet"),
this);
97 connect(m_removeSnippetAction, SIGNAL(triggered()),
this, SLOT(slotRemoveSnippet()));
98 addAction(m_removeSnippetAction);
100 addAction(separator);
102 m_getNewStuffAction =
new KAction(
KIcon(
"get-hot-new-stuff"),
i18n(
"Get New Snippets"),
this);
103 connect(m_getNewStuffAction, SIGNAL(triggered()),
this, SLOT(slotGHNS()));
104 addAction(m_getNewStuffAction);
106 connect(snippetTree->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(validateActions()));
114 void SnippetView::validateActions()
122 m_addRepoAction->setEnabled(
true);
123 m_editRepoAction->setEnabled(selectedRepo);
124 m_removeRepoAction->setEnabled(selectedRepo);
125 m_putNewStuffAction->setEnabled(selectedRepo);
127 m_addSnippetAction->setEnabled(selectedRepo || selectedSnippet);
128 m_editSnippetAction->setEnabled(selectedSnippet);
129 m_removeSnippetAction->setEnabled(selectedSnippet);
135 QModelIndex index = snippetTree->currentIndex();
136 index = m_proxy->mapToSource(index);
140 void SnippetView::slotSnippetClicked (
const QModelIndex & index)
153 void SnippetView::contextMenu (
const QPoint& pos)
155 QModelIndex index = snippetTree->indexAt( pos );
156 index = m_proxy->mapToSource(index);
161 menu.addTitle(
i18n(
"Snippets"));
163 menu.addAction(m_addRepoAction);
164 menu.addAction(m_getNewStuffAction);
166 menu.exec(snippetTree->mapToGlobal(pos));
167 }
else if (
Snippet* snippet = dynamic_cast<Snippet*>( item )) {
169 menu.addTitle(
i18n(
"Snippet: %1", snippet->text()));
171 menu.addAction(m_editSnippetAction);
172 menu.addAction(m_removeSnippetAction);
174 menu.exec(snippetTree->mapToGlobal(pos));
177 menu.addTitle(
i18n(
"Repository: %1", repo->text()));
179 menu.addAction(m_editRepoAction);
180 menu.addAction(m_removeRepoAction);
181 menu.addAction(m_putNewStuffAction);
184 menu.addAction(m_addSnippetAction);
186 menu.exec(snippetTree->mapToGlobal(pos));
190 void SnippetView::slotEditSnippet()
208 void SnippetView::slotAddSnippet()
225 void SnippetView::slotRemoveSnippet()
236 QApplication::activeWindow(),
237 i18n(
"Do you really want to delete the snippet \"%1\"?", item->text())
240 item->parent()->removeRow(item->row());
245 void SnippetView::slotAddRepo()
251 void SnippetView::slotEditRepo()
265 void SnippetView::slotRemoveRepo()
276 QApplication::activeWindow(),
277 i18n(
"Do you really want to delete the repository \"%1\" with all its snippets?", repo->text())
284 void SnippetView::slotFilterChanged()
289 void SnippetView::slotGHNS()
291 KNS3::DownloadDialog dialog(
"ktexteditor_codesnippets_core.knsrc",
this);
293 foreach (
const KNS3::Entry& entry, dialog.changedEntries() ) {
294 foreach (
const QString& path, entry.uninstalledFiles() ) {
295 if ( path.endsWith(
".xml") ) {
301 foreach (
const QString& path, entry.installedFiles() ) {
302 if ( path.endsWith(
".xml") ) {
309 void SnippetView::slotSnippetToGHNS()
319 KNS3::UploadDialog dialog(
"ktexteditor_codesnippets_core.knsrc",
this);
321 dialog.setUploadName(repo->text());
329 if (obj == snippetTree->viewport()) {
331 if ( (!singleClick && e->type() == QEvent::MouseButtonDblClick) || (singleClick && e->type() == QEvent::MouseButtonRelease) ) {
332 QMouseEvent* mouseEvent =
dynamic_cast<QMouseEvent*
>(e);
333 Q_ASSERT(mouseEvent);
334 QModelIndex clickedIndex = snippetTree->indexAt(mouseEvent->pos());
335 if (clickedIndex.isValid() && clickedIndex.parent().isValid()) {
336 slotSnippetClicked(clickedIndex);
342 return QObject::eventFilter(obj, e);
346 #include "snippetview.moc"
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
void insertSnippet(Snippet *snippet)
Inserts the given snippet into the currently active view.
static bool singleClick()
static KUrl fromPath(const QString &text)
void changeFilter(const QString &filter)
This dialog is used to create/edit snippet repositories and the snippets in them. ...
void save()
Save this repository to disk.
SnippetRepository * repositoryForFile(const QString &file)
Returns the repository for the given file if there is any.
Each object of this type represents a repository of snippets.
One object of this class represents a single snippet.
This is the main class of KDevelop's snippet plugin.
void remove()
Remove this repository from the disk.
const QString & file() const
The path to this repository's file.
static SnippetStore * self()
Retuns the SnippetStore.
SnippetView(KateSnippetGlobal *plugin, QWidget *parent=0)
This dialog is used to create/edit snippets in a given repository.
static int warningContinueCancel(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)