diff options
Diffstat (limited to 'src/qalculate_tde_utils.cpp')
-rw-r--r-- | src/qalculate_tde_utils.cpp | 307 |
1 files changed, 307 insertions, 0 deletions
diff --git a/src/qalculate_tde_utils.cpp b/src/qalculate_tde_utils.cpp new file mode 100644 index 0000000..7a52429 --- /dev/null +++ b/src/qalculate_tde_utils.cpp @@ -0,0 +1,307 @@ +/*************************************************************************** + * Copyright (C) 2005 by Niklas Knutsson * + * nq@altern.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "qalculate_tde_utils.h" +#include <qwidget.h> +#include <qlabel.h> +#include <qlineedit.h> +#include <qstring.h> +#include <qfont.h> +#include <qfontmetrics.h> +#include "kqalculate.h" + +tree_struct function_cats, unit_cats, variable_cats; +vector<void*> ia_units, ia_variables, ia_functions; +vector<MathFunction*> recent_functions; +vector<Variable*> recent_variables; +vector<Unit*> recent_units; +extern PrintOptions printops; +extern KnownVariable *vans[5]; +extern QWidget *topWidget; +extern KQalculate *mainWin; + +void insert_text_in_expression(const QString &str) { + mainWin->insert_text(str); +} + +bool is_answer_variable(Variable *v) { + return v == vans[0] || v == vans[1] || v == vans[2] || v == vans[3] || v == vans[4]; +} + +QString get_value_string(const MathStructure &mstruct_, bool rlabel, Prefix *prefix) { + printops.allow_non_usable = rlabel; + printops.prefix = prefix; + QString str = CALCULATOR->printMathStructureTimeOut(mstruct_, 100, printops).c_str(); + printops.allow_non_usable = false; + printops.prefix = NULL; + return str; +} + +void set_name_label_and_entry(ExpressionItem *item, QLineEdit *entry, QLabel *label) { + const ExpressionName *ename = &item->getName(1); + entry->setText(ename->name.c_str()); + if(item->countNames() > 1) { + QString str = "+ "; + for(size_t i = 2; i <= item->countNames(); i++) { + if(i > 2) str += ", "; + str += item->getName(i).name.c_str(); + } + label->setText(str); + } +} + +bool can_display_unicode_string_function(const char *str, void *arg) { + if(!arg) arg = (void*) topWidget; + QFontMetrics fm(((QWidget*) arg)->fontMetrics()); + QString qstr(str); + const QChar *qchars = qstr.unicode(); + for(uint i = 0; i < qstr.length(); i++) { + if(!fm.inFont(qchars[i])) return false; + } + return true; +} + +void generate_units_tree_struct() { + size_t cat_i, cat_i_prev; + bool b; + string str, cat, cat_sub; + Unit *u = NULL; + unit_cats.items.clear(); + unit_cats.objects.clear(); + unit_cats.parent = NULL; + ia_units.clear(); + list<tree_struct>::iterator it; + for(size_t i = 0; i < CALCULATOR->units.size(); i++) { + if(!CALCULATOR->units[i]->isActive()) { + b = false; + for(size_t i3 = 0; i3 < ia_units.size(); i3++) { + u = (Unit*) ia_units[i3]; + if(CALCULATOR->units[i]->title() < u->title()) { + b = true; + ia_units.insert(ia_units.begin() + i3, (void*) CALCULATOR->units[i]); + break; + } + } + if(!b) ia_units.push_back((void*) CALCULATOR->units[i]); + } else { + tree_struct *item = &unit_cats; + if(!CALCULATOR->units[i]->category().empty()) { + cat = CALCULATOR->units[i]->category(); + cat_i = cat.find("/"); cat_i_prev = 0; + b = false; + while(true) { + if(cat_i == string::npos) { + cat_sub = cat.substr(cat_i_prev, cat.length() - cat_i_prev); + } else { + cat_sub = cat.substr(cat_i_prev, cat_i - cat_i_prev); + } + b = false; + for(it = item->items.begin(); it != item->items.end(); ++it) { + if(cat_sub == it->item) { + item = &*it; + b = true; + break; + } + } + if(!b) { + tree_struct cat; + item->items.push_back(cat); + it = item->items.end(); + --it; + it->parent = item; + item = &*it; + item->item = cat_sub; + } + if(cat_i == string::npos) { + break; + } + cat_i_prev = cat_i + 1; + cat_i = cat.find("/", cat_i_prev); + } + } + b = false; + for(size_t i3 = 0; i3 < item->objects.size(); i3++) { + u = (Unit*) item->objects[i3]; + if(CALCULATOR->units[i]->title() < u->title()) { + b = true; + item->objects.insert(item->objects.begin() + i3, (void*) CALCULATOR->units[i]); + break; + } + } + if(!b) item->objects.push_back((void*) CALCULATOR->units[i]); + } + } + + unit_cats.sort(); + +} +void generate_variables_tree_struct() { + + size_t cat_i, cat_i_prev; + bool b; + string str, cat, cat_sub; + Variable *v = NULL; + variable_cats.items.clear(); + variable_cats.objects.clear(); + variable_cats.parent = NULL; + ia_variables.clear(); + list<tree_struct>::iterator it; + for(size_t i = 0; i < CALCULATOR->variables.size(); i++) { + if(!CALCULATOR->variables[i]->isActive()) { + //deactivated variable + b = false; + for(size_t i3 = 0; i3 < ia_variables.size(); i3++) { + v = (Variable*) ia_variables[i3]; + if(CALCULATOR->variables[i]->title() < v->title()) { + b = true; + ia_variables.insert(ia_variables.begin() + i3, (void*) CALCULATOR->variables[i]); + break; + } + } + if(!b) ia_variables.push_back((void*) CALCULATOR->variables[i]); + } else { + tree_struct *item = &variable_cats; + if(!CALCULATOR->variables[i]->category().empty()) { + cat = CALCULATOR->variables[i]->category(); + cat_i = cat.find("/"); cat_i_prev = 0; + b = false; + while(true) { + if(cat_i == string::npos) { + cat_sub = cat.substr(cat_i_prev, cat.length() - cat_i_prev); + } else { + cat_sub = cat.substr(cat_i_prev, cat_i - cat_i_prev); + } + b = false; + for(it = item->items.begin(); it != item->items.end(); ++it) { + if(cat_sub == it->item) { + item = &*it; + b = true; + break; + } + } + if(!b) { + tree_struct cat; + item->items.push_back(cat); + it = item->items.end(); + --it; + it->parent = item; + item = &*it; + item->item = cat_sub; + } + if(cat_i == string::npos) { + break; + } + cat_i_prev = cat_i + 1; + cat_i = cat.find("/", cat_i_prev); + } + } + b = false; + for(size_t i3 = 0; i3 < item->objects.size(); i3++) { + v = (Variable*) item->objects[i3]; + if(CALCULATOR->variables[i]->title() < v->title()) { + b = true; + item->objects.insert(item->objects.begin() + i3, (void*) CALCULATOR->variables[i]); + break; + } + } + if(!b) item->objects.push_back((void*) CALCULATOR->variables[i]); + } + } + + variable_cats.sort(); + +} +void generate_functions_tree_struct() { + + size_t cat_i, cat_i_prev; + bool b; + string str, cat, cat_sub; + MathFunction *f = NULL; + function_cats.items.clear(); + function_cats.objects.clear(); + function_cats.parent = NULL; + ia_functions.clear(); + list<tree_struct>::iterator it; + + for(size_t i = 0; i < CALCULATOR->functions.size(); i++) { + if(!CALCULATOR->functions[i]->isActive()) { + //deactivated function + b = false; + for(size_t i3 = 0; i3 < ia_functions.size(); i3++) { + f = (MathFunction*) ia_functions[i3]; + if(CALCULATOR->functions[i]->title() < f->title()) { + b = true; + ia_functions.insert(ia_functions.begin() + i3, (void*) CALCULATOR->functions[i]); + break; + } + } + if(!b) ia_functions.push_back((void*) CALCULATOR->functions[i]); + } else { + tree_struct *item = &function_cats; + if(!CALCULATOR->functions[i]->category().empty()) { + cat = CALCULATOR->functions[i]->category(); + cat_i = cat.find("/"); cat_i_prev = 0; + b = false; + while(true) { + if(cat_i == string::npos) { + cat_sub = cat.substr(cat_i_prev, cat.length() - cat_i_prev); + } else { + cat_sub = cat.substr(cat_i_prev, cat_i - cat_i_prev); + } + b = false; + for(it = item->items.begin(); it != item->items.end(); ++it) { + if(cat_sub == it->item) { + item = &*it; + b = true; + break; + } + } + if(!b) { + tree_struct cat; + item->items.push_back(cat); + it = item->items.end(); + --it; + it->parent = item; + item = &*it; + item->item = cat_sub; + } + if(cat_i == string::npos) { + break; + } + cat_i_prev = cat_i + 1; + cat_i = cat.find("/", cat_i_prev); + } + } + b = false; + for(size_t i3 = 0; i3 < item->objects.size(); i3++) { + f = (MathFunction*) item->objects[i3]; + if(CALCULATOR->functions[i]->title() < f->title()) { + b = true; + item->objects.insert(item->objects.begin() + i3, (void*) CALCULATOR->functions[i]); + break; + } + } + if(!b) item->objects.push_back((void*) CALCULATOR->functions[i]); + } + } + + function_cats.sort(); + +} |