editorpage.cpp

来自「This a source insight software in Linux.」· C++ 代码 · 共 656 行 · 第 1/2 页

CPP
656
字号
/*************************************************************************** * * Copyright (C) 2005 Elad Lahav (elad_lahav@users.sourceforge.net) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. *  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ***************************************************************************/#include <qfileinfo.h>#include <kdeversion.h>#include <ktexteditor/selectioninterface.h>#include <ktexteditor/viewcursorinterface.h>#include <ktexteditor/popupmenuinterface.h>#include <ktexteditor/editinterface.h>#include <kate/document.h>#include <kate/view.h>#include "editorpage.h"#include "kscopeconfig.h"/** * Class constructor. * @param	pDoc	The document object associated with this page * @param	pMenu	A Cscope queries popup menu to use with the editor * @param	pParent	The parent widget * @param	szName	The widget's name */EditorPage::EditorPage(KTextEditor::Document* pDoc, QPopupMenu* pMenu,	QTabWidget* pParent, const char* szName) : QHBox(pParent, szName),	m_pParentTab(pParent),	m_pDoc(pDoc),	m_bOpen(false),	m_bNewFile(false),	m_sName(""),	m_bWritable(true), /* new documents are writable by default */	m_bModified(false),	m_nLine(0),	m_bSaveNewSizes(false){	KTextEditor::PopupMenuInterface* pMenuIf;	KTextEditor::ViewCursorInterface* pCursorIf;		// Create code-completion objects (will be deleted by QObject destructor)	m_pCompletion = new SymbolCompletion(this, this);		// Set read-only mode, if required	if (Config().getReadOnlyMode())		m_pDoc->setReadWrite(false);		// Create the child widgets	m_pSplit = new QSplitter(this);	m_pCtagsList = new CtagsList(m_pSplit);	m_pView = m_pDoc->createView(m_pSplit);	m_pSplit->setResizeMode(m_pCtagsList, QSplitter::KeepSize);		// Perform tasks only when the document has been loaded completely	connect(m_pDoc, SIGNAL(completed()), this, SLOT(slotFileOpened()));		// Be notified when the text in the editor changes	connect(m_pDoc, SIGNAL(textChanged()), this, SLOT(slotSetModified()));	connect(m_pDoc, SIGNAL(undoChanged()), this, SLOT(slotUndoChanged()));		// Store the sizes of the child windows when the tag list is resized	// (since it may imply a move of the splitter divider)	connect(m_pCtagsList, SIGNAL(resized()), this, SLOT(slotChildResized()));	// Go to a symbol's line if it is selected in the tag list	connect(m_pCtagsList, SIGNAL(lineRequested(uint)), this,		SLOT(slotGotoLine(uint)));	// Add Ctag records to the tag list	connect(&m_ctags, SIGNAL(dataReady(FrontendToken*)), m_pCtagsList,		SLOT(slotDataReady(FrontendToken*)));			// Monitor Ctags' operation	connect(&m_ctags, SIGNAL(finished(uint)), m_pCtagsList, 		SLOT(slotCtagsFinished(uint)));			// Set the context menu	pMenuIf = dynamic_cast<KTextEditor::PopupMenuInterface*>(m_pView);	if (pMenuIf)		pMenuIf->installPopup(pMenu);	// Emit a signal whenever the cursor's position changes	pCursorIf = dynamic_cast<KTextEditor::ViewCursorInterface*>(m_pView);	if (pCursorIf) {		connect(m_pView, SIGNAL(cursorPositionChanged()), this,			SLOT(slotCursorPosChange()));	}}/** * Class destructor. */EditorPage::~EditorPage(){}/** * Returns a pointer to the editor document object embedded in this page. * @returns	the document pointer */KTextEditor::Document* EditorPage::getDocument(){	return m_pDoc;}/** * Returns a pointer to the editor view object embedded in this page. * @returns	the view pointer */KTextEditor::View* EditorPage::getView(){	return m_pView;}/**  * Returns the full path of the file being edited. * @return	The path of the file associated with the Document object, empty  *			string if no file is currently open */QString EditorPage::getFilePath(){	return m_pDoc->url().path();}/**  * Returns the name of the file being edited. * @return	The name of the file associated with the Document object, empty  *			string if no file is currently open */QString EditorPage::getFileName(){	return m_sName;}/** * Determines whether this file can be modified, according to the file-system * permissions, and KScope's global settings. * @return	true if this document can be changed, false otherwise */bool EditorPage::isWritable(){	// Check global settings first	if (Config().getReadOnlyMode())		return false;		// Return FS write permissions	return m_bWritable;}/** * Determines if the file edited in this page was modified, and the changes * were not yet saved. * @return	true if the file was modified, false otherwise */bool EditorPage::isModified(){	return m_pDoc->isModified();}/** * Opens a file for editing. * @param	sFileName	The full path name of the file to edit. */void EditorPage::open(const QString& sFileName){	// Open the given file	m_bOpen = false;	m_pDoc->openURL(sFileName);}/** * Marks the page as containing a new unnamed file. */void EditorPage::setNewFile(){	m_bNewFile = true;	emit newFile(this);}/** * Saves the edited file. */void EditorPage::save(){	if (m_pDoc->isModified())		m_pDoc->save();}/** * Closes an edited file. * @param	bForce	true to close the file regardless of any modifications,  *			false to prompt the user in case of unsaved chnages  * @return	true if the file has been closed, false if the user has aborted */bool EditorPage::close(bool bForce){	// To override the prompt-on-close behaviour, we need to mark the file	// as unmodified	if (bForce)		m_pDoc->setModified(false);		// Close the file, unless the user aborts the action	return m_pDoc->closeURL();}/** * Applies any changes to the user preferences concerning an editor window. */void EditorPage::applyPrefs(){	// Determine whether the editor should work in a read-only mode	if (m_bWritable)		m_pDoc->setReadWrite(!Config().getReadOnlyMode());		// Apply preferences to the tag list of this window	m_pCtagsList->applyPrefs();}/** * Sets the keyboard focus to the editor part of the page. * This method is called whenever the page is activated. It is more reasonable * to set the focus to the editor than to the tag list. */void EditorPage::setEditorFocus(){	m_pView->setFocus();	slotCursorPosChange();}/** * Returns the currently selected text in an open file. * @return	The selected text, or a null string if no text is currently * 			selected */QString EditorPage::getSelection(){	KTextEditor::SelectionInterface* pSelect;	// Get the selected text	pSelect = dynamic_cast<KTextEditor::SelectionInterface*>(m_pDoc);	if (!pSelect || !pSelect->hasSelection())		return QString::null;	// Return the selected text	return pSelect->selection();}/** * Returns a the complete word defined by the current cursor position. * Attempts to extract a valid C symbol from the location of the cursor, by * starting at the current line and column, and looking forward and backward * for non-symbol characters. * @return	A C symbol under the cursor, if any, or QString::null otherwise */QString EditorPage::getWordUnderCursor(uint* pPosInWord){	KTextEditor::ViewCursorInterface* pCursor;	KTextEditor::EditInterface* pEditIf;	QString sLine;	uint nLine, nCol, nFrom, nTo, nLast, nLength;	QChar ch;	// Get a cursor object	pCursor = dynamic_cast<KTextEditor::ViewCursorInterface*>(m_pView);	if (pCursor == NULL)		return QString::null;	// Get a pointer to the edit interface		pEditIf = dynamic_cast<KTextEditor::EditInterface*>(m_pDoc);	if (!pEditIf)		return QString::null;		// Get the line on which the cursor is positioned	pCursor->cursorPositionReal(&nLine, &nCol);	sLine = pEditIf->textLine(nLine);		// Find the beginning of the current word	for (nFrom = nCol; nFrom > 0;) {		ch = sLine.at(nFrom - 1);		if (!ch.isLetter() && !ch.isDigit() && ch != '_')			break;				nFrom--;	}		// Find the end of the current word	nLast = sLine.length();	for (nTo = nCol; nTo < nLast;) {		ch = sLine.at(nTo);		if (!ch.isLetter() && !ch.isDigit() && ch != '_')			break;				nTo++;	}		// Mark empty words	nLength = nTo - nFrom;	if (nLength == 0)		return QString::null;		// Return the in-word position, if required	if (pPosInWord != NULL)		*pPosInWord = nCol - nFrom;		// Extract the word under the cursor from the entire line	return sLine.mid(nFrom, nLength);}/**

⌨️ 快捷键说明

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