3 #include "TableViewContent.h"
12 #include <QMessageBox>
15 #include <QPaintEvent>
20 #include <QStyleOptionViewItemV4>
22 #include <QVBoxLayout>
28 #include "AbstractTableDelegate.h"
29 #include "AbstractTableModel.h"
30 #include "AbstractTreeItem.h"
31 #include "TableColumn.h"
32 #include "TableColumnList.h"
38 TableViewContent::TableViewContent(AbstractTableModel *someModel) {
42 connect(m_model, SIGNAL(modelModified()),
this, SLOT(refresh()));
43 connect(m_model, SIGNAL(filterProgressChanged(
int)),
44 this, SLOT(updateItemList()));
45 connect(
this, SIGNAL(modelDataChanged()),
46 m_model, SLOT(applyFilter()));
54 m_columns = getModel()->getColumns();
55 for (
int i = 0; i < m_columns->size(); i++) {
56 TableColumn *column = (*m_columns)[i];
58 connect(column, SIGNAL(visibilityChanged()),
this, SLOT(refresh()));
59 connect(column, SIGNAL(visibilityChanged()),
60 this, SLOT(updateHorizontalScrollBar()));
61 connect(column, SIGNAL(widthChanged()),
this, SLOT(refresh()));
65 m_mousePressPos =
new QPoint;
66 m_activeCell =
new QPair<AbstractTreeItem *, int>(NULL, -1);
69 m_lastShiftArrowSelectedCell =
new QPair< AbstractTreeItem *, int >;
70 m_lastShiftArrowSelectedCell->first = NULL;
72 verticalScrollBar()->setSingleStep(1);
74 m_rowHeight = QFontMetrics(font()).height() + ITEM_PADDING;
75 ASSERT(m_rowHeight > 0);
77 connect(horizontalScrollBar(), SIGNAL(valueChanged(
int)),
78 this, SIGNAL(horizontalScrollBarValueChanged(
int)));
80 setMouseTracking(
true);
81 updateHorizontalScrollBar();
85 setContextMenuPolicy(Qt::CustomContextMenu);
86 connect(
this, SIGNAL(customContextMenuRequested(QPoint)),
87 this, SLOT(showContextMenu(QPoint)));
91 TableViewContent::~TableViewContent() {
95 delete m_mousePressPos;
96 m_mousePressPos = NULL;
104 delete m_lastShiftSelection;
105 m_lastShiftSelection = NULL;
116 delete m_lastShiftArrowSelectedCell;
117 m_lastShiftArrowSelectedCell = NULL;
123 QSize TableViewContent::minimumSizeHint()
const {
124 return QWidget::minimumSizeHint();
128 QSize TableViewContent::sizeHint() {
129 return minimumSizeHint();
133 AbstractTableModel *TableViewContent::getModel() {
177 void TableViewContent::refresh() {
179 if (!m_model->isFiltering()) {
180 int rowCount = m_model->getVisibleRowCount();
181 verticalScrollBar()->setRange(0, qMax(rowCount - 1, 0));
186 m_lastShiftSelection->clear();
188 if (m_model->getSelectedItems().size() &&
189 rowsWithActiveColumnSelected->size()) {
191 clearColumnSelection();
194 viewport()->update();
199 void TableViewContent::updateHorizontalScrollBar(
bool scrollRight) {
202 TableColumnList visibleCols = m_columns->getVisibleColumns();
203 for (
int i = 0; i < visibleCols.size(); i++)
204 range += visibleCols[i]->getWidth() - 1;
208 horizontalScrollBar()->setRange(0, range - viewport()->width());
209 horizontalScrollBar()->setPageStep(viewport()->width());
212 horizontalScrollBar()->setValue(horizontalScrollBar()->maximum());
217 void TableViewContent::scrollTo(
219 if (newlySelectedItems.size())
220 scrollTo(newlySelectedItems.last());
224 void TableViewContent::scrollTo(AbstractTreeItem *newlySelectedItem) {
225 int row = getModel()->indexOfVisibleItem(newlySelectedItem);
228 int topRow = verticalScrollBar()->value();
231 verticalScrollBar()->setValue(row);
234 int wholeVisibleRowCount = viewport()->height() / m_rowHeight;
235 int bottomRow = topRow + wholeVisibleRowCount;
237 verticalScrollBar()->setValue(row - wholeVisibleRowCount + 1);
241 viewport()->update();
245 bool TableViewContent::eventFilter(
QObject *target, QEvent *event) {
246 return QObject::eventFilter(target, event);
250 void TableViewContent::mouseDoubleClickEvent(QMouseEvent *event) {
251 if (event->buttons() & Qt::LeftButton) {
252 int rowNum =
event->pos().y() / m_rowHeight;
254 if (m_activeCell->first && cellIsEditable(rowNum, m_activeCell->second)) {
255 const AbstractTableDelegate *delegate = m_model->getDelegate();
257 m_columns->getVisibleColumns()[m_activeCell->second];
261 m_editWidget = delegate->getWidget(col);
262 delegate->readData(m_editWidget, m_activeCell->first, col);
263 m_editWidget->setParent(
this);
264 m_editWidget->setFocus(Qt::OtherFocusReason);
267 viewport()->update();
272 void TableViewContent::mousePressEvent(QMouseEvent *event) {
273 if (event->buttons() & Qt::LeftButton) {
275 if (!(event->modifiers() & Qt::ShiftModifier))
276 updateActiveCell(event->pos());
278 int rowNum =
event->pos().y() / m_rowHeight;
279 int colNum = getColumnFromScreenX(event->pos().x());
288 if (rowNum >= 0 && rowNum < m_items->size() && m_activeCell->first) {
293 m_model->setGlobalSelection(
false);
295 if (cellIsEditable(rowNum, m_activeCell->second)) {
296 if (event->modifiers() & Qt::ControlModifier) {
297 if (rowsWithActiveColumnSelected->indexOf(m_activeCell->first) < 0)
298 rowsWithActiveColumnSelected->append(m_activeCell->first);
300 rowsWithActiveColumnSelected->removeAll(m_activeCell->first);
303 m_lastShiftSelection->clear();
306 if (event->modifiers() & Qt::ShiftModifier) {
307 updateColumnGroupSelection((*m_items)[rowNum]);
311 clearColumnSelection();
312 rowsWithActiveColumnSelected->append(m_activeCell->first);
314 m_lastShiftSelection->clear();
321 if (rowNum >= 0 && rowNum < m_items->size()) {
322 int columnNum = getColumnFromScreenX(event->pos().x());
324 if (columnNum != -1) {
325 TableColumn *column = m_columns->getVisibleColumns()[columnNum];
326 if (column->getTitle().isEmpty()) {
327 clearColumnSelection();
329 AbstractTreeItem *
const &item = m_items->at(rowNum);
332 if (event->modifiers() & Qt::ControlModifier) {
333 if (item->getPointerType() == AbstractTreeItem::Measure)
334 item->parent()->setSelected(!item->isSelected());
336 item->setSelected(!item->isSelected());
338 newlySelectedItems.append(item);
341 if (event->modifiers() & Qt::ShiftModifier) {
342 newlySelectedItems = updateRowGroupSelection(rowNum);
346 m_model->getSelectedItems();
348 foreach (AbstractTreeItem * selectedItem, selectedItems) {
349 if (selectedItem->getPointerType() ==
350 AbstractTreeItem::Measure)
351 selectedItem->parent()->setSelected(
false);
354 m_model->setGlobalSelection(
false);
356 if (item->getPointerType() == AbstractTreeItem::Measure)
357 item->parent()->setSelected(
true);
359 item->setSelected(
true);
361 newlySelectedItems.append(item);
366 newlySelectedItems.clear();
367 foreach (AbstractTreeItem * i, tmp) {
368 newlySelectedItems.append(i);
369 if (i->getPointerType() == AbstractTreeItem::Point) {
370 foreach (AbstractTreeItem * child, i->getChildren()) {
371 child->setSelected(
true);
372 newlySelectedItems.append(child);
377 emit tableSelectionChanged(newlySelectedItems);
386 viewport()->update();
387 emit tableSelectionChanged();
392 void TableViewContent::mouseReleaseEvent(QMouseEvent *event) {
396 void TableViewContent::mouseMoveEvent(QMouseEvent *event) {
398 if (event->buttons() & Qt::LeftButton) {
399 int rowNum =
event->pos().y() / m_rowHeight;
402 int yPos =
event->pos().y();
403 if (yPos >= 0 && rowNum < m_items->size() && m_activeCell->first) {
406 if (cellIsEditable(rowNum, m_activeCell->second)) {
407 updateColumnGroupSelection((*m_items)[rowNum]);
412 if (yPos >= 0 && rowNum < m_items->size()) {
415 int columnNum = getColumnFromScreenX(event->pos().x());
417 if (columnNum != -1) {
418 clearColumnSelection();
421 updateRowGroupSelection(rowNum);
423 foreach (AbstractTreeItem * i, tmp) {
424 newlySelectedItems.append(i);
425 if (i->getPointerType() == AbstractTreeItem::Point) {
426 foreach (AbstractTreeItem * child, i->getChildren()) {
427 child->setSelected(
true);
428 newlySelectedItems.append(child);
433 emit tableSelectionChanged(newlySelectedItems);
438 QScrollBar *vertScroll = verticalScrollBar();
440 if (yPos > viewport()->height() &&
441 vertScroll->value() < vertScroll->maximum()) {
443 vertScroll->setValue(vertScroll->value() + 1);
446 if (yPos < 0 && vertScroll->value() > vertScroll->minimum())
447 vertScroll->setValue(vertScroll->value() - 1);
450 viewport()->update();
451 emit tableSelectionChanged();
457 void TableViewContent::leaveEvent(QEvent *event) {
462 void TableViewContent::keyPressEvent(QKeyEvent *event) {
463 Qt::Key key = (Qt::Key) event->key();
466 if (key == Qt::Key_A && event->modifiers() == Qt::ControlModifier) {
468 clearColumnSelection();
469 m_model->setGlobalSelection(
true);
470 viewport()->update();
472 emit tableSelectionChanged();
476 else if (key == Qt::Key_Escape) {
480 setFocus(Qt::ActiveWindowFocusReason);
481 viewport()->update();
486 else if (key == Qt::Key_Delete) {
487 if (hasRowSelection())
488 deleteSelectedRows();
492 else if (key == Qt::Key_Return || key == Qt::Key_Enter) {
494 moveActiveCellDown();
498 else if (key == Qt::Key_Tab) {
500 moveActiveCellRight();
504 else if (key == Qt::Key_Up || key == Qt::Key_Down ||
505 key == Qt::Key_Left || key == Qt::Key_Right) {
506 if (!hasActiveCell()) {
508 if (m_items && m_items->size()) {
509 m_activeCell->first = (*m_items)[0];
510 m_activeCell->second = 1;
514 if (hasActiveCell() && !m_editWidget) {
516 ASSERT(m_items->size());
519 if (key == Qt::Key_Up && event->modifiers() == Qt::ShiftModifier) {
520 ASSERT(m_lastShiftArrowSelectedCell);
522 AbstractTreeItem *prevCell = m_lastShiftArrowSelectedCell->first ?
523 m_lastShiftArrowSelectedCell->first : m_activeCell->first;
525 int prevCellIndex = getModel()->indexOfVisibleItem(prevCell);
527 if (prevCellIndex > 0) {
529 getModel()->getItems(prevCellIndex - 1, prevCellIndex);
531 if (itemList.size()) {
532 AbstractTreeItem *curItem = itemList[0];
534 if (rowsWithActiveColumnSelected->contains(curItem) ||
535 curItem == m_activeCell->first)
536 rowsWithActiveColumnSelected->removeAll(prevCell);
538 rowsWithActiveColumnSelected->append(curItem);
540 if (curItem == m_activeCell->first)
541 m_lastShiftArrowSelectedCell->first = NULL;
543 m_lastShiftArrowSelectedCell->first = curItem;
544 m_lastShiftArrowSelectedCell->second = m_activeCell->second;
547 int m_itemsPrevIndex = m_items->indexOf(prevCell);
548 int m_itemsCurIndex = m_items->indexOf(curItem);
549 if (m_itemsCurIndex == -1) {
550 if (m_itemsPrevIndex == 0) {
551 verticalScrollBar()->setValue(qMax(0, prevCellIndex - 1));
560 viewport()->update();
566 else if (key == Qt::Key_Down && event->modifiers() == Qt::ShiftModifier) {
567 AbstractTreeItem *prevCell = m_lastShiftArrowSelectedCell->first ?
568 m_lastShiftArrowSelectedCell->first : m_activeCell->first;
570 int prevCellIndex = getModel()->indexOfVisibleItem(prevCell);
572 if (prevCellIndex >= 0 &&
573 prevCellIndex < getModel()->getVisibleRowCount() - 1) {
575 getModel()->getItems(prevCellIndex + 1, prevCellIndex + 2);
577 if (itemList.size()) {
578 AbstractTreeItem *curItem = itemList[0];
580 if (rowsWithActiveColumnSelected->contains(curItem) ||
581 curItem == m_activeCell->first)
582 rowsWithActiveColumnSelected->removeAll(prevCell);
584 rowsWithActiveColumnSelected->append(curItem);
586 if (curItem == m_activeCell->first)
587 m_lastShiftArrowSelectedCell->first = NULL;
589 m_lastShiftArrowSelectedCell->first = curItem;
590 m_lastShiftArrowSelectedCell->second = m_activeCell->second;
591 viewport()->update();
594 int m_itemsPrevIndex = m_items->indexOf(prevCell);
595 int m_itemsCurIndex = m_items->indexOf(curItem);
596 if (m_itemsCurIndex == -1) {
597 if (m_itemsPrevIndex == m_items->size() - 1) {
598 int visibleItemCount = getModel()->getVisibleRowCount();
599 verticalScrollBar()->setValue(qMin(visibleItemCount,
600 getModel()->indexOfVisibleItem(m_items->at(1))));
613 else if (key == Qt::Key_Up) {
618 else if (key == Qt::Key_Down) {
619 moveActiveCellDown();
623 else if (key == Qt::Key_Left) {
624 moveActiveCellLeft();
628 else if (key == Qt::Key_Right) {
629 moveActiveCellRight();
637 if (hasActiveCell() && !event->text().isEmpty()) {
638 if (!m_items->contains(m_activeCell->first))
639 scrollTo(m_activeCell->first);
641 ASSERT(m_items->contains(m_activeCell->first));
643 if (m_items->contains(m_activeCell->first) &&
644 cellIsEditable(m_items->indexOf(m_activeCell->first),
645 m_activeCell->second)) {
646 AbstractTableDelegate
const *delegate = m_model->getDelegate();
648 m_columns->getVisibleColumns()[m_activeCell->second];
652 m_editWidget = delegate->getWidget(col);
653 delegate->readData(m_editWidget, m_activeCell->first, col, event->text());
654 m_editWidget->setParent(
this);
655 m_editWidget->setFocus(Qt::OtherFocusReason);
658 viewport()->update();
664 void TableViewContent::finishEditing() {
667 m_columns->getVisibleColumns()[m_activeCell->second];
668 getModel()->getDelegate()->saveData(
669 m_editWidget, m_activeCell->first, col);
673 cellDataChanged(col);
674 setFocus(Qt::ActiveWindowFocusReason);
679 void TableViewContent::moveActiveCellUp() {
680 int activeIndex = m_items->indexOf(m_activeCell->first);
681 if (activeIndex != -1) {
682 if (activeIndex == 0) {
683 int row = qMax(0, getModel()->indexOfVisibleItem(
684 m_activeCell->first) - 1);
686 verticalScrollBar()->setValue(row);
689 m_activeCell->first = (*m_items)[qMax(0, activeIndex - 1)];
690 clearColumnSelection();
691 viewport()->update();
696 void TableViewContent::moveActiveCellDown() {
697 int activeIndex = m_items->indexOf(m_activeCell->first);
698 if (activeIndex != -1) {
699 if (activeIndex == m_items->size() - 1) {
700 int row = qMin(getModel()->getVisibleRowCount() - 1,
701 getModel()->indexOfVisibleItem(m_items->at(0)));
703 verticalScrollBar()->setValue(row + 1);
704 activeIndex = m_items->indexOf(m_activeCell->first);
707 m_activeCell->first = (*m_items)[qMin(activeIndex + 1, m_items->size() - 1)];
708 clearColumnSelection();
709 viewport()->update();
714 void TableViewContent::moveActiveCellLeft() {
715 m_activeCell->second = qMax(1, m_activeCell->second - 1);
716 int leftMostVisibleCol = getColumnFromScreenX(0);
717 if (leftMostVisibleCol == m_activeCell->second) {
718 horizontalScrollBar()->setValue(horizontalScrollBar()->value() -
719 m_columns->getVisibleColumns()[m_activeCell->second]->getWidth());
722 clearColumnSelection();
723 viewport()->update();
727 void TableViewContent::moveActiveCellRight() {
728 m_activeCell->second = qMin(m_columns->getVisibleColumns().size() - 1,
729 m_activeCell->second + 1);
730 int rightMostVisibleCol = getColumnFromScreenX(viewport()->width());
731 if (rightMostVisibleCol == m_activeCell->second) {
732 horizontalScrollBar()->setValue(horizontalScrollBar()->value() +
733 m_columns->getVisibleColumns()[m_activeCell->second]->getWidth());
736 clearColumnSelection();
737 viewport()->update();
741 void TableViewContent::paintEvent(QPaintEvent *event) {
744 if (m_model && m_columns) {
746 int rowCount = (int) ceil(viewport()->height() / (double) m_rowHeight);
748 QPainter painter(viewport());
749 painter.setRenderHint(QPainter::Antialiasing,
false);
752 bool editWidgetVisible =
false;
753 for (
int i = 0; i < rowCount; i++) {
755 QPoint relativeTopLeft(0, i * m_rowHeight);
756 QPoint scrollBarPos(horizontalScrollBar()->value(),
757 verticalScrollBar()->value());
758 QPoint absoluteTopLeft(relativeTopLeft + scrollBarPos);
759 QSize rowSize(viewport()->width(), (
int) m_rowHeight);
762 painter.fillRect(QRect(relativeTopLeft, rowSize), palette().base());
764 if (i < m_items->size()) {
804 if (m_items->at(i)->isSelected()) {
805 QPoint selectionTopLeft(-absoluteTopLeft.x(), relativeTopLeft.y());
806 QSize selectionSize(m_columns->getVisibleWidth(), (int) m_rowHeight);
808 QRect selectionRect(selectionTopLeft, selectionSize);
809 painter.fillRect(selectionRect, palette().highlight().color());
812 paintRow(&painter, i, absoluteTopLeft, relativeTopLeft);
816 for (
int i = 0; i < rowCount; i++) {
817 if (i < m_items->size()) {
818 QPoint relativeTopLeft(0, i * m_rowHeight);
819 if (m_editWidget && m_activeCell->first == m_items->at(i)) {
820 QPair<int, int> xRange(
824 QPoint(xRange.first - horizontalScrollBar()->value() - 1,
825 relativeTopLeft.y() + 1));
826 m_editWidget->resize(xRange.second - xRange.first, m_rowHeight + 1);
827 m_editWidget->setVisible(
true);
828 editWidgetVisible =
true;
831 if (m_activeCell->first == m_items->at(i)) {
832 QPair<int, int> activeXArea =
835 QRect activeArea(activeXArea.first, relativeTopLeft.y(),
836 activeXArea.second - activeXArea.first, m_rowHeight);
839 activeArea.left() - horizontalScrollBar()->value());
840 activeArea.adjust(-1, -1, -2, -1);
844 painter.drawRect(activeArea);
850 if (m_editWidget && !editWidgetVisible)
851 m_editWidget->setVisible(
false);
854 QWidget::paintEvent(event);
859 void TableViewContent::resizeEvent(QResizeEvent *event) {
860 QAbstractScrollArea::resizeEvent(event);
861 updateHorizontalScrollBar();
866 void TableViewContent::scrollContentsBy(
int dx,
int dy) {
867 QAbstractScrollArea::scrollContentsBy(dx, dy);
872 void TableViewContent::nullify() {
877 m_lastShiftArrowSelectedCell = NULL;
880 m_lastShiftSelection = NULL;
881 m_mousePressPos = NULL;
883 rowsWithActiveColumnSelected = NULL;
890 void TableViewContent::cellDataChanged(
const TableColumn *col) {
891 if (col->hasNetworkStructureEffect())
894 emit modelDataChanged();
898 void TableViewContent::clearActiveCell() {
899 m_activeCell->first = NULL;
900 m_activeCell->second = -1;
904 void TableViewContent::clearColumnSelection() {
905 ASSERT(m_lastShiftArrowSelectedCell);
906 m_lastShiftArrowSelectedCell->first = NULL;
907 rowsWithActiveColumnSelected->clear();
911 void TableViewContent::copyCellSelection(
bool allCells) {
912 if (hasActiveCell()) {
913 TableColumn *col = m_columns->getVisibleColumns()[m_activeCell->second];
915 QString colTitle = col->getTitle();
916 ASSERT(colTitle.size());
920 QString cellData = m_activeCell->first->getFormattedData(colTitle);
923 0, m_model->getVisibleRowCount()) : *rowsWithActiveColumnSelected;
924 ASSERT(selection.size());
926 bool needsDialog =
true;
928 for (
int i = 0; !done && i < selection.size(); i++) {
929 AbstractTreeItem *row = selection[i];
930 bool changeData =
true;
932 QString warningText =
933 m_model->getWarningMessage(row, col, cellData);
934 if (needsDialog && warningText.size()) {
935 QMessageBox::StandardButton status = QMessageBox::warning(
936 this,
"Change cells?", warningText, QMessageBox::Yes |
937 QMessageBox::No | QMessageBox::YesToAll | QMessageBox::NoToAll);
940 case QMessageBox::YesToAll:
943 case QMessageBox::NoToAll:
945 case QMessageBox::No:
953 row->setData(colTitle, cellData);
956 viewport()->update();
957 cellDataChanged(col);
962 void TableViewContent::createActions() {
967 this, SLOT(copySelection()));
970 m_applyToAllAct->setStatusTip(tr(
"Copy the contents of this cell to all"
971 "cells in the current column"));
973 this, SLOT(copyAll()));
977 tr(
"Delete the currently selected rows"));
979 this, SLOT(deleteSelectedRows()));
983 int TableViewContent::getColumnFromScreenX(
int screenX)
const {
987 column == -1 && i < m_columns->getVisibleColumns().size();
990 int deltaX = -horizontalScrollBar()->value();
992 if (cellXRange.first + deltaX < screenX &&
993 cellXRange.second + deltaX > screenX) {
1002 int TableViewContent::getRowFromScreenY(
int screenY)
const {
1004 int calculatedRowNum = screenY / m_rowHeight;
1006 if (calculatedRowNum >= 0 && calculatedRowNum < m_items->size() &&
1007 screenY >= 0 && screenY <= viewport()->height())
1008 rowNum = calculatedRowNum;
1014 bool TableViewContent::hasActiveCell()
const {
1015 return (m_activeCell->first && m_activeCell->second >= 0);
1019 bool TableViewContent::hasRowSelection()
const {
1020 return (m_model->getSelectedItems().size());
1024 bool TableViewContent::mouseInCellSelection(QPoint mousePos)
const {
1025 int colNum = getColumnFromScreenX(mousePos.x());
1026 AbstractTreeItem *row = m_items->at(getRowFromScreenY(mousePos.y()));
1028 return (rowsWithActiveColumnSelected->contains(row) &&
1029 m_activeCell->second == colNum);
1033 bool TableViewContent::mouseInRowSelection(QPoint mousePos)
const {
1034 AbstractTreeItem *row = m_items->at(getRowFromScreenY(mousePos.y()));
1036 return (m_model->getSelectedItems().contains(row));
1040 bool TableViewContent::rowIsValid(
int rowNum)
const {
1043 if (rowNum >= 0 && rowNum < m_items->size())
1050 bool TableViewContent::columnIsValid(
int colNum)
const {
1053 if (colNum >= 0 && colNum < m_columns->getVisibleColumns().size())
1060 bool TableViewContent::cellIsEditable(
int rowNum,
int colNum)
const {
1061 ASSERT(rowNum >= 0 && rowNum < m_items->size());
1062 ASSERT(colNum >= 0 && colNum < m_columns->getVisibleColumns().size());
1064 bool editable =
false;
1066 QString colName = m_columns->getVisibleColumns()[colNum]->getTitle();
1067 if (m_items->at(rowNum)->isSelectable() &&
1068 m_items->at(rowNum)->isDataEditable(colName) &&
1069 !m_columns->getVisibleColumns()[colNum]->isReadOnly())
1076 bool TableViewContent::isDataColumn(
int colNum)
const {
1077 return m_columns->getVisibleColumns()[colNum]->getTitle().size();
1081 void TableViewContent::paintRow(QPainter *painter,
int rowNum,
1082 QPoint absolutePosition, QPoint relativePosition) {
1084 ASSERT(rowNum >= 0 && rowNum < m_items->size());
1086 QPoint point(-absolutePosition.x(), relativePosition.y());
1088 AbstractTreeItem *item = (*m_items)[rowNum];
1092 QPen originalPen = painter->pen();
1094 QPoint textPoint(point.x() + ITEM_INDENTATION,
1095 point.y() + ITEM_PADDING / 2);
1098 int textHeight = m_rowHeight - ITEM_PADDING;
1100 QFontMetrics metrics(font());
1102 QPen gridPen(Qt::gray);
1105 TableColumnList visibleCols = m_columns->getVisibleColumns();
1106 for (
int i = 0; i < visibleCols.size(); i++) {
1108 QPair<int, int> cellXRange(visibleCols.getVisibleXRange(i));
1109 QRect cellRect(cellXRange.first, point.y(),
1110 cellXRange.second - cellXRange.first, m_rowHeight);
1111 cellRect.moveLeft(cellRect.left() - horizontalScrollBar()->value() - 1);
1113 QString columnTitle = visibleCols[i]->getTitle();
1114 QRect textRect(textPoint, QSize(cellRect.right() - textPoint.x(),
1117 bool textCentered =
false;
1118 if (!columnTitle.isEmpty()) {
1119 text = item->getFormattedData(columnTitle);
1121 if (rowsWithActiveColumnSelected->indexOf(item) != -1 &&
1122 m_activeCell->second == i) {
1124 if (m_activeCell->first != item) {
1125 painter->fillRect(cellRect,
1126 QBrush(palette().highlight().color()));
1127 painter->setPen(palette().highlightedText().color());
1130 painter->setPen(palette().text().color());
1134 if (item->isSelected()) {
1135 painter->setPen(palette().highlightedText().color());
1147 if (!cellIsEditable(rowNum, i)) {
1148 painter->setPen(palette().color(QPalette::Disabled,
1152 painter->setPen(palette().text().color());
1159 text = QString::number(rowNum + verticalScrollBar()->value() + 1);
1160 textCentered =
true;
1163 int x = cellRect.center().x();
1164 QLinearGradient gradient(x, cellRect.top(), x, cellRect.bottom());
1166 bool selected = item->isSelected();
1167 QColor color = selected ? palette().highlight().color() :
1168 palette().button().color();
1170 int adjustment = 110;
1171 gradient.setColorAt(0, color.lighter(adjustment));
1172 gradient.setColorAt(1, color.darker(adjustment));
1173 painter->fillRect(cellRect, gradient);
1175 painter->setPen(palette().highlightedText().color());
1177 painter->setPen(palette().text().color());
1180 int flags = Qt::TextDontClip;
1182 flags |= Qt::AlignCenter;
1186 QFont normalFont = painter->font();
1188 if (item->getPointerType() == AbstractTreeItem::Measure) {
1189 ControlMeasure *cm = (ControlMeasure *) item->getPointer();
1190 if (cm && cm->Parent() && cm->Parent()->GetRefMeasure() == cm) {
1191 QFont boldFont(normalFont);
1192 boldFont.setBold(
true);
1193 painter->setFont(boldFont);
1197 painter->drawText(textRect, flags,
1198 metrics.elidedText(text, Qt::ElideRight,
1199 textRect.width() - ITEM_INDENTATION));
1200 painter->setFont(normalFont);
1202 textPoint.setX(cellRect.right() + ITEM_INDENTATION);
1203 painter->setPen(originalPen);
1205 painter->setPen(gridPen);
1206 painter->drawLine(QPoint(cellRect.right(), point.y()),
1207 QPoint(cellRect.right(), point.y() + m_rowHeight));
1208 painter->setPen(originalPen);
1211 int left = -horizontalScrollBar()->value() - 1;
1212 int right = m_columns->getVisibleWidth();
1214 gridPen.setWidth(2);
1215 painter->setPen(gridPen);
1216 painter->drawLine(QPoint(left, point.y() + m_rowHeight),
1217 QPoint(right, point.y() + m_rowHeight));
1218 painter->setPen(originalPen);
1223 void TableViewContent::updateActiveCell(QPoint screenPos) {
1224 if (m_editWidget && m_activeCell->first && m_activeCell->second >= 0) {
1226 const TableColumn *col =
1227 m_columns->getVisibleColumns()[m_activeCell->second];
1228 m_model->getDelegate()->saveData(m_editWidget, m_activeCell->first,
1231 cellDataChanged(col);
1233 catch (IException &e) {
1234 QMessageBox::critical(
this,
"Failed to Set Data", e.what());
1238 int rowNum = getRowFromScreenY(screenPos.y());
1239 int oldActiveColumn = m_activeCell->second;
1244 AbstractTreeItem *item = (*m_items)[rowNum];
1246 TableColumnList visibleCols = m_columns->getVisibleColumns();
1247 for (
int i = 0; i < visibleCols.size(); i++) {
1249 QRect cellRect(cellXRange.first, m_rowHeight * rowNum,
1250 cellXRange.second - cellXRange.first, m_rowHeight);
1252 cellRect.moveLeft(cellRect.left() - horizontalScrollBar()->value());
1254 if (cellRect.contains(screenPos) &&
1255 (oldActiveColumn != -1 || !visibleCols[i]->getTitle().isEmpty())) {
1256 m_activeCell->first = item;
1257 m_activeCell->second = i;
1262 if (oldActiveColumn != m_activeCell->second) {
1263 clearColumnSelection();
1267 clearColumnSelection();
1271 void TableViewContent::updateColumnGroupSelection(AbstractTreeItem *item) {
1273 foreach (AbstractTreeItem * row, *m_lastShiftSelection) {
1274 if (rowsWithActiveColumnSelected->indexOf(row) != -1)
1275 rowsWithActiveColumnSelected->removeOne(row);
1280 *m_lastShiftSelection = m_model->getItems(
1284 m_lastShiftSelection->clear();
1287 foreach (AbstractTreeItem * row, *m_lastShiftSelection) {
1288 if (rowsWithActiveColumnSelected->indexOf(row) == -1)
1289 rowsWithActiveColumnSelected->append(row);
1295 TableViewContent::updateRowGroupSelection(
int lastRow) {
1296 foreach (AbstractTreeItem * row, *m_lastShiftSelection) {
1297 if (row->getPointerType() == AbstractTreeItem::Point)
1298 foreach (AbstractTreeItem * child, row->getChildren())
1299 child->setSelected(
false);
1301 if (row->getPointerType() == AbstractTreeItem::Measure)
1302 row->parent()->setSelected(
false);
1304 row->setSelected(
false);
1308 *m_lastShiftSelection = m_model->getItems(
1312 m_lastShiftSelection->clear();
1316 foreach (AbstractTreeItem * row, *m_lastShiftSelection) {
1317 row->setSelected(
true);
1319 if (row->getPointerType() == AbstractTreeItem::Measure)
1320 row->parent()->setSelected(
true);
1322 newlySelectedItems.append(row);
1325 return newlySelectedItems;
1329 void TableViewContent::copySelection() {
1330 copyCellSelection(
false);
1334 void TableViewContent::copyAll() {
1335 copyCellSelection(
true);
1339 void TableViewContent::deleteSelectedRows() {
1341 QMessageBox::StandardButton status = QMessageBox::warning(
1342 this,
"Delete row(s)?",
"Delete selected row(s)?",
1343 QMessageBox::Yes | QMessageBox::No);
1345 if (status == QMessageBox::Yes) {
1349 emit rebuildModels(selectedRows);
1350 emit modelDataChanged();
1352 m_lastShiftSelection->clear();
1357 void TableViewContent::updateItemList() {
1361 int startRow = verticalScrollBar()->value();
1362 int rowCount = (int) ceil(viewport()->height() / (double) m_rowHeight);
1364 foreach (AbstractTreeItem * item,
1365 m_model->getItems(startRow, startRow + rowCount)) {
1366 m_items->append(item);
1369 viewport()->update();
1374 void TableViewContent::showContextMenu(QPoint mouseLocation) {
1375 QMenu contextMenu(
this);
1379 if (hasRowSelection() && mouseInRowSelection(mouseLocation)) {
1381 contextMenu.exec(mapToGlobal(mouseLocation));
1386 if (hasActiveCell() && mouseInCellSelection(mouseLocation)) {
1387 if (rowsWithActiveColumnSelected->size() > 1)
1391 contextMenu.exec(mapToGlobal(mouseLocation));