Example #1
0
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();
}
Example #2
0
void QSnippetEdit::on_leSnippetScope_editingFinished()
{
	if ( m_editedSnippet < 0 )
		return;
	
	QSnippet *snip = m_manager->snippet(m_editedSnippet);
	
	snip->setContexts(leSnippetScope->text().split(_cxt_splitter));
}
Example #3
0
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());
}
Example #4
0
bool QSnippetManager::loadSnippetFromString(const QString& name, const QString& s, const QString& type)
{
	QSnippetPatternLoader *pl = patternLoader(type);
	
	QSnippet *snip = pl->loadSnippet(s);
	
	if ( snip )
		snip->setName(name);
	
	addSnippet(snip);
	
	return snip;
}
Example #5
0
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;
}
Example #6
0
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);
}
Example #7
0
bool QSnippetManager::loadSnippetFromFile(const QString& file, const QString& type)
{
	QFile f(file);
	
	if ( !f.open(QFile::ReadOnly | QFile::Text) )
	{
		qWarning("Unable to load snippet from %s", qPrintable(file));
		return false;
	}
	
	//qDebug("loading from : %s", qPrintable(file));
	
	QString s = QString::fromLocal8Bit(f.readAll());
	
	static const QRegExp meta("# name:(\\S+) context:(\\S*)[^\n]*\n");
	
	int idx = meta.indexIn(s);
	bool metaMatch = idx != -1;
	
	if ( metaMatch )
	{
		//qDebug("meta! : %i => %s", idx, qPrintable(meta.cap(0)));
		s.remove(idx, meta.matchedLength());
	}
	
	if ( s.endsWith('\n') )
		s.chop(1);
	
	bool ok = loadSnippetFromString(metaMatch ? meta.cap(1) : QFileInfo(file).baseName(), s, type);
	
	if ( ok )
	{
		QSnippet *snip = m_snippets.last();
		snip->setContexts(metaMatch ? meta.cap(2).split(",") : QStringList("all"));
	}
	
	return ok;
}
Example #8
0
void QSnippetEdit::on_tbCreateSnippet_clicked()
{
	QString name = leSnippetName->text();
	QString pattern = eSnippet->text();
	QStringList contexts = leSnippetScope->text().split(_cxt_splitter);
	
	if ( pattern.endsWith('\n') )
		pattern.chop(1);
	
	if ( name.isEmpty() || pattern.isEmpty() )
	{
		QMessageBox::information(0, tr("Missing data"), tr("Please provide a name and a content to create a new snippet"));
		return;
	}
	
	// TODO : allow pattern loader choice...
	bool ok = m_manager->loadSnippetFromString(name, pattern, "Simple");
	
	if ( !ok )
	{
		QMessageBox::warning(0, tr("Error"), tr("Invalid snippet pattern."));
		return;
	}
	
	eSnippet->setText(QString());
	leSnippetScope->clear();
	leSnippetName->clear();
	
	
	QSnippet *snip = m_manager->snippet(m_manager->snippetCount() - 1);
	//snip->setName(name);
	snip->setContexts(contexts);
	
	
	lwSnippets->setCurrentRow(0);
}