src/gui/text/qtextcontrol.cpp
changeset 30 5dc02b23752f
parent 22 79de32ba3296
child 37 758a864f9613
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    89 #endif
    89 #endif
    90 
    90 
    91 QT_BEGIN_NAMESPACE
    91 QT_BEGIN_NAMESPACE
    92 
    92 
    93 #ifndef QT_NO_CONTEXTMENU
    93 #ifndef QT_NO_CONTEXTMENU
    94 #if defined(Q_WS_WIN)
    94 #if defined(Q_WS_WIN) || defined(Q_WS_X11)
    95 extern bool qt_use_rtl_extensions;
    95 extern bool qt_use_rtl_extensions;
    96 #endif
    96 #endif
    97 #endif
    97 #endif
    98 
    98 
    99 // could go into QTextCursor...
    99 // could go into QTextCursor...
   439         QObject::connect(doc, SIGNAL(contentsChanged()), q, SLOT(_q_updateCurrentCharFormatAndSelection()));
   439         QObject::connect(doc, SIGNAL(contentsChanged()), q, SLOT(_q_updateCurrentCharFormatAndSelection()));
   440         QObject::connect(doc, SIGNAL(cursorPositionChanged(QTextCursor)), q, SLOT(_q_emitCursorPosChanged(QTextCursor)));
   440         QObject::connect(doc, SIGNAL(cursorPositionChanged(QTextCursor)), q, SLOT(_q_emitCursorPosChanged(QTextCursor)));
   441         QObject::connect(doc, SIGNAL(documentLayoutChanged()), q, SLOT(_q_documentLayoutChanged()));
   441         QObject::connect(doc, SIGNAL(documentLayoutChanged()), q, SLOT(_q_documentLayoutChanged()));
   442 
   442 
   443         // convenience signal forwards
   443         // convenience signal forwards
   444         QObject::connect(doc, SIGNAL(contentsChanged()), q, SIGNAL(textChanged()));
       
   445         QObject::connect(doc, SIGNAL(undoAvailable(bool)), q, SIGNAL(undoAvailable(bool)));
   444         QObject::connect(doc, SIGNAL(undoAvailable(bool)), q, SIGNAL(undoAvailable(bool)));
   446         QObject::connect(doc, SIGNAL(redoAvailable(bool)), q, SIGNAL(redoAvailable(bool)));
   445         QObject::connect(doc, SIGNAL(redoAvailable(bool)), q, SIGNAL(redoAvailable(bool)));
   447         QObject::connect(doc, SIGNAL(modificationChanged(bool)), q, SIGNAL(modificationChanged(bool)));
   446         QObject::connect(doc, SIGNAL(modificationChanged(bool)), q, SIGNAL(modificationChanged(bool)));
   448         QObject::connect(doc, SIGNAL(blockCountChanged(int)), q, SIGNAL(blockCountChanged(int)));
   447         QObject::connect(doc, SIGNAL(blockCountChanged(int)), q, SIGNAL(blockCountChanged(int)));
   449     }
   448     }
   450 
   449 
   451     bool previousUndoRedoState = doc->isUndoRedoEnabled();
   450     bool previousUndoRedoState = doc->isUndoRedoEnabled();
   452     if (!document)
   451     if (!document)
   453         doc->setUndoRedoEnabled(false);
   452         doc->setUndoRedoEnabled(false);
   454 
   453 
       
   454     //Saving the index save some time.
       
   455     static int contentsChangedIndex = QTextDocument::staticMetaObject.indexOfSignal("contentsChanged()");
       
   456     static int textChangedIndex = QTextControl::staticMetaObject.indexOfSignal("textChanged()");
   455     // avoid multiple textChanged() signals being emitted
   457     // avoid multiple textChanged() signals being emitted
   456     QObject::disconnect(doc, SIGNAL(contentsChanged()), q, SIGNAL(textChanged()));
   458     QMetaObject::disconnect(doc, contentsChangedIndex, q, textChangedIndex);
   457 
   459 
   458     if (!text.isEmpty()) {
   460     if (!text.isEmpty()) {
   459         // clear 'our' cursor for insertion to prevent
   461         // clear 'our' cursor for insertion to prevent
   460         // the emission of the cursorPositionChanged() signal.
   462         // the emission of the cursorPositionChanged() signal.
   461         // instead we emit it only once at the end instead of
   463         // instead we emit it only once at the end instead of
   486     } else if (clearDocument) {
   488     } else if (clearDocument) {
   487         doc->clear();
   489         doc->clear();
   488     }
   490     }
   489     cursor.setCharFormat(charFormatForInsertion);
   491     cursor.setCharFormat(charFormatForInsertion);
   490 
   492 
   491     QObject::connect(doc, SIGNAL(contentsChanged()), q, SIGNAL(textChanged()));
   493     QMetaObject::connect(doc, contentsChangedIndex, q, textChangedIndex);
   492     emit q->textChanged();
   494     emit q->textChanged();
   493     if (!document)
   495     if (!document)
   494         doc->setUndoRedoEnabled(previousUndoRedoState);
   496         doc->setUndoRedoEnabled(previousUndoRedoState);
   495     _q_updateCurrentCharFormatAndSelection();
   497     _q_updateCurrentCharFormatAndSelection();
   496     if (!document)
   498     if (!document)
   743 
   745 
   744 void QTextControl::undo()
   746 void QTextControl::undo()
   745 {
   747 {
   746     Q_D(QTextControl);
   748     Q_D(QTextControl);
   747     d->repaintSelection();
   749     d->repaintSelection();
       
   750     const int oldCursorPos = d->cursor.position();
   748     d->doc->undo(&d->cursor);
   751     d->doc->undo(&d->cursor);
       
   752     if (d->cursor.position() != oldCursorPos)
       
   753         emit cursorPositionChanged();
       
   754     emit microFocusChanged();
   749     ensureCursorVisible();
   755     ensureCursorVisible();
   750 }
   756 }
   751 
   757 
   752 void QTextControl::redo()
   758 void QTextControl::redo()
   753 {
   759 {
   754     Q_D(QTextControl);
   760     Q_D(QTextControl);
   755     d->repaintSelection();
   761     d->repaintSelection();
       
   762     const int oldCursorPos = d->cursor.position();
   756     d->doc->redo(&d->cursor);
   763     d->doc->redo(&d->cursor);
       
   764         if (d->cursor.position() != oldCursorPos)
       
   765         emit cursorPositionChanged();
       
   766     emit microFocusChanged();
   757     ensureCursorVisible();
   767     ensureCursorVisible();
   758 }
   768 }
   759 
   769 
   760 QTextControl::QTextControl(QObject *parent)
   770 QTextControl::QTextControl(QObject *parent)
   761     : QObject(*new QTextControlPrivate, parent)
   771     : QObject(*new QTextControlPrivate, parent)
   844         return;
   854         return;
   845     QMimeData *data = createMimeDataFromSelection();
   855     QMimeData *data = createMimeDataFromSelection();
   846     QApplication::clipboard()->setMimeData(data);
   856     QApplication::clipboard()->setMimeData(data);
   847 }
   857 }
   848 
   858 
   849 void QTextControl::paste()
   859 void QTextControl::paste(QClipboard::Mode mode)
   850 {
   860 {
   851     const QMimeData *md = QApplication::clipboard()->mimeData();
   861     const QMimeData *md = QApplication::clipboard()->mimeData(mode);
   852     if (md)
   862     if (md)
   853         insertFromMimeData(md);
   863         insertFromMimeData(md);
   854 }
   864 }
   855 #endif
   865 #endif
   856 
   866 
   881 }
   891 }
   882 
   892 
   883 void QTextControl::processEvent(QEvent *e, const QMatrix &matrix, QWidget *contextWidget)
   893 void QTextControl::processEvent(QEvent *e, const QMatrix &matrix, QWidget *contextWidget)
   884 {
   894 {
   885     Q_D(QTextControl);
   895     Q_D(QTextControl);
   886     if (d->interactionFlags & Qt::NoTextInteraction)
   896     if (d->interactionFlags == Qt::NoTextInteraction) {
   887         return;
   897         e->ignore();
       
   898         return;
       
   899     }
   888 
   900 
   889     d->contextWidget = contextWidget;
   901     d->contextWidget = contextWidget;
   890 
   902 
   891     if (!d->contextWidget) {
   903     if (!d->contextWidget) {
   892         switch (e->type()) {
   904         switch (e->type()) {
  1227 #ifndef QT_NO_CLIPBOARD
  1239 #ifndef QT_NO_CLIPBOARD
  1228     else if (e == QKeySequence::Cut) {
  1240     else if (e == QKeySequence::Cut) {
  1229            q->cut();
  1241            q->cut();
  1230     }
  1242     }
  1231     else if (e == QKeySequence::Paste) {
  1243     else if (e == QKeySequence::Paste) {
  1232            q->paste();
  1244         QClipboard::Mode mode = QClipboard::Clipboard;
       
  1245 #ifdef Q_WS_X11
       
  1246         if (e->modifiers() == (Qt::CTRL | Qt::SHIFT) && e->key() == Qt::Key_Insert)
       
  1247             mode = QClipboard::Selection;
       
  1248 #endif
       
  1249         q->paste(mode);
  1233     }
  1250     }
  1234 #endif
  1251 #endif
  1235     else if (e == QKeySequence::Delete) {
  1252     else if (e == QKeySequence::Delete) {
  1236         QTextCursor localCursor = cursor;
  1253         QTextCursor localCursor = cursor;
  1237         localCursor.deleteChar();
  1254         localCursor.deleteChar();
  1762     if (!hasFocus)
  1779     if (!hasFocus)
  1763         return;
  1780         return;
  1764     QMenu *menu = q->createStandardContextMenu(docPos, contextWidget);
  1781     QMenu *menu = q->createStandardContextMenu(docPos, contextWidget);
  1765     if (!menu)
  1782     if (!menu)
  1766         return;
  1783         return;
  1767     menu->exec(screenPos);
  1784     menu->setAttribute(Qt::WA_DeleteOnClose);
  1768     delete menu;
  1785     menu->popup(screenPos);
  1769 #endif
  1786 #endif
  1770 }
  1787 }
  1771 
  1788 
  1772 bool QTextControlPrivate::dragEnterEvent(QEvent *e, const QMimeData *mimeData)
  1789 bool QTextControlPrivate::dragEnterEvent(QEvent *e, const QMimeData *mimeData)
  1773 {
  1790 {
  2073                 menu->addAction(imActions.at(i));
  2090                 menu->addAction(imActions.at(i));
  2074         }
  2091         }
  2075     }
  2092     }
  2076 #endif
  2093 #endif
  2077 
  2094 
  2078 #if defined(Q_WS_WIN)
  2095 #if defined(Q_WS_WIN) || defined(Q_WS_X11)
  2079     if ((d->interactionFlags & Qt::TextEditable) && qt_use_rtl_extensions) {
  2096     if ((d->interactionFlags & Qt::TextEditable) && qt_use_rtl_extensions) {
  2080 #else
  2097 #else
  2081     if (d->interactionFlags & Qt::TextEditable) {
  2098     if (d->interactionFlags & Qt::TextEditable) {
  2082 #endif
  2099 #endif
  2083         menu->addSeparator();
  2100         menu->addSeparator();