00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <QtGui/QLineEdit>
00015 #include <SeExpression.h>
00016 #include <SeExprFunc.h>
00017 #include "SeExprEdCompletionModel.h"
00018
00019 std::vector<QString> SeExprEdCompletionModel::builtins;
00020
00021 SeExprEdCompletionModel::SeExprEdCompletionModel(QObject* parent)
00022 :QAbstractItemModel(parent)
00023 {
00024 if(builtins.size()==0){
00025 std::vector<std::string> builtins_std;
00026 SeExprFunc::getFunctionNames(builtins_std);
00027 for(unsigned int i=0;i<builtins_std.size();i++) builtins.push_back(QString(builtins_std[i].c_str()));
00028 }
00029 }
00030
00031 void SeExprEdCompletionModel::clearVariables()
00032 {
00033 variables.clear();variables_comment.clear();
00034 }
00035
00036 void SeExprEdCompletionModel::addVariable(const QString& str,const QString& comment)
00037 {
00038 variables.push_back(str);variables_comment.push_back(comment);
00039 }
00040
00041 void SeExprEdCompletionModel::clearFunctions()
00042 {
00043 functions.clear();functions_comment.clear();functionNameToFunction.clear();
00044 }
00045
00046 void SeExprEdCompletionModel::addFunction(const QString& str,const QString& comment)
00047 {
00048 functionNameToFunction[str]=functions_comment.size();
00049 functions.push_back(str);
00050 functions_comment.push_back(comment);
00051 }
00052
00053 void SeExprEdCompletionModel::syncExtras(const SeExprEdCompletionModel& otherModel)
00054 {
00055 functionNameToFunction=otherModel.functionNameToFunction;
00056 functions=otherModel.functions;
00057 functions_comment=otherModel.functions_comment;
00058 variables=otherModel.variables;
00059 variables_comment=otherModel.variables_comment;
00060 }
00061
00062 QVariant SeExprEdCompletionModel::data(const QModelIndex& index,int role) const
00063 {
00064 static QColor variableColor=QColor(100,200,250),functionColor=QColor(100,250,200),
00065 backgroundColor(50,50,50);
00066
00067 if(!index.isValid()) return QVariant();
00068 int row=index.row(),column=index.column();
00069
00070 int functions_offset=builtins.size();
00071 int variables_offset=functions_offset+functions.size();
00072 int local_variables_offset=variables_offset+variables.size();
00073
00074 if(role==Qt::BackgroundRole) return backgroundColor;
00075
00076 if(role==Qt::FontRole && column==0){
00077 QFont font;
00078 font.setBold(true);
00079 return font;
00080 }
00081
00082 if(row<functions_offset){
00083 int index=row;
00084 if(role==Qt::DisplayRole || role==Qt::EditRole){
00085 if(column==0) return QVariant(builtins[index]);
00086 else if(column==1) return QVariant(getFirstLine(SeExprFunc::getDocString(builtins[index].toStdString().c_str())));
00087 }else if(role==Qt::ForegroundRole)
00088 return functionColor;
00089 }else if(row<variables_offset){
00090 int index=row-functions_offset;
00091 if(role==Qt::DisplayRole || role==Qt::EditRole){
00092 if(column==0) return QVariant(functions[index]);
00093 else if(column==1) return QVariant(getFirstLine(functions_comment[index].toStdString()));
00094 }else if(role==Qt::ForegroundRole)
00095 return functionColor;
00096 }else if(row<local_variables_offset){
00097 int index=row-variables_offset;
00098 if(role==Qt::DisplayRole || role==Qt::EditRole){
00099 if(column==0) return QVariant(variables[index]);
00100 else if(column==1) return QVariant(variables_comment[index]);
00101 }else if(role==Qt::ForegroundRole)
00102 return variableColor;
00103 }else if(row<local_variables_offset+(int)local_variables.size()){
00104 int index=row-local_variables_offset;
00105 if(role==Qt::DisplayRole || role==Qt::EditRole){
00106 if(column==0) return QVariant(local_variables[index]);
00107 else if(column==1) return QVariant("Local");
00108 }else if(role==Qt::ForegroundRole)
00109 return variableColor;
00110 }
00111 return QVariant();
00112 }
00113
00114 QString SeExprEdCompletionModel::getDocString(const QString& s)
00115 {
00116 std::map<QString,int>::iterator i=functionNameToFunction.find(s);
00117 if(i!=functionNameToFunction.end()){
00118 return functions_comment[i->second];
00119 }else
00120 return SeExprFunc::getDocString(s.toStdString().c_str()).c_str();
00121 }