/* * MathPlanner 3.0.5 - Mathematical design tool. * Copyright(C) 2002 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" sqrt_object::~sqrt_object() { delete Upper; delete Lower; } math_node sqrt_object::Calculate() { math_node uv,lv; uv=Upper->Calculate(); lv=Lower->Calculate(); if (Upper->IsEmpty()) uv.SetReal(2.0); if (uv.type==MN_REAL && lv.type==MN_REAL) { if (lv.mpl.m.R<0) { if ((((int)uv.mpl.m.R)&1)==1) { //negative, odd value.SetReal(-pow(-lv.mpl.m.R,(1.0/uv.mpl.m.R))); return(value); } else lv.type=MN_COMPLEX, lv.mpl.m.I=0; //negative, even } else { value.SetReal(pow(lv.mpl.m.R,(1.0/uv.mpl.m.R))); return(value); } } if ((uv.type==MN_COMPLEX || uv.type==MN_REAL) && (lv.type==MN_COMPLEX || lv.type==MN_REAL)) { value.SetComplex(mpl_cnroot(lv.Complex(),uv.Complex())); return(value); } Error_flag=true; AppControl->AddError(QObject::tr("Only Real & Complex are supported")); return(value); } void sqrt_object::Build() { Upper->Build(); Lower->Build(); } void sqrt_object::SetFontSize(int id) { Upper->SetFontSize(id); Lower->SetFontSize(id); } void sqrt_object::SeekObject(QPoint p) { if (Upper->Bounds().Rect().contains(p)) { Upper->SeekObject(p); return; } if (Lower->Bounds().Rect().contains(p)) { Lower->SeekObject(p); return; } SetFocus(); } void sqrt_object::InsertString(str_range ran,string_object *st) { st->CopyRange(ran,AppControl->Clipboard); st->Cut(ran,true); Lower->PasteSelection(AppControl->Clipboard); SetFocus(); } void sqrt_object::Init() { Lower->SetFocus(); } bool sqrt_object::AutoDeletion() { if (Upper->IsEmpty() && Lower->IsEmpty() && Lower->HasFocus()==false && Upper->HasFocus()==false && HasFocus()==false) return(true); return(false); } void sqrt_object::SendCall(int i) { Upper->SendCall(i); Lower->SendCall(i); } sqrt_object::sqrt_object(DataStorage *msg) :m_base_object(msg) { Reset(); Type=OT_SQRT_OBJECT; MainType=OMT_OPERATOR; Priority=PRIORITY_NONE; SubObject=SUB_NONE; pare.Init(AppControl); DataStorage *ar=new DataStorage(); msg->ReadDataStorage("sqrt:Upper",ar); Upper=new string_object(ar,AppControl,function); delete ar; ar=new DataStorage(); msg->ReadDataStorage("sqrt:Lower",ar); Lower=new string_object(ar,AppControl,function); delete ar; function->AddString(Upper); function->AddString(Lower); Upper->SetParent(this); Lower->SetParent(this); Bug("Root unarchived"); } DataStorage *sqrt_object::BuildStorage() { DataStorage *msg=m_base_object::BuildStorage(); DataStorage *ar=Upper->BuildStorage(); msg->AddDataStorage("sqrt:Upper",ar); delete ar; ar=Lower->BuildStorage(); msg->AddDataStorage("sqrt:Lower",ar); delete ar; return(msg); } sqrt_object::sqrt_object(ApplicationControl *a,m_function_object *o,class string_object *str) :m_base_object(a,o,str) { Reset(); Type=OT_SQRT_OBJECT; MainType=OMT_OPERATOR; Priority=PRIORITY_NONE; SubObject=SUB_NONE; pare.Init(AppControl); Upper=new string_object(10,AppControl,function); Lower=new string_object(10,AppControl,function); Upper->SetFontSize(string->GetFontSize(FONT_TYPE_EXPONENT)); function->AddString(Upper); function->AddString(Lower); Upper->SetParent(this); Lower->SetParent(this); } void sqrt_object::CalculateBounds() { old_bounds=bounds; layout_rect up; Upper->CalculateBounds(); Lower->CalculateBounds(); up=Upper->Bounds(); if (!function->HasFocus() && Upper->IsEmpty()) { up.ascent=0; up.descent=0; up.width=0; } pare.SetSize(0,Lower->Bounds()); pare.SetSize(1,up); bounds=pare.GetOuterRect(); } void sqrt_object::MoveTo(QPoint p) { pare.SetPosition(p); bounds.SetPosition(p); Lower->MoveTo(pare.GetPosition(0)); Upper->MoveTo(pare.GetPosition(1)); } void sqrt_object::Draw(QPainter *paint) { bool foc; Upper->SetRequired(false); if (function->HasFocus() || !Upper->IsEmpty()) Upper->Draw(paint); Lower->Draw(paint); paint->setPen(AppControl->GetColor(FONT_TYPE_OPERATOR)); paint->setBrush(AppControl->GetColor(FONT_TYPE_OPERATOR)); pare.Draw(paint); paint->setBrush(Qt::NoBrush); if (HasFocus() && function->HasFocus()) { paint->setPen(focus_pen); paint->drawRect(bounds.Rect()); } foc=(HasFocus() | Upper->HasFocus() | Lower->HasFocus()) & function->HasFocus(); if (Error_flag && !foc) { paint->setPen(error_pen); paint->drawRect(bounds.Rect()); } } void sqrt_object::KeyReceived(key_code key) { // *********************** // KEY DELIVERY HANDLER // *********************** if (key.code==Qt::Key_Down || key.code==Qt::Key_Up) { if (HasFocus()==true && key.code==Qt::Key_Down) Lower->SetFocus(); if (HasFocus()==true && key.code==Qt::Key_Up) Upper->SetFocus(); } else KeyFeedback(key); } void sqrt_object::KeyFeedback(key_code key) { key.insert_mode=MODE_AUTO; KeyOutput(key); } void sqrt_object::MessageReceived(DataStorage *msg,int rec) { if (rec==TO_OPERATOR) { if (ISMSG(MSG_REMOVE)) { Lower->CopyAll(AppControl->Clipboard); string->MarkForDeletion(string->index); string->RemoveFromString(string->index); string->ChangeIndex(-1); string->PasteSelection(AppControl->Clipboard); } } } base_object_message *sqrt_object::CreateControlObject() { Bug("########### sqrt_mes created"); return(new sqrt_mes(AppControl)); } sqrt_mes::sqrt_mes(ApplicationControl *a) :base_object_message(a) { hbox=new QHBox(); but1=new QToolButton(hbox); but1->setText(tr("Remove")); but1->setAutoRaise(true); connect(but1,SIGNAL(clicked()),this,SLOT(Button())); } sqrt_mes::~sqrt_mes() { delete but1; } QWidget *sqrt_mes::MainWidget() { return(hbox); } void sqrt_mes::Button() { POSTMESSAGE(MSG_REMOVE,TO_OPERATOR); }