funcaux.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Tomeu Borras Riera and Josep Burcion            *
00003  *   tborras@conetxia.com                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00026 #include <QString>
00027 #include <QTextEdit>
00028 #include <QDir>
00029 #include <QTextStream>
00030 #include <QLocale>
00031 #include <QProcess>
00032 
00033 #include "funcaux.h"
00034 #include "configuracion.h"
00035 
00036 
00038 QMainWindow *g_main = NULL;
00039 
00040 
00042 QString editaTexto(QString texto) {
00043     QTextEdit *ed = new QTextEdit(0);
00044     ed->setFixedSize(450, 250);
00045     ed->setPlainText(texto);
00046     g_main->setEnabled(FALSE);
00047     ed->show();
00048     while (!ed->isHidden()) {
00049         theApp->processEvents();
00050     } // end while
00051     g_main->setEnabled(TRUE);
00052     QString vuelta = ed->toPlainText();
00053     return vuelta;
00054 }
00055 
00056 
00058 QString XMLProtect(const QString &string) {
00059     /*    QString s = string;
00060         s.replace("&", "&amp;");
00061         s.replace(">", "&gt;");
00062         s.replace("<", "&lt;");
00063         s.replace("\"", "&quot;");
00064         s.replace("\'", "&apos;");
00065         return s;
00066     */
00069 
00070     QString cadena = string;
00071     QString cadenatmp = "";
00072     QChar *data = cadena.data();
00073 
00075     cadenatmp.replace("&", "&#38;");
00076     cadenatmp.replace(">", "&#62;");
00077     cadenatmp.replace("<", "&#60;");
00078     cadenatmp.replace("\"", "&#34;");
00079     cadenatmp.replace("\'", "&#39;");
00080 
00081     int i;
00082     for (i = 0; i < cadena.length(); i++) {
00083         if (data->unicode() > 127) {
00084             cadenatmp = cadenatmp + QString("&#") + QString::number(data->unicode()) + QString(";");
00085         } else if (data->unicode() == 10 | data->unicode() == 13) {
00087             cadenatmp = cadenatmp + QString("<br />");
00088         } else {
00089             cadenatmp = cadenatmp + QString(*data);
00090         } // end if
00091         ++data;
00092     } // end if
00093 
00094     return cadenatmp;
00095 }
00096 
00097 
00102 string extiendecodigo(string cad, unsigned int num1) {
00103     string cod = cad;
00104     unsigned int num = num1;
00105     if (cod.length() < num) {
00106         string str7(num - cod.length() + 1, '0');
00107         int pos = cod.find(".", 0);
00108         if (pos > 0) {
00109             cod.replace(pos, 1, str7);
00110         } // end if
00111     } // end if
00112     return (cod);
00113 }
00114 
00115 
00120 QString extiendecodigo(QString cad, unsigned int num1) {
00121     _depura("funcaux::extiendecodigo", 0, cad +"--"+QString::number(num1));
00122     QString cod = cad;
00123     int num = num1;
00124     if (cod.length() < num) {
00125         string str7(num - cod.length() + 1, '0');
00126         int pos = cod.indexOf(".", 0);
00127         if (pos > 0) {
00128             cod.replace(pos, 1, str7.c_str());
00129         } // end if
00130     } // end if
00131     _depura("END funcaux::extiendecodigo", 0);
00132     return (cod);
00133 }
00134 
00135 
00140 float fround(float n, unsigned d) {
00141     return floor(n * pow(10., d) + .5) / pow (10., d);
00142 }
00143 
00144 
00148 int roundI(double valor) {
00149     int retorno;
00150     double mayor = floor(valor);
00151     if ((mayor - valor) >= 0.5)
00152         retorno = (int) mayor - 1;
00153     else
00154         retorno = (int) mayor;
00155     return retorno;
00156 }
00157 
00158 
00163 QDate normalizafecha(QString fechaintro) {
00164     QDate fecharesult;
00165     int d, M, y;
00166     switch (fechaintro.length()) {
00167     case 4: 
00168         d = fechaintro.mid(0, 2).toInt();
00169         M = fechaintro.mid(2, 2).toInt();
00170         y = QDate::currentDate().year();
00171         break;
00172     case 5:
00173         d = fechaintro.mid(0, 2).toInt();
00174         M = fechaintro.mid(3, 2).toInt();
00175         y = QDate::currentDate().year();
00176         break;
00177     case 6: 
00178         d = fechaintro.mid(0, 2).toInt();
00179         M = fechaintro.mid(2, 2).toInt();
00180         y = 2000 + fechaintro.mid(4, 2).toInt();
00181         break;
00182     case 8:
00183         if (fechaintro.contains("/", Qt::CaseSensitive) || fechaintro.contains("-", Qt::CaseSensitive)) {
00185             d = fechaintro.mid(0, 2).toInt();
00186             M = fechaintro.mid(3, 2).toInt();
00187             y = 2000 + fechaintro.mid(6, 2).toInt();
00188         } else {
00190             d = fechaintro.mid(0,2).toInt();
00191             M = fechaintro.mid(2,2).toInt();
00192             y = fechaintro.mid(4,4).toInt();
00193         } // end if
00194         break;
00195     case 10: 
00196         d = fechaintro.mid(0, 2).toInt();
00197         M = fechaintro.mid(3, 2).toInt();
00198         y = fechaintro.mid(6, 4).toInt();
00199         break;
00200     default:
00201         d = QDate::currentDate().day();
00202         M = QDate::currentDate().month();
00203         y = QDate::currentDate().year();
00204     } // end switch
00205     if (!fecharesult.setDate(y, M, d))
00206         fecharesult = QDate::currentDate();
00207     return (fecharesult);
00208 }
00209 
00210 
00217 
00220 QString ajustacodigo(QString cad, unsigned int num1) {
00221     QString cod = cad;
00222     unsigned int longcad = cad.length();
00223     if (longcad > 4) {
00224         if (longcad < num1) {
00225             string str7(num1 - longcad, '0');
00226             cod = cad.left(4);
00227             cod += QString(str7.c_str());
00228             cod += cad.right(longcad - 4);
00229         } // end if
00230         if (longcad > num1) {
00231             cod = cad.left(4);
00232             cod += cad.right(num1 - 4);
00233         } // end if
00234     } // end if
00235     return (cod);
00236 }
00237 
00238 
00240 void reemplazaarchivo(QString archivo, QString texto1, QString texto2, QString archivo2) {
00241     QString cadena = " sed -e \"s&" + texto1 + "&" + texto2 + "&g\"  " + archivo + " > " + archivo2 + "";
00242     system(cadena.toAscii().data());
00243 }
00244 
00245 
00248 void generaPDF(const QString arch) {
00249     _depura("generaPDF " + arch, 0);
00250     QDir::setCurrent(confpr->valor(CONF_DIR_USER));
00251     QString cadsys;
00252 
00253 #ifdef WINDOWS
00254 
00255     cadsys = confpr->valor(CONF_PYTHON) + " " + confpr->valor(CONF_PROGDATA) + "trml2pdf\\bgtrml2pdf " + arch + ".rml > " + confpr->valor(CONF_DIR_USER) + arch + ".pdf";
00256     system(cadsys.toAscii());
00257     _depura(cadsys, 0);
00258     cadsys = confpr->valor(CONF_FLIP) + " -u " + confpr->valor(CONF_DIR_USER) + arch + ".pdf";
00259     system(cadsys.toAscii().data());
00260     _depura(cadsys, 0);
00261 #else
00262 
00263     cadsys = "bgtrml2pdf " + arch + ".rml > " + arch + ".pdf";
00264     system(cadsys.toAscii().data());
00265 #endif
00266 }
00267 
00268 
00272 void invocaPDF(const QString arch) {
00273     generaPDF(arch);
00274     QString cadsys = confpr->valor(CONF_PDF) + " " + confpr->valor(CONF_DIR_USER) + arch + ".pdf &";
00275     system(cadsys.toAscii().data());
00276 }
00277 
00278 
00280 void mailsendPDF(const QString arch, const QString to, const QString subject, const QString message) {
00281     //FIXME: REVISAR PARAMETROS de mailsend o la posibilidad de anyadir otros programas
00282     //para enviar correo desde la ventana de configuracion del programa.
00283     QString cadsys = "mailsend -h " + arch + " -d " + to + " -f bulmages@iglues.org -t test@iglues.org -sub " + subject + " -m " + message;
00284     system(cadsys.toAscii().data());
00285 }
00286 
00287 QString windowID(const QString &app) {
00288 
00289     QString cad = "";
00290 
00291     if (app != "") {
00292         cad = "xwininfo -int -name \""+app+"\" | grep xwininfo | awk '{print $4}' > /tmp/xwinfo";
00293     } else {
00294         cad = "xwininfo -int | grep Window | awk '{print $4}' > /tmp/xwinfo";
00295     } // end if
00296 
00297     system(cad.toAscii());
00298 
00299     QString winId = "";
00300 
00301     QFile file("/tmp/xwinfo");
00302     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
00303         return "";
00304 
00305     QTextStream in(&file);
00306     if (!in.atEnd()) {
00307         winId = in.readLine();
00308     } // end if
00309 
00310 
00311     file.close();
00312 
00313     return winId;
00314 }
00315 
00316 
00324 #ifdef DEPURA_DEBUG
00325 void _depura(const QString &cad, int nivel, const QString &param) {
00327     if (confpr == NULL) {
00328         return;
00329     } // end if
00330 
00331     static bool semaforo = 0;
00332 
00333 
00334     if (confpr->valor(CONF_DEBUG) == "TRUE") {
00335         static QFile file(confpr->valor(CONF_DIR_USER) + "bulmagesout.txt");
00336         static QTextStream out(&file);
00337         if (!semaforo) {
00338             if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
00339                 return;
00340             semaforo = 1;
00341         } // end if
00342         static int supnivel = 0;
00343         static int indice = 0;
00344         static QString mensajesanulados[7000];
00345         static QString clasesanuladas[7000];
00346         static int indiceclases = 0;
00347 
00348         if (nivel == 5) {
00349             supnivel = 0;
00350             nivel = 2;
00351         } // end if
00352         if (nivel == 4) {
00353             supnivel = 2;
00354             nivel = 2;
00355         } // end if
00356         if (nivel == 0) {
00357             out << cad << " " << param << "\n" << flush;
00358         } else if (nivel == 1) {
00359             out << cad << " " << param << "\n" << flush;
00360         } // end if
00361         for (int i = 0; i < indice; i++) {
00362             if (cad == mensajesanulados[i]) {
00363                 return;
00364             } // end if
00365         } // end for
00366         for (int i = 0; i < indiceclases; i++) {
00367             if (cad.left(cad.indexOf("::")) == clasesanuladas[i]) {
00368                 return;
00369             } // end if
00370         } // end for
00371 
00372         if (nivel == 2 || (supnivel == 2 && nivel == 0) || nivel == 3) {
00373             out << cad << " " << param << "\n" << flush;
00374             int err = QMessageBox::information(NULL,
00375                                                QApplication::translate("funcaux", "Informacion de depuracion"),
00376                                                cad + " " + param,
00377                                                QApplication::translate("funcaux", "&Continuar"),
00378                                                QApplication::translate("funcaux", "&Omitir"),
00379                                                QApplication::translate("funcaux", "Omitir &clase"),
00380                                                0, 1);
00381             if (err == 1) {
00382                 mensajesanulados[indice++] = cad;
00383             } // end if
00384             if (err == 2) {
00385                 clasesanuladas[indiceclases++] = cad.left(cad.indexOf("::"));
00386             } // end if
00387         } // end if
00388 
00389         file.flush();
00390     }
00391 }
00392 
00393 #else
00394 
00395 inline void _depura(const QString &, int , const QString &) {
00396     return;
00397 }
00398 #endif
00399 
00400 void mensajeInfo(QString cad) {
00401     QMessageBox::information(NULL,
00402                              QApplication::translate("funcaux", "Informacion del programa"),
00403                              cad, QApplication::translate("funcaux", "&Continuar"),
00404                              QString::null, 0);
00405 }
00406 
00407 
00408 void mensajeAviso(QString cad) {
00409     QMessageBox::warning(NULL,
00410                          QApplication::translate("funcaux", "Aviso del programa"),
00411                          cad, QApplication::translate("funcaux", "&Continuar"),
00412                          QString::null, 0);
00413 }
00414 
00415 
00416 void mensajeError(QString cad) {
00417     QMessageBox::critical(NULL,
00418                           QApplication::translate("funcaux", "Error del programa"),
00419                           cad, QApplication::translate("funcaux", "&Continuar"),
00420                           QString::null, 0);
00421 }
00422 
00423 
00424 QString  num2texto(QString numero, QString moneda, QString singular) {
00426     if (numero == "0" || numero == "00") {
00427         return "cero " + moneda;
00428     } // end if
00429 
00431     if (numero == "1") {
00432         return "un " + singular;
00433     } // end if
00434 
00435     QMap<QString , QString > numeros;
00436     //numeros["unidad"][0][0] = "cero";
00437     numeros["unidad10"] = "un";
00438     numeros["unidad20"] = "dos";
00439     numeros["unidad30"] = "tres";
00440     numeros["unidad40"] = "cuatro";
00441     numeros["unidad50"] = "cinco";
00442     numeros["unidad60"] = "seis";
00443     numeros["unidad70"] = "siete";
00444     numeros["unidad80"] = "ocho";
00445     numeros["unidad90"] = "nueve";
00446 
00447     numeros["decenas10"] = "diez";
00448     numeros["decenas20"] = "veinte";
00449     numeros["decenas30"] = "treinta";
00450     numeros["decenas40"] = "cuarenta";
00451     numeros["decenas50"] = "cincuenta";
00452     numeros["decenas60"] = "sesenta";
00453     numeros["decenas70"] = "setenta";
00454     numeros["decenas80"] = "ochenta";
00455     numeros["decenas90"] = "noventa";
00456     numeros["decenas110"] = "dieci";
00457     numeros["decenas111"] = "once";
00458     numeros["decenas112"] = "doce";
00459     numeros["decenas113"] = "trece";
00460     numeros["decenas114"] = "catorce";
00461     numeros["decenas115"] = "quince";
00462     numeros["decenas21"] = "veinti";
00463     numeros["decenas31"] = "treinta y ";
00464     numeros["decenas41"] = "cuarenta y ";
00465     numeros["decenas51"] = "cincuenta y ";
00466     numeros["decenas61"] = "sesenta y ";
00467     numeros["decenas71"] = "setenta y ";
00468     numeros["decenas81"] = "ochenta y ";
00469     numeros["decenas91"] = "noventa y ";
00470 
00471     numeros["centenas10"] = "cien";
00472     numeros["centenas20"] = "doscientos ";
00473     numeros["centenas30"] = "trecientos ";
00474     numeros["centenas40"] = "cuatrocientos ";
00475     numeros["centenas50"] = "quinientos ";
00476     numeros["centenas60"] = "seiscientos ";
00477     numeros["centenas70"] = "setecientos ";
00478     numeros["centenas80"] = "ochocientos ";
00479     numeros["centenas90"] = "novecientos ";
00480     numeros["centenas11"] = "ciento ";
00481 
00482     QMap<QString, QString> postfijos;
00483     postfijos["1-0"] = "";
00484     postfijos["10-0"] = "";
00485     postfijos["100-0"] = "";
00486     postfijos["1000-0"] = " mil ";
00487     postfijos["10000-0"] = " mil ";
00488     postfijos["100000-0"] = " mil ";
00489     postfijos["1000000-0"] = " millon ";
00490     postfijos["10000000-0"] = " millon ";
00491     postfijos["100000000-0"] = " millon ";
00492     postfijos["1000000-1"] = " millones ";
00493     postfijos["10000000-1"] = " millones ";
00494     postfijos["100000000-1"] = " millones ";
00495 
00496     QString decimal_break = ".";
00497     //echo "test run on ".$numero."<br>";
00498     QString entero = numero.split(decimal_break).at(0);
00499     QString decimal = numero.split(decimal_break).at(1);
00500 
00501 //    $entero=strtok($numero,$decimal_break);
00502 //    $decimal=strtok($decimal_break);
00503     if (decimal == "") {
00504         decimal = "00";
00505     } // end if
00506     if (decimal.size() < 2) {
00507         decimal = decimal + "0";
00508     } // end if
00509     if (decimal.size() > 2) {
00510         decimal = decimal.right(2);
00511     } // end if
00512     //echo "entero ".$entero."<br> decimal ".$decimal."<br>";
00513 
00514     QString entero_breakdown = entero;
00515 
00516     QString breakdown_key = "1000000000000";
00517     QString num_string="";
00518     QMap<QString, QString> breakdown;
00519     QString chundreds = "";
00520     QString tens = "";
00521     QString ctens = "";
00522     QString cctens = "";
00523     QString ones = "";
00524     QString cpostfijos = "";
00525     while (breakdown_key.toDouble() > 0.5) {
00526 //  _depura(num_string, 2);
00527         breakdown["entero" + breakdown_key + "number"]= /*floor(*/ QString::number(entero_breakdown.toLongLong() / breakdown_key.toLongLong());
00528 
00529 //  _depura(breakdown["entero"+breakdown_key+"number"], 2);
00530 
00531         //echo " ".$breakdown["entero"][$breakdown_key]["number"]."<br>";
00532         if (breakdown["entero" + breakdown_key+"number"].toLongLong() > 0) {
00533             //echo " further process <br>";
00534             breakdown["entero" + breakdown_key+"100"] = /*floor(*/ QString::number(breakdown["entero" + breakdown_key + "number"].toLongLong() / 100);
00535             breakdown["entero" + breakdown_key+"10"] = /*floor( */ QString::number((breakdown["entero" + breakdown_key + "number"].toLongLong() % 100) / 10);
00536             breakdown["entero" + breakdown_key+"1"] = /*floor(*/   QString::number(breakdown["entero" + breakdown_key + "number"].toLongLong() % 10);
00537             //echo " 100 ->".$breakdown["entero"][$breakdown_key][100]."<br>";
00538             //echo " 10   ->".$breakdown["entero"][$breakdown_key][10]."<br>";
00539             //echo " 1     ->".$breakdown["entero"][$breakdown_key][1]."<br>";
00540 
00541             QString hundreds = breakdown["entero" + breakdown_key + "100"];
00542             // if not a closed value at hundredths
00543             if ((breakdown["entero" + breakdown_key + "10"].toLongLong() + breakdown["entero"+breakdown_key+"1"].toLongLong()) > 0) {
00544                 chundreds = "1";
00545             } else {
00546                 chundreds = "0";
00547             } // end if
00548 
00549             if (numeros.contains("centenas" + hundreds + chundreds)) {
00550                 //echo " centenas ".numeros["centenas"][$hundreds][$chundreds]."<br>";
00551                 num_string += numeros["centenas" + hundreds + chundreds];
00552             } else {
00553                 //echo " centenas ".numeros["centenas"][$hundreds][0]."<br>";
00554                 if (numeros.contains("centenas" + hundreds + "0")) {
00555                     num_string += numeros["centenas" + hundreds + "0"];
00556                 }
00557             } // end if
00558 
00559             if ((breakdown["entero" + breakdown_key + "1"].toLongLong()) > 0) {
00560                 ctens = "1";
00561                 tens = breakdown["entero" + breakdown_key + "10"];
00562                 //echo "NOT CLOSE TENTHS<br>";
00563                 if (breakdown["entero" + breakdown_key + "10"].toLongLong() == 1) {
00564                     if (breakdown["entero" + breakdown_key + "1"].toLongLong() < 6 ) {
00565                         cctens = breakdown["entero" + breakdown_key + "1"];
00566                         //echo " decenas ".numeros["decenas"][$tens][$ctens][$cctens]."<br>";
00567                         num_string += numeros["decenas" + tens + ctens + cctens];
00568                     } else {
00569                         //echo " decenas ".numeros["decenas"][$tens][$ctens][0]."<br>";
00570                         num_string += numeros["decenas" + tens + ctens + "0"];
00571                     } // end if
00572                 } else {
00573                     //echo " decenas ".numeros["decenas"][$tens][$ctens]."<br>";
00574                     if (numeros.contains("decenas" + tens + ctens)) {
00575                         num_string += numeros["decenas" + tens + ctens];
00576                     }
00577                 } // end if
00578             } else {
00579                 //echo "CLOSED TENTHS<br>";
00580                 ctens = "0";
00581                 tens = breakdown["entero" + breakdown_key + "10"];
00582                 //echo " decenas ".numeros["decenas"][$tens][$ctens]."<br>";
00583                 if (numeros.contains("decenas" + tens + ctens)) {
00584                     num_string += numeros["decenas" + tens + ctens];
00585                 } // end if
00586             } // end if
00587 
00588             if (cctens == "") {
00589                 ones = breakdown["entero" + breakdown_key + "1"];
00590                 if (numeros.contains("unidad" + ones + "0")) {
00591                     //echo " tens ".numeros["unidad"][$ones][0]."<br>";
00592                     num_string += numeros["unidad" + ones + "0"];
00593                 } // end if
00594             } // end if
00595 
00596             cpostfijos = "-1";
00597             if (breakdown["entero" + breakdown_key + "number"].toLongLong() > 1) {
00598                 cpostfijos = "1";
00599             } // end if
00600 
00601             if (postfijos.contains(breakdown_key + "-" + cpostfijos)) {
00602                 num_string += postfijos[breakdown_key + "-" + cpostfijos];
00603             } else {
00604                 num_string += postfijos[breakdown_key + "-0"];
00605             } // end if
00606         } // end if
00607         cctens = "";
00608         entero_breakdown = QString::number(entero_breakdown.toInt() % breakdown_key.toLongLong());
00609         breakdown_key = QString::number(breakdown_key.toLongLong() / 1000);
00610 
00611         //echo "CADENA ".$num_string."<br>";
00612     } // end while
00613 //    return  num_string+" "+moneda+" "+decimal+"/100 M.N.";
00614     if (decimal != "0" && decimal != "00") {
00615         return num_string + " " + moneda + " con "+ num2texto(decimal + ".00"," centimos", " centimo");
00616     } else {
00617         return num_string + " " + moneda;
00618     } // end if
00619 }
00620 
00621 
00622 void centrarEnPantalla(QWidget *ventana) {
00623     QRect rect;
00624     QDesktopWidget *escritorio = new QDesktopWidget();
00625     rect = escritorio->availableGeometry();
00626     ventana->move (rect.center() - ventana->rect().center());
00627 }
00628 
00632 void cargaTraducciones(const QString &traduccion) {
00634     QTranslator *traductor = new QTranslator(0);
00635     if (confpr->valor(CONF_TRADUCCION) == "locales") {
00636         traductor->load(traduccion + QString("_") + QLocale::system().name(),
00637                         confpr->valor(CONF_DIR_TRADUCCION).toAscii().constData());
00638     } else {
00639         QString archivo = traduccion + "_" + confpr->valor(CONF_TRADUCCION);
00640         traductor->load(archivo, confpr->valor(CONF_DIR_TRADUCCION).toAscii().constData());
00641     } // end if
00642     theApp->installTranslator(traductor);
00643 }
00644 
00645 

Generated on Sat Dec 15 00:01:21 2007 for BulmaGes by  doxygen 1.5.1