⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textedit.cpp

📁 用Qt4编写的linux IDE开发环境
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    lastModified = QFileInfo( file ).lastModified();    file.close();    if ( m_lineNumbers )        m_lineNumbers->setDigitNumbers( QString::number(linesCount()).length() );    if ( m_completion  && !m_mainImpl->buildQtDatabase() )    {        emit initParse(InitCompletion::Completion, m_editor->filename(), toPlainText(), true, false, false, QString());    }    QApplication::restoreOverrideCursor();    return true;}//void TextEdit::activateLineNumbers(bool activate){    if ( activate && m_lineNumbers==0 )        setLineNumbers( new LineNumbers(this, m_editor) );    else if ( !activate && m_lineNumbers )        setLineNumbers( 0 );}//void TextEdit::setSelectionBorder(bool activate){    if ( activate && m_selectionBorder==0 )        setSelectionBorder( new SelectionBorder(this) );    else if ( !activate && m_selectionBorder )        setSelectionBorder( (SelectionBorder*)0 );}//void TextEdit::autobrackets(){    textCursor().insertText( "\n" );    autoIndent();    textCursor().insertText( "\n" );    textCursor().insertText( "}" );    autoUnindent();    setTextCursor( getLineCursor(currentLineNumber()-1) );    QTextCursor cursor = textCursor();    cursor.movePosition(QTextCursor::EndOfLine );    setTextCursor( cursor );}//void TextEdit::autoIndent(){    QTextBlock blocAIndenter;    QTextBlock b = textCursor().block();    if (  b.previous().isValid() && !b.previous().text().isEmpty() )        blocAIndenter = b;    else        return;    QTextBlock blocAvant = blocAIndenter.previous();    if ( !blocAvant.isValid() )        return;    QString simple = blocAvant.text().simplified();    QString blancs;    for (int i=0; i< blocAvant.text().length(); i++)    {        if ( blocAvant.text().at(i) == ' ' || blocAvant.text().at(i) == '\t' )        {            QString s = blocAvant.text().at(i);            if ( m_tabSpaces )            {                int nbSpaces = tabStopWidth() / fontMetrics().width( " " );                QString spaces;                for (int i = 0; i<nbSpaces; i++)                    spaces += " " ;                s.replace("\t", spaces);            }            blancs += s;        }        else            break;    }    if ( simple.simplified().length() && ((simple.contains("(") && simple.contains(")")                                           && QString("if:while:do:switch:foreach").contains( simple.section("(", 0, 0).simplified() )                                           && (simple.contains('{') || simple.right(1) != ";" ) )                                          || QString("else:case:default").indexOf( simple.simplified() ) == 0                                          || simple.simplified().at(0) == '{' || simple.simplified().at( simple.simplified().length()-1 ) == '{' ))    {        if ( m_tabSpaces )        {            int nbSpaces = tabStopWidth() / fontMetrics().width( " " );            for (int i = 0; i<nbSpaces; i++)                blancs +=  " " ;        }        else            blancs += "\t";    }    textCursor().insertText( blancs );    return;}//void TextEdit::comment(ActionComment action){    // Trent's implementation    QTextCursor cursor = textCursor();		//when there is no selection startPos and endPos are equal to position()    int startPos = cursor.selectionStart();    int endPos = cursor.selectionEnd();	QTextBlock startBlock = document()->findBlock(startPos);    QTextBlock endBlock = document()->findBlock(endPos);    	//special case : the end of the selection is at the beginning of a line	if ( startPos != endPos && cursor.atBlockStart()) {		endBlock = document()->findBlock(endPos).previous();	}		int firstLine = lineNumber( startBlock );    int lastLine = lineNumber( endBlock );    QTextBlock block = startBlock;    cursor.beginEditBlock();    cursor.setPosition(startPos);	while (!(endBlock < block))    {        QString text = block.text();        if (!text.isEmpty()) {	        int i = 0;	        while (i < text.length() && text.at(i).isSpace())	            i++;	        if (action == Comment)	        {	            if (text.mid(i, 2) != "//")	                text.insert(i, "//");	        }	        else if (action == Uncomment)	        {	            if (text.mid(i, 2) == "//")	                text.remove(i, 2);	        }	        else if (action == Toggle)	        {	            if (text.mid(i, 2) == "//")	                text.remove(i, 2);	            else	                text.insert(i, "//");	        }	        cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);	        cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);	        cursor.insertText(text);		}        cursor.movePosition(QTextCursor::NextBlock);        block = cursor.block();    }	cursor.endEditBlock();    // Reselect blocks    selectLines(firstLine, lastLine);}//void TextEdit::autoUnindent(){    QTextBlock b = textCursor().block();    if (  !b.previous().isValid() || b.previous().text().isEmpty() )        return;    QString caractere = b.text().simplified();    if (  caractere!="{" && caractere!="}" )        return;    QTextBlock blocAvant = b.previous();    if ( !blocAvant.isValid() )        return;    QString blancs;    for (int i=0; i< blocAvant.text().length(); i++)    {        if ( blocAvant.text().at(i) == ' ' || blocAvant.text().at(i) == '\t' )            blancs += blocAvant.text().at(i);        else            break;    }    if ( blancs.length() && caractere=="}" && blocAvant.text().simplified()!="{")    {        if ( m_tabSpaces )        {            int nbSpaces = tabStopWidth() / fontMetrics().width( " " );            for (int i = 0; i<nbSpaces; i++)                if ( blancs.at(0) == ' ' )                    blancs.remove(0, 1);        }        else            blancs.remove(0, 1);    }    QTextCursor cursor = textCursor();    cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor );    cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor );    cursor.removeSelectedText();    cursor.insertText(blancs+caractere);    setTextCursor( cursor );}//void TextEdit::setSyntaxHighlight(bool activate ){    if ( activate && cpphighlighter==0 )        cpphighlighter = new CppHighlighter( this->document() );    else if ( !activate && cpphighlighter )    {        delete cpphighlighter;        cpphighlighter = 0;    }}//void TextEdit::setSyntaxColors(QTextCharFormat a, QTextCharFormat b, QTextCharFormat c, QTextCharFormat d, QTextCharFormat e, QTextCharFormat f, QTextCharFormat g){    if (!cpphighlighter)        return;    cpphighlighter->setPreprocessorFormat( a );    cpphighlighter->setClassFormat( b );    cpphighlighter->setSingleLineCommentFormat( c );    cpphighlighter->setMultiLineCommentFormat( d );    cpphighlighter->setQuotationFormat( e );    cpphighlighter->setFunctionFormat( f );    cpphighlighter->setKeywordFormat( g );    cpphighlighter->setDocument( document() );}bool TextEdit::close(QString filename){    if ( document()->isModified() )    {        // Proposer sauvegarde        int rep = QMessageBox::question(this, "QDevelop",                                        tr("Save \"%1\"").arg(filename), tr("Yes"), tr("No"), tr("Cancel"), 0, 2 );        if ( rep == 2 )            return false;        if ( rep == 0 )        {            QDateTime info;            return save(filename, info);        }    }    return true;}//bool TextEdit::save(QString filename, QDateTime &lastModified){    if ( !document()->isModified() )        return true;    QFile file( filename );    if (!file.open(QIODevice::WriteOnly))    {        QMessageBox::about(0, "QDevelop",tr("Unable to save %1").arg(filename));        return false;    }    QApplication::setOverrideCursor(Qt::WaitCursor);    QString s = toPlainText();    if ( m_endLine != MainImpl::Default )    {        s.replace("\r\n", "\n");        if ( m_endLine == MainImpl::Windows )            s.replace("\n", "\r\n");    }    int mib = m_mainImpl->mibCodec();    QTextCodec *codec = QTextCodec::codecForMib(mib);    QTextStream out(&file);    out.setCodec(codec);    out << s;    file.close();    QFile last( filename );    lastModified = QFileInfo( last ).lastModified();    QApplication::restoreOverrideCursor();    document()->setModified( false );    return true;}//void TextEdit::setMouseHidden( bool hidden ){	if ( hidden == m_mouseHidden )		return;	viewport()->setCursor( hidden ? Qt::BlankCursor : Qt::IBeamCursor );	setMouseTracking( hidden );	m_mouseHidden = hidden;}//void TextEdit::resizeEvent( QResizeEvent* e ){    QTextEdit::resizeEvent( e );    QRect margeNumerotationGeometry;    if ( m_lineNumbers )    {        margeNumerotationGeometry = QRect( viewport()->geometry().topLeft(), QSize( m_lineNumbers->width(), viewport()->height() ) );        margeNumerotationGeometry.moveLeft( margeNumerotationGeometry.left() -m_lineNumbers->width() );        if ( m_selectionBorder )            margeNumerotationGeometry.moveLeft( margeNumerotationGeometry.left() -m_selectionBorder->width() );        if ( m_lineNumbers->geometry() != margeNumerotationGeometry )            m_lineNumbers->setGeometry( margeNumerotationGeometry );    }    else    {        margeNumerotationGeometry.setTopRight( viewport()->geometry().topLeft() );    }    QRect margeSelectionGeometry;    if ( m_selectionBorder )    {        margeSelectionGeometry = QRect(  margeNumerotationGeometry.topRight(), QSize( m_selectionBorder->width(), viewport()->height() ) );        if ( m_lineNumbers )            margeSelectionGeometry.moveLeft( m_lineNumbers->width() );        else            margeSelectionGeometry.moveLeft( margeSelectionGeometry.left() -m_selectionBorder->width() );        if ( m_selectionBorder->geometry() != margeSelectionGeometry )            m_selectionBorder->setGeometry( margeSelectionGeometry );    }}QTextCursor TextEdit::getLineCursor( int line ) const{    int count = 1;    for ( QTextBlock b = document()->begin(); b.isValid(); b = b.next(), count++ )    {        if ( count == line )        {            return QTextCursor( b );            break;        }    }    QTextCursor c = textCursor();    c.movePosition( QTextCursor::End );    c.movePosition( QTextCursor::StartOfLine );    return c;}//void TextEdit::dialogGotoLine(){    QDialog *dial = new QDialog;    Ui::GotoLine ui;    ui.setupUi(dial);    ui.horizontalSlider->setMaximum( linesCount()-1 );    ui.horizontalSlider->setPageStep( (linesCount()-1)/10 );    ui.spinBox->setMaximum( linesCount()-1 );    ui.spinBox->setValue( currentLineNumber() );    ui.spinBox->selectAll();    ui.spinBox->setFocus();    if ( dial->exec() == QDialog::Accepted )        gotoLine( ui.spinBox->value(), true );    delete dial;}//void TextEdit::gotoLine( int line, bool moveTop ){    if ( moveTop )        setTextCursor( getLineCursor( linesCount() ) );    setTextCursor( getLineCursor( line ) );    setFocus( Qt::OtherFocusReason );    //    ensureCursorVisible();    if ( moveTop )    {        QTextCursor cursor = textCursor();        if ( cursor.isNull() )            return;        QTextCursor c = textCursor();        bool mouvementReussi;        do        {            mouvementReussi = c.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, 1 );            setTextCursor( c );            ensureCursorVisible();        }        while (mouvementReussi && cursorRect(cursor).y() > 25 );        cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);        cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor, 1 );        setTextCursor( cursor );    }}//void TextEdit::setLineNumbers( LineNumbers* g ){    if ( g == m_lineNumbers )        return;    if ( m_lineNumbers )        delete m_lineNumbers;    m_lineNumbers = g;    connect( m_mainImpl, SIGNAL( resetExecutedLine() ), m_lineNumbers, SLOT( slotResetExecutedLine() ) );    connect(m_lineNumbers, SIGNAL( digitNumbersChanged() ), this, SLOT( slotAdjustSize() ) );    int margeGaucheSelection = 0;    if ( m_selectionBorder )        margeGaucheSelection = m_selectionBorder->width();    if ( m_lineNumbers )    {        setViewportMargins( m_lineNumbers->width()+margeGaucheSelection, 0, 0, 0 );        if ( !m_lineNumbers->isVisible() )            m_lineNumbers->show();    }    else        setViewportMargins( margeGaucheSelection, 0, 0, 0 );}//void TextEdit::slotAdjustSize(){    int margeGaucheSelection = 0;    int margeGaucheMargeNumerotation = 0;    if ( m_selectionBorder )        margeGaucheSelection = m_selectionBorder->width();    if ( m_lineNumbers )        margeGaucheMargeNumerotation = m_lineNumbers->width();    setViewportMargins( margeGaucheMargeNumerotation+margeGaucheSelection, 0, 0, 0 );}//LineNumbers* TextEdit::lineNumbers(){    return m_lineNumbers;}//void TextEdit::setSelectionBorder( SelectionBorder* m ){    if ( m == m_selectionBorder )        return;    if ( m_selectionBorder )        delete m_selectionBorder;    m_selectionBorder = m;    int margeGaucheMargeNumerotation = 0;    if ( m_lineNumbers )        margeGaucheMargeNumerotation = m_lineNumbers->width();    if ( m_selectionBorder )    {        setViewportMargins( m_selectionBorder->width()+margeGaucheMargeNumerotation, 0, 0, 0 );        if ( !m_selectionBorder->isVisible() )            m_selectionBorder->show();    }    else    {        setViewportMargins( margeGaucheMargeNumerotation, 0, 0, 0);    }}//SelectionBorder* TextEdit::selectionBorder(){    return m_selectionBorder;}//void TextEdit::findText(){    m_editor->find();}//void TextEdit::gotoMatchingBracket(){    int pos;    QTextCursor cursor = textCursor();    if ( cursor.position() == m_matchingBegin )        pos = m_matchingEnd;    else        pos = m_matchingBegin;    if ( pos != -1 )    {        cursor.setPosition(pos, QTextCursor::MoveAnchor);        setTextCursor( cursor );    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -