示例#1
0
        void evaluate()
        {
            if(m_binding) {
                if(m_binding_init) {
                    m_value = m_binding();

                    if(m_notifier) {
                        m_notifier(m_value);
                    }
                }
                else {
                    // capture inputs for this property
                    m_capture_failed = false;
                    setCurrent(this);
                    m_value = m_binding();
                    setCurrent(nullptr);

                    if(!m_capture_failed) {
                        m_binding_init = true;

                        if(m_notifier) {
                            m_notifier(m_value);
                        }
                    }
                    else {
                        resetBinding();
                    }
                }
            }
        }
示例#2
0
	bool completeCommonPrefix(){
		QString my_curWord=getCurWord();
		if (my_curWord.isEmpty()) return false;
		if (!completer) return false;
		/*if (!completer->list->isVisible()) {
			resetBinding();
			return false;
		}*/
		// get list of most recent choices
		const QList<CompletionWord> &words=completer->listModel->getWords();
		// filter list for longest common characters
		if (words.count()>1) {
			QString myResult=words.first().word;
			int my_start=my_curWord.length();
			my_curWord=completer->listModel->getLastWord().word;

			for (int j=my_start; (j<my_curWord.length()&&j<myResult.length()); j++) {
				if (myResult[j]!=my_curWord[j]) {
					myResult=myResult.left(j);
				}
			}

			removeRightWordPart();
			editor->insertText(myResult.right(myResult.length()-my_start));
			maxWritten+=myResult.length()-my_start;
			completer->filterList(getCurWord(),getMostUsed());
			if (!completer->list->currentIndex().isValid())
				select(completer->list->model()->index(0,0,QModelIndex()));
			return true;
		} else {
			insertCompletedWord();
			resetBinding();
			return true;
		}
	}	
示例#3
0
	virtual bool mousePressEvent(QMouseEvent* mouse, QEditor *editor) {
		// remove unused argument warnings
		(void) mouse;
		(void) editor;

		resetBinding();
		return false;
	}
示例#4
0
	//moves the selection index to the next/previous delta-th entry in the suggestion list
	bool selectDelta(const int delta){
		if (!completer || !completer->list || !completer->list->isVisible()) {
			resetBinding();
			return false;
		}
		QModelIndex ind=completer->list->model()->index(completer->list->currentIndex().row()+delta,0,QModelIndex());
		if (!ind.isValid()) {
			if (delta<0) ind=completer->list->model()->index(0,0,QModelIndex());
			else if (delta>0) ind=completer->list->model()->index(completer->list->model()->rowCount()-1,0,QModelIndex());
		}
		if (ind.isValid()) select(ind);
		return true;
	}
示例#5
0
	void bindTo(QEditor * edit, LatexCompleter* caller, bool forced, int start) {
		if (active) resetBinding();
		active=true;
		completer=caller;
		editor=edit;
		oldBinding=(editor->inputBindings().count()>0?editor->inputBindings()[0]:0);
		editor->setInputBinding(this);
		curStart=start>0?start:0;
		maxWritten=editor->cursor().columnNumber();
		if (maxWritten<start) maxWritten=start;
		curLine=editor->cursor().line();
		showAlways=forced;//curWord!="\\";
		completer->filterList(getCurWord());
		if (showAlways) {
			completer->list->show();
			select(completer->list->model()->index(0,0,QModelIndex()));
		}
	}
示例#6
0
	bool completeCommonPrefix(){
		QString my_curWord=getCurWord();
		if (my_curWord.isEmpty()) return false;
		if (!completer) return false;
		/*if (!completer->list->isVisible()) {
			resetBinding();
			return false;
		}*/
		// get list of most recent choices
		QStringList my_words;
		const QList<CompletionWord> &words=completer->listModel->getWords();
		for (int i=0; i<words.count(); i++) {
			if (words[i].word.startsWith(my_curWord,Qt::CaseInsensitive))
				my_words.append(words[i].word);
		}
		// filter list for longest common characters
		if (my_words.count()>1) {
			QString myResult=my_words[0];
			int my_start=my_curWord.length();
			for (int i=1; i<my_words.count(); i++) {
				my_curWord=my_words[i];

				for (int j=my_start; (j<my_curWord.length()&&j<myResult.length()); j++) {
					if (myResult[j]!=my_curWord[j]) {
						myResult=myResult.left(j);

					}
				}
			}

			removeRightWordPart();
			editor->cursor().insertText(myResult.right(myResult.length()-my_start));
			maxWritten+=myResult.length()-my_start;
			completer->filterList(getCurWord());
			if (!completer->list->currentIndex().isValid())
				select(completer->list->model()->index(0,0,QModelIndex()));
			return true;
		} else {
			insertCompletedWord();
			resetBinding();
			return true;
		}
	}	
