• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

E:/sqlmaster/src/textedit.cpp

Go to the documentation of this file.
00001 #include "textedit.h"
00002 #include <QtGui>
00003 #include <QCompleter>
00004 #include <QKeyEvent>
00005 #include <QAbstractItemView>
00006 #include <QtDebug>
00007 #include <QApplication>
00008 #include <QModelIndex>
00009 #include <QAbstractItemModel>
00010 #include <QScrollBar>
00011 //--------------------------------------------------------------------------------
00012 TextEdit::TextEdit(QWidget *parent)
00013 : QTextEdit(parent), c(0)
00014 {
00015 
00016 }
00017 //--------------------------------------------------------------------------------
00018 TextEdit::~TextEdit()
00019 {
00020 }
00021 //--------------------------------------------------------------------------------
00022 void TextEdit::setCompleter(QCompleter *completer)
00023 {
00024     if (c)
00025         QObject::disconnect(c, 0, this, 0);
00026 
00027     c = completer;
00028 
00029     if (!c)
00030         return;
00031 
00032     c->setWidget(this);
00033     c->setCompletionMode(QCompleter::PopupCompletion);
00034     c->setCaseSensitivity(Qt::CaseInsensitive);
00035     QObject::connect(c, SIGNAL(activated(QString)),
00036                      this, SLOT(insertCompletion(QString)));
00037 }
00038 //--------------------------------------------------------------------------------
00039 QCompleter *TextEdit::completer() const
00040 {
00041     return c;
00042 }
00043 //--------------------------------------------------------------------------------
00044 void TextEdit::insertCompletion(const QString& completion)
00045 {
00046     if (c->widget() != this)
00047         return;
00048     QTextCursor tc = textCursor();
00049     int extra = completion.length() - c->completionPrefix().length();
00050     tc.movePosition(QTextCursor::Left);
00051     tc.movePosition(QTextCursor::EndOfWord);
00052     tc.insertText(completion.right(extra));
00053     setTextCursor(tc);
00054 }
00055 //--------------------------------------------------------------------------------
00056 QString TextEdit::textUnderCursor() const
00057 {
00058     QTextCursor tc = textCursor();
00059     tc.select(QTextCursor::WordUnderCursor);
00060     return tc.selectedText();
00061 }
00062 //--------------------------------------------------------------------------------
00063 void TextEdit::focusInEvent(QFocusEvent *e)
00064 {
00065     if (c)
00066         c->setWidget(this);
00067     QTextEdit::focusInEvent(e);
00068 }
00069 //--------------------------------------------------------------------------------
00070 void TextEdit::keyPressEvent(QKeyEvent *e)
00071 {
00072     if (c && c->popup()->isVisible()) {
00073         // The following keys are forwarded by the completer to the widget
00074        switch (e->key()) {
00075        case Qt::Key_Enter:
00076        case Qt::Key_Return:
00077        case Qt::Key_Escape:
00078        case Qt::Key_Tab:
00079        case Qt::Key_Backtab:
00080             e->ignore();
00081             return; // let the completer do default behavior
00082        default:
00083            break;
00084        }
00085     }
00086 
00087     bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
00088     if (!c || !isShortcut) // do not process the shortcut when we have a completer
00089         QTextEdit::keyPressEvent(e);
00090 //--------------------------------------------------------------------------------
00091     const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
00092     if (!c || (ctrlOrShift && e->text().isEmpty()))
00093         return;
00094 
00095     static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
00096     bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
00097     QString completionPrefix = textUnderCursor();
00098 
00099     if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 1
00100                       || eow.contains(e->text().right(1)))) {
00101         c->popup()->hide();
00102         return;
00103     }
00104 
00105     if (completionPrefix != c->completionPrefix()) {
00106         c->setCompletionPrefix(completionPrefix);
00107         c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
00108     }
00109     QRect cr = cursorRect();
00110     cr.setWidth(c->popup()->sizeHintForColumn(0)
00111                 + c->popup()->verticalScrollBar()->sizeHint().width());
00112     c->complete(cr); // popup it up!
00113 }
00114 //-------------------------------------------------------------------------------
00115 

Generated on Wed Dec 1 2010 08:43:40 for SQL Master by  doxygen 1.7.2