TableCell PageItem_Table::cellAt(const QPointF& point) const { QPointF gridPoint = getTransform().inverted().map(point) - gridOffset(); if (!QRectF(0, 0, tableWidth(), tableHeight()).contains(gridPoint)) return TableCell(); // Outside table grid. return cellAt( qUpperBound(m_rowPositions, gridPoint.y()) - m_rowPositions.begin() - 1, qUpperBound(m_columnPositions, gridPoint.x()) - m_columnPositions.begin() - 1); }
void PageItem_Table::resize(double width, double height) { ASSERT_VALID(); /* * Distribute width proportionally to columns, but don't let any column width below * MinimumColumnWidth. */ double requestedWidthFactor = width / tableWidth(); double oldMinWidth = *std::min_element(m_columnWidths.begin(), m_columnWidths.end()); double newMinWidth = qMax(oldMinWidth * requestedWidthFactor, MinimumColumnWidth); double actualWidthFactor = newMinWidth / oldMinWidth; for (int col = 0; col < columns(); ++col) { m_columnWidths[col] *= actualWidthFactor; m_columnPositions[col] *= actualWidthFactor; } /* * Distribute height proportionally to rows, but don't let any row height below * MinimumRowHeight. */ double requestedHeightFactor = height / tableHeight(); double oldMinHeight = *std::min_element(m_rowHeights.begin(), m_rowHeights.end()); double newMinHeight = qMax(oldMinHeight * requestedHeightFactor, MinimumRowHeight); double actualHeightFactor = newMinHeight / oldMinHeight; for (int row = 0; row < rows(); ++row) { m_rowHeights[row] *= actualHeightFactor; m_rowPositions[row] *= actualHeightFactor; } // Update cells. TODO: Not for entire table. updateCells(); emit changed(); ASSERT_VALID(); }
//-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RifEclipseDataTableFormatter::outputHorizontalLine(RifEclipseOutputTableLine& comment) { if (comment.lineType == HORIZONTAL_LINE) { int charCount = tableWidth(); QChar fillChar = ' '; if (!comment.data.empty()) { QString firstString = comment.data[0]; if (!firstString.isEmpty()) { fillChar = firstString[0]; } } QString str; str.fill(fillChar, charCount); m_out << m_commentPrefix << str << "\n"; } }
TableHandle PageItem_Table::hitTest(const QPointF& point, double threshold) const { const QPointF framePoint = getTransform().inverted().map(point); const QPointF gridPoint = framePoint - gridOffset(); const QRectF gridRect = QRectF(0.0, 0.0, tableWidth(), tableHeight()); // Test if hit is outside frame. if (!QRectF(0.0, 0.0, width(), height()).contains(framePoint)) return TableHandle(TableHandle::None); // Test if hit is outside table. if (!gridRect.adjusted(-threshold, -threshold, threshold, threshold).contains(gridPoint)) return TableHandle(TableHandle::None); const double tableHeight = this->tableHeight(); const double tableWidth = this->tableWidth(); const double x = gridPoint.x(); const double y = gridPoint.y(); // Test if hit is on left edge of table. if (x <= threshold) return TableHandle(TableHandle::RowSelect); // Test if hit is on top edge of table. if (y <= threshold) return TableHandle(TableHandle::ColumnSelect); // Test if hit is on bottom right corner of table. if (x >= tableWidth - threshold && y >= tableHeight - threshold) return TableHandle(TableHandle::TableResize); // Test if hit is on right edge of table. if (y >= tableHeight - threshold && y <= tableHeight + threshold) return TableHandle(TableHandle::RowResize, rows() - 1); // Test if hit is on bottom edge of table. if (x >= tableWidth - threshold && x <= tableWidth + threshold) return TableHandle(TableHandle::ColumnResize, columns() - 1); const TableCell hitCell = cellAt(point); const QRectF hitRect = hitCell.boundingRect(); // Test if hit is on cell interior. if (hitRect.adjusted(threshold, threshold, -threshold, -threshold).contains(gridPoint)) return TableHandle(TableHandle::CellSelect); // Hit interior of cell. const double toLeft = x - hitRect.left(); const double toRight = hitRect.right() - x; const double toTop = y - hitRect.top(); const double toBottom = hitRect.bottom() - y; TableHandle handle(TableHandle::None); // Test which side of the cell was hit. if (qMin(toLeft, toRight) < qMin(toTop, toBottom)) { handle.setType(TableHandle::ColumnResize); handle.setIndex((toLeft < toRight ? hitCell.column() : hitCell.column() + hitCell.columnSpan()) - 1); } else { handle.setType(TableHandle::RowResize); handle.setIndex((toTop < toBottom ? hitCell.row() : hitCell.row() + hitCell.rowSpan()) - 1); } return handle; }