示例#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();
}
示例#2
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;
}