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

📄 editor.cpp

📁 用Qt4编写的linux IDE开发环境
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/** This file is part of QDevelop, an open-source cross-platform IDE* Copyright (C) 2006  Jean-Luc Biord** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA** Contact e-mail: Jean-Luc Biord <jl.biord@free.fr>* Program URL   : http://qdevelop.org**/#define QD qDebug() << __FILE__ << __LINE__ << ":"//#include "editor.h"#include "mainimpl.h"#include "lineedit.h"#include "replaceimpl.h"#include "tabwidget.h"#include "linenumbers.h"#include "logbuild.h"#include <QComboBox>#include <QTextCursor>#include <QTextBlock>#include <QDialog>#include <QKeyEvent>#include <QFile>#include <QTextStream>#include <QMessageBox>#include <QGridLayout>#include <QSpacerItem>#include <QPair>#include <QToolButton>#include <QTimer>#include <QProcess>#include "cpphighlighter.h"#include <QTextDocumentFragment>#include <QFileInfo>#include <QDir>#include <QDebug>Editor::Editor(TabWidget * parent, MainImpl *mainimpl, InitCompletion *completion, QString name)        : QWidget(parent){    m_parent = parent;    m_mainimpl = mainimpl;    m_completion = completion;    m_filename =  name;    int vposLayout = 0;    m_comboClasses = m_comboMethods = 0;    m_otherFileButton = m_refreshButton = 0;    m_editorToolbar = 0;    m_textEdit = new TextEdit(this, mainimpl, completion);    m_backward = false;    m_activeEditor = false;    m_nameOtherFile = "";    //    QGridLayout *gridLayout = new QGridLayout(this);    gridLayout->setSpacing(0);    gridLayout->setMargin(0);    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));    if ( suffixe( m_filename ).toLower() == "cpp" || suffixe( m_filename ).toLower() == "cc" || suffixe( m_filename ).toLower() == "c" ||          suffixe( m_filename ).toLower() == "h" || suffixe( m_filename ).toLower() == "hpp"        )    {        QHBoxLayout *hboxLayout = new QHBoxLayout();        hboxLayout->setSpacing(6);        hboxLayout->setMargin(6);        hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));        //        /*m_maximizedButton = 0;        m_maximizedButton = new QToolButton(this);        m_maximizedButton->setIcon(QIcon(":/divers/images/window_fullscreen.png"));                m_maximizedButton->setToolTip( tr("Show maximized") );        connect(m_maximizedButton, SIGNAL(clicked()), this, SLOT(slotMaximizeButtonClicked()));        hboxLayout->addWidget(m_maximizedButton);*/        //        m_otherFileButton = new QToolButton(this);        connect(m_otherFileButton, SIGNAL(clicked()), this, SLOT(slotOtherFile()));        hboxLayout->addWidget(m_otherFileButton);        //        if  ( (suffixe( m_filename ).toLower() != "h") && (suffixe( m_filename ).toLower() != "hpp") )        {            m_refreshButton = new QToolButton(this);            m_refreshButton->setIcon(QIcon(":/toolbar/images/refresh.png"));            m_refreshButton->setToolTip( tr("Refresh classes and methods lists") );            connect(m_refreshButton, SIGNAL(clicked()), this, SLOT(slotClassesMethodsList()) );            hboxLayout->addWidget(m_refreshButton);            //            m_comboClasses = new QComboBox(this);            m_comboClasses->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);            m_comboClasses->setSizeAdjustPolicy(QComboBox::AdjustToContents);            m_comboClasses->setLineEdit( new LineEdit(m_comboClasses) );            m_comboClasses->setEditable( true );            m_comboClasses->setAutoCompletion( true );            m_comboClasses->setObjectName(QString::fromUtf8("m_comboClasses"));            connect(m_comboClasses, SIGNAL(activated(QString)), this, SLOT(slotComboClasses(QString)) );            hboxLayout->addWidget(m_comboClasses);            //            m_comboMethods = new QComboBox(this);            m_comboMethods->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);            m_comboMethods->setSizeAdjustPolicy(QComboBox::AdjustToContents);            m_comboMethods->setMaximumSize( 500, m_comboMethods->height());            m_comboMethods->setLineEdit( new LineEdit(m_comboClasses) );            m_comboMethods->setEditable( true );            m_comboMethods->setAutoCompletion( true );            m_comboMethods->setMaxVisibleItems( 25 );            m_comboMethods->setObjectName(QString::fromUtf8("comboMethodes"));            m_comboMethods->lineEdit()->setAlignment(Qt::AlignLeft);            connect(m_comboMethods, SIGNAL(activated(int)), this, SLOT(slotComboMethods(int)) );            hboxLayout->addWidget(m_comboMethods);        }        //        QSpacerItem *spacerItem = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);        hboxLayout->addItem(spacerItem);        //        //        updateOtherFile(m_filename); // Update "other file" button        m_otherFileButton->setIcon(QIcon(m_otherFileIcon));        m_editorToolbar = new QWidget( this );        m_editorToolbar->setLayout( hboxLayout );        //gridLayout->addLayout(editorToolbar, vposLayout++, 0, 1, 1);        gridLayout->addWidget(m_editorToolbar, vposLayout++, 0, 1, 1);    }    connect(m_textEdit, SIGNAL(editorModified(bool)), this, SLOT(slotModifiedEditor(bool)) );    //    m_findWidget = new QWidget;    uiFind.setupUi(m_findWidget);    connect(uiFind.toolClose, SIGNAL(clicked()), m_findWidget, SLOT(hide()) );    connect(uiFind.editFind, SIGNAL(textChanged(QString)), this, SLOT(slotFindWidget_textChanged(QString)) );    connect(uiFind.editFind, SIGNAL(returnPressed()), this, SLOT(slotFindNext()) );    connect(uiFind.toolPrevious, SIGNAL(clicked()), this, SLOT(slotFindPrevious()) );    connect(uiFind.toolNext, SIGNAL(clicked()), this, SLOT(slotFindNext()) );    //    autoHideTimer = new QTimer(this);    autoHideTimer->setInterval(5000);    autoHideTimer->setSingleShot(true);    connect(autoHideTimer, SIGNAL(timeout()), m_findWidget, SLOT(hide()));    //    gridLayout->addWidget(m_textEdit, vposLayout++, 0, 1, 1);    gridLayout->addWidget(m_findWidget, vposLayout++, 0, 1, 1);    uiFind.labelWrapped->setVisible(false);    m_findWidget->hide();    //    connect(&m_timerUpdateClasses, SIGNAL(timeout()), this, SLOT(slotTimerUpdateClasses()));    connect(&m_timerCheckLastModified, SIGNAL(timeout()), this, SLOT(slotTimerCheckIfModifiedOutside()));}//void Editor::updateOtherFile(QString currentFile){    if ( !m_otherFileButton ) return;    QString suffix;    QString base = currentFile.left(currentFile.lastIndexOf(".") );    m_nameOtherFile = "";    if ( suffixe( currentFile ).toLower() == "h" || suffixe( currentFile ).toLower() == "hpp" )        suffix = "cpp";    else if ( suffixe( m_filename ).toLower() == "cpp" || suffixe( m_filename ).toLower() == "cc"  || suffixe( m_filename ).toLower() == "c")        suffix = "h";    // if the current file has one of the above suffixes and the other file exists, use it    if (!suffix.isEmpty() && QFileInfo(base+"."+suffix).exists()) {        m_nameOtherFile = base+"."+suffix;        m_otherFileButton->setVisible(true);        m_otherFileButton->setToolTip( tr("Open %1").arg(shortFilename(m_nameOtherFile)) );        m_otherFileIcon = ":/treeview/images/"+suffix+".png";        m_otherFileButton->setIcon(QIcon(m_otherFileIcon));    }    else // otherwise, don't display the button        m_otherFileButton->setVisible(false);    emit otherFileChanged();}//void Editor::setActiveEditor(bool b){    m_activeEditor = b;    if ( b && m_showTreeClasses )    {        m_timerUpdateClasses.start(m_intervalUpdatingClasses);        if ( m_completion )        {            m_completion->disconnect( m_textEdit );            connect(m_completion, SIGNAL(completionList(TagList)), m_textEdit, SLOT(slotCompletionList(TagList)) );            connect(m_completion, SIGNAL(completionHelpList(TagList)), m_textEdit, SLOT(slotCompletionHelpList(TagList)) );        }    }    else    {        slotTimerUpdateClasses();        m_timerUpdateClasses.stop();        if ( m_completion )            m_completion->disconnect( m_textEdit );    }}//void Editor::setShowTreeClasses(bool s){    m_showTreeClasses=s;    if ( !m_showTreeClasses )        m_timerUpdateClasses.stop();}//void Editor::slotTimerUpdateClasses(){    QByteArray array( m_textEdit->toPlainText().toLocal8Bit() );    if ( !array.count() )        return;    char *ptr = array.data();    quint16 check = qChecksum(ptr, array.length());    if ( check != m_checksum )    {        m_checksum = check;        emit updateClasses( filename(), m_textEdit->toPlainText());        m_mainimpl->slotCompile(true);    }    if( m_textEdit->hasFocus() )    	m_textEdit->setMouseHidden( true );    else    	m_textEdit->setMouseHidden( false );}//void Editor::slotTimerCheckIfModifiedOutside(){    QFile file(m_filename);    if ( m_lastModified != QFileInfo( file ).lastModified() )    {        m_timerCheckLastModified.stop();        int rep = QMessageBox::question(this, "QDevelop",                                        tr("The file \"%1\"\nwas modified outside editor.\n\n").arg(m_filename)+                                        tr("What do you want to do?"),                                        tr("Overwrite"), tr("Reload File") );        if ( rep == 0 ) // Overwrite        {            m_textEdit->document()->setModified( true );            save();        }        else if ( rep == 1 ) // Reload        {            open(false);        }        m_timerCheckLastModified.start( 5000 );    }    checkBookmarks();}//void Editor::checkBookmarks(){    QMenu *menu = m_mainimpl->bookmarksMenu();    QList<QAction *> actions = menu->actions();    foreach(QAction *action, actions)    {        Bookmark bookmark = action->data().value<Bookmark>();        QTextBlock block = bookmark.second;        bool found = false;        for ( QTextBlock b = m_textEdit->document()->begin(); b.isValid(); b = b.next() )        {            BlockUserData *blockUserData = (BlockUserData*)block.userData();            if ( block==b && blockUserData && blockUserData->bookmark )            {                found = true;            }        }        if ( !found  )        {            m_mainimpl->toggleBookmark(this, "", false, block);        }    }}//void Editor::clearAllBookmarks(){    int line = 1;    for ( QTextBlock block = m_textEdit->document()->begin(); block.isValid(); block = block.next(), line++ )    {        BlockUserData *blockUserData = (BlockUserData*)block.userData();        if ( blockUserData && blockUserData->bookmark )            toggleBookmark( line );    }}//Editor::~Editor(){}//void Editor::replace(){    ReplaceImpl *dialog = new ReplaceImpl(this, m_textEdit, m_replaceOptions);    dialog->exec();    m_replaceOptions = dialog->replaceOptions();    delete dialog;}//bool Editor::open(bool silentMode){    bool ret = m_textEdit->open(silentMode, m_filename, m_lastModified);    if ( ret && (suffixe( m_filename ).toLower() == "cpp" || suffixe( m_filename ).toLower() == "cc"))        slotClassesMethodsList();    QByteArray array( m_textEdit->toPlainText().toLocal8Bit() );    if ( array.count() )    {        char *ptr = array.data();        quint16 check = qChecksum(ptr, array.length());

⌨️ 快捷键说明

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