{
Q_OBJECT
public:
explicit LineEdit
(QWidget *parent
= 0);
protected:
signals:
private slots:
void textEditedSlot
(const QString &);
private:
};
{
completer= new TreeModelCompleter(this);//TreeModelCompleter is my implementation of QCompleter, works ok with qtextedit
completer->setWidget(this);
}
void LineEdit
::insertCompletion(QString completion
) {
bool done = false;
int initPos = cursorPosition();
int pos = initPos;
while(!done){
bool moved=true;
if(pos == 0) moved = false;
else { --pos; rep = t.at(pos) + rep; }
if(!moved) done = true;
else if(rep.
contains(QRegExp("[\\s+-&!,|<>?/ \\(\\) \\{\\} ]"))) {
if(!done) rep.remove(0, 1);
done = true;
++pos;
}
}
setText(t.replace(pos, initPos-pos+1, completion));
}
void LineEdit
::textEditedSlot(const QString &text
) {
QString completionPrefix
= textUnderCursor
();
if (completionPrefix != completer->completionPrefix()) {
completer->setCompletionPrefix(completionPrefix);
completer->popup()->setCurrentIndex(completer->popup()->model()->index(0,0));
}
cr.setWidth(completer->popup()->sizeHintForColumn(0)
+ completer->popup()->verticalScrollBar()->sizeHint().width());
completer->complete(cr); // popup it up!
}
QString LineEdit
::textUnderCursor() {
bool done = false;
int initPos = cursorPosition();
int pos = initPos;
while(!done){
bool moved=true;
if(pos == 0) moved = false;
else {--pos;rep = t.at(pos) + rep; }
if(!moved) done = true;
else if(rep.
contains(QRegExp("[\\s+-&!,|<>?/ \\(\\) \\{\\} ]"))) {
if(!done) rep.remove(0, 1);
done = true;
++pos;
}
}
if(!thisString.
isEmpty() && rep.
contains(QString("this"))){ rep
= rep.
replace(QString("this"), thisString
);
}
return rep;
}
class LineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit LineEdit(QWidget *parent = 0);
protected:
signals:
private slots:
void insertCompletion(QString);
void textEditedSlot(const QString &);
private:
QCompleter* completer;
QString textUnderCursor();
};
LineEdit::LineEdit(QWidget *parent) : QLineEdit(parent)
{
completer= new TreeModelCompleter(this);//TreeModelCompleter is my implementation of QCompleter, works ok with qtextedit
completer->setWidget(this);
QObject::connect(completer, SIGNAL(activated(QString)), this, SLOT(insertCompletion(QString)));
QObject::connect(this, SIGNAL(textEdited(QString)), this, SLOT(textEditedSlot(QString)));
}
void LineEdit::insertCompletion(QString completion)
{
bool done = false;
QString rep;
int initPos = cursorPosition();
int pos = initPos;
QString t = text();
while(!done){
bool moved=true;
if(pos == 0) moved = false;
else { --pos; rep = t.at(pos) + rep; }
if(!moved) done = true;
else if(rep.contains(QRegExp("[\\s+-&!,|<>?/ \\(\\) \\{\\} ]")))
{
if(!done) rep.remove(0, 1);
done = true;
++pos;
}
}
setText(t.replace(pos, initPos-pos+1, completion));
}
void LineEdit::textEditedSlot(const QString &text)
{
QString completionPrefix = textUnderCursor();
if (completionPrefix != completer->completionPrefix()) {
completer->setCompletionPrefix(completionPrefix);
completer->popup()->setCurrentIndex(completer->popup()->model()->index(0,0));
}
QRect cr = cursorRect();
cr.setWidth(completer->popup()->sizeHintForColumn(0)
+ completer->popup()->verticalScrollBar()->sizeHint().width());
completer->complete(cr); // popup it up!
}
QString LineEdit::textUnderCursor()
{
bool done = false;
QString rep;
int initPos = cursorPosition();
int pos = initPos;
QString t = text();
while(!done){
bool moved=true;
if(pos == 0) moved = false;
else {--pos;rep = t.at(pos) + rep; }
if(!moved) done = true;
else if(rep.contains(QRegExp("[\\s+-&!,|<>?/ \\(\\) \\{\\} ]")))
{
if(!done) rep.remove(0, 1);
done = true;
++pos;
}
}
if(!thisString.isEmpty() && rep.contains(QString("this"))){
rep = rep.replace(QString("this"), thisString);
}
return rep;
}
To copy to clipboard, switch view to plain text mode
Bookmarks