示例#7
0
	void cursorPositionChanged(QEditor* edit) {
		if (edit!=editor) return; //should never happen
		QDocumentCursor c=editor->cursor();
		if (c.line()!=curLine || c.columnNumber()<curStart) resetBinding();
	}
示例#8
0
	virtual bool keyPressEvent(QKeyEvent *event, QEditor *editor) {
		if (event->key()==Qt::Key_Shift || event->key()==Qt::Key_Alt || event->key()==Qt::Key_Control)
			return false;
		if (!active) return false; //we should never have been called
		bool handled=false;
		if (event->key()==Qt::Key_Backspace) {
			maxWritten--;
			editor->cursor().deletePreviousChar();
			if (editor->cursor().columnNumber()<=curStart) {
				resetBinding();
				return true;
			} else if (editor->cursor().columnNumber()+1<=curStart && !showAlways) {
				completer->list->hide();
				return true;
			}
			handled=true;
		} else if (event->key()==Qt::Key_Escape) {
			resetBinding();
			return true;
		} else if (event->key()==Qt::Key_Delete) {
			if (editor->cursor().columnNumber()<maxWritten) maxWritten--;
			editor->cursor().deleteChar();
			handled=true;
		} else if (event->key()==Qt::Key_Left) {
			if (event->modifiers()) {
				resetBinding();
				return false;
			}
			editor->setCursorPosition(curLine.lineNumber(),editor->cursor().columnNumber()-1);
			if (editor->cursor().columnNumber()<=curStart) resetBinding();
			handled=true;
		} else if (event->key()==Qt::Key_Right) {
			if (event->modifiers()) {
				resetBinding();
				return false;
			}
			editor->setCursorPosition(curLine.lineNumber(),editor->cursor().columnNumber()+1);
			if (editor->cursor().columnNumber()>maxWritten) resetBinding();
			handled=true;
		} else if (event->key()==Qt::Key_Up) return selectDelta(-1);
		else if (event->key()==Qt::Key_Down) return selectDelta(+1);
		else if (event->key()==Qt::Key_PageUp) return  selectDelta(-5);
		else if (event->key()==Qt::Key_PageDown) return  selectDelta(+5);
		else if (event->key()==Qt::Key_Home) {
			if (!completer->list->isVisible()) {
				resetBinding();
				return false;
			}
			QModelIndex ind=completer->list->model()->index(0,0,QModelIndex());
			if (ind.isValid()) select(ind);
			return true;
		} else if (event->key()==Qt::Key_End) {
			if (!completer->list->isVisible()) {
				resetBinding();
				return false;
			}
			QModelIndex ind=completer->list->model()->index(completer->list->model()->rowCount()-1,0,QModelIndex());
			if (ind.isValid()) select(ind);
			return true;
		}  else if (event->key()==Qt::Key_Tab) {
			return completeCommonPrefix();
		} else if (event->key()==Qt::Key_Return || event->key()==Qt::Key_Enter) {
			if (!insertCompletedWord()) {
				editor->cursor().insertText("\n");
				curLine=editor->document()->line(curLine.lineNumber()+1);
				editor->setCursorPosition(curLine.lineNumber(),curLine.length());
			}
			resetBinding();
			return true;
		} else {
			if (event->text().length()!=1 || event->text()==" ") {
				resetBinding();
				return false;
			}

			QChar written=event->text().at(0);
			if (written=='\\') {
				if (getCurWord()=="\\") {
					curStart+=2;
					maxWritten=curStart;
				} else if (getCurWord()=="") {
					maxWritten=curStart+1;
				} else {
					insertCompletedWord();
					QDocumentCursor edc=editor->cursor();
					if (edc.hasSelection()) {
						edc.removeSelectedText();
						editor->setCursor(edc);
					}
					curStart=edc.columnNumber();
					maxWritten=curStart+1;
				}
				editor->cursor().insertText(written);
				handled=true;
			} else if (completer->acceptChar(written,editor->cursor().columnNumber()-curStart)) {
				maxWritten++;
				editor->cursor().insertText(written);
				if (editor->cursor().columnNumber()+1>curStart)
					completer->list->show();
				handled=true;
			} else if (LatexCompleter::config && LatexCompleter::config->eowCompletes && 
		           event->text().length()==1 && getCommonEOW().contains(event->text().at(0))) {
				insertCompletedWord();
				resetBinding();
				return false; //return false to let the default implementation handle it and insert the new character
			} else {
				resetBinding();
				return false;
			}
		}
		completer->filterList(getCurWord());
		if (!completer->list->currentIndex().isValid())
			select(completer->list->model()->index(0,0,QModelIndex()));
		return handled;
	}
