Skip to content

Commit

Permalink
Spreadsheet changes
Browse files Browse the repository at this point in the history
Various changes to support in-place editing, and more.
  • Loading branch information
realthunder committed Jul 16, 2019
1 parent c6d28ea commit f5f93bf
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 180 deletions.
32 changes: 12 additions & 20 deletions src/Mod/Spreadsheet/App/Cell.cpp
Expand Up @@ -102,7 +102,6 @@ Cell::Cell(const CellAddress &_address, PropertySheet *_owner)
: address(_address)
, owner(_owner)
, used(0)
, expression(0)
, alignment(ALIGNMENT_HIMPLIED | ALIGNMENT_LEFT | ALIGNMENT_VIMPLIED | ALIGNMENT_VCENTER)
, style()
, foregroundColor(0, 0, 0, 1)
Expand Down Expand Up @@ -142,7 +141,7 @@ Cell &Cell::operator =(const Cell &rhs)

address = rhs.address;

setExpression(rhs.expression ? rhs.expression->copy() : 0);
setExpression(App::ExpressionPtr(rhs.expression ? rhs.expression->copy() : nullptr));
setAlignment(rhs.alignment);
setStyle(rhs.style);
setBackground(rhs.backgroundColor);
Expand All @@ -166,16 +165,14 @@ Cell &Cell::operator =(const Cell &rhs)

Cell::~Cell()
{
if (expression)
delete expression;
}

/**
* Set the expression tree to \a expr.
*
*/

void Cell::setExpression(App::Expression *expr)
void Cell::setExpression(App::ExpressionPtr &&expr)
{
PropertySheet::AtomicPropertyChange signaller(*owner);

Expand Down Expand Up @@ -204,9 +201,7 @@ void Cell::setExpression(App::Expression *expr)
expr->comment.clear();
}

if (expression)
delete expression;
expression = expr;
expression = std::move(expr);
setUsed(EXPRESSION_SET, !!expression);

/* Update dependencies */
Expand Down Expand Up @@ -247,21 +242,21 @@ const App::Expression *Cell::getExpression(bool withFormat) const
bool Cell::getStringContent(std::string & s, bool persistent) const
{
if (expression) {
if (freecad_dynamic_cast<App::StringExpression>(expression)) {
s = static_cast<App::StringExpression*>(expression)->getText();
if (freecad_dynamic_cast<App::StringExpression>(expression.get())) {
s = static_cast<App::StringExpression*>(expression.get())->getText();
char * end;
errno = 0;
double d = strtod(s.c_str(), &end);
(void)d; // fix gcc warning
if (!*end && errno == 0)
s = "'" + s;
}
else if (freecad_dynamic_cast<App::ConstantExpression>(expression))
else if (freecad_dynamic_cast<App::ConstantExpression>(expression.get()))
s = "=" + expression->toString();
else if (freecad_dynamic_cast<App::NumberExpression>(expression))
else if (freecad_dynamic_cast<App::NumberExpression>(expression.get()))
s = expression->toString();
else
s = "=" + expression->toString();
s = "=" + expression->toString(persistent);

return true;
}
Expand All @@ -272,7 +267,7 @@ bool Cell::getStringContent(std::string & s, bool persistent) const
}

void Cell::afterRestore() {
auto expr = freecad_dynamic_cast<StringExpression>(expression);
auto expr = freecad_dynamic_cast<StringExpression>(expression.get());
if(expr)
setContent(expr->getText().c_str());
}
Expand All @@ -285,7 +280,7 @@ void Cell::setContent(const char * value)
clearException();
if (value != 0) {
if(owner->sheet()->isRestoring()) {
expression = new App::StringExpression(owner->sheet(),value);
expression.reset(new App::StringExpression(owner->sheet(),value));
setUsed(EXPRESSION_SET, true);
return;
}
Expand Down Expand Up @@ -320,7 +315,7 @@ void Cell::setContent(const char * value)
}

try {
setExpression(expr);
setExpression(App::ExpressionPtr(expr));
signaller.tryInvoke();
} catch (Base::Exception &e) {
if(value) {
Expand All @@ -330,7 +325,7 @@ void Cell::setContent(const char * value)
_value += value;
value = _value.c_str();
}
setExpression(new App::StringExpression(owner->sheet(), value));
setExpression(App::ExpressionPtr(new App::StringExpression(owner->sheet(), value)));
setParseException(e.what());
}
}
Expand Down Expand Up @@ -770,9 +765,6 @@ void Cell::save(std::ostream &os, const char *indent, bool noContent) const {
os << "colSpan=\"" << colSpan << "\" ";
}

