26 #include "ui_editsnippet.h"
30 #include <QToolButton>
32 #include <KLocalizedString>
33 #include <KPushButton>
35 #include <KMimeTypeTrader>
36 #include <KTextEditor/Document>
37 #include <KTextEditor/View>
38 #include <KToolInvocation>
39 #include <KMessageBox>
40 #include <KMessageWidget>
49 KParts::ReadWritePart* part= KMimeTypeTrader::self()->createPartInstanceFromQuery<KParts::ReadWritePart>(
50 "text/plain", tabWidget, tabWidget);
51 KTextEditor::Document* document = qobject_cast<KTextEditor::Document*>(part);
53 Q_ASSERT(document->action(
"file_save"));
54 document->action(
"file_save")->setEnabled(
false);
56 KTextEditor::View* view = qobject_cast< KTextEditor::View* >( document->widget() );
64 button->
setIcon(KIcon(
"help-contents"));
69 return qMakePair(view, button);
73 :
KDialog(parent), m_ui(new Ui::EditSnippetBase), m_repo(repository)
74 , m_snippet(snippet), m_topBoxModified(false)
78 setButtons(Apply | Cancel | Ok);
79 m_ui->setupUi(mainWidget());
83 m_snippetView = pair.first;
87 connect(pair.second, SIGNAL(clicked(
bool)),
88 this, SLOT(slotSnippetDocumentation()));
91 m_scriptsView = pair.first;
92 m_scriptsView->document()->setMode(
"JavaScript");
93 m_scriptsView->document()->setText(m_repo->
script());
94 m_scriptsView->document()->setModified(
false);
95 connect(pair.second, SIGNAL(clicked(
bool)),
96 this, SLOT(slotScriptDocumentation()));
98 m_ui->verticalLayout->setMargin(0);
99 m_ui->formLayout->setMargin(0);
101 m_ui->snippetShortcutWidget->layout()->setMargin(0);
103 connect(
this, SIGNAL(okClicked()),
this, SLOT(save()));
104 connect(
this, SIGNAL(applyClicked()),
this, SLOT(save()));
106 connect(m_ui->snippetNameEdit, SIGNAL(textEdited(
QString)),
this, SLOT(topBoxModified()));
107 connect(m_ui->snippetNameEdit, SIGNAL(textEdited(
QString)),
this, SLOT(validate()));
108 connect(m_ui->snippetArgumentsEdit, SIGNAL(textEdited(
QString)),
this, SLOT(topBoxModified()));
109 connect(m_ui->snippetPostfixEdit, SIGNAL(textEdited(
QString)),
this, SLOT(topBoxModified()));
110 connect(m_ui->snippetPrefixEdit, SIGNAL(textEdited(
QString)),
this, SLOT(topBoxModified()));
111 connect(m_ui->snippetShortcutWidget, SIGNAL(shortcutChanged(KShortcut)),
this, SLOT(topBoxModified()));
112 connect(m_snippetView->document(), SIGNAL(textChanged(KTextEditor::Document*)),
this, SLOT(validate()));
116 setWindowTitle(
i18n(
"Edit Snippet %1 in %2", m_snippet->
text(), m_repo->
text()));
118 m_ui->snippetArgumentsEdit->setText(m_snippet->
arguments());
119 m_snippetView->document()->setText(m_snippet->
snippet());
120 m_ui->snippetNameEdit->setText(m_snippet->
text());
121 m_ui->snippetPostfixEdit->setText(m_snippet->
postfix());
122 m_ui->snippetPrefixEdit->setText(m_snippet->
prefix());
123 m_ui->snippetShortcutWidget->setShortcut(m_snippet->
action()->shortcut());
126 m_snippetView->document()->setModified(
false);
127 m_topBoxModified =
false;
129 setWindowTitle(
i18n(
"Create New Snippet in Repository %1", m_repo->
text()));
134 m_ui->snippetNameEdit->setFocus();
136 QSize initSize = sizeHint();
138 setInitialSize(initSize);
148 m_snippetView->document()->setText(text);
152 void EditSnippet::validate()
154 const QString& name = m_ui->snippetNameEdit->text();
155 bool valid = !name.
isEmpty() && !m_snippetView->document()->isEmpty();
158 for (
int i = 0; i < name.
length(); ++i ) {
161 m_ui->messageWidget->setText(
i18n(
"Snippet name cannot contain spaces"));
162 m_ui->messageWidget->animatedShow();
168 m_ui->messageWidget->animatedHide();
171 button(Ok)->setEnabled(valid);
172 button(Apply)->setEnabled(valid);
175 void EditSnippet::save()
177 Q_ASSERT(!m_ui->snippetNameEdit->text().isEmpty());
184 m_snippet->
setArguments(m_ui->snippetArgumentsEdit->text());
185 m_snippet->
setSnippet(m_snippetView->document()->text());
186 m_snippetView->document()->setModified(
false);
187 m_snippet->
setText(m_ui->snippetNameEdit->text());
188 m_snippet->
setPostfix(m_ui->snippetPostfixEdit->text());
189 m_snippet->
setPrefix(m_ui->snippetPrefixEdit->text());
190 m_snippet->
action()->setShortcut(m_ui->snippetShortcutWidget->shortcut());
191 m_repo->
setScript(m_scriptsView->document()->text());
192 m_scriptsView->document()->setModified(
false);
193 m_topBoxModified =
false;
196 setWindowTitle(
i18n(
"Edit Snippet %1 in %2", m_snippet->
text(), m_repo->
text()));
199 void EditSnippet::slotSnippetDocumentation()
201 KToolInvocation::invokeHelp(
"kate-application-plugin-snippets",
"kate");
204 void EditSnippet::slotScriptDocumentation()
206 KToolInvocation::invokeHelp(
"dev-scripting-api",
"kate");
211 if (m_topBoxModified || m_snippetView->document()->isModified() || m_scriptsView->document()->isModified()) {
212 int ret = KMessageBox::warningContinueCancel(qApp->activeWindow(),
213 i18n(
"The snippet contains unsaved changes. Do you want to continue and lose all changes?"),
214 i18n(
"Warning - Unsaved Changes")
216 if (ret == KMessageBox::Cancel) {
223 void EditSnippet::topBoxModified()
225 m_topBoxModified =
true;
228 #include "editsnippet.moc"
void setSnippet(const QString &snippet)
Sets the actual contents of this snippet.
void setHeight(int height)
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
void setArguments(const QString &arguments)
Sets the display arguments of this snippet.
void setPrefix(const QString &prefix)
Sets the display prefix of this snippet.
KAction * action()
Action to trigger insertion of this snippet.
EditSnippet(SnippetRepository *repo, Snippet *snippet, QWidget *parent=0)
snippet set to 0 when you want to create a new snippet.
void setSnippetText(const QString &text)
void save()
Save this repository to disk.
void appendRow(const QList< QStandardItem * > &items)
void setPostfix(const QString &postfix)
Sets the display postfix of this snippet.
QString script() const
The QtScript(s) associated with this repository.
Each object of this type represents a repository of snippets.
One object of this class represents a single snippet.
QString prefix() const
Returns the display prefix of this snippet.
void setScript(const QString &script)
Sets the QtScript(s) associated with this repository.
void setText(const QString &text)
QString snippet() const
Returns the actual contents of this snippet.
void addStretch(int stretch)
const QChar at(int position) const
void setText(const QString &text)
QString arguments() const
Returns the display arguments of this snippet.
QPair< KTextEditor::View *, QToolButton * > getViewForTab(QWidget *tabWidget)
QString postfix() const
Returns the display postfix of this snippet.
QStringList fileTypes() const
The valid filetypes for the snippets contained in this repository.
void addLayout(QLayout *layout, int stretch)