utils.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           utils.cpp  -  description
00003                              -------------------
00004     begin                : Wed Jul 31 2002
00005     copyright            : (C) 2002-2006 by Ben Swerts
00006     email                : bswerts@users.sourceforge.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00020 
00021 // C++ header files
00022 #include <cmath>
00023 
00024 // Qt header files
00025 #include <qcheckbox.h>
00026 #include <qstring.h>
00027 #include <qstringlist.h>
00028 #include <qtable.h>
00029 
00030 // Xbrabo header files
00031 #include "utils.h"
00032 
00033 
00035 QString inputLine(const QString keyword, const double a, const double b, const double c)
00038 {
00039   QString astr, bstr, cstr;
00040   astr.setNum(a,'f',10).truncate(10);
00041   bstr.setNum(b,'f',10).truncate(10);
00042   cstr.setNum(c,'f',10).truncate(10);
00043 
00044   QString result = keyword + "    ";
00045   result.truncate(4);
00046   result += "      ";
00047   result += astr;
00048   if(fabs(c) > 0.000000001)
00049   {
00050     if(fabs(b) > 0.000000001)
00051       result += bstr + cstr;
00052     else
00053       result += "          " + cstr;
00054   }
00055   else if(fabs(b) > 0.000000001)
00056     result += bstr;
00057 
00058   return result;
00059 }
00060 
00062 QString inputLine(const QString keyword, const int a, const int b, const int c)
00066 {
00067   QString astr, bstr, cstr;
00068   if(a != 0)
00069   {
00070     astr.setNum(a);
00071     astr += ".         ";
00072     astr.truncate(10);
00073   }
00074   else
00075     astr = "          ";
00076   bstr.setNum(b);
00077   bstr += ".         ";
00078   bstr.truncate(10);
00079   cstr.setNum(c);
00080   cstr += ".         ";
00081   cstr.truncate(10);
00082 
00083   QString result = keyword + "    ";
00084   result.truncate(4);
00085   result += "      " + astr;
00086   if(c != 0)
00087   {
00088     if(b != 0)
00089       result += bstr + cstr;
00090     else
00091       result += "          " + cstr;
00092   }
00093   else if(b != 0)
00094     result += bstr;
00095 
00096   return result;
00097 }
00098 
00100 QString inputLine(const QString keyword, const int a, const double b)
00104 {
00105   QString astr, bstr;
00106   astr.setNum(a);
00107   astr += ".         ";
00108   astr.truncate(10);
00109   bstr.setNum(b,'f',10).truncate(10);
00110 
00111   QString result = keyword + "    ";
00112   result.truncate(4);
00113   result += "      " + astr;
00114   if(fabs(b) > 0.000000001)
00115     result += bstr;
00116 
00117   return result;
00118 }
00119 
00121 QString validatedSYMMLine(QCheckBox* bx, QCheckBox* by, QCheckBox* bz, QCheckBox* bxy, QCheckBox* bxz, QCheckBox* byz, QCheckBox* bxyz)
00124 {
00125   bool x, y, z, xy, yz, xz, xyz;
00126 
00127   x = bx->isChecked();
00128   y = by->isChecked();
00129   z = bz->isChecked();
00130   xy = bxy->isChecked();
00131   yz = byz->isChecked();
00132   xz = bxz->isChecked();
00133   xyz = bxyz->isChecked();
00134 
00135   if(x && y) xy = true;
00136   if(y && z) yz = true;
00137   if(x && z) xz = true;
00138   if(x && y && z) xyz = true;
00139 
00140   if(x && y && z && xy && yz && xz && xyz)
00141     return QString("symm      all");
00142 
00143   QString result = "symm      ";
00144   if(x)   result += "x         ";
00145   if(y)   result += "y         ";
00146   if(z)   result += "z         ";
00147   if(xy)  result += "xy        ";
00148   if(yz)  result += "yz        ";
00149   if(xz)  result += "xz        ";
00150   if(xyz) result += "xyz       ";
00151 
00152   return result;
00153 }
00154 
00156 template<typename T> void limitRange(const T& min, T& value, const T& max)
00158 {  
00159   if(min < max)
00160   {
00161     if(value < min)
00162       value = min;
00163     else if(value > max)
00164       value = max;
00165   }
00166   else
00167   { // when wrong input is given, still do correct limiting
00168     if(value < max)
00169       value = max;
00170     else if(value > min)
00171       value = min;
00172   }
00173 }
00174 
00176 void resetTable(QTable* table)
00178 {  
00179   table->setNumRows(3);
00180   table->selectCells(0, 0, 2, 7);
00181   table->clearSelection();
00182 }
00183 
00185 void saveTable(const QTable* table, unsigned int& numLines, vector<unsigned int>& hPos, vector<unsigned int>& vPos, QStringList& contents)
00188 {
00189   numLines = table->numRows();
00190   hPos.clear();
00191   vPos.clear();
00192   contents.clear();
00193   for(unsigned int j = 0; j < numLines; j++)
00194   {
00195     for(unsigned int i = 0; i < 8; i++)
00196     {
00197       QString cellContents = table->text(i,j);
00198       if(!cellContents.isNull())
00199       {
00200         hPos.push_back(i);
00201         vPos.push_back(j);
00202         contents += cellContents;
00203       }
00204     }
00205   }  
00206 }
00207 
00209 void restoreTable(QTable*  table, const unsigned int& numLines, const vector<unsigned int>& hPos, const vector<unsigned int>& vPos, const QStringList& contents)
00212 {
00213   table->setNumRows(numLines);
00214   table->selectCells(0, 0, numLines - 1, 7);
00215   table->clearSelection();
00216   
00217   for(unsigned int i = 0; i < hPos.size(); i++)
00218   {
00219     if(contents[i].length() > 10)
00220     {
00221       int cellSpan = (contents[i].length() -1)/10 + 1;
00222       table->item(hPos[i], vPos[i])->setSpan(1,cellSpan);
00223     }
00224     else if(table->item(hPos[i], vPos[i])->colSpan() != 1)
00225       table->item(hPos[i], vPos[i])->setSpan(1,1);
00226 
00227     table->setText(hPos[i], vPos[i], contents[i]);
00228   }
00229 }
00230 
00232 void checkTableOverflow(QTable* table, const int row, const int col)
00235 {  
00237   int cellSpan = 1;
00238   if(table->text(row, col).length() > 10)
00239     cellSpan = (table->text(row, col).length() - 1)/10 + 1;
00241   if((col + cellSpan) > 8)
00242     cellSpan = 8 - col;
00244   table->item(row, col)->setSpan(1, cellSpan);
00245 }
00246 
00248 void addTableRow(QTable* table)
00251 {  
00253   for(int row = 0; row < table->numRows(); row++)
00254   {
00255     if(table->isRowSelected(row, true))
00256     {
00257       table->insertRows(row);
00258       return;
00259     }
00260   }
00262   int numberOfRows = table->numRows();
00263   numberOfRows++;
00264   table->setNumRows(numberOfRows);  
00265 }
00266 
00268 bool removeTableRow(QTable* table)
00271 {
00272   bool succes = false;
00273   for(int row = 0; row < table->numRows(); row++)
00274   {
00275     if(table->isRowSelected(row, true))
00276     {
00277       table->removeRow(row);
00278       succes = true;
00279     }
00280   }
00281   return succes;
00282 }
00283 
00285 bool clearTableSelection(QTable* table)
00287 {  
00288   if(table->numSelections() > 0)
00289   {
00291     for(int row = 0; row < table->numRows(); row++)
00292     {
00293       for(int col = 0; col < 8; col++)
00294       {
00295         if(table->isSelected(row, col))
00296           table->clearCell(row, col);
00297       }
00298     }
00299     table->clearSelection(true);
00300     return true;
00301   }
00302   // else
00303   return false;
00304 }
00305 
00307 QStringList tableContents(const QTable* table)
00310 {
00311   QStringList result;
00312 
00313   for(int row = 0; row < table->numRows(); row++)
00314   {
00315     if(emptyTableRow(table, row))
00316       continue;
00317 
00318     QString line;
00319     for(int col = 0; col < 8; col++)
00320     {
00321       if(table->text(row, col).isEmpty())
00322         line += "          ";
00323       else if(table->item(row, col)->colSpan() != 1)
00324       {
00325         QString lineX0 = table->text(row, col) + "          ";
00326         lineX0.truncate(10 * table->item(row, col)->colSpan());
00327         line += lineX0;        
00328         col += table->item(row, col)->colSpan();
00329       }
00330       else
00331       {
00332         QString line10 = table->text(row, col) + "          ";
00333         line10.truncate(10);
00334         line += line10;
00335       }
00336     }
00337     result += line.latin1();
00338   }
00339   return result;
00340 }
00341 
00343 bool emptyTableRow(const QTable* table, const int row)
00345 {
00346   if(row < 0 || row > table->numRows())
00347     return false;
00348 
00349   for(int col = 0; col < 8; col++)
00350   {
00351     if(!table->text(row, col).isEmpty())
00352       return false;
00353   }
00354   return true;
00355 }
00356 
00358 int firstEmptyTableRow(QTable* table)
00361 {
00362   for(int row = 0; row < table->numRows(); row++)
00363   {
00364     if(emptyTableRow(table, row))
00365       return row;
00366   }
00367   addTableRow(table);
00368   return table->numRows() - 1;
00369 }
00370 

Generated on Fri May 19 14:31:56 2006 for Brabosphere by  doxygen 1.4.6-NO