/* * MathPlanner 3.2.0 - Mathematical design tool. * Copyright(C) 2003 Jarmo Nikkanen * * 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. * * You should have received a copy of the GNU General Public License with this program. * */ #include "Operators.h" #include "AppControl.h" #include "Error.h" #include "Function_object.h" #include "ConfigReader.h" #include "Datastore.h" #include "Object_string.h" #include "Extern.h" math_node exp_object::Calculate(math_node val) { math_node eval; eval=Inner->Calculate(); if (Opr==0) { if (val.type==MN_REAL && eval.type==MN_REAL) { value.SetReal(pow(val.mpl.m.R,eval.mpl.m.R)); return(value); } else if ((val.type==MN_COMPLEX || val.type==MN_REAL) && (eval.type==MN_COMPLEX || eval.type==MN_REAL)) { value.SetComplex(mpl_cnpower(val.Complex(),eval.Complex())); return(value); } } Error_flag=true; AppControl->AddError(QObject::tr("Value type is not supported by this function")); return(value); } math_node exp_object::Calculate() { math_node eval; eval=Inner->Calculate(); if (eval.type==MN_REAL) { if (Opr==1) { value.SetReal(pow(NEPER,eval.mpl.m.R)); return(value); } if (Opr==2) { value.SetReal(pow(10,eval.mpl.m.R)); return(value); } } if (eval.type==MN_COMPLEX) { if (Opr==1) { value.SetComplex(mpl_ce(eval.Complex())); return(value); } if (Opr==2) { value.SetComplex(mpl_cexp(eval.Complex())); return(value); } } else { Error_flag=true; AppControl->AddError(QObject::tr("Value type is not supported by this function")); } return(value); } void exp_object::Build() { Inner->Build(); } void exp_object::SendCall(int i) { Inner->SendCall(i); } void exp_object::SeekObject(QPoint p) { if (Inner->Bounds().Rect().contains(p)) { Inner->SeekObject(p); return; } SetFocus(); } bool exp_object::AutoDeletion() { if (Inner->IsEmpty() && Inner->HasFocus()==false) return(true); return(false); } // This is called when multible objects are selected void exp_object::InsertString(str_range ran,string_object *st) { if (Opr==0) { st->SetParenthesis(ran); } else { st->CopyRange(ran,AppControl->Clipboard); st->Cut(ran,true); Inner->PasteSelection(AppControl->Clipboard); } Inner->SetFocus(); } exp_object::exp_object(DataStorage *msg) :m_base_object(msg) { Reset(); exp.Init(AppControl); text.Init(AppControl); Type=OT_EXPONENT_OBJECT; MainType=OMT_OPERATOR; Opr=msg->ReadInt("exp:type"); QString Exp=AppControl->Prefs->String("ExpSymbol"); if (Opr==0) strcpy(Opr_Str_A,""),SubObject=SUB_LEFT,Priority=PRIORITY_POWER; if (Opr==1) strcpy(Opr_Str_A,"e"),SubObject=SUB_NONE,Priority=PRIORITY_NONE; if (Opr==2) strcpy(Opr_Str_A,Exp.latin1()),SubObject=SUB_NONE,Priority=PRIORITY_NONE; DataStorage *ar=new DataStorage(); msg->ReadDataStorage("exp:Inner",ar); Inner=new string_object(ar,AppControl,function); delete ar; function->AddString(Inner); Inner->SetParent(this); Bug("exp object unarchived"); } exp_object::~exp_object() { delete Inner; } DataStorage *exp_object::BuildStorage() { DataStorage *msg=m_base_object::BuildStorage(); msg->AddInt("exp:type",Opr); DataStorage *ar=Inner->BuildStorage(); msg->AddDataStorage("exp:Inner",ar); delete ar; return(msg); } void exp_object::SetFontSize(int id) { Inner->SetFontSize(id); } exp_object::exp_object(int type,ApplicationControl *a,m_function_object *o,class string_object *str) :m_base_object(a,o,str) { Reset(); exp.Init(a); text.Init(a); Inner=new string_object(10,AppControl,function); Inner->SetFontSize(string->GetFontSize(FONT_TYPE_EXPONENT)); QString Exp=AppControl->Prefs->String("ExpSymbol"); Opr=type; if (Opr==0) strcpy(Opr_Str_A,""),SubObject=SUB_LEFT,Priority=PRIORITY_POWER; if (Opr==1) strcpy(Opr_Str_A,"e"),SubObject=SUB_NONE,Priority=PRIORITY_NONE; if (Opr==2) strcpy(Opr_Str_A,Exp.latin1()),SubObject=SUB_NONE,Priority=PRIORITY_NONE; Type=OT_EXPONENT_OBJECT; MainType=OMT_OPERATOR; function->AddString(Inner); Inner->SetParent(this); Inner->SetFocus(); } void exp_object::CalculateBounds() { old_bounds=bounds; layout_rect lo; QFont f=string->GetFont(FONT_TYPE_OPERATOR); Inner->CalculateBounds(); exp.SetSize(1,Inner->Bounds()); if (Opr==1 || Opr==2) exp.SetSize(0,text.GetOuterRect(f,Opr_Str_A)); else { m_base_object *obj=string->GetLeftSideObject(this); if (obj) { obj->CalculateBounds(); lo=obj->bounds; lo.width=0; exp.SetSize(0,lo); } } bounds=exp.GetOuterRect(); } void exp_object::MoveTo(QPoint p) { exp.SetPosition(p); bounds.SetPosition(p); Inner->MoveTo(exp.GetPosition(1)); text.SetPosition(exp.GetPosition(0)); } void exp_object::Draw(QPainter *paint) { bool foc; paint->setFont(string->GetFont(FONT_TYPE_OPERATOR)); paint->setPen(AppControl->GetColor(FONT_TYPE_OPERATOR)); text.Draw(paint); Inner->Draw(paint); if (HasFocus() && function->HasFocus()) { paint->setPen(focus_pen); paint->drawRect(bounds.Rect()); } foc=(HasFocus() | Inner->HasFocus()) & function->HasFocus(); if (Error_flag && !foc) { paint->setPen(error_pen); paint->drawRect(bounds.Rect()); } } void exp_object::KeyReceived(key_code key) { if (key.code==Qt::Key_Up) { if (HasFocus()==true && key.code==Qt::Key_Up) Inner->SetFocus(); } else KeyFeedback(key); } void exp_object::KeyFeedback(key_code key) { key.insert_mode=MODE_AUTO; KeyOutput(key); } void exp_object::MessageReceived(DataStorage *msg,int rec) { if (rec==TO_OPERATOR) { if (ISMSG(MSG_REMOVE)) { Inner->CopyAll(AppControl->Clipboard); string->MarkForDeletion(string->index); string->RemoveFromString(string->index); string->ChangeIndex(-1); string->PasteSelection(AppControl->Clipboard); } } } base_object_message *exp_object::CreateControlObject() { return(new sqrt_mes(AppControl)); }