1 /*
2 * Copyright Disney Enterprises, Inc.  All rights reserved.
3 * Copyright (C) 2020 L. E. Segovia <amy@amyspark.me>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License
7 * and the following modification to it: Section 6 Trademarks.
8 * deleted and replaced with:
9 *
10 * 6. Trademarks. This License does not grant permission to use the
11 * trade names, trademarks, service marks, or product names of the
12 * Licensor and its affiliates, except as required for reproducing
13 * the content of the NOTICE file.
14 *
15 * You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * @file ExprCompletionModel.h
19 * @brief Provides a model for providing completion items
20 * @author  aselle
21 */
22 
23 #ifndef ExprCompletionModel_h
24 #define ExprCompletionModel_h
25 
26 #include <QtCore/QAbstractItemModel>
27 #include <QtCore/QString>
28 #include <QtCore/QSize>
29 #include <vector>
30 
31 class ExprCompletionModel : public QAbstractItemModel  // ItemModel
32 {
33   public:
34     // clear/add functions (these are ones that will be resolved with resolveFunc()
35     void clearFunctions();
36     void addFunction(const QString& function, const QString& docString);
37 
38     // clear/add user variables (these are ones that will be resolved with resolveVar()
39     void clearVariables();
40     void addVariable(const QString& str, const QString& comment);
41 
42     // add extras
43     void syncExtras(const ExprCompletionModel& otherModel);
44 
45     ExprCompletionModel(QObject* parent = 0);
46 
index(int row,int column,const QModelIndex &)47     QModelIndex index(int row, int column, const QModelIndex&) const { return createIndex(row, column, nullptr); }
48 
parent(const QModelIndex &)49     QModelIndex parent(const QModelIndex&) const { return QModelIndex(); }
50 
51     int rowCount(const QModelIndex& parent = QModelIndex()) const {
52         Q_UNUSED(parent);
53         int count = builtins.size() + functions.size() + variables.size() + local_variables.size();
54         return count;
55     }
56 
columnCount(const QModelIndex & parent)57     int columnCount(const QModelIndex& parent) const {
58         Q_UNUSED(parent);
59         return 2;
60     }
61 
getFirstLine(const std::string & all)62     QString getFirstLine(const std::string& all) const {
63         size_t newline = all.find("\n");
64         if (newline != std::string::npos)
65             return QString::fromStdString(all.substr(0, newline));
66         else
67             return QString::fromStdString(all);
68     }
69 
70     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
71 
72     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const {
73         Q_UNUSED(orientation);
74         if (role == Qt::DisplayRole)
75             return QVariant();
76         else if (role == Qt::SizeHintRole) {
77             if (section == 0)
78                 return QVariant(QSize(100, 1));
79             else
80                 return QVariant(QSize(200, 1));
81         } else
82             return QVariant();
83     }
84     std::vector<QString> local_variables;  // only the expression editor itself should modify these
85 
86     QString getDocString(const QString& s);
87 
88   private:
89     Q_OBJECT;
90 
91     std::vector<QString> builtins;
92     std::vector<QString> functions, functions_comment;
93     std::map<QString, int> functionNameToFunction;
94     std::vector<QString> variables, variables_comment;
95 };
96 
97 #endif
98