if(editMode)
os << "editMode=\"" << editMode << "\" ";

os << "/>";
if(!noContent)
os << std::endl;
Expand Down
11 changes: 2 additions & 9 deletions src/Mod/Spreadsheet/App/Cell.h
Expand Up @@ -37,11 +37,6 @@ class XMLReader;
class Writer;
}

namespace App {
class Expression;
class ExpressionVisitor;
}

namespace Spreadsheet {

class PropertySheet;
Expand Down Expand Up @@ -152,9 +147,7 @@ class SpreadsheetExport Cell {

void setParseException(const std::string & e);

//void setExpression(const Expression * expr);

void setExpression(App::Expression *expr);
void setExpression(App::ExpressionPtr &&expr);

void setUsed(int mask, bool state = true);

Expand Down Expand Up @@ -184,7 +177,7 @@ class SpreadsheetExport Cell {
PropertySheet * owner;

int used;
App::Expression * expression;
mutable App::ExpressionPtr expression;
int alignment;
std::set<std::string> style;
App::Color foregroundColor;
Expand Down
9 changes: 6 additions & 3 deletions src/Mod/Spreadsheet/App/PropertyColumnWidths.cpp
Expand Up @@ -63,8 +63,11 @@ App::Property *PropertyColumnWidths::Copy() const

void PropertyColumnWidths::Paste(const App::Property &from)
{
setValues(static_cast<const PropertyColumnWidths&>(from).getValues());
}

void PropertyColumnWidths::setValues(const std::map<int,int> &values) {
aboutToSetValue();
const PropertyColumnWidths * frompcw = static_cast<const PropertyColumnWidths*>(&from);

std::map<int, int>::const_iterator i;

Expand All @@ -79,8 +82,8 @@ void PropertyColumnWidths::Paste(const App::Property &from)
clear();

/* Copy new map from from */
i = frompcw->begin();
while (i != frompcw->end()) {
i = values.begin();
while (i != values.end()) {
insert(*i);
dirty.insert(i->first);
++i;
Expand Down
2 changes: 2 additions & 0 deletions src/Mod/Spreadsheet/App/PropertyColumnWidths.h
Expand Up @@ -39,6 +39,8 @@ class SpreadsheetExport PropertyColumnWidths : public App::Property, std::map<in

void setValue(int col, int width);

void setValues(const std::map<int,int> &);

std::map<int, int> getValues() const {
return *this;
}
Expand Down
11 changes: 7 additions & 4 deletions src/Mod/Spreadsheet/App/PropertyRowHeights.cpp
Expand Up @@ -34,7 +34,7 @@

using namespace Spreadsheet;

const int PropertyRowHeights::defaultHeight = 20;
const int PropertyRowHeights::defaultHeight = 30;

TYPESYSTEM_SOURCE(Spreadsheet::PropertyRowHeights , App::Property);

Expand All @@ -56,8 +56,11 @@ App::Property *PropertyRowHeights::Copy() const

void PropertyRowHeights::Paste(const Property &from)
{
setValues(static_cast<const PropertyRowHeights&>(from).getValues());
}

void PropertyRowHeights::setValues(const std::map<int,int> &values) {
aboutToSetValue();
const PropertyRowHeights * fromprh = static_cast<const PropertyRowHeights*>(&from);

std::map<int, int>::const_iterator i;

Expand All @@ -72,8 +75,8 @@ void PropertyRowHeights::Paste(const Property &from)
clear();

/* Copy new map from from */
i = fromprh->begin();
while (i != fromprh->end()) {
i = values.begin();
while (i != values.end()) {
insert(*i);
dirty.insert(i->first);
++i;
Expand Down
2 changes: 2 additions & 0 deletions src/Mod/Spreadsheet/App/PropertyRowHeights.h
Expand Up @@ -39,6 +39,8 @@ class SpreadsheetExport PropertyRowHeights : public App::Property, std::map<int,

void setValue(int row, int height);

void setValues(const std::map<int,int> &);

int getValue(int row) const {
std::map<int, int>::const_iterator i = find(row);
return i != end() ? i->second : defaultHeight;
Expand Down

0 comments on commit f5f93bf

Please sign in to comment.