Пример #1
0
bool pEditor::openFile( const QString& fileName, const QString& codec )
{
    /*if ( isModified() )
        return false;*/

    QApplication::setOverrideCursor( Qt::WaitCursor );
    
    // open file
    QFile f( fileName );
    if ( !f.open( QFile::ReadOnly ) )
    {
        MonkeyCore::messageManager()->appendMessage( tr( "Cannot read file %1:\n%2." ).arg( fileName ).arg( f.errorString() ) );
        QApplication::restoreOverrideCursor();
        return false;
    }

    // remember filename
    setProperty( "fileName", fileName );
    setProperty( "codec", codec );

    // set lexer and apis
    setLexer( pMonkeyStudio::lexerForFileName( fileName ) );

    // set properties
    pMonkeyStudio::setEditorProperties( this );

    // load file
    QTextCodec* c = QTextCodec::codecForName( codec.toUtf8() );
    QString datas = c->toUnicode( f.readAll() );
    setText( datas );
    setModified( false );

    // convert tabs if needed
    if ( pMonkeyStudio::convertTabsUponOpen() )
        convertTabs();
    
    //autodetect indent, if need
    if ( pMonkeyStudio::autoDetectIndent() )
    {
        autoDetectIndent ();
    }
    
    //autodetect eol, if need
    if ( pMonkeyStudio::autoDetectEol() )
    {
        autoDetectEol();
    }
    
    // make backup if needed
    if ( pMonkeyStudio::createBackupUponOpen() )
        makeBackup();

    // convert eol
    if ( pMonkeyStudio::autoEolConversion() )
        convertEols( eolMode() );
    
    QApplication::restoreOverrideCursor();
    
    return true;
}
Пример #2
0
void DocumentEditor::setAutoDetectIndent(bool enable_) {
	_autoDetectIndent = enable_;
	if(_autoDetectIndent)
		autoDetectIndent();
}
Пример #3
0
bool DocumentEditor::load(const QString &fileName_) {
	QFile file(fileName_);
	if (!file.open(QFile::ReadOnly)) {
		QMessageBox::warning(this, PACKAGE_NAME,
							 tr("Cannot read file %1:\n%2.")
							 .arg(fileName_)
							 .arg(file.errorString()));
		return false;
	}

	///@todo better charset detection
	char bom[6];
	file.read(bom, 6);
	file.reset();
	detectBOM(bom);

	QString shebang;
	for(uint8_t i = 0; i < 5; i++){
		if(bom[i] == '#'){
			if(bom[i+1] == '!'){
				char line[80];
				file.readLine(line, 80);
				file.reset();
				shebang = line;
			}
		}
	}

	QTextCodec* codec = QTextCodec::codecForName(_codec.toUtf8());
	if(codec == 0) {
		QMessageBox::critical(this, PACKAGE_NAME,
							  tr("Cannot load file %1:\nUnsupported charset %2 !!")
							  .arg(fileName_).arg(_codec));
		return false;
	}

	QApplication::setOverrideCursor(Qt::WaitCursor);

	QString data = codec->toUnicode(file.readAll());
	setText(data);

	_fullPath = fileName_;
	_isNew = false;
	setModified(false);

	if(_autoDetectEol)
		autoDetectEol();
	if(_autoDetectIndent)
		autoDetectIndent();

	//add lexer
	QsciLexer* l = lexer();
	//detach lexer from document before delete it
	setLexer(0);
	if(l != 0) {
		delete l;
		l = 0;
	}
	setLexer(LexerManager::getInstance().getAutoLexer(this, shebang));

	//reload settings for lexer
	Settings settings;
	settings.applyToDocument(this);

	//add it to the watcher
	_watcher.addPath(_fullPath);

	QApplication::restoreOverrideCursor();
	_notified = true;

	return true;
}