SeExpr
ExprCompletionModel.cpp
Go to the documentation of this file.
1 /*
2 * Copyright Disney Enterprises, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License
6 * and the following modification to it: Section 6 Trademarks.
7 * deleted and replaced with:
8 *
9 * 6. Trademarks. This License does not grant permission to use the
10 * trade names, trademarks, service marks, or product names of the
11 * Licensor and its affiliates, except as required for reproducing
12 * the content of the NOTICE file.
13 *
14 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * @file ExprCompletionModel.h
18 * @brief This provides an expression editor for SeExpr syntax with auto ui features
19 * @author aselle
20 */
21 #include <QtGui/QLineEdit>
22 #include <Expression.h>
23 #include <SeExpr2/ExprFunc.h>
24 #include "ExprCompletionModel.h"
25 
26 std::vector<QString> ExprCompletionModel::builtins;
27 
29  :QAbstractItemModel(parent)
30 {
31  if(builtins.size()==0){
32  std::vector<std::string> builtins_std;
34  for(unsigned int i=0;i<builtins_std.size();i++) builtins.push_back(QString(builtins_std[i].c_str()));
35  }
36 }
37 
39 {
40  variables.clear();variables_comment.clear();
41 }
42 
43 void ExprCompletionModel::addVariable(const QString& str,const QString& comment)
44 {
45  variables.push_back(str);variables_comment.push_back(comment);
46 }
47 
49 {
50  functions.clear();functions_comment.clear();functionNameToFunction.clear();
51 }
52 
53 void ExprCompletionModel::addFunction(const QString& str,const QString& comment)
54 {
56  functions.push_back(str);
57  functions_comment.push_back(comment);
58 }
59 
61 {
63  functions=otherModel.functions;
65  variables=otherModel.variables;
67 }
68 
69 QVariant ExprCompletionModel::data(const QModelIndex& index,int role) const
70 {
71  static QColor variableColor=QColor(100,200,250),functionColor=QColor(100,250,200),
72  backgroundColor(50,50,50);
73 
74  if(!index.isValid()) return QVariant();
75  int row=index.row(),column=index.column();
76 
77  int functions_offset=builtins.size();
78  int variables_offset=functions_offset+functions.size();
79  int local_variables_offset=variables_offset+variables.size();
80 
81  if(role==Qt::BackgroundRole) return backgroundColor;
82 
83  if(role==Qt::FontRole && column==0){
84  QFont font;
85  font.setBold(true);
86  return font;
87  }
88 
89  if(row<functions_offset){
90  int index=row;
91  if(role==Qt::DisplayRole || role==Qt::EditRole){
92  if(column==0) return QVariant(builtins[index]);
93  else if(column==1) return QVariant(getFirstLine(SeExpr2::ExprFunc::getDocString(builtins[index].toStdString().c_str())));
94  }else if(role==Qt::ForegroundRole)
95  return functionColor; // darkGreen;
96  }else if(row<variables_offset){
97  int index=row-functions_offset;
98  if(role==Qt::DisplayRole || role==Qt::EditRole){
99  if(column==0) return QVariant(functions[index]);
100  else if(column==1) return QVariant(getFirstLine(functions_comment[index].toStdString()));
101  }else if(role==Qt::ForegroundRole)
102  return functionColor; //darkGreen;
103  }else if(row<local_variables_offset){
104  int index=row-variables_offset;
105  if(role==Qt::DisplayRole || role==Qt::EditRole){
106  if(column==0) return QVariant(variables[index]);
107  else if(column==1) return QVariant(variables_comment[index]);
108  }else if(role==Qt::ForegroundRole)
109  return variableColor;
110  }else if(row<local_variables_offset+(int)local_variables.size()){
111  int index=row-local_variables_offset;
112  if(role==Qt::DisplayRole || role==Qt::EditRole){
113  if(column==0) return QVariant(local_variables[index]);
114  else if(column==1) return QVariant("Local");
115  }else if(role==Qt::ForegroundRole)
116  return variableColor;
117  }
118  return QVariant();
119 }
120 
121 QString ExprCompletionModel::getDocString(const QString& s)
122 {
123  std::map<QString,int>::iterator i=functionNameToFunction.find(s);
124  if(i!=functionNameToFunction.end()){
125  return functions_comment[i->second];
126  }else
127  return SeExpr2::ExprFunc::getDocString(s.toStdString().c_str()).c_str();
128 }
static std::string getDocString(const char *functionName)
Get doc string for a specific function.
Definition: ExprFunc.cpp:174
std::vector< QString > variables
void addVariable(const QString &str, const QString &comment)
static void getFunctionNames(std::vector< std::string > &names)
Get a list of registered builtin and DSO generated functions.
Definition: ExprFunc.cpp:167
std::vector< QString > local_variables
void addFunction(const QString &function, const QString &docString)
std::map< QString, int > functionNameToFunction
The result is computed int int< br >< divstyle="margin-left:40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed).&nbsp
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
ExprCompletionModel(QObject *parent=0)
std::vector< QString > functions_comment
std::vector< QString > functions
std::vector< QString > variables_comment
QString getFirstLine(const std::string &all) const
void syncExtras(const ExprCompletionModel &otherModel)
static std::vector< QString > builtins
QString getDocString(const QString &s)