////////////////////////////////////////////////////////////////////////////// // Calculate the height of the next row to be displayed. // If a header row should be included this is calculated also. // // Assumptions // This function should be called in an itteration after Drawing and before // calling the Iteration function. // // Parameters // height [out] - Calculated height // // TODO: Width and height initialised with 1 which is the border of the table. // There is no way to change the border/inner line of the table. // Thus assumed 1 pixel. ////////////////////////////////////////////////////////////////////////////// void CrossTab::CalculateNextRowSize(int& height) { // Calculate which row should be calculated int startIndex = m_rowIndexStored; if (m_rowIndexStored == m_rowIndex.count()) { startIndex = m_rowIndexStoredLast; } // Calculate rows height = 1; bool includedRow(false); CrossTabRowIndex::iterator rowIt(m_rowIndex.begin()); for (int i=0 ; rowIt != m_rowIndex.end() ; ++i, ++rowIt) { // Keep header in mind Keep iteration in mind if ((m_columnHeaderEachPage && (i==0)) || (i >= startIndex)) { // Calculate new size and index height += rowIt.value().m_rowMaxHeight + m_cellTopMargin + m_cellBottomMargin; if (i >= startIndex) { includedRow = true; break; } } } // Height is zero when no rows should be displayed anymore if (!includedRow) { height = 0; } }
////////////////////////////////////////////////////////////////////////////// // Calculate the size the remaining table needs // // Parameters // rect [out] - used space for display of table // // TODO // Redisplay of headers columns and rows not calculated. // ////////////////////////////////////////////////////////////////////////////// void CrossTab::CalculateTableSize(QRect& rect) { // Calculate columns { int width(1); CrossTabColumnIndex::iterator columnIt(m_columnIndex.begin()); for (int j=0 ; columnIt != m_columnIndex.end() ; ++j, ++columnIt) { // Keep header in mind Keep iteration in mind if ((m_rowHeaderEachPage && (j==0)) || ((j >= m_columnIndexStored))) { // Calculate new size and index width += columnIt.value().m_columnMaxWidth + m_cellLeftMargin + m_cellRightMargin; } } rect.setWidth(width); } // Calculate rows { int height(1); CrossTabRowIndex::iterator rowIt(m_rowIndex.begin()); for (int i=0 ; rowIt != m_rowIndex.end() ; ++i, ++rowIt) { // Keep header in mind Keep iteration in mind if ((m_columnHeaderEachPage && (i==0)) || (i >= m_rowIndexStored)) { // Calculate new size and index height += rowIt.value().m_rowMaxHeight + m_cellTopMargin + m_cellBottomMargin; } } rect.setHeight(height); } }
void PageItem_Table::removeColumns(int index, int numColumns) { ASSERT_VALID(); if (!validColumn(index) || numColumns < 1 || numColumns >= columns() || index + numColumns > columns()) return; // Remove column widths, column positions and columns of cells. double removedWidth = 0.0; for (int i = 0; i < numColumns; ++i) { // Remove columns widths and positions. removedWidth += m_columnWidths.takeAt(index); m_columnPositions.removeAt(index); // Remove and invalidate cells. QMutableListIterator<QList<TableCell> > rowIt(m_cellRows); while (rowIt.hasNext()) rowIt.next().takeAt(index).setValid(false); } // Adjust following columns. for (int nextColumn = index; nextColumn < columns() - numColumns; ++nextColumn) { // Adjust position of following column. m_columnPositions[nextColumn] -= removedWidth; // "Move" cells in following column left. foreach (QList<TableCell> cellRow, m_cellRows) cellRow[nextColumn].moveLeft(numColumns); } // Update column spans. updateSpans(index, numColumns, ColumnsRemoved); // Decrease number of columns. m_columns -= numColumns; // Update cells. TODO: Not for entire table. updateCells(); // Remove any invalid cells from selection. QMutableSetIterator<TableCell> cellIt(m_selection); while (cellIt.hasNext()) if (!cellIt.next().isValid()) cellIt.remove(); // Move to cell to the right. moveTo(cellAt(m_activeRow, qMin(m_activeColumn + 1, columns() - 1))); emit changed(); ASSERT_VALID(); }
////////////////////////////////////////////////////////////////////////////// // draw ////////////////////////////////////////////////////////////////////////////// void CrossTab::Draw(QPainter & paint) { paint.save(); // Calculate the number of rows and columns that we can display int lastColumn(0); int lastRow(0); CalculateDisplayedRowsAndColumns(lastColumn, lastRow, m_rect); paint.drawRect(m_rect); QRect sampleRect(0,0,0,0); // Name itemRectangle QRect saveRect(0,0,0,0); int idRow=m_rowIndexStored; int idCol=m_columnIndexStored; // Start at the correct place CrossTabRowIndex::iterator rowIt(m_rowIndex.begin()); CrossTabColumnIndex::iterator columnIt(m_columnIndex.begin()); for (idRow = 0, rowIt = m_rowIndex.begin() ; ((idRow <= lastRow) && (rowIt != m_rowIndex.end())); ++idRow, ++rowIt) { // Should this row cell be displayed // Skip header column if not wanted if ( ((0 == idRow) && (0 != m_rowIndexStored) && !m_columnHeaderEachPage) || //Skip already displayed rows ((0 < idRow) && (idRow < m_rowIndexStored)) || //Skip rows that are not part of the already drawn table part (m_tableWrapDisplayAllColumnsFirst && (0 < idRow) && (idRow > m_rowIndexStoredLast) && (0 != m_columnIndexStored)) ) { continue; } for (idCol = 0, columnIt = m_columnIndex.begin() ; ((idCol <= lastColumn) && (columnIt != m_columnIndex.end())); ++idCol, ++columnIt) { // Should this column cell be displayed // Skip header row if not wanted if (((0 == idCol) && (0 != m_columnIndexStored) && !m_rowHeaderEachPage) || // Skip already displayed columns ((0 < idCol) && (idCol < m_columnIndexStored))) { continue; } // Get width of this column QString dataTekst (columnIt.key()); sampleRect.setWidth (columnIt.value().m_columnMaxWidth + m_cellLeftMargin + m_cellRightMargin); sampleRect.setHeight (rowIt.value().m_rowMaxHeight + m_cellTopMargin + m_cellBottomMargin); // Save rectangle saveRect = sampleRect; paint.setBackgroundMode(Qt::OpaqueMode); // Draw headers row if (idRow == 0) { paint.setBrush(QBrush(QColor(0, 0, 255, 127), Qt::SolidPattern)); } // Header column else if ((idRow != 0) && (idCol == 0)) { // Odd rows if ((idRow%2) != 0 ) { paint.setBrush(QBrush(QColor(0, 0, 255, 200), Qt::SolidPattern)); } // Even rows else { paint.setBrush(QBrush(QColor(90, 210, 255, 150), Qt::SolidPattern)); } } // Values else if ((idRow != 0) && (idCol != 0)) { // Odd rows if ((idRow%2) != 0 ) { paint.setBrush(QBrush(QColor(0, 0, 0, 0), Qt::SolidPattern)); } // Even rows else { paint.setBrush(QBrush(QColor(90, 210, 255, 127), Qt::SolidPattern)); } } // Draw rectangle paint.drawRect(sampleRect); paint.setBackgroundMode(Qt::TransparentMode); paint.setPen(Qt::SolidLine); // Skip margins: Adjust rectangle for data insertion sampleRect.setX(sampleRect.x() + m_cellLeftMargin); sampleRect.setY(sampleRect.y() + m_cellTopMargin); sampleRect.setWidth (columnIt.value().m_columnMaxWidth); sampleRect.setHeight (rowIt.value().m_rowMaxHeight); // get Font QFontMetrics fm(GetFont()); QRect dataRect; // Header if ((idRow == 0) && (idCol == 0)) { // Do nothing } // Headerrow else if ((idRow == 0) && (idCol != 0)) { dataRect = fm.boundingRect(sampleRect, m_hAlignMap["column"] | m_vAlignMap["column"], columnIt.key()); paint.drawText(dataRect, m_hAlignMap["column"] | m_vAlignMap["column"], columnIt.key()); } // Headercol else if ((idCol == 0) && (idRow != 0)) { dataRect = fm.boundingRect(sampleRect, m_hAlignMap["row"] | m_vAlignMap["row"], rowIt.key()); paint.drawText(dataRect, m_hAlignMap["row"] | m_vAlignMap["row"], rowIt.key()); } // Value else { dataRect = fm.boundingRect(sampleRect, m_hAlignMap["value"] | m_vAlignMap["value"], GetValue(columnIt.key(), rowIt.key())); paint.drawText(dataRect, m_hAlignMap["value"] | m_vAlignMap["value"], GetValue(columnIt.key(), rowIt.key())); } // Restore rectangle sampleRect = saveRect; sampleRect.setX(sampleRect.x() + sampleRect.width()); } // end for // Reset basic column id idCol = m_columnIndexStored; // Adjust item display rectangle sampleRect.setY(sampleRect.y()+sampleRect.height()); sampleRect.setX(0); } // Store indexes for iterative purpose m_rowIndexStored = lastRow + 1; m_columnIndexStored = lastColumn + 1; // Now that we are done return the paint device back to the state // it was when we started to mess with it paint.restore(); }
////////////////////////////////////////////////////////////////////////////// // Calculate the last index of column and row that can be displayed // // Parameters // lastColumn [out] - last column Id that can be displayed // lastRow [out] - last row Id that can be displayed // rect [in] - available space for display of table // [out] - used space for display of table // // TODO: Width and height initialised with 2 which is the border of the table. // There is no way to change the border/inner line of the table. // Thus assumed 1 pixel. ////////////////////////////////////////////////////////////////////////////// void CrossTab::CalculateDisplayedRowsAndColumns(int& lastColumn, int& lastRow, QRect& rect) { // Keep interation in mind lastColumn = m_columnIndexStored; lastRow = m_rowIndexStored; // Calculate columns { int width(1); bool filled(false); CrossTabColumnIndex::iterator columnIt(m_columnIndex.begin()); for (int j=0 ; !filled && (columnIt != m_columnIndex.end()); ++j, ++columnIt) { // Keep header in mind Keep iteration in mind if ((m_rowHeaderEachPage && (j==0)) || (j >= m_columnIndexStored)) { // Calculate new size and index width += columnIt.value().m_columnMaxWidth + m_cellLeftMargin + m_cellRightMargin; lastColumn = j; // Exceeding the size if (width > rect.width()) { // Revert and stop lastColumn = j-1; width -= (columnIt.value().m_columnMaxWidth + m_cellLeftMargin + m_cellRightMargin); filled = true; } } } rect.setWidth(width); } // Calculate rows { int height(1); bool filled(false); CrossTabRowIndex::iterator rowIt(m_rowIndex.begin()); for (int i=0 ; !filled && (rowIt != m_rowIndex.end()); ++i, ++rowIt) { // If the table was wrapped to a new page we should not print more rows // than the first part of the table if (m_tableWrapDisplayAllColumnsFirst && (i >= m_rowIndexStoredLast) && (0 != m_columnIndexStored)) { break; } // Keep header in mind Keep iteration in mind if ((m_columnHeaderEachPage && (i==0)) || (i >= m_rowIndexStored)) { // Calculate new size and index height += rowIt.value().m_rowMaxHeight + m_cellTopMargin + m_cellBottomMargin; lastRow = i; // Exceeding the size if (height > rect.height()) { // Revert and stop lastRow = i-1; height -= (rowIt.value().m_rowMaxHeight + m_cellTopMargin + m_cellBottomMargin); filled = true; } } } rect.setHeight(height); } }