1 /*
2  * Copyright (C) 2011 Elvis Stansvik <elvstone@gmail.com>
3  *
4  * For general Scribus (>=1.3.2) copyright and licensing information please refer
5  * to the COPYING file provided with the program. Following this notice may exist
6  * a copyright and/or license notice that predates the release of Scribus 1.3.2
7  * for which a new license (GPL+exception) is in place.
8  */
9 
10 #include <QDebug>
11 
12 #include "cellarea.h"
13 
CellArea(int row,int column,int width,int height)14 CellArea::CellArea(int row, int column, int width, int height) :
15 	m_row(row), m_column(column), m_width(width), m_height(height)
16 {
17 }
18 
19 //CellArea::CellArea(const CellArea& other) : m_row(other.m_row), m_column(other.m_column),
20 //	m_width(other.m_width), m_height(other.m_height)
21 //{
22 //}
23 
contains(int row,int column) const24 bool CellArea::contains(int row, int column) const
25 {
26 	if (!isValid())
27 		return false;
28 
29 	return row >= this->row() && row <= bottom() &&
30 			column >= this->column() && column <= right();
31 }
32 
contains(const CellArea & area) const33 bool CellArea::contains(const CellArea& area) const
34 {
35 	if (!isValid() && !area.isValid())
36 		return false;
37 
38 	return area.column() >= column() && area.right() <= right() &&
39 			area.row() >= row() && area.bottom() <= bottom();
40 }
41 
intersects(const CellArea & area)42 bool CellArea::intersects(const CellArea& area)
43 {
44 	if (!isValid() && !area.isValid())
45 		return false;
46 
47 	// !(outside) || contains(area)
48 	return !(area.column() > right() || area.right() < column() ||
49 			 area.bottom() < row() || area.row() > bottom()) || contains(area);
50 }
51 
translated(int rows,int columns) const52 CellArea CellArea::translated(int rows, int columns) const
53 {
54 	CellArea area(row() + rows, column() + columns, width(), height());
55 	return area;
56 }
57 
translate(int rows,int columns)58 void CellArea::translate(int rows, int columns)
59 {
60 	m_row += rows;
61 	m_column += columns;
62 }
63 
adjusted(int rows,int columns,int width,int height) const64 CellArea CellArea::adjusted(int rows, int columns, int width, int height) const
65 {
66 	CellArea area(row() + rows, column() + columns, this->width() + width, this->height() + height);
67 	return area;
68 }
69 
adjust(int rows,int columns,int width,int height)70 void CellArea::adjust(int rows, int columns, int width, int height)
71 {
72 	m_row += rows;
73 	m_column += columns;
74 	m_width += width;
75 	m_height += height;
76 }
77 
united(CellArea & area) const78 CellArea CellArea::united(CellArea& area) const
79 {
80 	int newRow = qMin(row(), area.row());
81 	int newColumn = qMin(column(), area.column());
82 	int newWidth = qMax(right() - newColumn + 1, area.right() - newColumn + 1);
83 	int newHeight = qMax(bottom() - newRow + 1, area.bottom() - newRow + 1);
84 	CellArea unitedArea(newRow, newColumn, newWidth, newHeight);
85 	return unitedArea;
86 }
87 
adjustedForRowInsertion(int index,int numRows)88 CellArea CellArea::adjustedForRowInsertion(int index, int numRows)
89 {
90 	CellArea adjustedArea(*this);
91 
92 	if (numRows < 1 || index > bottom())
93 		return adjustedArea; // No rows inserted, or inserted after area.
94 
95 	if (index <= row())
96 		adjustedArea.adjust(numRows, 0, 0, 0); // Inserted before area.
97 	else
98 		adjustedArea.adjust(0, 0, 0, numRows); // Inserted inside area.
99 
100 	return adjustedArea;
101 }
102 
adjustedForRowRemoval(int index,int numRows)103 CellArea CellArea::adjustedForRowRemoval(int index, int numRows)
104 {
105 	CellArea adjustedArea(*this);
106 
107 	if (numRows < 1 || index > bottom())
108 		return adjustedArea; // No rows removed, or removed after area.
109 
110 	int end = index + numRows - 1;
111 	if (end < row())
112 	{
113 		// Removed before area.
114 		adjustedArea.adjust(-numRows, 0, 0, 0);
115 	}
116 	else
117 	{
118 		// Removed inside area.
119 		int removedInsideArea = qMin(bottom(), end) - qMax(row(), index) + 1;
120 		adjustedArea.adjust(0, 0, 0, -removedInsideArea);
121 	}
122 
123 	return adjustedArea;
124 }
125 
adjustedForColumnInsertion(int index,int numColumns)126 CellArea CellArea::adjustedForColumnInsertion(int index, int numColumns)
127 {
128 	CellArea adjustedArea(*this);
129 
130 	if (numColumns < 1 || index > right())
131 		return adjustedArea; // No columns inserted, or inserted after area.
132 
133 	if (index <= column())
134 		adjustedArea.adjust(0, numColumns, 0, 0); // Inserted before area.
135 	else
136 		adjustedArea.adjust(0, 0, numColumns, 0); // Inserted inside area.
137 
138 	return adjustedArea;
139 }
140 
adjustedForColumnRemoval(int index,int numColumns)141 CellArea CellArea::adjustedForColumnRemoval(int index, int numColumns)
142 {
143 	CellArea adjustedArea(*this);
144 
145 	if (numColumns < 1 || index > right())
146 		return adjustedArea; // No columns removed, or removed after area.
147 
148 	int end = index + numColumns - 1;
149 	if (end < column())
150 	{
151 		// Removed before area.
152 		adjustedArea.adjust(0, -numColumns, 0, 0);
153 	}
154 	else
155 	{
156 		// Removed inside area.
157 		int removedInsideArea = qMin(right(), end) - qMax(column(), index) + 1;
158 		adjustedArea.adjust(0, 0, -removedInsideArea, 0);
159 	}
160 
161 	return adjustedArea;
162 }
163 
operator ==(const CellArea & lhs,const CellArea & rhs)164 bool operator==(const CellArea& lhs, const CellArea& rhs)
165 {
166 	return lhs.row() == rhs.row() && lhs.column() == rhs.column() &&
167 			lhs.width() == rhs.width() && lhs.height() == rhs.height();
168 }
169 
operator !=(const CellArea & lhs,const CellArea & rhs)170 bool operator!=(const CellArea& lhs, const CellArea& rhs)
171 {
172 	return lhs.row() != rhs.row() || lhs.column() != rhs.column() ||
173 			lhs.width() != rhs.width() || lhs.height() != rhs.height();
174 }
175 
operator <<(QDebug debug,const CellArea & area)176 QDebug operator<<(QDebug debug, const CellArea& area)
177 {
178 	debug.nospace() << "(" << area.row() << ", " << area.column() << " " << area.width() << "x" << area.height() << ")";
179 	return debug.space();
180 }
181