CellRange.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /************************************************************************/
  2. /* Copyright (C), 2016-2020, [Home], 保留所有权利;
  3. /* 模 块 名:;
  4. /* 描 述:;
  5. /*
  6. /* 版 本:[V];
  7. /* 作 者:[Home];
  8. /* 日 期:[1/29/2017];
  9. /*
  10. /*
  11. /* 注 意:;
  12. /*
  13. /* 修改记录:[Home];
  14. /* 修改日期:;
  15. /* 修改版本:;
  16. /* 修改内容:;
  17. /************************************************************************/
  18. #ifndef __CELL_RANGE__
  19. #define __CELL_RANGE__
  20. class CCellID
  21. {
  22. public:
  23. // 行,列号;
  24. int row, col;
  25. public:
  26. CCellID(int nRow = -1, int nCol = -1) : row(nRow), col(nCol) {}
  27. int IsValid() const { return (row >= 0 && col >= 0); }
  28. int operator==(const CCellID& rhs) { return (row == rhs.row && col == rhs.col); }
  29. int operator!=(const CCellID& rhs) { return !operator==(rhs); }
  30. };
  31. class CCellRange
  32. {
  33. public:
  34. CCellRange(int nMinRow = -1, int nMinCol = -1, int nMaxRow = -1, int nMaxCol = -1)
  35. {
  36. Set(nMinRow, nMinCol, nMaxRow, nMaxCol);
  37. }
  38. void Set(int nMinRow = -1, int nMinCol = -1, int nMaxRow = -1, int nMaxCol = -1);
  39. int IsValid() const;
  40. int InRange(int row, int col) const;
  41. int InRange(const CCellID& cellID) const;
  42. int Count() { return (m_nMaxRow - m_nMinRow + 1) * (m_nMaxCol - m_nMinCol + 1); }
  43. CCellID GetTopLeft() const;
  44. CCellRange Intersect(const CCellRange& rhs) const;
  45. int GetMinRow() const { return m_nMinRow; }
  46. void SetMinRow(int minRow) { m_nMinRow = minRow; }
  47. int GetMinCol() const { return m_nMinCol; }
  48. void SetMinCol(int minCol) { m_nMinCol = minCol; }
  49. int GetMaxRow() const { return m_nMaxRow; }
  50. void SetMaxRow(int maxRow) { m_nMaxRow = maxRow; }
  51. int GetMaxCol() const { return m_nMaxCol; }
  52. void SetMaxCol(int maxCol) { m_nMaxCol = maxCol; }
  53. int GetRowSpan() const { return m_nMaxRow - m_nMinRow + 1; }
  54. int GetColSpan() const { return m_nMaxCol - m_nMinCol + 1; }
  55. void operator=(const CCellRange& rhs);
  56. int operator==(const CCellRange& rhs);
  57. int operator!=(const CCellRange& rhs);
  58. protected:
  59. int m_nMinRow;
  60. int m_nMinCol;
  61. int m_nMaxRow;
  62. int m_nMaxCol;
  63. };
  64. inline void CCellRange::Set(int minRow, int minCol, int maxRow, int maxCol)
  65. {
  66. m_nMinRow = minRow;
  67. m_nMinCol = minCol;
  68. m_nMaxRow = maxRow;
  69. m_nMaxCol = maxCol;
  70. }
  71. inline void CCellRange::operator=(const CCellRange& rhs)
  72. {
  73. Set(rhs.m_nMinRow, rhs.m_nMinCol, rhs.m_nMaxRow, rhs.m_nMaxCol);
  74. }
  75. inline int CCellRange::operator==(const CCellRange& rhs)
  76. {
  77. return ((m_nMinRow == rhs.m_nMinRow) && (m_nMinCol == rhs.m_nMinCol) &&
  78. (m_nMaxRow == rhs.m_nMaxRow) && (m_nMaxCol == rhs.m_nMaxCol));
  79. }
  80. inline int CCellRange::operator!=(const CCellRange& rhs)
  81. {
  82. return !operator==(rhs);
  83. }
  84. /************************************************************************/
  85. /* 函数:IsValid[1/28/2017 Home];
  86. /* 描述:单元格有效范围是否正确;
  87. /* 参数:;
  88. /* [IN] :;
  89. /* [OUT] :;
  90. /* [IN/OUT] :;
  91. /* 返回:void;
  92. /* 注意:;
  93. /* 示例:;
  94. /*
  95. /* 修改:;
  96. /* 日期:;
  97. /* 内容:;
  98. /************************************************************************/
  99. inline int CCellRange::IsValid() const
  100. {
  101. //return (m_nMinRow >= 0 && m_nMinCol >= 0 && m_nMaxRow >= 0 && m_nMaxCol >= 0 && m_nMinRow <= m_nMaxRow && m_nMinCol <= m_nMaxCol);
  102. return (m_nMinRow >= 0 && m_nMinCol >= 0 && m_nMinRow <= m_nMaxRow && m_nMinCol <= m_nMaxCol);
  103. }
  104. /************************************************************************/
  105. /* 函数:InRange[1/28/2017 Home];
  106. /* 描述:单元格是否有范围内;
  107. /* 参数:;
  108. /* [IN] :;
  109. /* [OUT] :;
  110. /* [IN/OUT] :;
  111. /* 返回:void;
  112. /* 注意:;
  113. /* 示例:;
  114. /*
  115. /* 修改:;
  116. /* 日期:;
  117. /* 内容:;
  118. /************************************************************************/
  119. inline int CCellRange::InRange(int row, int col) const
  120. {
  121. return (row >= m_nMinRow && row <= m_nMaxRow && col >= m_nMinCol && col <= m_nMaxCol);
  122. }
  123. inline int CCellRange::InRange(const CCellID& cellID) const
  124. {
  125. return InRange(cellID.row, cellID.col);
  126. }
  127. /************************************************************************/
  128. /* 函数:GetTopLeft[1/28/2017 Home];
  129. /* 描述:获取左上角单元格;
  130. /* 参数:;
  131. /* [IN] :;
  132. /* [OUT] :;
  133. /* [IN/OUT] :;
  134. /* 返回:void;
  135. /* 注意:;
  136. /* 示例:;
  137. /*
  138. /* 修改:;
  139. /* 日期:;
  140. /* 内容:;
  141. /************************************************************************/
  142. inline CCellID CCellRange::GetTopLeft() const
  143. {
  144. return CCellID(m_nMinRow, m_nMinCol);
  145. }
  146. /************************************************************************/
  147. /* 函数:Intersect[1/28/2017 Home];
  148. /* 描述:两范围是否有相交;
  149. /* 参数:;
  150. /* [IN] :;
  151. /* [OUT] :;
  152. /* [IN/OUT] :;
  153. /* 返回:void;
  154. /* 注意:;
  155. /* 示例:;
  156. /*
  157. /* 修改:;
  158. /* 日期:;
  159. /* 内容:;
  160. /************************************************************************/
  161. inline CCellRange CCellRange::Intersect(const CCellRange& rhs) const
  162. {
  163. return CCellRange(max(m_nMinRow, rhs.m_nMinRow), max(m_nMinCol, rhs.m_nMinCol),
  164. min(m_nMaxRow, rhs.m_nMaxRow), min(m_nMaxCol, rhs.m_nMaxCol));
  165. }
  166. #endif // __CELL_RANGE__;