示例#9
0
    virtual bool keyPressEvent(QKeyEvent *event, QEditor *editor)
    {
        if (event->key()==Qt::Key_Shift || event->key()==Qt::Key_Alt || event->key()==Qt::Key_Control)
            return false;
        if (!active) return false; //we should never have been called
        bool handled=false;
        if (event->key()==Qt::Key_Backspace) {
            maxWritten--;
            editor->cursor().deletePreviousChar();
            if (editor->cursor().columnNumber()<=curStart) {
                resetBinding();
                return true;
            } else if (editor->cursor().columnNumber()+1<=curStart && !showAlways){
                completer->list->hide();
                return true;
            }
            handled=true;
        } else if (event->key()==Qt::Key_Delete) {
            if (editor->cursor().columnNumber()<maxWritten) maxWritten--;
            editor->cursor().deleteChar();
            handled=true;
        } else if (event->key()==Qt::Key_Left) {
            if (event->modifiers()) {
                resetBinding();
                return false;
            }
            editor->setCursorPosition(curLine.lineNumber(),editor->cursor().columnNumber()-1);
            if (editor->cursor().columnNumber()<=curStart) resetBinding();
            handled=true;
        } else if (event->key()==Qt::Key_Right) {
            if (event->modifiers()) {
                resetBinding();
                return false;
            }
            editor->setCursorPosition(curLine.lineNumber(),editor->cursor().columnNumber()+1);
            if (editor->cursor().columnNumber()>maxWritten) resetBinding();
            handled=true;
        } else if (event->key()==Qt::Key_Up) {
            if (!completer->list->isVisible()) {
                resetBinding();
                return false;
            }
            QModelIndex ind=completer->list->model()->index(completer->list->currentIndex().row()-1,0,QModelIndex());
            if (ind.isValid()) select(ind);
            return true;
        } else if (event->key()==Qt::Key_Down) {
            if (!completer->list->isVisible()) {
                resetBinding();
                return false;
            }
            QModelIndex ind=completer->list->model()->index(completer->list->currentIndex().row()+1,0,QModelIndex());
            if (ind.isValid()) select(ind);
            return true;
        } else if (event->key()==Qt::Key_PageUp) {
            if (!completer->list->isVisible()) {
                resetBinding();
                return false;
            }
            QModelIndex ind=completer->list->model()->index(completer->list->currentIndex().row()-5,0,QModelIndex());
            if (!ind.isValid()) ind=completer->list->model()->index(0,0,QModelIndex());
            if (ind.isValid()) select(ind);
            return true;
        } else if (event->key()==Qt::Key_PageDown) {
            if (!completer->list->isVisible()) {
                resetBinding();
                return false;
            }
            QModelIndex ind=completer->list->model()->index(completer->list->currentIndex().row()+5,0,QModelIndex());
            if (!ind.isValid()) ind=completer->list->model()->index(completer->list->model()->rowCount()-1,0,QModelIndex());
            if (ind.isValid()) select(ind);
            return true;
        } else if (event->key()==Qt::Key_Home) {
            if (!completer->list->isVisible()) {
                resetBinding();
                return false;
            }
            QModelIndex ind=completer->list->model()->index(0,0,QModelIndex());
            if (ind.isValid()) select(ind);
            return true;
        } else if (event->key()==Qt::Key_End) {
            if (!completer->list->isVisible()) {
                resetBinding();
                return false;
            }
            QModelIndex ind=completer->list->model()->index(completer->list->model()->rowCount()-1,0,QModelIndex());
            if (ind.isValid()) select(ind);
            return true;
        }  else if (event->key()==Qt::Key_Tab) {
            if (!completer->list->isVisible()) {
                resetBinding();
                return false;
            }
            // get list of most recent choices
            QStringList my_words;
            QList<CompletionWord> words=completer->getWords();
            QString my_curWord=getCurWord();
            for (int i=0;i<words.count();i++){
                if (words[i].word.startsWith(my_curWord,Qt::CaseInsensitive))
                    my_words.append(words[i].word);
            }
            // filter list for longest common characters
            QString myResult=my_words[0];
            int my_start=my_curWord.length();
            if(my_words.count()>1){
                for (int i=1;i<my_words.count();i++){
                    my_curWord=my_words[i];

                    for(int j=my_start;(j<my_curWord.length()&&j<myResult.length());j++){
                        if(myResult[j]!=my_curWord[j]){
                            myResult=myResult.left(j);

                        }
                    }
                }

                removeRightWordPart();
                editor->cursor().insertText(myResult.right(myResult.length()-my_start));
                maxWritten+=myResult.length()-my_start;
                handled=true;
            } else {
                insertCompletedWord();
                resetBinding();
                return true;
            }
        } else if (event->key()==Qt::Key_Return || event->key()==Qt::Key_Enter) {
            if (!insertCompletedWord()) {
                editor->cursor().insertText("\n");
                curLine=editor->document()->line(curLine.lineNumber()+1);
                editor->setCursorPosition(curLine.lineNumber(),curLine.length());
            }
            resetBinding();
            return true;
        } else {
            if (event->text().length()!=1 || event->text()==" ") {
                resetBinding();
                return false;
            }

            QChar written=event->text().at(0);
            if (written=='\\') {
                if (getCurWord()=="\\") {
                    curStart+=2;
                    maxWritten=curStart;
                } else if (getCurWord()=="") {
                    maxWritten=curStart+1;
                } else {
                    insertCompletedWord();
                    curStart=editor->cursor().columnNumber();
                    maxWritten=curStart+1;
                }
                editor->cursor().insertText(written);
                handled=true;
            } else if (completer->acceptChar(written,editor->cursor().columnNumber()-curStart)) {
                maxWritten++;
                editor->cursor().insertText(written);
                if (editor->cursor().columnNumber()+1>curStart)
                    completer->list->show();
                handled=true;
            } else {
                resetBinding();
                return false;
            }
        }
        completer->updateList(getCurWord());
        if (!completer->list->currentIndex().isValid())
            select(completer->list->model()->index(0,0,QModelIndex()));
        return handled;
    }
