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 <QLineEdit>
22 #include <SeExpr2/Expression.h>
23 #include <SeExpr2/ExprFunc.h>
24 #include "ExprCompletionModel.h"
25
26 std::vector<QString> ExprCompletionModel::builtins;
27
28 ExprCompletionModel::ExprCompletionModel(QObject* parent) : QAbstractItemModel(parent) {
29 if (builtins.size() == 0) {
30 std::vector<std::string> builtins_std;
31 SeExpr2::ExprFunc::getFunctionNames(builtins_std);
32 for (unsigned int i = 0; i < builtins_std.size(); i++) builtins.push_back(QString(builtins_std[i].c_str()));
33 }
34 }
35
36 void ExprCompletionModel::clearVariables() {
37 variables.clear();
38 variables_comment.clear();
39 }
40
41 void ExprCompletionModel::addVariable(const QString& str, const QString& comment) {
42 variables.push_back(str);
43 variables_comment.push_back(comment);
44 }
45
46 void ExprCompletionModel::clearFunctions() {
47 functions.clear();
48 functions_comment.clear();
49 functionNameToFunction.clear();
50 }
51
52 void ExprCompletionModel::addFunction(const QString& str, const QString& comment) {
53 functionNameToFunction[str] = functions_comment.size();
54 functions.push_back(str);
55 functions_comment.push_back(comment);
56 }
57
58 void ExprCompletionModel::syncExtras(const ExprCompletionModel& otherModel) {
59 functionNameToFunction = otherModel.functionNameToFunction;
60 functions = otherModel.functions;
61 functions_comment = otherModel.functions_comment;
62 variables = otherModel.variables;
63 variables_comment = otherModel.variables_comment;
64 }
65
66 QVariant ExprCompletionModel::data(const QModelIndex& index, int role) const {
67 static QColor variableColor = QColor(100, 200, 250), functionColor = QColor(100, 250, 200),
68 backgroundColor(50, 50, 50);
69
70 if (!index.isValid()) return QVariant();
71 int row = index.row(), column = index.column();
72
73 int functions_offset = builtins.size();
74 int variables_offset = functions_offset + functions.size();
75 int local_variables_offset = variables_offset + variables.size();
76
77 if (role == Qt::BackgroundRole) return backgroundColor;
78
79 if (role == Qt::FontRole && column == 0) {
80 QFont font;
81 font.setBold(true);
82 return font;
83 }
84
85 if (row < functions_offset) {
86 int index = row;
87 if (role == Qt::DisplayRole || role == Qt::EditRole) {
88 if (column == 0)
89 return QVariant(builtins[index]);
90 else if (column == 1)
91 return QVariant(getFirstLine(SeExpr2::ExprFunc::getDocString(builtins[index].toStdString().c_str())));
92 } else if (role == Qt::ForegroundRole)
93 return functionColor; // darkGreen;
94 } else if (row < variables_offset) {
95 int index = row - functions_offset;
96 if (role == Qt::DisplayRole || role == Qt::EditRole) {
97 if (column == 0)
98 return QVariant(functions[index]);
99 else if (column == 1)
100 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)
107 return QVariant(variables[index]);
108 else if (column == 1)
109 return QVariant(variables_comment[index]);
110 } else if (role == Qt::ForegroundRole)
111 return variableColor;
112 } else if (row < local_variables_offset + (int)local_variables.size()) {
113 int index = row - local_variables_offset;
114 if (role == Qt::DisplayRole || role == Qt::EditRole) {
115 if (column == 0)
116 return QVariant(local_variables[index]);
117 else if (column == 1)
118 return QVariant("Local");
119 } else if (role == Qt::ForegroundRole)
120 return variableColor;
121 }
122 return QVariant();
123 }
124
125 QString ExprCompletionModel::getDocString(const QString& s) {
126 std::map<QString, int>::iterator i = functionNameToFunction.find(s);
127 if (i != functionNameToFunction.end()) {
128 return functions_comment[i->second];
129 } else
130 return SeExpr2::ExprFunc::getDocString(s.toStdString().c_str()).c_str();
131 }
ExprCompletionModel::getDocString
QString getDocString(const QString &s)
Definition: ExprCompletionModel.cpp:125
ExprCompletionModel::clearVariables
void clearVariables()
Definition: ExprCompletionModel.cpp:36
index
The result is computed int int< br >< div style="margin-left: 40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed). &nbsp
ExprCompletionModel::getFirstLine
QString getFirstLine(const std::string &all) const
Definition: ExprCompletionModel.h:61
SeExpr2::ExprFunc::getFunctionNames
static void getFunctionNames(std::vector< std::string > &names)
Get a list of registered builtin and DSO generated functions.
Definition: ExprFunc.cpp:176
ExprCompletionModel::functionNameToFunction
std::map< QString, int > functionNameToFunction
Definition: ExprCompletionModel.h:90
ExprFunc.h
ExprCompletionModel::functions_comment
std::vector< QString > functions_comment
Definition: ExprCompletionModel.h:89
ExprCompletionModel::local_variables
std::vector< QString > local_variables
Definition: ExprCompletionModel.h:83
ExprCompletionModel::addVariable
void addVariable(const QString &str, const QString &comment)
Definition: ExprCompletionModel.cpp:41
ExprCompletionModel::index
QModelIndex index(int row, int column, const QModelIndex &) const
Definition: ExprCompletionModel.h:46
ExprCompletionModel::clearFunctions
void clearFunctions()
Definition: ExprCompletionModel.cpp:46
ExprCompletionModel::ExprCompletionModel
ExprCompletionModel(QObject *parent=0)
Definition: ExprCompletionModel.cpp:28
ExprCompletionModel::variables
std::vector< QString > variables
Definition: ExprCompletionModel.h:91
ExprCompletionModel
Definition: ExprCompletionModel.h:30
ExprCompletionModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: ExprCompletionModel.cpp:66
ExprCompletionModel::functions
std::vector< QString > functions
Definition: ExprCompletionModel.h:89
ExprCompletionModel.h
ExprCompletionModel::builtins
static std::vector< QString > builtins
Definition: ExprCompletionModel.h:88
ExprCompletionModel::addFunction
void addFunction(const QString &function, const QString &docString)
Definition: ExprCompletionModel.cpp:52
SeExpr2::ExprFunc::getDocString
static std::string getDocString(const char *functionName)
Get doc string for a specific function.
Definition: ExprFunc.cpp:183
Expression.h
ExprCompletionModel::variables_comment
std::vector< QString > variables_comment
Definition: ExprCompletionModel.h:91
ExprCompletionModel::syncExtras
void syncExtras(const ExprCompletionModel &otherModel)
Definition: ExprCompletionModel.cpp:58