KSeExpr  4.0.4.0
ExprCompletionModel.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2 // SPDX-License-Identifier: LicenseRef-Apache-2.0
3 // SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 /*
6  * @file ExprCompletionModel.h
7  * @brief This provides an expression editor for SeExpr syntax with auto ui features
8  * @author aselle, amyspark
9  */
10 #include "ExprCompletionModel.h"
11 #include <KSeExpr/ExprFunc.h>
12 #include <KSeExpr/Expression.h>
13 #include <QCoreApplication>
14 #include <QLineEdit>
15 
16 static const char *CONTEXT = "builtin";
17 
19  : QAbstractItemModel(parent)
20 {
21  std::vector<std::string> builtins_std;
23  for (const auto &func : builtins_std) {
24  // Remember to extract all strings from the builtins! -- amyspark
25  builtins.push_back(QCoreApplication::translate(CONTEXT, func.c_str()));
26  }
27 }
28 
30 {
31  variables.clear();
32  variables_comment.clear();
33 }
34 
35 void ExprCompletionModel::addVariable(const QString &str, const QString &comment)
36 {
37  variables.push_back(str);
38  variables_comment.push_back(comment);
39 }
40 
42 {
43  functions.clear();
44  functions_comment.clear();
45  functionNameToFunction.clear();
46 }
47 
48 void ExprCompletionModel::addFunction(const QString &str, const QString &comment)
49 {
51  functions.push_back(str);
52  functions_comment.push_back(comment);
53 }
54 
56 {
58  functions = otherModel.functions;
60  variables = otherModel.variables;
62 }
63 
64 QVariant ExprCompletionModel::data(const QModelIndex &index, int role) const
65 {
66  static QColor variableColor = QColor(100, 200, 250);
67  static QColor functionColor = QColor(100, 250, 200);
68  static QColor backgroundColor(50, 50, 50);
69 
70  if (!index.isValid())
71  return QVariant();
72  auto row = static_cast<size_t>(index.row());
73  auto column = index.column();
74 
75  auto functions_offset = builtins.size();
76  auto variables_offset = functions_offset + functions.size();
77  auto local_variables_offset = variables_offset + variables.size();
78 
79  if (role == Qt::BackgroundRole)
80  return backgroundColor;
81 
82  if (role == Qt::FontRole && column == 0) {
83  QFont font;
84  font.setBold(true);
85  return font;
86  }
87 
88  if (row < functions_offset) {
89  auto index = row;
90  if (role == Qt::DisplayRole || role == Qt::EditRole) {
91  if (column == 0)
92  return QVariant(builtins[index]);
93  else if (column == 1)
94  return QVariant(getFirstLine(KSeExpr::ExprFunc::getDocString(builtins[index].toStdString().c_str())));
95  } else if (role == Qt::ForegroundRole)
96  return functionColor; // darkGreen;
97  } else if (row < variables_offset) {
98  auto index = row - functions_offset;
99  if (role == Qt::DisplayRole || role == Qt::EditRole) {
100  if (column == 0)
101  return QVariant(functions[index]);
102  else if (column == 1)
103  return QVariant(getFirstLine(functions_comment[index].toStdString()));
104  } else if (role == Qt::ForegroundRole)
105  return functionColor; // darkGreen;
106  } else if (row < local_variables_offset) {
107  auto index = row - variables_offset;
108  if (role == Qt::DisplayRole || role == Qt::EditRole) {
109  if (column == 0)
110  return QVariant(variables[index]);
111  else if (column == 1)
112  return QVariant(variables_comment[index]);
113  } else if (role == Qt::ForegroundRole)
114  return variableColor;
115  } else if (row < local_variables_offset + local_variables.size()) {
116  auto index = row - local_variables_offset;
117  if (role == Qt::DisplayRole || role == Qt::EditRole) {
118  if (column == 0)
119  return QVariant(local_variables[index]);
120  else if (column == 1)
121  return QVariant(tr("Local"));
122  } else if (role == Qt::ForegroundRole)
123  return variableColor;
124  }
125  return QVariant();
126 }
127 
128 QString ExprCompletionModel::getDocString(const QString &s)
129 {
130  auto i = functionNameToFunction.find(s);
131  if (i != functionNameToFunction.end()) {
132  return functions_comment[i->second];
133  } else
134  return tr(KSeExpr::ExprFunc::getDocString(s.toStdString().c_str()).c_str());
135 }
static const char * CONTEXT
QModelIndex index(int row, int column, const QModelIndex &) const override
std::vector< QString > local_variables
static QString getFirstLine(const std::string &all)
std::vector< QString > functions
std::vector< QString > variables
std::vector< QString > functions_comment
QString getDocString(const QString &s)
ExprCompletionModel(QObject *parent=0)
void syncExtras(const ExprCompletionModel &otherModel)
void addVariable(const QString &str, const QString &comment)
void addFunction(const QString &, const QString &)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
std::vector< QString > variables_comment
std::vector< QString > builtins
std::map< QString, int > functionNameToFunction
static void getFunctionNames(std::vector< std::string > &names)
Get a list of registered builtin and DSO generated functions.
Definition: ExprFunc.cpp:166
static std::string getDocString(const char *functionName)
Get doc string for a specific function.
Definition: ExprFunc.cpp:174