示例#10
0
	virtual bool keyPressEvent(QKeyEvent *event, QEditor *editor) {
		Q_ASSERT (completer && completer->listModel);
		if (!completer || !completer->listModel) return false;
		if (event->key()==Qt::Key_Shift || event->key()==Qt::Key_Alt || event->key()==Qt::Key_AltGr || event->key()==Qt::Key_Control)
			return false;
		if (!active) return false; //we should never have been called
		bool handled=false;
		if (event->key()==Qt::Key_Backspace) {
			maxWritten--;
			editor->cursor().deletePreviousChar();
			if (editor->cursor().columnNumber()<=curStart) {
				resetBinding();
				return true;
			} else if (editor->cursor().columnNumber()+1<=curStart && !showAlways) {
				completer->widget->hide();
				return true;
			}
			handled=true;
		} else if (event->key()==Qt::Key_Escape) {
			simpleRestoreAutoOverride("");
			resetBinding();
			return true;
		} else if (event->key()==Qt::Key_Delete) {
			if (editor->cursor().columnNumber()<maxWritten) maxWritten--;
			if(completer->forcedRef){
				if(editor->cursor().nextChar()=='}')
					completer->forcedRef=false;
			}
			editor->cursor().deleteChar();
			handled=true;
		} else if (event->key()==Qt::Key_Left) {
			if (event->modifiers()) {
				resetBinding();
				return false;
			}
			editor->setCursorPosition(curLine.lineNumber(),editor->cursor().columnNumber()-1);
			if (editor->cursor().columnNumber()<=curStart) resetBinding();
			handled=true;
		} else if (event->key()==Qt::Key_Right) {
			if (event->modifiers()) {
				resetBinding();
				return false;
			}
			editor->setCursorPosition(curLine.lineNumber(),editor->cursor().columnNumber()+1);
			if (editor->cursor().columnNumber()>maxWritten) resetBinding();
			handled=true;
		} else if (event->key()==Qt::Key_Up) return selectDelta(-1);
		else if (event->key()==Qt::Key_Down) return selectDelta(+1);
		else if (event->key()==Qt::Key_PageUp) return  selectDelta(-5);
		else if (event->key()==Qt::Key_PageDown) return  selectDelta(+5);
		else if (event->key()==Qt::Key_Home) {
			if (!completer->list->isVisible()) {
				resetBinding();
				return false;
			}
			QModelIndex ind=completer->list->model()->index(0,0,QModelIndex());
			if (ind.isValid()) select(ind);
			return true;
		} else if (event->key()==Qt::Key_End) {
			if (!completer->list->isVisible()) {
				resetBinding();
				return false;
			}
			QModelIndex ind=completer->list->model()->index(completer->list->model()->rowCount()-1,0,QModelIndex());
			if (ind.isValid()) select(ind);
			return true;
		}  else if (event->key()==Qt::Key_Tab) {
			return completeCommonPrefix();
		}  else if (event->key()==Qt::Key_Space && event->modifiers()==Qt::ShiftModifier) {
			//showMostUsed=!showMostUsed;
			//handled=true;
			showMostUsed++;
			if(showMostUsed>2)
				showMostUsed=0;
			completer->tbAbove->setCurrentIndex(showMostUsed);
			completer->tbBelow->setCurrentIndex(showMostUsed);
			return true;
		} else if (event->key()==Qt::Key_Return || event->key()==Qt::Key_Enter) {
			if (!insertCompletedWord()) {
				editor->insertText("\n");
				//curLine=editor->document()->line(curLine.lineNumber()+1);
				//editor->setCursorPosition(curLine.lineNumber(),curLine.length());
			}
			resetBinding();
			return true;
		} else {
			if (event->text().length()!=1 || event->text()==" ") {
				resetBinding();
				return false;
			}

			QChar written=event->text().at(0);
			if (written=='\\') {
				if (getCurWord()=="\\") {
					resetBinding();
					return false;
				} else if (getCurWord()=="") {
					maxWritten=curStart+1;
				} else {
					if (LatexCompleter::config && LatexCompleter::config->eowCompletes) {
						insertCompletedWord();
					}
					QDocumentCursor edc=editor->cursor();
					if (edc.hasSelection()) {
						edc.removeSelectedText();
						editor->setCursor(edc);
					}
					curStart=edc.columnNumber();
					maxWritten=curStart+1;
				}
				editor->cursor().insertText(written);
				//editor->insertText(written);
				handled=true;
			} else if (completer->acceptChar(written,editor->cursor().columnNumber()-curStart)) {
				insertText(written);
				handled=true;
			} else if (event->text().length()==1 && getCommonEOW().contains(event->text().at(0)) ) {
				QString curWord = getCurWord();
				if (curWord=="\\" || !LatexCompleter::config || !LatexCompleter::config->eowCompletes) {
					resetBinding();
					simpleRestoreAutoOverride(written);
					return false;
				}
				const QList<CompletionWord> &words=completer->listModel->getWords();
				QString newWord;
				int eowchars = 10000;
				foreach (const CodeSnippet& w, words){
					if (!w.word.startsWith(curWord)) continue;
					if (w.word.length() == curWord.length()) {
						newWord = w.word;
						break;
					}
					int newoffset = w.lines.first().indexOf(written, curWord.length());
					if (newoffset < 0) continue;
					int neweowchars=0;
					for (int i=curWord.length(); i<newoffset; i++)
						if (getCommonEOW().contains(w.lines.first()[i]))
							neweowchars++;
					if (neweowchars<eowchars) {
						newWord = w.word;
						eowchars = neweowchars;
						if (eowchars==1) break;
					}
				}

				if (!newWord.isEmpty() && newWord.length()!=curWord.length()) {
					QString insertion = newWord.mid(curWord.length(), newWord.indexOf(written, curWord.length()) - curWord.length() + 1); //choose text until written eow character
					insertText(insertion);
					//insertText(written);
					handled = true;
				} else {
					insertCompletedWord();
					if (newWord.isEmpty())
						simpleRestoreAutoOverride(written);
					//insertText(written);
					resetBinding();
					return false;//oldBinding->keyPressEvent(event,editor); //call old input binding for long words (=> key replacements after completions, but the user can still write \")
				}
			} else {