Spreadsheet: Refactored code to use AtomicPropertyChangeInterface.

This commit is contained in:
Eivind Kvedalen 2016-02-08 00:11:03 +01:00 committed by wmayer
parent 53dcaccd4e
commit 8c943d0ad7
4 changed files with 39 additions and 60 deletions

View File

@ -118,7 +118,7 @@ Cell::Cell(const Cell &other)
Cell &Cell::operator =(const Cell &rhs)
{
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
used = 0;
address = rhs.address;
@ -149,7 +149,7 @@ Cell::~Cell()
void Cell::setExpression(App::Expression *expr)
{
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
/* Remove dependencies */
owner->removeDependencies(address);
@ -209,7 +209,7 @@ bool Cell::getStringContent(std::string & s) const
void Cell::setContent(const char * value)
{
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
App::Expression * expr = 0;
setUsed(PARSE_EXCEPTION_SET, false);
@ -258,7 +258,7 @@ void Cell::setContent(const char * value)
void Cell::setAlignment(int _alignment)
{
if (_alignment != alignment) {
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
alignment = _alignment;
setUsed(ALIGNMENT_SET, alignment != (ALIGNMENT_HIMPLIED | ALIGNMENT_LEFT | ALIGNMENT_VIMPLIED | ALIGNMENT_VCENTER));
@ -284,7 +284,7 @@ bool Cell::getAlignment(int & _alignment) const
void Cell::setStyle(const std::set<std::string> & _style)
{
if (_style != style) {
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
style = _style;
setUsed(STYLE_SET, style.size() > 0);
@ -310,7 +310,7 @@ bool Cell::getStyle(std::set<std::string> & _style) const
void Cell::setForeground(const App::Color &color)
{
if (color != foregroundColor) {
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
foregroundColor = color;
setUsed(FOREGROUND_COLOR_SET, foregroundColor != App::Color(0, 0, 0, 1));
@ -336,7 +336,7 @@ bool Cell::getForeground(App::Color &color) const
void Cell::setBackground(const App::Color &color)
{
if (color != backgroundColor) {
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
backgroundColor = color;
setUsed(BACKGROUND_COLOR_SET, backgroundColor != App::Color(1, 1, 1, 0));
@ -371,7 +371,7 @@ void Cell::setDisplayUnit(const std::string &unit)
}
if (newDisplayUnit != displayUnit) {
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
displayUnit = newDisplayUnit;
setUsed(DISPLAY_UNIT_SET, !displayUnit.isEmpty());
@ -394,7 +394,7 @@ bool Cell::getDisplayUnit(DisplayUnit &unit) const
void Cell::setAlias(const std::string &n)
{
if (alias != n) {
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
owner->revAliasProp.erase(alias);
@ -426,7 +426,7 @@ bool Cell::getAlias(std::string &n) const
void Cell::setComputedUnit(const Base::Unit &unit)
{
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
computedUnit = unit;
setUsed(COMPUTED_UNIT_SET, !computedUnit.isEmpty());
@ -454,7 +454,7 @@ bool Cell::getComputedUnit(Base::Unit & unit) const
void Cell::setSpans(int rows, int columns)
{
if (rows != rowSpan || columns != colSpan) {
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
rowSpan = rows;
colSpan = columns;
@ -538,7 +538,7 @@ void Cell::restore(Base::XMLReader &reader)
const char* colSpan = reader.hasAttribute("colSpan") ? reader.getAttribute("colSpan") : 0;
// Don't trigger multiple updates below; wait until everything is loaded by calling unfreeze() below.
PropertySheet::Signaller signaller(*owner);
PropertySheet::AtomicPropertyChange signaller(*owner);
if (content) {
setContent(content);

View File

@ -239,18 +239,19 @@ Cell * PropertySheet::createCell(CellAddress address)
PropertySheet::PropertySheet(Sheet *_owner)
: Property()
, AtomicPropertyChangeInterface()
, owner(_owner)
, signalCounter(0)
{
}
PropertySheet::PropertySheet(const PropertySheet &other)
: dirty(other.dirty)
: Property()
, AtomicPropertyChangeInterface()
, dirty(other.dirty)
, mergedCells(other.mergedCells)
, owner(other.owner)
, propertyNameToCellMap(other.propertyNameToCellMap)
, documentObjectToCellMap(other.documentObjectToCellMap)
, signalCounter(0)
{
std::map<CellAddress, Cell* >::const_iterator i = other.data.begin();
@ -273,7 +274,7 @@ App::Property *PropertySheet::Copy(void) const
void PropertySheet::Paste(const Property &from)
{
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
const PropertySheet * froms = static_cast<const PropertySheet*>(&from);
@ -541,7 +542,7 @@ void PropertySheet::clear(CellAddress address)
if (i == data.end())
return;
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
// Spit cell to clean up mergeCells map; all data is in first cell anyway
splitCell(address);
@ -571,7 +572,7 @@ void PropertySheet::moveCell(CellAddress currPos, CellAddress newPos, std::map<A
std::map<CellAddress, Cell*>::const_iterator i = data.find(currPos);
std::map<CellAddress, Cell*>::const_iterator j = data.find(newPos);
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
if (j != data.end())
clear(newPos);
@ -684,7 +685,7 @@ void PropertySheet::insertRows(int row, int count)
RewriteExpressionVisitor visitor(CellAddress(row, CellAddress::MAX_COLUMNS), count, 0);
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
for (std::vector<CellAddress>::const_reverse_iterator i = keys.rbegin(); i != keys.rend(); ++i) {
std::map<CellAddress, Cell*>::iterator j = data.find(*i);
@ -732,7 +733,7 @@ void PropertySheet::removeRows(int row, int count)
RewriteExpressionVisitor visitor(CellAddress(row + count - 1, CellAddress::MAX_COLUMNS), -count, 0);
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
for (std::vector<CellAddress>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
std::map<CellAddress, Cell*>::iterator j = data.find(*i);
@ -770,7 +771,7 @@ void PropertySheet::insertColumns(int col, int count)
RewriteExpressionVisitor visitor(CellAddress(CellAddress::MAX_ROWS, col), 0, count);
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
for (std::vector<CellAddress>::const_reverse_iterator i = keys.rbegin(); i != keys.rend(); ++i) {
std::map<CellAddress, Cell*>::iterator j = data.find(*i);
@ -818,7 +819,7 @@ void PropertySheet::removeColumns(int col, int count)
RewriteExpressionVisitor visitor(CellAddress(CellAddress::MAX_ROWS, col + count - 1), 0, -count);
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
for (std::vector<CellAddress>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
std::map<CellAddress, Cell*>::iterator j = data.find(*i);
@ -859,7 +860,7 @@ bool PropertySheet::mergeCells(CellAddress from, CellAddress to)
}
}
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
// Clear cells that will be hidden by the merge
for (int r = from.row(); r <= to.row(); ++r)
@ -888,7 +889,7 @@ void PropertySheet::splitCell(CellAddress address)
return;
CellAddress anchor = i->second;
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
cellAt(anchor)->getSpans(rows, cols);
for (int r = anchor.row(); r <= anchor.row() + rows; ++r)
@ -1127,7 +1128,7 @@ void PropertySheet::renamedDocumentObject(const App::DocumentObject * docObj)
std::map<CellAddress, Cell* >::iterator i = data.begin();
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
RelabelDocumentObjectExpressionVisitor v(documentObjectName[docObj], docObj->Label.getValue());
while (i != data.end()) {
@ -1148,7 +1149,7 @@ void PropertySheet::renamedDocument(const App::Document * doc)
std::map<CellAddress, Cell* >::iterator i = data.begin();
/* Resolve all cells */
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
RelabelDocumentExpressionVisitor v(documentName[doc], doc->Label.getValue());
while (i != data.end()) {
@ -1218,7 +1219,7 @@ const std::set<std::string> &PropertySheet::getDeps(CellAddress pos) const
void PropertySheet::recomputeDependencies(CellAddress key)
{
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
removeDependencies(key);
addDependencies(key);
@ -1227,7 +1228,7 @@ void PropertySheet::recomputeDependencies(CellAddress key)
void PropertySheet::rebuildDocDepList()
{
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
docDeps.clear();
BuildDocDepsExpressionVisitor v(docDeps);
@ -1255,7 +1256,7 @@ void PropertySheet::resolveAll()
std::map<CellAddress, Cell* >::iterator i = data.begin();
/* Resolve all cells */
Signaller signaller(*this);
AtomicPropertyChange signaller(*this);
while (i != data.end()) {
recomputeDependencies(i->first);
setDirty(i->first);
@ -1263,18 +1264,3 @@ void PropertySheet::resolveAll()
}
touch();
}
PropertySheet::Signaller::Signaller(PropertySheet &sheet)
: mSheet(sheet)
{
if (mSheet.signalCounter == 0)
mSheet.aboutToSetValue();
mSheet.signalCounter++;
}
PropertySheet::Signaller::~Signaller()
{
mSheet.signalCounter--;
if (mSheet.signalCounter == 0)
mSheet.hasSetValue();
}

View File

@ -37,7 +37,7 @@ class Sheet;
class PropertySheet;
class SheetObserver;
class PropertySheet : public App::Property {
class PropertySheet : public App::Property, private App::AtomicPropertyChangeInterface<PropertySheet> {
TYPESYSTEM_HEADER();
public:
@ -129,14 +129,6 @@ public:
const std::set<App::DocumentObject*> & getDocDeps() const { return docDeps; }
class Signaller {
public:
Signaller(PropertySheet & sheet);
~Signaller();
private:
PropertySheet & mSheet;
};
void recomputeDependencies(CellAddress key);
PyObject *getPyObject(void);
@ -157,10 +149,16 @@ private:
PropertySheet(const PropertySheet & other);
friend class Signaller;
/* friends */
friend class AtomicPropertyChange;
friend class SheetObserver;
friend class Cell;
friend class Sheet;
Cell *cellAt(CellAddress address);
Cell *nonNullCellAt(CellAddress address);
@ -171,8 +169,6 @@ private:
bool rowSortFunc(const CellAddress &a, const CellAddress &b);
friend class Cell;
/*! Set of cells that have been marked dirty */
std::set<CellAddress> dirty;
@ -230,9 +226,6 @@ private:
/*! Mapping of alias property to cell position */
std::map<std::string, CellAddress> revAliasProp;
/*! Internal counter used to track when to emit aboutToSet and hasSetValue calls */
int signalCounter;
/*! The associated python object */
Py::Object PythonObject;
};

View File

@ -144,7 +144,7 @@ bool Sheet::importFromFile(const std::string &filename, char delimiter, char quo
std::ifstream file;
int row = 0;
PropertySheet::Signaller signaller(cells);
PropertySheet::AtomicPropertyChange signaller(cells);
clearAll();