PDA

View Full Version : Move QCompleter on QRect x,y



patrik08
6th August 2007, 12:47
i find QsciScintilla a super libs but code AutoCompletion not run ok... it display a not clickable widged.... ( only keyboard can secect!)

i have implement the QT4 QCompleter combobox and its run Super..

only its display on not correct place ....
my problem how i can find the cursor QRect on QsciScintilla?



i bring QsciScintilla getWord(int position) to public and i can bring word to QCompleter class...





/* completer from qt4 make it */
void QViScintilla::CursorAtIndex(int line ,int cool )
{
/* start from connect(this, SIGNAL(cursorPositionChanged(int,int)),this, SLOT(CursorAtIndex(int,int))); */
setAutoCompletionFillupsEnabled(false); /* ensure not start scite autoxx */
int newPos = SendScintilla(SCI_GETCURRENTPOS);
CURRENTWORD = getWord(newPos); /* bring getWord(int position) to public !!!!! */
qDebug() << "### word " << CURRENTWORD; /* word piece on cursor */
}

/* now i display it so ... its not correct cursor position ..... display qt4 QCompleter */
QPoint pointers(QCursor::pos());
QRect cr = QRect ( posX, posY ,20,20);
cr.setWidth(c->popup()->sizeHintForColumn(0) + c->popup()->verticalScrollBar()->sizeHint().width());
c->complete(cr); // popup it up!

patrik08
8th August 2007, 11:33
I resolve the problem to paint on QsciScintilla a QT4 QCompleter read this code line...

to become the same X,Y as

QTextEdit::cursorRect()






/* constructor */
connect(this, SIGNAL(cursorPositionChanged(int,int)),this, SLOT(CursorAtIndex(int,int)));
baseram = QApplication::clipboard(); /* capture word on clipboard & keyPressEvent */
completerbase = new QCompleter();
completerbase->setModel(modelFromFile(":/img/_htmlwordslist.txt"));
completerbase->setModelSorting(QCompleter::CaseInsensitivelySorte dModel);
completerbase->setCaseSensitivity(Qt::CaseInsensitive);
setCompleter(completerbase);


/* constructor */

/* completer from qt4 make it */
void QViScintilla::CursorAtIndex(int line ,int cool )
{
/* start from connect(this, SIGNAL(cursorPositionChanged(int,int)),this, SLOT(CursorAtIndex(int,int))); */
setAutoCompletionFillupsEnabled(false);
int newPos = SendScintilla(SCI_GETCURRENTPOS);
posX = SendScintilla(SCI_POINTXFROMPOSITION, 0, newPos);
posY = SendScintilla(SCI_POINTYFROMPOSITION, 0, newPos);
CURRENTWORD = TextUnderCursor(newPos); /* bring getWord(int position) to public !!!!! */
///////////qDebug() << "### word " << CURRENTWORD; /* word piece on cursor */
}

void QViScintilla::setCompleter(QCompleter *completer)
{
if (c)
QObject::disconnect(c, 0, this, 0);
c = completer;
if (!c) {
return;
}
c->setWidget(this);
c->setCompletionMode(QCompleter::PopupCompletion);
c->setCaseSensitivity(Qt::CaseSensitive);
connect(c, SIGNAL(activated(const QString&)), this, SLOT(insertCompletion(const QString&)));
}



void QViScintilla::keyPressEvent(QKeyEvent *e)
{

if (c && c->popup()->isVisible()) {
// The following keys are forwarded by the completer to the widget
switch (e->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Backtab:
e->ignore();
return; // let the completer do default behavior
default:
break;
}
}

bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
if (!c || !isShortcut) {
QsciScintillaBase::keyPressEvent(e);
}

const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
if (!c || (ctrlOrShift && CURRENTWORD.isEmpty())) {
return;
}
static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;

if (!isShortcut && (hasModifier || CURRENTWORD.isEmpty()|| completionPrefix.length() < 2 || eow.contains(CURRENTWORD.right(1)))) {
c->popup()->hide();
return;
}

if (CURRENTWORD != c->completionPrefix()) {
c->setCompletionPrefix(CURRENTWORD);
c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
}

QRect cr = QRect (posX, posY ,20,20);
cr.setWidth(c->popup()->sizeHintForColumn(0) + c->popup()->verticalScrollBar()->sizeHint().width());
c->complete(cr); // popup it up!
}



QAbstractItemModel *QViScintilla::modelFromFile(const QString& fileName)
{

QFile file(fileName);
if (!file.open(QFile::ReadOnly))
return new QStringListModel(completer);

QApplication::setOverrideCursor(QCursor(Qt::WaitCu rsor));
QStringList words;

while (!file.atEnd()) {
QByteArray line = file.readLine();
if (!line.isEmpty())
words << line.trimmed();
}

QApplication::restoreOverrideCursor();
return new QStringListModel(words, completer);
}


/*
on class QSCINTILLA_EXPORT QsciScintilla : public QsciScintillaBase
append on public ! && rebuild lib.. static
inline QString TextUnderCursor(int &pos) {
return getWord(pos);
}

*/








### on Qt4.pro set .. +mac intel + ppc to have only one static lib
!win32:VERSION = 3.0.0

TEMPLATE = lib
TARGET = qscintilla2
CONFIG += qt warn_off release staticlib thread
INCLUDEPATH = . ../include ../src
DEFINES = QT SCI_LEXER

# Handle both Qt v4 and v3.

DESTDIR = ../../all_os_libs


macx {
QMAKE_MAC_SDK=/Developer/SDKs/MacOSX10.4u.sdk
CONFIG+=x86 ppc
}