/*************************************************************************** editor.cpp - description ------------------- begin : Sun Mar 26 2000 copyright : (C) 2000 by Alexander Theel email : alex.theel@gmx.net ***************************************************************************/ /*************************************************************************** * * * 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. * * * ***************************************************************************/ #include "editor.h" #include #include #include "../../CTuxCardsConfiguration.h" #include // ------------------------------------------------------------------------------- Editor::Editor( QWidget *pParent, const char *pName ) : QTextEdit( pParent, pName ) , mpActiveElement( NULLPTR ) , SEMAPHORE_TEXT_WAS_SET( FALSE ) , meLinebreakMode( LINEBREAK_CLASSIC ) // ------------------------------------------------------------------------------- { initialize(); } // ------------------------------------------------------------------------------- Editor::~Editor( void ) // ------------------------------------------------------------------------------- { mpActiveElement = NULLPTR; } // ************** IView ********************************************************* // ------------------------------------------------------------------------------- void Editor::aboutToRemoveElement( CInformationElement* pIE ) // ------------------------------------------------------------------------------- { if ( mpActiveElement == pIE ) { mpActiveElement = NULLPTR; } } // ************** IView - End **************************************************** /** * returns the complete Text */ // ------------------------------------------------------------------------------- QString Editor::getText( void ) // ------------------------------------------------------------------------------- { return text(); } // ------------------------------------------------------------------------------- void Editor::setText( QString text ) // ------------------------------------------------------------------------------- { SEMAPHORE_TEXT_WAS_SET = TRUE; QTextEdit::setText(text); } /** * Rereads the information from the currently active * informationElement; usually in course of the event * 'CInformationElement::informationHasChanged()'. */ // ------------------------------------------------------------------------------- void Editor::rereadInformation( void ) // ------------------------------------------------------------------------------- { if ( NULLPTR == mpActiveElement ) return; setText( mpActiveElement->getInformation() ); } // ------------------------------------------------------------------------------- void Editor::clear( void ) // ------------------------------------------------------------------------------- { initialize(); } // ------------------------------------------------------------------------------- void Editor::initialize( void ) // ------------------------------------------------------------------------------- { setText(""); mpActiveElement = NULLPTR; SEMAPHORE_TEXT_WAS_SET = FALSE; connect( this, SIGNAL(textChanged()), this, SLOT(sendUndoAvailableSignal())); connect( this, SIGNAL(textChanged()), this, SLOT(sendRedoAvailableSignal())); } /** * This method sends the signal 'undoAvailable()' because the base * class (qtextedit qt-3.0.5) does not send it correctly. But the * signal is not send whenever the text was set with 'setText()' * method to prevent that the user gets the text from another * document while doing an undo. */ // ------------------------------------------------------------------------------- void Editor::sendUndoAvailableSignal( void ) // ------------------------------------------------------------------------------- { if ( isUndoAvailable() ) { if ( SEMAPHORE_TEXT_WAS_SET ) SEMAPHORE_TEXT_WAS_SET = FALSE; else emit undoAvailable( TRUE ); } } /** * Similar to the method above ('sendUndoAvailableSignal()'). * This method sends the signal 'redoAvailable()' because the base * class (qtextedit qt-3.0.5) does not send it correctly. */ // ------------------------------------------------------------------------------- void Editor::sendRedoAvailableSignal( void ) // ------------------------------------------------------------------------------- { if ( isRedoAvailable() ) emit redoAvailable(true); } // ------------------------------------------------------------------------------- void Editor::writeCurrentTextToActiveInformationElement( void ) // ------------------------------------------------------------------------------- { if ( !mpActiveElement ) return; // saving cursor- and scrollbarPositions // int line, pos; getCursorPosition(&line, &pos); // int verticalScrollBarPosition = verticalScrollBar()->value(); // int horizontalScrollBarPosition = horizontalScrollBar()->value(); // cout<<"vorher: Text "<")<setInformation(getText()); // cout<<"nachher: Text "<")<setValue(verticalScrollBarPosition); // horizontalScrollBar()->setValue(horizontalScrollBarPosition); } // ------------------------------------------------------------------------------- void Editor::setWordWrap( int wordWrap ) // ------------------------------------------------------------------------------- { if( wordWrap == 0 ) QTextEdit::setWordWrap(QTextEdit::NoWrap); else if( wordWrap == 1 ) QTextEdit::setWordWrap(QTextEdit::WidgetWidth); else { QTextEdit::setWordWrap(QTextEdit::FixedColumnWidth); QTextEdit::setWrapColumnOrWidth(wordWrap); } } /** * Ignores the key combinations we need within 'mainwindow'. */ // ------------------------------------------------------------------------------- void Editor::keyPressEvent( QKeyEvent* pKeyEv ) // ------------------------------------------------------------------------------- { if ( !pKeyEv ) return; //std::cout<<"key = "<key(); // If // CTRL + S (save) or // ALT + left cursor (history one back / previous entry) or // ALT + right cursor (history one forward / next entry) // CTRL + F (shortcut for "Searching") // is called, then ignore it. if( ( (pKeyEv->state() == ControlButton) && (pKeyEv->key() == Key_S) ) || ( (pKeyEv->state() == AltButton) && (pKeyEv->key() == Key_Left) ) || ( (pKeyEv->state() == AltButton) && (pKeyEv->key() == Key_Right) ) || ( (ControlButton == pKeyEv->state()) && (Qt::Key_F == pKeyEv->key()) ) ) { //std::cout<<" -> ignore"<ignore(); } // if the shift button is down while pressing return or enter, insert a new paragraph else if ( ( (ShiftButton == pKeyEv->state()) || (ControlButton == pKeyEv->state()) ) && ( (Qt::Key_Return == pKeyEv->key()) || (Qt::Key_Enter == pKeyEv->key()) ) ) { if ( LINEBREAK_MODERN == meLinebreakMode ) { //std::cout<<" -> shift/ctrl + enter/return"<type(), Qt::Key_Return, pKeyEv->ascii(), 0 ) ); } else { QTextEdit::keyPressEvent( pKeyEv ); } } // if a simple return or enter is pressed, do not insert a new paragraph else if ( (Qt::Key_Return == pKeyEv->key()) || (Qt::Key_Enter == pKeyEv->key()) ) { if ( LINEBREAK_MODERN == meLinebreakMode ) { //std::cout<<" -> simple enter/return"<type(), Qt::Key_Return, pKeyEv->ascii(), ControlButton ) ); } else { QTextEdit::keyPressEvent( pKeyEv ); } } /*else if ( (pKeyEv->state() == ShiftButton) && (pKeyEv->key() == Qt::Key_Return) ) { QTextEdit::keyPressEvent( new QKeyEvent( pKeyEv->type(), Qt::Key_Enter, pKeyEv->ascii(), ControlButton ) ); } */ // CTRL + A -> select all else if ( (ControlButton == pKeyEv->state()) && (Qt::Key_A == pKeyEv->key()) ) { selectAll(); } // CTRL + B -> bold else if ( (ControlButton == pKeyEv->state()) && (Qt::Key_B == pKeyEv->key()) ) { if ( Qt::RichText == textFormat() ) setBold( !bold() ); } // CTRL + I -> italic else if ( (ControlButton == pKeyEv->state()) && (Qt::Key_I == pKeyEv->key()) ) { if ( Qt::RichText == textFormat() ) setItalic( !italic() ); } // CTRL + U -> underline else if ( (ControlButton == pKeyEv->state()) && (Qt::Key_U == pKeyEv->key()) ) { if ( Qt::RichText == textFormat() ) setUnderline( !underline() ); } else { // otherwise call the super-method //std::cout<setPixmap("newImg", QPixmap("/home/alex/aufgehoben/bilder/alex.gif")); setText( " Hier ist ein Bild ." ); return; */ if ( !pElement ) return; //std::cout<<"Editor::activeInformationElementChanged()"<getDescription()<setInformationYPos( contentsY() ); } if ( pElement->getInformationFormat() == &InformationFormat::RTF ) { emit formatRecognized( InformationFormat::RTF ); setTextFormat( Qt::RichText ); } else if ( pElement->getInformationFormat() == &InformationFormat::ASCII ) { emit formatRecognized( InformationFormat::ASCII ); setTextFormat( Qt::PlainText ); } setText( pElement->getInformation() ); sync(); setContentsPos( 0, pElement->getInformationYPos() ); mpActiveElement = pElement; connect( mpActiveElement, SIGNAL(informationHasChanged()), this, SLOT(rereadInformation()) ); } // ------------------------------------------------------------------------------- // If a new entry ('pNewIE') is selected and if autoencryption is turned on, the // currently active entry ('pActiveIE') is encrypted. // // The element 'pNewIE' is the new selected one. It is needed for comparision with // the active one. If both are the same elements nothing is done within this // method. // ------------------------------------------------------------------------------- void Editor::autoEncryptActiveInformationElement( CInformationElement* pActiveIE, CInformationElement* pNewIE ) // ------------------------------------------------------------------------------- { if ( !pActiveIE || !pNewIE ) return; if ( pActiveIE == pNewIE ) return; //std::cout<<"autoencrypt .. "; if ( CTuxCardsConfiguration::getInstance().getBoolValue( CTuxCardsConfiguration::B_AUTOENCRYPT_ENTRY ) ) { if ( pActiveIE->isEncryptionEnabled() ) { if ( !pActiveIE->isCurrentlyEncrypted() ) { //std::cout<<"(encrypt) .. "; pActiveIE->encrypt(); } } } //std::cout<<"done."<getInformation().contains("
"); } // ------------------------------------------------------------------------------- void Editor::setLinebreakMode( eLinebreakMode eMode ) // ------------------------------------------------------------------------------- { meLinebreakMode = eMode; } /** * Override the regular copy() methode, so that an enter within * a richtext element (separating paragraphs) is represented by * two \n. */ /*void Editor::copy(){ if ( textFormat() == Qt::PlainText ) { QTextEdit::copy(); return; } QString text = selectedText();//mpActiveElement->getInformation(); text = text.replace(QRegExp("\n"), "\n\n"); QClipboard *cb = QApplication::clipboard(); cb->setText( text ); } */ /** * Override the regular paste() methode, so that lines are * not separated by each other with an blank line. */ // ------------------------------------------------------------------------------- void Editor::paste() // ------------------------------------------------------------------------------- { //std::cout<<"Editor::paste()"<text(); if (text) { // do not create new paragraphs text = text.replace(QRegExp("\n"), QChar( 0x2028)); insert( text, false, false); } } else { std::cout<<"\tPlaintext detected"<data(mode)->provides("application/x-qrichtext")) ) { QString text = pCb->text(mode); if (text) { // do not create new paragraphs text = text.replace(QRegExp("\n"), QChar( 0x2028)); pCb->setText( text, mode ); } } }