! \class FunctionsWidget \brief Widget for selecting supported mathematical functions that can be used in expressions in \c ExpressionTextEdit. \ingroup frontend */
| 19 | \ingroup frontend |
| 20 | */ |
| 21 | FunctionsWidget::FunctionsWidget(QWidget* parent) |
| 22 | : QWidget(parent) { |
| 23 | using Groups = Parsing::FunctionGroups; |
| 24 | |
| 25 | ui.setupUi(this); |
| 26 | ui.bInsert->setIcon(QIcon::fromTheme(QStringLiteral("edit-paste"))); |
| 27 | ui.bCancel->setIcon(QIcon::fromTheme(QStringLiteral("dialog-cancel"))); |
| 28 | m_expressionParser = ExpressionParser::getInstance(); |
| 29 | |
| 30 | QVector<int> separators; |
| 31 | for (int i = 0; i < (int)Groups::END; i++) { |
| 32 | const auto group = static_cast<Groups>(i); |
| 33 | ui.cbGroup->addItem(Parsing::FunctionGroupsToString(group), (int)i); |
| 34 | |
| 35 | // Add separator before these groups |
| 36 | if (group == Groups::ColumnStatistics || group == Groups::AiryFunctionsAndDerivatives || group == Groups::RandomNumberGenerator |
| 37 | || group == Groups::GaussianDistribution) |
| 38 | separators.append(i); |
| 39 | } |
| 40 | |
| 41 | for (int i = 0; i < separators.count(); i++) |
| 42 | ui.cbGroup->insertSeparator(separators.at(i) + i); // + i because of previous added separator |
| 43 | |
| 44 | // SLOTS |
| 45 | connect(ui.leFilter, &QLineEdit::textChanged, this, &FunctionsWidget::filterChanged); |
| 46 | connect(ui.cbGroup, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &FunctionsWidget::groupChanged); |
| 47 | connect(ui.bInsert, &QPushButton::clicked, this, &FunctionsWidget::insertClicked); |
| 48 | connect(ui.bCancel, &QPushButton::clicked, this, &FunctionsWidget::canceled); |
| 49 | connect(ui.lwFunctions, &QListWidget::itemDoubleClicked, this, &FunctionsWidget::insertClicked); |
| 50 | |
| 51 | // set the focus to the search field and select the first group after the widget is shown |
| 52 | QTimer::singleShot(0, this, [=]() { |
| 53 | ui.leFilter->setFocus(); |
| 54 | this->groupChanged(0); |
| 55 | }); |
| 56 | |
| 57 | // set the minimum size to show the longest funciton description without any horizontal scroll bar |
| 58 | const QStringList& names = m_expressionParser->functionsDescriptions(); |
| 59 | QString maxName; |
| 60 | int maxLength = 0; |
| 61 | for (const auto& name : names) { |
| 62 | int length = name.length(); |
| 63 | if (length > maxLength) { |
| 64 | maxLength = length; |
| 65 | maxName = name; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | QFont font; |
| 70 | QFontMetrics fm(font); |
| 71 | const auto margins = layout()->contentsMargins(); |
| 72 | const int width = fm.horizontalAdvance(maxName) + margins.left() + margins.right() + qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent); |
| 73 | setMinimumWidth(width); |
| 74 | } |
| 75 | |
| 76 | /*! |
| 77 | * shows all functions of the selected group and selects the first one in the QStringList |
nothing calls this directly
no test coverage detected