void QSnippetEdit::on_lwSnippets_currentRowChanged(int idx) { if ( (idx - 1) == m_editedSnippet ) return; if ( maybeSave() ) { lwSnippets->setCurrentRow(m_editedSnippet); return; } m_editedSnippet = idx - 1; if ( idx <= 0 ) { eSnippet->setText(QString()); leSnippetName->setText(QString()); leSnippetScope->setText(QString()); } else { QSnippet *snip = m_manager->snippet(m_editedSnippet); eSnippet->setText(snip->pattern()); leSnippetName->setText(snip->name()); leSnippetScope->setText(snip->contexts().join(",")); //eSnippet->highlight(); } eSnippet->setFocus(); }
void QSnippetEdit::on_leSnippetName_editingFinished() { if ( m_editedSnippet < 0 ) return; QSnippet *snip = m_manager->snippet(m_editedSnippet); snip->setName(leSnippetName->text()); lwSnippets->item(m_editedSnippet + 1)->setText(snip->name()); }
bool QSnippetEdit::maybeSave() { static const QRegExp nonTrivial("\\S"); QString pattern = eSnippet->text(); if ( pattern.endsWith('\n') ) pattern.chop(1); QString name = leSnippetName->text(); QStringList contexts = leSnippetScope->text().split(_cxt_splitter); bool nonTrivialPattern = pattern.contains(nonTrivial); if ( m_editedSnippet >= 0 ) { QSnippet *snip = m_manager->snippet(m_editedSnippet); if ( snip->pattern() != pattern ) { int ret = QMessageBox::warning(this, tr("Unsaved changes"), tr("Do you want to save pattern changes to snippet %1 ?") .arg(snip->name()), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Yes ); if ( ret == QMessageBox::Cancel ) { return true; } else if ( ret == QMessageBox::Yes ) { snip->setPattern(pattern); } } } else if ( nonTrivialPattern ) { int ret = QMessageBox::warning(this, tr("Unsaved changes"), tr("The current snippet data will be discarded. Do you want to continue?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes ); if ( ret == QMessageBox::No ) { return true; } } return false; }
bool QSnippetBinding::keyPressEvent(QKeyEvent *event, QEditor *editor) { /* if ( event->modifiers() & Qt::ControlModifier ) { for ( int i = 0; i < qMin(10, m->snippetCount()); ++i ) { if ( event->key() == (Qt::Key_F1 + i) ) { m->snippet(i)->insert(editor); return true; } } } */ if ( (event->modifiers() & Qt::AltModifier) && (event->key() == Qt::Key_Space || event->text() == " ") ) { QDocumentCursor c = editor->cursor(); //c.select(QDocumentCursor::SelectWord); if ( !c.hasSelection() ) { c.movePosition(1, QDocumentCursor::PreviousWord, QDocumentCursor::KeepAnchor); editor->setCursor(c); } QString s = c.selectedText(); for ( int i = 0; i < m_manager->snippetCount(); ++i ) { QSnippet *snip = m_manager->snippet(i); if ( snip->name() == s ) { snip->insert(editor); return true; } } } return QEditorInputBinding::keyPressEvent(event, editor); }