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
26std::vector<QString> ExprCompletionModel::builtins;
27
28ExprCompletionModel::ExprCompletionModel(QObject* parent) : QAbstractItemModel(parent) {
29 if (builtins.size() == 0) {
30 std::vector<std::string> 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
37 variables.clear();
38 variables_comment.clear();
39}
40
41void ExprCompletionModel::addVariable(const QString& str, const QString& comment) {
42 variables.push_back(str);
43 variables_comment.push_back(comment);
44}
45
47 functions.clear();
48 functions_comment.clear();
50}
51
52void ExprCompletionModel::addFunction(const QString& str, const QString& comment) {
54 functions.push_back(str);
55 functions_comment.push_back(comment);
56}
57
60 functions = otherModel.functions;
62 variables = otherModel.variables;
64}
65
66QVariant 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
125QString 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}
std::vector< QString > local_variables
static std::vector< QString > builtins
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
std::vector< QString > functions
std::vector< QString > variables
std::vector< QString > functions_comment
QString getFirstLine(const std::string &all) const
QModelIndex index(int row, int column, const QModelIndex &) const
QString getDocString(const QString &s)
ExprCompletionModel(QObject *parent=0)
void syncExtras(const ExprCompletionModel &otherModel)
void addVariable(const QString &str, const QString &comment)
std::vector< QString > variables_comment
void addFunction(const QString &function, const QString &docString)
std::map< QString, int > functionNameToFunction
static std::string getDocString(const char *functionName)
Get doc string for a specific function.
Definition: ExprFunc.cpp:183
static void getFunctionNames(std::vector< std::string > &names)
Get a list of registered builtin and DSO generated functions.
Definition: ExprFunc.cpp:176
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