1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
|
/* This file is part of the KDE project
Copyright 2010 Marijn Kruisselbrink <mkruisselbrink@kde.org>
Copyright 2006-2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
Copyright 2004 Tomas Mecir <mecirt@gmail.com>
Copyright 1999-2002,2004 Laurent Montel <montel@kde.org>
Copyright 2002,2004 Ariya Hidayat <ariya@kde.org>
Copyright 2002-2003 Norbert Andres <nandres@web.de>
Copyright 2003 Stefan Hetzl <shetzl@chello.at>
Copyright 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
Copyright 2002 Harri Porten <porten@kde.org>
Copyright 2002 John Dailey <dailey@vt.edu>
Copyright 1999-2001 David Faure <faure@kde.org>
Copyright 2000-2001 Werner Trobin <trobin@kde.org>
Copyright 2000 Simon Hausmann <hausmann@kde.org
Copyright 1998-1999 Torben Weis <weis@kde.org>
Copyright 1999 Michael Reiher <michael.reiher@gmx.de>
Copyright 1999 Reginald Stadlbauer <reggie@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef CALLIGRA_SHEETS_CELL
#define CALLIGRA_SHEETS_CELL
#include <QSharedDataPointer>
#include <QSharedPointer>
#include <QTextDocument>
#include "Global.h"
#include "Style.h"
class QDomElement;
class QDomDocument;
class QRect;
class QPoint;
class QDate;
class KLocale;
namespace Calligra
{
namespace Sheets
{
class Conditions;
class Database;
class Doc;
class Formula;
class Sheet;
class Validity;
class Value;
class CellTest;
/**
* An accessor to the actual cell data.
* The Cell object acts as accessor to the actual data stored in the separate
* storages in CellStorage. It provides methods to alter and retrieve this data
* and methods related to loading and saving the contents.
*/
class CALLIGRA_SHEETS_ODF_EXPORT Cell
{
public:
/**
* Constructor.
* Creates the null cell.
* \note Accessing the sheet(), column() or row() methods or any related method, that makes use
* of the former, will fail.
*/
Cell();
/**
* Constructor.
* Creates a Cell for accessing the data in \p sheet at position \p col , \p row .
*/
Cell(const Sheet* sheet, int column, int row);
/**
* Constructor.
* Creates a Cell for accessing the data in \p sheet at position \p pos .
*/
Cell(const Sheet* sheet, const QPoint& pos);
/**
* Copy constructor.
*/
Cell(const Cell& other);
/**
* Destructor.
*/
~Cell();
/**
* \return the sheet this cell belongs to
*/
Sheet* sheet() const;
/**
* Returns the locale setting of this cell.
*/
KLocale* locale() const;
/**
* Returns true, if this is a default cell, i.e. if the cell has no value, formula, link and
* does not merge any other cells, and has no custom style.
*/
bool isDefault() const;
/**
* Returns true, if this is a cell with default content, i.e. if the cell has no value, formula, link and
* does not merge any other cells. This is the same as isDefault, except that the style
* is not taken into account here.
*/
bool hasDefaultContent() const;
/**
* Returns true, if this cell has no content, i.e no value and no formula.
*/
bool isEmpty() const;
/**
* Returns true if this cell is the null cell.
*/
bool isNull() const;
/**
* Returns true if this cell holds a formula.
*/
bool isFormula() const;
/**
* Returns the cell's column.
*/
int column() const;
/**
* Returns the cell's row.
*/
int row() const;
/**
* Returns the name of the cell. For example, the cell in first column and
* first row is "A1".
*/
QString name() const;
/**
* Returns the full name of the cell, i.e. including the worksheet name.
* Example: "Sheet1!A1"
*/
QString fullName() const;
/**
* Returns the column name of the cell.
*/
QString columnName() const;
/**
* Given the cell position, this static function returns the name of the cell.
* Example: name(5,4) will return "E4".
*/
static QString name(int col, int row);
/**
* Given the sheet and cell position, this static function returns the full name
* of the cell, i.e. with the name of the sheet.
*/
static QString fullName(const Sheet *s, int col, int row);
/**
* Given the column number, this static function returns the corresponding
* column name, i.e. the first column is "A", the second is "B", and so on.
*/
static QString columnName(uint column);
/**
* \return the output text, e.g. the result of a formula
*/
QString displayText(const Style& s = Style(), Value* v = 0, bool *showFormula = 0) const;
/**
* \return the comment associated with this cell
*/
QString comment() const;
void setComment(const QString& comment);
/**
* \return the conditions associated with this cell
*/
Conditions conditions() const;
void setConditions(const Conditions& conditions);
/**
* \return the database associated with this cell
*/
Database database() const;
/**
* The cell's formula. Usable to analyze the formula's tokens.
* \return pointer to the cell's formula object
*/
Formula formula() const;
/**
* Sets \p formula as associated formula of this cell.
*/
void setFormula(const Formula& formula);
/**
* Returns the link associated with cell. It is empty if this cell
* contains no link.
*/
QString link() const;
/**
* Sets a link for this cell. For example, setLink( "mailto:joe@somewhere.com" )
* will open a new e-mail if this cell is clicked.
* Possible choices for link are URL (web, ftp), e-mail address, local file,
* or another cell.
*/
void setLink(const QString& link);
/**
* \return the Style associated with this Cell
*/
Style style() const;
/**
* The effective style takes conditional style attributes into account.
* \return the effective Style associated with this Cell
*/
Style effectiveStyle() const;
void setStyle(const Style& style);
/**
* \return the validity checks associated with this cell
*/
Validity validity() const;
void setValidity(Validity validity);
/**
* Returns the value that this cell holds. It could be from the user
* (i.e. when s/he enters a value) or a result of formula.
*/
const Value value() const;
/**
* Sets the value for this cell.
* It also clears all errors, if the value itself is not an error.
* In addition to this, it calculates the outstring and sets the dirty
* flags so that a redraw is forced.
* \param value the new value
*
* \see setUserInput, parseUserInput
*/
void setValue(const Value& value);
/**
* Returns the richtext that this cell holds.
*/
QSharedPointer<QTextDocument> richText() const;
/**
* Sets the richtext for this cell.
* If \p text is empty, richtext is removed.
*
* \param text the new richtext
*/
void setRichText(QSharedPointer<QTextDocument> text);
/**
* Return the text the user entered. This could be a value (e.g. "14.03")
* or a formula (e.g. "=SUM(A1:A10)")
*/
QString userInput() const;
/**
* Sets the user input without parsing it.
* If \p text is a formula, creates a formula object and sets \p text as
* its expression. Otherwise, simply stores \p text as user input.
*
* \see parseUserInput, setValue
*/
void setUserInput(const QString& text);
/**
* Sets the user input without parsing it, without clearing existing formulas
* userinput or rich text. Used during loading of documents only!
* If \p text is a formula, creates a formula object and sets \p text as
* its expression. Otherwise, simply stores \p text as user input.
*
* \see parseUserInput, setValue
*/
void setRawUserInput(const QString& text);
/**
* Sets the user input and parses it.
* If \p text is a formula, creates a formula object and sets \p text as
* its expression. Otherwise, parses \p text, creates an appropriate value,
* including the proper type, validates the value and, if accepted, stores
* \p text as the user input and the value as the cell's value.
*
* \see setUserInput, setValue
*/
void parseUserInput(const QString& text);
/**
* \ingroup NativeFormat
*/
bool load(const KoXmlElement& cell,
int _xshift, int _yshift,
Paste::Mode pm = Paste::Normal,
Paste::Operation op = Paste::OverWrite,
bool paste = false);
/**
* \ingroup NativeFormat
* Save this cell.
* @param doc document to save cell in
* @param xOffset x offset
* @param yOffset y offset
* @param era set this to true if you want to encode relative references as absolutely (they will be switched
* back to relative references during decoding) - is used for cutting to clipboard
* Usually this is false, to only store the properties explicitly set.
*/
QDomElement save(QDomDocument& doc, int xOffset = 0, int yOffset = 0, bool era = false);
/**
* \ingroup NativeFormat
* Decodes a string into a time value.
*/
QTime toTime(const KoXmlElement &element);
/**
* \ingroup NativeFormat
* Decodes a string into a date value.
*/
QDate toDate(const KoXmlElement &element);
/**
* Copies the format from \p cell .
*
* @see copyAll(Cell *cell)
*/
void copyFormat(const Cell& cell);
/**
* Copies the content from \p cell .
*
* @see copyAll(Cell *cell)
*/
void copyContent(const Cell& cell);
/**
* Copies the format and the content from \p cell .
*
* @see copyContent( const Cell& cell )
* @see copyFormat( const Cell& cell )
*/
void copyAll(const Cell& cell);
/**
* @return the width of this cell as double
*/
double width() const;
/**
* @return the height of this cell as double
*/
double height() const;
/**
* \return the position of this cell
*/
QPoint cellPosition() const;
/**
* @return true if the cell should be printed in a print out.
* That's the case, if it has any content, border, backgroundcolor,
* or background brush.
*
* @see Sheet::print
*/
bool needsPrinting() const;
//
//END
//
//////////////////////////////////////////////////////////////////////////
//
//BEGIN Merging
//
/**
* If this cell is part of a merged cell, then the marker may
* never reside on this cell.
*
* @return true if another cell has this one merged into itself.
*/
bool isPartOfMerged() const;
/**
* \return the merging cell (might be this cell)
*/
Cell masterCell() const;
/**
* Merge a number of cells, i.e. force the cell to occupy other
* cells space. If '_x' and '_y' are 0 then the merging is
* disabled.
*
* @param _col is the column this cell is assumed to be in.
* @param _row is the row this cell is assumed to be in.
* @param _x tells to occupy _x additional cells in the horizontal
* @param _y tells to occupy _y additional cells in the vertical
*
*/
void mergeCells(int _col, int _row, int _x, int _y);
/**
* @return true if the cell is forced to obscure other cells.
*/
bool doesMergeCells() const;
/**
* @return the number of obscured cells in the horizontal direction as a
* result of cell merging (forced obscuring)
*/
int mergedXCells() const;
/**
* @return the number of obscured cells in the vertical direction as a
* result of cell merging (forced obscuring)
*/
int mergedYCells() const;
//
//END Merging
//
//////////////////////////////////////////////////////////////////////////
//
//BEGIN Matrix locking
//
bool isLocked() const;
QRect lockedCells() const;
//
//END Matrix locking
//
//////////////////////////////////////////////////////////////////////////
//
//BEGIN Cut & paste
//
/**
* Encodes the cell's formula into a text representation.
*
* \param fixedReferences encode relative references absolutely (this is used for copying
* a cell to make the paste operation create a formula that points
* to the original cells, not the cells at the same relative position)
* \see decodeFormula()
*/
QString encodeFormula(bool fixedReferences = false) const;
/**
* Decodes a text representation \p text into a formula expression.
*
* \see encodeFormula()
*/
QString decodeFormula(const QString& text) const;
/**
* Merges the @p new_text with @p old_text during a paste operation.
* If both texts represent doubles, then the operation is performed on both
* values and the result is returned. If both texts represents a formula or
* one a formula and the other a double value, then a formula is returned.
* In all other cases @p new_text is returned.
*
* @return the merged text.
*/
QString pasteOperation(const QString &new_text, const QString &old_text, Paste::Operation op);
//
//END Cut & paste
//
//////////////////////////////////////////////////////////////////////////
//
//BEGIN
//
/**
* Parses the formula.
* @return @c false on error.
*/
bool makeFormula();
//
//END
//
//////////////////////////////////////////////////////////////////////////
//
//BEGIN Effective style attributes
//
/** returns horizontal alignment, depending on style and value type */
int effectiveAlignX() const;
/** returns true, if cell format is of date type or content is a date */
bool isDate() const;
/** returns true, if cell format is of time type or content is a time */
bool isTime() const;
/** returns true, if cell format is of text type */
bool isText() const;
//
//END Effective style attributes
//
//////////////////////////////////////////////////////////////////////////
//
//BEGIN Operators
//
/**
* Assignment.
*/
Cell& operator=(const Cell& other);
/**
* Tests whether this cell's location is less than the \p other 's.
* (QMap support)
* \note Does not compare the cell attributes/data.
*/
bool operator<(const Cell& other) const;
/**
* Tests for equality with \p other 's location only.
* (QHash support)
* \note Does not compare the cell attributes/data.
*/
bool operator==(const Cell& other) const;
/**
* Is null.
*/
bool operator!() const;
//
//END Operators
//
//////////////////////////////////////////////////////////////////////////
//
//BEGIN
//
/**
* Tests for equality of all cell attributes/data to those in \p other .
*/
bool compareData(const Cell& other) const;
private:
friend class CellTest;
class Private;
QSharedDataPointer<Private> d;
/**
* \ingroup NativeFormat
*/
bool loadCellData(const KoXmlElement &text, Paste::Operation op, const QString &dataType = QString());
/**
* \ingroup NativeFormat
*/
bool saveCellResult(QDomDocument& doc, QDomElement& result, QString str);
};
inline uint qHash(const Cell& cell)
{
return (static_cast<uint>(cell.column()) << 16) + static_cast<uint>(cell.row());
}
} // namespace Sheets
} // namespace Calligra
Q_DECLARE_TYPEINFO(Calligra::Sheets::Cell, Q_MOVABLE_TYPE);
Q_DECLARE_METATYPE(Calligra::Sheets::Cell)
/***************************************************************************
QDebug support
****************************************************************************/
inline QDebug operator<<(QDebug str, const Calligra::Sheets::Cell& cell)
{
return str << qPrintable(QString("%1%2").arg(Calligra::Sheets::Cell::columnName(cell.column())).arg(QString::number(cell.row())));
}
#endif // CALLIGRA_SHEETS_CELL
|