kspread

krs_cell.cpp

00001 /*
00002  *  Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
00003  *  Copyright (c) 2006 Isaac Clerencia <isaac@warp.es>
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU Library 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 Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "krs_cell.h"
00021 
00022 #include "manipulator.h"
00023 #include "manipulator_data.h"
00024 
00025 namespace Kross { namespace KSpreadCore {
00026 
00027 Cell::Cell(KSpread::Cell* cell, KSpread::Sheet* sheet, uint col, uint row)
00028     : Kross::Api::Class<Cell>("KSpreadCell"), m_cell(cell), m_sheet(sheet), m_col(col), m_row(row)
00029 {
00030     this->addFunction0< Kross::Api::Variant >("value", this, &Cell::value);
00031     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("setValue", this, &Cell::setValue);
00032 
00033     this->addFunction0< Kross::Api::Variant >("column", this, &Cell::column);
00034     this->addFunction0< Kross::Api::Variant >("row", this, &Cell::row);
00035 
00036     this->addFunction0< Cell >("previousCell", this, &Cell::previousCell);
00037     this->addFunction0< Cell >("nextCell", this, &Cell::nextCell);
00038     this->addFunction1< void, Cell >("setPreviousCell", this, &Cell::setPreviousCell);
00039     this->addFunction1< void, Cell >("setNextCell", this, &Cell::setNextCell);
00040 
00041     this->addFunction0< Kross::Api::Variant >("name", this, &Cell::name);
00042     this->addFunction0< Kross::Api::Variant >("fullName", this, &Cell::fullName);
00043 
00044     this->addFunction0< Kross::Api::Variant >("comment", this, &Cell::comment);
00045     this->addFunction1< void, Kross::Api::Variant >("setComment", this, &Cell::setComment);
00046 
00047     this->addFunction0< Kross::Api::Variant >("getFormatString", this, &Cell::getFormatString);
00048     this->addFunction1< void, Kross::Api::Variant >("setFormatString", this, &Cell::setFormatString);
00049 
00050     this->addFunction0< Kross::Api::Variant >("text", this, &Cell::text);
00051     this->addFunction1< void, Kross::Api::Variant >("setText", this, &Cell::setText);
00052 
00053     this->addFunction0< Kross::Api::Variant >("textColor", this, &Cell::textColor);
00054     this->addFunction1< void, Kross::Api::Variant >("setTextColor", this, &Cell::setTextColor);
00055     this->addFunction0< Kross::Api::Variant >("backgroundColor", this, &Cell::backgroundColor);
00056     this->addFunction1< void, Kross::Api::Variant >("setBackgroundColor", this, &Cell::setBackgroundColor);
00057 }
00058 
00059 Cell::~Cell() {
00060 }
00061 
00062 const QString Cell::getClassName() const {
00063     return "Kross::KSpreadCore::KSpreadCell";
00064 }
00065 
00066 QVariant Cell::toVariant(const KSpread::Value& value) const
00067 {
00068     //Should we use following value-format enums here?
00069     //fmt_None, fmt_Boolean, fmt_Number, fmt_Percent, fmt_Money, fmt_DateTime, fmt_Date, fmt_Time, fmt_String
00070 
00071     switch(value.type()) {
00072         case KSpread::Value::Empty:
00073             return QVariant();
00074         case KSpread::Value::Boolean:
00075             return QVariant( value.asBoolean() );
00076         case KSpread::Value::Integer:
00077             return static_cast<Q_LLONG>(value.asInteger());
00078         case KSpread::Value::Float:
00079             return (float)value.asFloat();
00080         case KSpread::Value::String:
00081             return value.asString();
00082         case KSpread::Value::Array: {
00083             QValueList<QVariant> colarray;
00084             for(uint j = 0; j < value.rows(); j++) {
00085                 QValueList<QVariant> rowarray;
00086                 for( uint i = 0; i < value.columns(); i++) {
00087                     KSpread::Value v = value.element(i,j);
00088                     rowarray.append( toVariant(v) );
00089                 }
00090                 colarray.append(rowarray);
00091             }
00092             return colarray;
00093         } break;
00094         case KSpread::Value::CellRange:
00095             //FIXME: not yet used
00096             return QVariant();
00097         case KSpread::Value::Error:
00098             return QVariant();
00099     }
00100     return QVariant();
00101 }
00102 
00103 QVariant Cell::value() const {
00104     return toVariant( m_cell->value() );
00105 }
00106 
00107 bool Cell::setValue(const QVariant& value) {
00108     KSpread::Value v = m_cell->value();
00109     switch(value.type()) {
00110         case QVariant::Bool: v.setValue( value.toBool() ); break;
00111         case QVariant::ULongLong: v.setValue( (long)value.toLongLong() ); break;
00112         case QVariant::Int: v.setValue( value.toInt() ); break;
00113         case QVariant::Double: v.setValue( value.toDouble() ); break;
00114         case QVariant::String: v.setValue( value.toString() ); break;
00115         case QVariant::Date: v.setValue( value.toDate() ); break;
00116         case QVariant::Time: v.setValue( value.toTime() ); break;
00117         case QVariant::DateTime: v.setValue( value.toDateTime() ); break;
00118         default: return false;
00119     }
00120     return true;
00121 }
00122 
00123 int Cell::column() const {
00124     return m_cell->column();
00125 }
00126 
00127 int Cell::row() const  {
00128     return m_cell->row();
00129 }
00130 
00131 Cell* Cell::previousCell() const {
00132     KSpread::Cell* c = m_cell->previousCell();
00133     return c ? new Cell(c,c->sheet(),c->column(),c->row()) : 0;
00134 }
00135 
00136 Cell* Cell::nextCell() const {
00137     KSpread::Cell* c = m_cell->nextCell();
00138     return c ? new Cell(c,c->sheet(),c->column(),c->row()) : 0;
00139 }
00140 
00141 void Cell::setPreviousCell(Cell* c) {
00142     return m_cell->setPreviousCell(c->m_cell);
00143 }
00144 
00145 void Cell::setNextCell(Cell* c) {
00146     return m_cell->setNextCell(c->m_cell);
00147 }
00148 
00149 const QString Cell::name() const {
00150     return m_cell->name();
00151 }
00152 
00153 const QString Cell::fullName() const {
00154     return m_cell->fullName();
00155 }
00156 
00157 const QString Cell::comment() const {
00158     return m_cell->format()->comment(m_col, m_row);
00159 }
00160 
00161 void Cell::setComment(const QString& c) {
00162     return m_cell->format()->setComment(c);
00163 }
00164 
00165 const QString Cell::getFormatString() const {
00166     return m_cell->format()->getFormatString(m_col, m_row);
00167 }
00168 
00169 void Cell::setFormatString(const QString& format) {
00170     m_cell->format()->setFormatString(format);
00171 }
00172 
00173 const QString Cell::text() const {
00174     return m_cell->text();
00175 }
00176 
00177 bool Cell::setText(const QString& text) {
00178     KSpread::ProtectedCheck prot;
00179     prot.setSheet (m_sheet);
00180     prot.add (QPoint (m_col, m_row));
00181     if (prot.check())
00182     return false;
00183 
00184     KSpread::DataManipulator *dm = new KSpread::DataManipulator ();
00185     dm->setSheet (m_sheet);
00186     dm->setValue (text);
00187     dm->setParsing (true);
00188     dm->add (QPoint (m_col, m_row));
00189     dm->execute ();
00190 
00191     return true;
00192 }
00193 
00194 const QString Cell::textColor() {
00195     return m_cell->format()->textColor(m_col, m_row).name();
00196 }
00197 
00198 void Cell::setTextColor(const QString& textcolor) {
00199     m_cell->format()->setTextColor( QColor(textcolor) );
00200 }
00201 
00202 const QString Cell::backgroundColor() {
00203     return m_cell->format()->bgColor(m_col, m_row).name();
00204 }
00205 
00206 void Cell::setBackgroundColor(const QString& backgroundcolor) {
00207     m_cell->format()->setBgColor( QColor(backgroundcolor) );
00208 }
00209 
00210 }
00211 }
KDE Home | KDE Accessibility Home | Description of Access Keys