⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gwcalcfunc.cpp

📁 嵌入式linux下的一个高级计算器程序的源码
💻 CPP
字号:
/*    gwCalcFunc.cpp  by Dr. Gerhard Wanderer   (gerhard.wanderer@i-dail.de)    $Id: gwCalcFunc.cpp,v 1.2 2002/12/15 21:11:57 gerhard Exp gerhard $    Pocket Calculator for the Zaurus and for Linux with QT-Library    Function Editor Popup    =====================================================================*/#include <ctype.h>#include <malloc.h>#include <math.h>#include <qfont.h>#include <stdio.h>#include <string.h>#include <qwidget.h>#include <qpushbutton.h>#include <qlabel.h>#include <qcombobox.h>#include "gwCalc.h"//voidFuncList::setPosition(int beg)  //  // set position of the scroll-list (parameter names) according  // scrollbar position{  char  *tmp, *text;  QString  qs;  int   i;  if (!initial) { // if initial, don't read outdated values from screen    // transfer names from screen to tempory list; remember modifications    // vbeg is the index to the first entry before scollbar change    for (i = 0; i < 6; i++) {      qs = p[i]->text();      if(qs) {	tmp = (char*)qs.latin1();		if (tmp && strlen(tmp)) {	  if (i + vbeg == count) count = i + vbeg+1;	  text = (char*)malloc(strlen(tmp)+1);	  strcpy(text, tmp);	  	  if (va[vbeg+i]) free (va[vbeg+i]);	  va[vbeg+i] = text;	}	else {	  if (vbeg+i < count) {	    count = vbeg+i; 	    va[vbeg+i] = NULL;	  }	}      }    }  }  vbeg = beg;  // copy new values according changed scrollbar position  for (i = 0; i < 6; i++){    if ((i + beg < count) && va[i+beg])       p[i]->setText(va[i+beg]);    else      p[i]->setText("");  }   initial = 0;} voidFuncList::Activate(int pos)  //   // select a funcition from the function list  // display the parameterlist and function definition  // of the selected function{  int  i;  cur = pos;  if (canz > 0) {    f->setText(c[cur+1].Name);    // copy variable names to temporary array, allowing    // ignoring of changes    count = c[cur+1].Vars;    for (i = 0; i < 100; i++) va[i] = NULL;    for (i = 0; i < count; i++) {      va[i] = (char*) malloc(strlen(c[cur+1].Var[i].Name)+1);      strcpy(va[i], c[cur+1].Var[i].Name);    }    initial = 1;   // set parameterlist without read from screen    setPosition(0);      inpfeld->clear();     inpfeld->insert(c[cur+1].Def); // show function definition  }  else    count = 0;}voidFuncList::setList(CALCFKT *cal, int anz)  //  // set the pointer to the functionlist; add functionlist  // names to combobox{  int  i;  c    = cal;  canz = anz;  for (i = 0; i < anz; i++)    fkt->insertItem(cal[i+1].Name, -1);  for (i = 0; i < 100; i++)     va[i] = NULL;  // display the first function in the list  Activate(0); } intFuncList::getNumber()  // Query the number of functions{  return canz;}voidFuncList::InsertButtonS01()  // add a new function to the function list{  cur = canz;  canz++;  //create a new function entry  c[cur+1].Name = NULL;  c[cur+1].Def  = NULL;  c[cur+1].Vars = 0;  // add a preliminary name to the combobox  fkt->insertItem("NewFunction", -1);    if (canz == 1)     initial = 0;  // call modify fucntion to read in the function parameters  InsertButtonS03();  // refresh the display for the added function  Activate(cur);  }voidFuncList::InsertButtonS02()  // delete a function from the function list{  int i;  if (canz) {    // remove the memory allocated by curr. function    free(c[cur+1].Name);    free(c[cur+1].Def);    for (i = 0; i < c[cur+1].Vars; i++)      free(c[cur+1].Var[i].Name);        // remove the gap in the functionlist    for (i = cur+1; i < canz; i++)       memcpy(&c[i], &c[i+1], sizeof(CALCFKT));        canz--;    // remove the funtion from the combobox    fkt->removeItem(cur);  }  // activate the next function for display  if (cur > canz-1) cur = canz - 1;  Activate(cur);}voidFuncList::InsertButtonS03()  // modify a function{  char    *tmp, *text;  QString  qs;  int      i;    // read the new function name  qs = f->text();  if(qs) {    tmp = (char*)qs.latin1();    if (tmp && strlen(tmp)) {      text = (char*)malloc(strlen(tmp)+1);      strcpy(text, tmp);      if (c[cur+1].Name) free(c[cur+1].Name);      c[cur+1].Name = text;         // change the name in the combobox      fkt->changeItem(qs, cur);    }  }  // read the new function definition  qs = inpfeld->text();  if(qs) {    tmp = (char*)qs.latin1();    if (tmp && strlen(tmp)) {      text = (char*)malloc(strlen(tmp)+1);      strcpy(text, tmp);      if (c[cur+1].Def) free(c[cur+1].Def);      c[cur+1].Def = text;    }  }  // update the paramter list  initial = 0;  setPosition(vbeg); // read paramters from screen  // delete old values of parameterlist  for ( i = 0; i < c[cur+1].Vars; i++)     free(c[cur+1].Var[i].Name);  // ensure clean pointers  for ( i = 0; i < 100; i++)     c[cur+1].Var[i].Name = NULL;  // set parameterlist to new values  for ( i = 0; i < count; i++)     c[cur+1].Var[i].Name = va[i];  // acual new parameter count  c[cur+1].Vars = count;  // refresh the display  fkt->setCurrentItem(cur);}voidFuncList::InsertButtonS04()  // Copy Key  // this key brings the actual function to the  // formual field in the main widget{  done(cur + 10);}voidFuncList::InsertButtonS05()  // Clear input fields  // this function erases all input fields for the  // function definition. {  int  i;  inpfeld->setText("");  f->setText("");  for (i = 0; i < 6; i++)    p[i]->setText("");  for (i = 0; i < 100; i++) {    if (va[i]) free(va[i]);    va[i] = NULL;  }}FuncList::FuncList(int x, int y)  :  QDialog (0, 0, TRUE){  int i;#ifdef ZAURUS  setGeometry(0,16,240,200);#else  setGeometry(x, y,240,MAINHEIGHT);#endif  setCaption("Functions List");  cur = -1;  vbeg = 0;  ql1 = new QLabel (this);   ql1->setGeometry(10,4,60,20);   ql1->setText("Function");  fkt = new QComboBox(this);   fkt->setGeometry(5, 25, 110, 20);  fkt->setEditable(FALSE);  fkt->setMaxCount(49);  ql4 = new QLabel (this);   ql4->setGeometry(10,45,60,20);   ql4->setText("Name");  f = new QLineEdit(this);   f->setGeometry(5, 65, 100, 20);   f->setMaxLength(40);  ql2 = new QLabel (this);   ql2->setGeometry(125,4,60,20);   ql2->setText("Parameter");  for (i = 0; i < 6; i++) {    p[i] = new QLineEdit(this);     p[i]->setGeometry(125, i*20+25, 100, 20);     p[i]->setMaxLength(40);  }  psb = new QScrollBar(Vertical, this);  psb->setGeometry(225,27,14,115);  psb->setMinValue(0);  psb->setMaxValue(49);  psb->setPageStep(5);  ql3 = new QLabel (this);   ql3->setGeometry(10,DEFLINE,60,20);   ql3->setText("Definition");  inpfeld = new gwCalcEdit(this);    inpfeld->setGeometry (5, DEFLINE+18, 230, DEFHEIGHT);  inpfeld->setMaxLineLength(25);    inpfeld->setWordWrap(QMultiLineEdit::WidgetWidth);  s01 = new QPushButton("Add",    this);  s02 = new QPushButton("Del",    this);  s03 = new QPushButton("Modify", this);  s04 = new QPushButton("Copy",   this);  s05 = new QPushButton("Clear",  this);#ifdef ZAURUS  s01->setGeometry( 5, 110, 35,20);  s02->setGeometry(40, 110, 35,20);  s03->setGeometry(75, 110, 45,20);  s04->setGeometry( 5,  90, 35,20);  s05->setGeometry(85,  90, 35,20);#else  s01->setGeometry( 5, 140, 55,24);  s02->setGeometry(61, 140, 55,24);  s03->setGeometry( 5, 115, 55,24);  s04->setGeometry( 5,  90, 55,24);  s05->setGeometry(61, 115, 55,24);#endif  s01->setFont (QFont("Helvetica", FONT10, QFont::Normal));  s02->setFont (QFont("Helvetica", FONT10, QFont::Normal));  s03->setFont (QFont("Helvetica", FONT10, QFont::Normal));  s04->setFont (QFont("Helvetica", FONT10, QFont::Normal));  s05->setFont (QFont("Helvetica", FONT10, QFont::Normal));  connect (fkt, SIGNAL(activated(int)), this, SLOT (Activate(int)));    connect (psb, SIGNAL(valueChanged(int)), this, SLOT (setPosition(int)));    connect (s01, SIGNAL(clicked()), this, SLOT (InsertButtonS01()));  connect (s02, SIGNAL(clicked()), this, SLOT (InsertButtonS02()));  connect (s03, SIGNAL(clicked()), this, SLOT (InsertButtonS03()));  connect (s04, SIGNAL(clicked()), this, SLOT (InsertButtonS04()));  connect (s05, SIGNAL(clicked()), this, SLOT (InsertButtonS05()));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -