Back to index

scribus-ng  1.3.4.dfsg+svn20071115
prefstable.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008  *   Copyright (C) 2004 by Riku Leino                                      *
00009  *   riku.leino@gmail.com                                                      *
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  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00019  *   GNU General Public License for more details.                          *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU General Public License     *
00022  *   along with this program; if not, write to the                         *
00023  *   Free Software Foundation, Inc.,                                       *
00024  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00025  ***************************************************************************/
00026 
00027 #include "prefstable.h"
00028 
00029 PrefsTable::PrefsTable(QString tableName)
00030 {
00031        name = tableName;
00032        rowCount = 0;
00033        colCount = 0;
00034 }
00035 
00036 QString PrefsTable::getName()
00037 {
00038        return name;
00039 }
00040 
00041 int PrefsTable::height()
00042 {
00043        return rowCount;
00044 }
00045 
00046 int PrefsTable::getRowCount()
00047 {
00048        return rowCount;
00049 }
00050 
00051 int PrefsTable::width()
00052 {
00053        return colCount;
00054 }
00055 
00056 int PrefsTable::getColCount()
00057 {
00058        return colCount;
00059 }
00060 
00061 QString PrefsTable::get(int row, int col, const QString& defValue)
00062 {
00063        checkSize(row, col, defValue);
00064        if ((*table[row])[col] == "__NOT__SET__")
00065               table[row]->insert(table[row]->at(col), defValue);
00066 
00067        return (*table[row])[col];
00068 }
00069 
00070 void PrefsTable::set(int row, int col, const char* value)
00071 {
00072        set(row, col, QString(value));
00073 }
00074 
00075 void PrefsTable::set(int row, int col, const std::string& value)
00076 {
00077        set(row, col, QString(value.c_str()));
00078 }
00079 
00080 void PrefsTable::set(int row, int col, const QString& value)
00081 {
00082        checkSize(row, col, "__NOT__SET__");
00083        table[row]->insert(table[row]->at(col), value);
00084 }
00085 
00086 int PrefsTable::getInt(int row, int col, int defValue)
00087 {
00088        QString stringValue = get(row, col, QString("%1").arg(defValue));
00089        bool ok = false;
00090        int ivalue = stringValue.toInt(&ok);
00091        if (!ok)
00092               ivalue = defValue;
00093        return ivalue;
00094 }
00095 
00096 void PrefsTable::set(int row, int col, int value)
00097 {
00098        set(row, col, QString("%1").arg(value));
00099 }
00100 
00101 void PrefsTable::set(int row, int col, uint value)
00102 {
00103        set(row, col, QString("%1").arg(value));
00104 }
00105 
00106 uint PrefsTable::getUInt(int row, int col, uint defValue)
00107 {
00108        QString stringValue = get(row, col, QString("%1").arg(defValue));
00109        bool ok = false;
00110        int uivalue = stringValue.toUInt(&ok);
00111        if (!ok)
00112               uivalue = defValue;
00113        return uivalue;
00114 }
00115 
00116 double PrefsTable::getDouble(int row, int col, double defValue)
00117 {
00118        QString stringValue = get(row, col, QString("%1").arg(defValue));
00119        bool ok = false;
00120        double dvalue = stringValue.toDouble(&ok);
00121        if (!ok)
00122               dvalue = defValue;
00123        return dvalue;
00124 }
00125 
00126 void PrefsTable::set(int row, int col, double value)
00127 {
00128        set(row, col, QString("%1").arg(value));
00129 }
00130 
00131 bool PrefsTable::getBool(int row, int col, bool defValue)
00132 {
00133        QString stringValue = get(row, col, QString("%1").arg(defValue));
00134        bool ok = false;
00135        int ivalue = stringValue.toInt(&ok);
00136        if (!ok)
00137               ivalue = defValue;
00138        return ivalue;
00139 }
00140 
00141 void PrefsTable::set(int row, int col, bool value)
00142 {
00143        set(row, col, QString("%1").arg(value));
00144 }
00145 
00146 int PrefsTable::find(int searchCol, const QString& what)
00147 {
00148        if ((searchCol < 0) || (searchCol >= width()))
00149               return -1;
00150 
00151        int rowi = -1;
00152        for (int i = 0; i < height(); ++i)
00153        {
00154               if ((get(i, searchCol, "__NOT__SET__") == what) &&
00155                   (get(i, searchCol, "__NOT__SET__") != "__NOT__SET__"))
00156               {
00157                      rowi = i;
00158                      break;
00159               }
00160        }
00161 
00162        return rowi;
00163 }
00164 
00165 void PrefsTable::removeRow(int colIndex, const QString& what)
00166 {
00167        if ((colIndex < 0) || (colIndex >= width()))
00168               return;
00169 
00170        Table::iterator it = table.begin();
00171        for (int i = 0; i < height(); ++i, ++it)
00172        {
00173               if (get(i, colIndex, "__NOT__SET__") == what)
00174               {
00175                      table.erase(it);
00176                      --rowCount;
00177               }
00178        }
00179 }
00180 
00181 void PrefsTable::checkSize(int rowIndex, int colIndex, QString defValue)
00182 {
00183        checkHeight(rowIndex);
00184        checkWidth(rowIndex, colIndex, defValue);
00185 }
00186 
00187 void PrefsTable::checkHeight(int rowIndex)
00188 {
00189        if (rowCount < (rowIndex + 1))
00190        {
00191               for (int i = 0; i < ((rowIndex + 1) - rowCount); ++i)
00192                      table.push_back(new QStringList());
00193               rowCount = rowIndex + 1;
00194        }
00195 }
00196 
00197 void PrefsTable::checkWidth(int rowIndex, int colIndex, QString defValue)
00198 {
00199        if (static_cast<int>(table[rowIndex]->size()) <= (colIndex + 1))
00200        {
00201               for (int i = 0; i < ((colIndex + 1) - static_cast<int>(table[rowIndex]->size())); ++i)
00202               {
00203                      if (i == colIndex - static_cast<int>(table[rowIndex]->size()))
00204                             table[rowIndex]->push_back(defValue);
00205                      else
00206                             table[rowIndex]->push_back("__NOT__SET__");
00207               }
00208               colCount = colIndex + 1;
00209        }
00210 }
00211 
00212 void PrefsTable::clear()
00213 {
00214        rowCount = 0;
00215        colCount = 0;
00216        table.clear();
00217 }
00218 
00219 PrefsTable::~PrefsTable()
00220 {
00221 }