Commit 0b22699f authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Extend OperatorsModel means

Expose enough information through the OperatorsModel to be able to
complete from QtQuick
parent a96542ab
......@@ -28,6 +28,14 @@ OperatorsModel::OperatorsModel(QObject *parent) : QAbstractTableModel(parent), m
{
}
QHash<int, QByteArray> OperatorsModel::roleNames() const
{
return QAbstractTableModel::roleNames().unite( {
{IsVariableRole, "isVariable"},
{DescriptionRole, "description"}
});
}
QVariant OperatorsModel::data(const QModelIndex & index, int role) const
{
QVariant ret;
......@@ -65,6 +73,15 @@ QVariant OperatorsModel::data(const QModelIndex & index, int role) const
QFont f;
f.setItalic(true);
ret=f;
} else if(role==DescriptionRole && index.column()==0) {
Analitza::Operator oper((Analitza::Operator::OperatorType) (index.row()+1));
switch(index.column()) {
case 0:
ret=description(oper);
break;
}
} else if(role==IsVariableRole && index.column()==0) {
ret=index.row()<Analitza::Operator::nOfOps-2;
}
return ret;
}
......@@ -587,6 +604,13 @@ QString OperatorsModel::standardFunctionCallHelp(const QString& funcname, int pa
}
QString OperatorsModel::lastWord(int pos, const QString& exp)
{
int act=pos-1;
for(; act>=0 && exp[act].isLetter(); act--) {}
return exp.mid(act+1, pos-act-1);
}
/*QString OperatorsModel::operToString(const Operator& op) const
{
......
......@@ -35,6 +35,11 @@ class ANALITZAGUI_EXPORT OperatorsModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Roles {
IsVariableRole = Qt::UserRole + 1,
DescriptionRole
};
/** Constructor. Creates a new Operator Model. */
explicit OperatorsModel(QObject *parent=0);
......@@ -46,6 +51,8 @@ class ANALITZAGUI_EXPORT OperatorsModel : public QAbstractTableModel
static QString example(const Analitza::Operator& o);
QHash<int, QByteArray> roleNames() const override;
/** Adds an entry to the model. */
// void addEntry(int i, const QString&, const QString&, const QString& ex=QString());
......@@ -62,6 +69,7 @@ class ANALITZAGUI_EXPORT OperatorsModel : public QAbstractTableModel
QString parameterHelp(const QModelIndex& idx, int param, bool inbounds) const;
static QString standardFunctionCallHelp(const QString& funcname, int param, int paramcount, bool inbounds, bool isbounded);
Q_SCRIPTABLE static QString lastWord(int pos, const QString &exp);
private:
QSharedPointer<Analitza::Variables> m_vars;
......
......@@ -132,8 +132,8 @@ void ExpressionEdit::updateCompleter()
void ExpressionEdit::completed(const QString& newText)
{
int c = newText.length() - lastWord(textCursor().selectionStart()).length();
QString toInsert=newText.right(c);
int l = OperatorsModel::lastWord(textCursor().selectionStart(), text()).length();
QString toInsert=newText.mid(l);
if(Analitza::Expression::whatType(newText) == Analitza::Object::oper && !isMathML())
toInsert += '(';
insertPlainText(toInsert);
......@@ -242,7 +242,7 @@ void ExpressionEdit::keyPressEvent(QKeyEvent * e)
default:
QPlainTextEdit::keyPressEvent(e);
m_history.last() = this->toPlainText();
QString last = lastWord(textCursor().selectionStart());
QString last = OperatorsModel::lastWord(textCursor().selectionStart(), text());
if(!last.isEmpty()) {
m_completer->setCompletionPrefix(last);
m_completer->complete();
......@@ -269,15 +269,6 @@ void ExpressionEdit::keyPressEvent(QKeyEvent * e)
setCorrect(m_highlight->isCorrect());
}
QString ExpressionEdit::lastWord(int pos)
{
QString exp=text();
int act=pos-1;
for(; act>=0 && exp[act].isLetter(); act--) {}
return exp.mid(act+1, pos-act-1);
}
void ExpressionEdit::cursorMov()
{
int pos=this->textCursor().position();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment