📄 textedit.cpp
字号:
QTextCursor c = textCursor(); c.beginEditBlock(); if ( !c.hasSelection() ) { if(indenter) { c.insertText( indentString ); } else { //delete the previous character if it's a space or a tab int p = textCursor().position() - 1; if( p>0 && p< m_plainText.length() ) { QChar s = m_plainText.at( p ); if ( s == '\t' || s == ' ') { c.deletePreviousChar(); } } } c.endEditBlock(); return; } int debut = c.selectionStart(); int fin = c.selectionEnd(); // QTextBlock blocDebut = document()->findBlock(debut); QTextBlock blocFin = document()->findBlock(fin); //special case if ( c.atBlockStart()) { blocFin = document()->findBlock(fin).previous(); } c.clearSelection(); if ( blocDebut == blocFin ) { curseurActuel.insertText( indentString ); setTextCursor( curseurActuel ); c.endEditBlock(); return; } QTextBlock block = blocDebut; while ( block.isValid() && !(blocFin < block) ) { c.setPosition(block.position(), QTextCursor::MoveAnchor); if ( !indenter ) { if ( block.text().count() && (block.text().at(0) == '\t' || block.text().at(0) == ' ') ) c.deleteChar(); } else { c.insertText( indentString ); } setTextCursor( c ); block = block.next(); } int ligneDebut = lineNumber(blocDebut); int ligneFin = lineNumber(blocFin); selectLines(ligneDebut, ligneFin); c.endEditBlock(); }//void TextEdit::slotUnindent(){ slotIndent(false);}//void TextEdit::mouseDoubleClickEvent( QMouseEvent * event ){#if 0 mousePosition = event->pos(); QTextCursor cursor = textCursor(); int pos = cursor.position(); while ( pos>0 && QString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_").contains( m_plainText.at( pos-1 ).toUpper() ) ) pos--; cursor.setPosition(pos, QTextCursor::MoveAnchor); setTextCursor( cursor ); // while ( pos < m_plainText.length() && QString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_").contains( m_plainText.at( pos ).toUpper() ) ) pos++; cursor.setPosition(pos, QTextCursor::KeepAnchor); setTextCursor( cursor );#endif QTextEdit::mouseDoubleClickEvent(event);}//void TextEdit::setExecutedLine(int line){ if ( m_lineNumbers) { m_lineNumbers->setExecutedLine(line); }}//void TextEdit::contextMenuEvent(QContextMenuEvent * e){ mousePosition = e->pos(); m_lineNumber = lineNumber( e->pos() ); QMenu *menu = createStandardContextMenu(); menu->clear(); connect(menu->addAction(QIcon(":/treeview/images/cpp.png"), tr("Goto Implementation")), SIGNAL(triggered()), this, SLOT(slotGotoImplementation()) ); connect(menu->addAction(QIcon(":/treeview/images/h.png"), tr("Goto Declaration")), SIGNAL(triggered()), this, SLOT(slotGotoDeclaration()) );// connect(menu->addAction(QIcon(":/toolbar/images/undo.png"), tr("Undo")), SIGNAL(triggered()), this, SLOT(undo()) );// connect(menu->addAction(QIcon(":/toolbar/images/redo.png"), tr("Redo")), SIGNAL(triggered()), this, SLOT(redo()) ); menu->addSeparator(); connect(menu->addAction(QIcon(":/toolbar/images/editcut.png"), tr("Cut")), SIGNAL(triggered()), this, SLOT(cut()) ); connect(menu->addAction(QIcon(":/toolbar/images/editcopy.png"), tr("Copy")), SIGNAL(triggered()), this, SLOT(copy()) ); connect(menu->addAction(QIcon(":/toolbar/images/editpaste.png"), tr("Paste")), SIGNAL(triggered()), this, SLOT(paste()) ); menu->addSeparator(); connect(menu->addAction(QIcon(":/toolbar/images/indente.png"), tr("Selection Indent")), SIGNAL(triggered()), this, SLOT(slotIndent()) ); connect(menu->addAction(QIcon(":/toolbar/images/desindente.png"), tr("Selection Unindent")), SIGNAL(triggered()), this, SLOT(slotUnindent()) ); QTextCursor cursor = textCursor(); menu->addSeparator(); connect(menu->addAction(tr("Select All")), SIGNAL(triggered()), this, SLOT(selectAll()) ); menu->addSeparator(); connect(menu->addAction(QIcon(":/toolbar/images/find.png"), tr("Find...")), SIGNAL(triggered()), m_editor, SLOT(find()) ); menu->addSeparator(); connect(menu->addAction(QIcon(":/divers/images/bookmark.png"), tr("Toggle Bookmark")), SIGNAL(triggered()), this, SLOT(slotToggleBookmark()) ); connect(menu->addAction(QIcon(":/divers/images/breakpoint.png"), tr("Toggle Breakpoint")), SIGNAL(triggered()), this, SLOT(slotToggleBreakpoint()) ); // // menu->exec(e->globalPos()); delete menu;}//void TextEdit::selectLines(int debut, int fin){ if ( debut > fin ) qSwap( debut, fin); QTextCursor c = textCursor(); c.movePosition(QTextCursor::Start ); c.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, debut-1 ); c.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor, fin-debut+1 ); setTextCursor( c ); ensureCursorVisible();}////QString TextEdit::wordUnderCursor(const QPoint & pos, bool select){ QTextCursor save(textCursor()); QTextCursor cursor; if ( pos.isNull() ) cursor = textCursor(); else cursor = cursorForPosition ( pos ); // int curpos = cursor.position(); while ( curpos>0 && QString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_").contains( m_plainText.at( curpos-1 ).toUpper() ) ) curpos--; cursor.setPosition(curpos, QTextCursor::MoveAnchor); setTextCursor( cursor ); // while ( curpos < m_plainText.length() && QString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_").contains( m_plainText.at( curpos ).toUpper() ) ) curpos++; cursor.setPosition(curpos, QTextCursor::KeepAnchor); QString word = cursor.selectedText().simplified(); // if ( select ) setTextCursor( cursor ); else setTextCursor( save ); return word;}//QString TextEdit::wordUnderCursor(const QString text){ int begin = text.length(); while ( begin>0 && QString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_").contains( text.at( begin-1 ).toUpper() ) ) begin--; // int end = begin; while ( end < text.length() && QString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_").contains( text.at( end ).toUpper() ) ) end++; QString word = text.mid(begin, end-begin); return word;}//QString TextEdit::classNameUnderCursor(const QPoint & pos, bool addThis){ QTextCursor cursor; if ( pos.isNull() ) cursor = textCursor(); else cursor = cursorForPosition ( pos ); QString c = m_plainText.left(cursor.position()); QString classname = m_completion->className(c); if ( classname.isEmpty() && addThis ) { c += " this->"; classname = m_completion->className(c); } return classname;}//int TextEdit::currentLineNumber(QTextCursor cursor){ if( cursor.isNull() ) return lineNumber( textCursor() ); else return lineNumber( cursor );}//int TextEdit::currentLineNumber(QTextBlock block){ int lineNumber = 1; for ( QTextBlock b =document()->begin(); b.isValid(); b = b.next(), lineNumber++) { if ( b == block ) { return lineNumber; } } return -1;}//int TextEdit::lineNumber(QTextCursor cursor){ QTextBlock blocCurseur = cursor.block(); int m_lineNumber = 1; for ( QTextBlock block =document()->begin(); block.isValid() && block != blocCurseur; block = block.next() ) m_lineNumber++; return m_lineNumber++;}//int TextEdit::lineNumber(QTextBlock b){ int m_lineNumber = 1; for ( QTextBlock block =document()->begin(); block.isValid() && block != b; block = block.next() ) m_lineNumber++; return m_lineNumber++;}//int TextEdit::linesCount(){ int line = 1; for ( QTextBlock block = document()->begin(); block.isValid(); block = block.next() ) line++; return line;}//int TextEdit::lineNumber(QPoint point){ return lineNumber( cursorForPosition( point ) );}////void TextEdit::slotToggleBookmark(){ m_editor->toggleBookmark( m_lineNumber ); m_lineNumbers->update();}////void TextEdit::slotToggleBreakpoint(){ m_editor->toggleBreakpoint( m_lineNumber, QString(), true ); m_lineNumbers->update();}void TextEdit::insertText(QString text, int insertAfterLine){ if ( m_tabSpaces ) { int nbSpaces = tabStopWidth() / fontMetrics().width( " " ); QString spaces; for (int i = 0; i<nbSpaces; i++) spaces += " " ; text.replace("\t", spaces); } if ( insertAfterLine == -1 ) { QTextCursor c = textCursor(); c.movePosition( QTextCursor::End ); c.movePosition( QTextCursor::EndOfLine ); c.insertText( "\n" + text ); setTextCursor( c ); } else { gotoLine(insertAfterLine, false); textCursor().insertText( text ); }}//void TextEdit::slotGotoImplementation(){ QString classname; // classNameUnderCursor is a long computing. Call only for .cpp files because with .h the result // is always "" if ( m_editor->filename().toLower().endsWith(".cpp") ) classname = classNameUnderCursor(mousePosition, false); QString name = wordUnderCursor(mousePosition); if ( name.isEmpty() ) return; const QList<ParsedItem> *itemsList; itemsList = m_mainImpl->treeClassesItems(); bool found = false; for (int i = 0; i < itemsList->size(); ++i) { ParsedItem parsedItem = itemsList->at( i ); if ( parsedItem.classname == classname && parsedItem.name == name) { if ( !parsedItem.implementation.isEmpty() ) { QString s = parsedItem.implementation; QString filename = s.section("|", 0, 0); int numLine = s.section("|", -1, -1).toInt(); if ( QFileInfo(filename).isFile() ) m_mainImpl->openFile(QStringList(filename) , numLine, false, true); found = true; break; } } /* Below, the item in database has the same filename that the current editor and the same line number. The cursor is on a declaration in a header (.h). Open the implementation (.cpp). */ /* If sender() is not null, this function is called from the context menu. Otherwise from the main menu */ else if (m_editor->filename().toLower().endsWith(".h") && parsedItem.declaration.section("|", 0, 0) == m_editor->filename() && parsedItem.name == name && parsedItem.declaration.section("|", 1, 1).toInt() == currentLineNumber( sender() ? cursorForPosition(mousePosition) : QTextCursor() ) ) { QString s = parsedItem.implementation; QString filename = s.section("|", 0, 0); int numLine = s.section("|", -1, -1).toInt(); if ( QFileInfo(filename).isFile() ) m_mainImpl->openFile(QStringList(filename) , numLine, false, true); found = true; break; } } // Now if the text is an .cpp, find the first name in database with the name "name" // Perhaps return a bad result but it should work many time. if ( !found && m_editor->filename().toLower().endsWith(".cpp")) { for (int i = 0; i < itemsList->size(); ++i) { ParsedItem parsedItem = itemsList->at( i ); if ( parsedItem.name == name ) { QString s = parsedItem.implementation; QString filename = s.section("|", 0, 0); int numLine = s.section("|", -1, -1).toInt(); if ( QFileInfo(filename).isFile() ) m_mainImpl->openFile(QStringList(filename) , numLine, false, true); break; } } }}//void TextEdit::slotGotoDeclaration(){ QString classname = classNameUnderCursor(mousePosition, false); QString name = wordUnderCursor(mousePosition); const QList<ParsedItem> *itemsList = m_mainImpl->treeClassesItems(); bool found = false; for (int i = 0; i < itemsList->size(); ++i) { ParsedItem parsedItem = itemsList->at( i ); if ( parsedItem.classname == classname && parsedItem.name == name) { if ( !parsedItem.declaration.isEmpty() ) { QString s = parsedItem.declaration; QString filename = s.section("|", 0, 0); int numLine = s.section("|", -1, -1).toInt(); if ( QFileInfo(filename).isFile() ) m_mainImpl->openFile(QStringList(filename) , numLine, false, true); found = true; break; } } } if ( !found && m_editor->filename().toLower().endsWith(".cpp")) { for (int i = 0; i < itemsList->size(); ++i) { ParsedItem parsedItem = itemsList->at( i ); if ( parsedItem.name == name ) { QString s = parsedItem.declaration; QString filename = s.section("|", 0, 0); int numLine = s.section("|", -1, -1).toInt(); if ( QFileInfo(filename).isFile() ) m_mainImpl->openFile(QStringList(filename) , numLine, false, true); break; } } }}//void TextEdit::completionHelp(){ if ( !m_completion ) return; QString c = m_plainText.left( textCursor().position() ); if( c.right(1) == "(" ) c = c.left( c.count()-1 ); QString name = wordUnderCursor(c); if( QString(":if:else:for:return:connect:while:do:").contains( name ) ) return; c = c.section(name, 0, 0); emit initParse(InitCompletion::Completion, m_editor->filename(), c, true, true, true, name);}void TextEdit::setTextColor(QColor c){ QPalette p = palette(); p.setColor( QPalette::Text, c ); setPalette(p); viewport()->update();}void TextEdit::saveAsTemp(){ QString filename = m_editor->filename().section(".cpp", 0, 0) + "-qdeveloptmp.cpp"; QFile file( filename ); if (!file.open(QIODevice::WriteOnly)) { return; } QTextStream out(&file); out << toPlainText(); file.close();}QString TextEdit::tempFilename(){ return m_editor->filename().section(".cpp", 0, 0) + "-qdeveloptmp.cpp";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -