11 #include "TableViewContent.h"
17 #include <QApplication>
21 #include <QMessageBox>
24 #include <QPaintEvent>
29 #include <QStyleOptionViewItemV4>
31 #include <QVBoxLayout>
33 #include "ControlMeasure.h"
34 #include "IException.h"
37 #include "AbstractTableDelegate.h"
38 #include "AbstractTableModel.h"
39 #include "AbstractTreeItem.h"
40 #include "TableColumn.h"
41 #include "TableColumnList.h"
42 #include <ControlPoint.h>
55 connect(m_model, SIGNAL(modelModified()),
this, SLOT(
refresh()));
56 connect(m_model, SIGNAL(filterProgressChanged(
int)),
58 connect(
this, SIGNAL(modelDataChanged()),
59 m_model, SLOT(applyFilter()));
67 m_columns =
getModel()->getColumns();
68 for (
int i = 0; i < m_columns->size(); i++) {
71 connect(column, SIGNAL(visibilityChanged()),
this, SLOT(
refresh()));
72 connect(column, SIGNAL(visibilityChanged()),
74 connect(column, SIGNAL(widthChanged()),
this, SLOT(
refresh()));
82 m_lastShiftArrowSelectedCell->first = NULL;
84 verticalScrollBar()->setSingleStep(1);
86 m_rowHeight = QFontMetrics(font()).height() + ITEM_PADDING;
87 ASSERT(m_rowHeight > 0);
89 connect(horizontalScrollBar(), SIGNAL(valueChanged(
int)),
90 this, SIGNAL(horizontalScrollBarValueChanged(
int)));
92 setMouseTracking(
true);
97 setContextMenuPolicy(Qt::CustomContextMenu);
98 connect(
this, SIGNAL(customContextMenuRequested(QPoint)),
101 m_activeControlNet =
false;
118 delete m_lastShiftSelection;
119 m_lastShiftSelection = NULL;
133 delete m_lastShiftArrowSelectedCell;
134 m_lastShiftArrowSelectedCell = NULL;
146 return QWidget::minimumSizeHint();
217 m_activeControlNet = activeNet;
226 if (!m_model->isFiltering()) {
227 int rowCount = m_model->getVisibleRowCount();
228 verticalScrollBar()->setRange(0, qMax(rowCount - 1, 0));
233 m_lastShiftSelection->clear();
235 if (m_model->getSelectedItems().size() &&
241 viewport()->update();
255 for (
int i = 0; i < visibleCols.size(); i++)
256 range += visibleCols[i]->getWidth() - 1;
260 horizontalScrollBar()->setRange(0, range - viewport()->width());
261 horizontalScrollBar()->setPageStep(viewport()->width());
264 horizontalScrollBar()->setValue(horizontalScrollBar()->maximum());
276 if (newlySelectedItems.size())
277 scrollTo(newlySelectedItems.last());
287 int row =
getModel()->indexOfVisibleItem(newlySelectedItem);
290 int topRow = verticalScrollBar()->value();
293 verticalScrollBar()->setValue(row);
296 int wholeVisibleRowCount = viewport()->height() / m_rowHeight;
297 int bottomRow = topRow + wholeVisibleRowCount;
299 verticalScrollBar()->setValue(row - wholeVisibleRowCount + 1);
303 viewport()->update();
316 return QObject::eventFilter(target, event);
326 if (event->buttons() & Qt::LeftButton) {
327 int rowNum =
event->pos().y() / m_rowHeight;
329 if (m_activeCell->first &&
cellIsEditable(rowNum, m_activeCell->second)) {
332 m_columns->getVisibleColumns()[m_activeCell->second];
336 m_editWidget = delegate->getWidget(col);
337 delegate->readData(m_editWidget, m_activeCell->first, col);
338 m_editWidget->setParent(
this);
339 m_editWidget->setFocus(Qt::OtherFocusReason);
342 viewport()->update();
354 if (event->buttons() & Qt::LeftButton) {
356 if (!(event->modifiers() & Qt::ShiftModifier))
359 int rowNum =
event->pos().y() / m_rowHeight;
369 if (rowNum >= 0 && rowNum < m_items->size() && m_activeCell->first) {
374 m_model->setGlobalSelection(
false);
377 if (event->modifiers() & Qt::ControlModifier) {
384 m_lastShiftSelection->clear();
387 if (event->modifiers() & Qt::ShiftModifier) {
395 m_lastShiftSelection->clear();
402 if (rowNum >= 0 && rowNum < m_items->size()) {
405 if (columnNum != -1) {
406 TableColumn *column = m_columns->getVisibleColumns()[columnNum];
407 if (column->getTitle().isEmpty()) {
413 if (event->modifiers() & Qt::ControlModifier) {
414 if (item->getPointerType() == AbstractTreeItem::Measure)
415 item->parent()->setSelected(!item->isSelected());
417 item->setSelected(!item->isSelected());
419 newlySelectedItems.append(item);
422 if (event->modifiers() & Qt::ShiftModifier) {
427 m_model->getSelectedItems();
430 if (selectedItem->getPointerType() ==
431 AbstractTreeItem::Measure)
432 selectedItem->parent()->setSelected(
false);
435 m_model->setGlobalSelection(
false);
437 if (item->getPointerType() == AbstractTreeItem::Measure)
438 item->parent()->setSelected(
true);
440 item->setSelected(
true);
442 newlySelectedItems.append(item);
447 newlySelectedItems.clear();
449 newlySelectedItems.append(i);
450 if (i->getPointerType() == AbstractTreeItem::Point) {
452 child->setSelected(
true);
453 newlySelectedItems.append(child);
458 emit tableSelectionChanged(newlySelectedItems);
467 viewport()->update();
468 emit tableSelectionChanged();
489 if (event->buttons() & Qt::LeftButton) {
490 int rowNum =
event->pos().y() / m_rowHeight;
493 int yPos =
event->pos().y();
494 if (yPos >= 0 && rowNum < m_items->size() && m_activeCell->first) {
503 if (yPos >= 0 && rowNum < m_items->size()) {
508 if (columnNum != -1) {
515 newlySelectedItems.append(i);
516 if (i->getPointerType() == AbstractTreeItem::Point) {
518 child->setSelected(
true);
519 newlySelectedItems.append(child);
524 emit tableSelectionChanged(newlySelectedItems);
529 QScrollBar *vertScroll = verticalScrollBar();
531 if (yPos > viewport()->height() &&
532 vertScroll->value() < vertScroll->maximum()) {
534 vertScroll->setValue(vertScroll->value() + 1);
537 if (yPos < 0 && vertScroll->value() > vertScroll->minimum())
538 vertScroll->setValue(vertScroll->value() - 1);
541 viewport()->update();
542 emit tableSelectionChanged();
564 Qt::Key key = (Qt::Key) event->key();
567 if (key == Qt::Key_A && event->modifiers() == Qt::ControlModifier) {
570 m_model->setGlobalSelection(
true);
571 viewport()->update();
573 emit tableSelectionChanged();
577 else if (key == Qt::Key_Escape) {
581 setFocus(Qt::ActiveWindowFocusReason);
582 viewport()->update();
587 else if (key == Qt::Key_Delete) {
593 else if (key == Qt::Key_Return || key == Qt::Key_Enter) {
599 else if (key == Qt::Key_Tab) {
605 else if (key == Qt::Key_Up || key == Qt::Key_Down ||
606 key == Qt::Key_Left || key == Qt::Key_Right) {
609 if (m_items && m_items->size()) {
610 m_activeCell->first = (*m_items)[0];
611 m_activeCell->second = 1;
617 ASSERT(m_items->size());
620 if (key == Qt::Key_Up && event->modifiers() == Qt::ShiftModifier) {
621 ASSERT(m_lastShiftArrowSelectedCell);
624 m_lastShiftArrowSelectedCell->first : m_activeCell->first;
626 int prevCellIndex =
getModel()->indexOfVisibleItem(prevCell);
628 if (prevCellIndex > 0) {
630 getModel()->getItems(prevCellIndex - 1, prevCellIndex);
632 if (itemList.size()) {
636 curItem == m_activeCell->first)
641 if (curItem == m_activeCell->first)
642 m_lastShiftArrowSelectedCell->first = NULL;
644 m_lastShiftArrowSelectedCell->first = curItem;
645 m_lastShiftArrowSelectedCell->second = m_activeCell->second;
648 int m_itemsPrevIndex = m_items->indexOf(prevCell);
649 int m_itemsCurIndex = m_items->indexOf(curItem);
650 if (m_itemsCurIndex == -1) {
651 if (m_itemsPrevIndex == 0) {
652 verticalScrollBar()->setValue(qMax(0, prevCellIndex - 1));
661 viewport()->update();
667 else if (key == Qt::Key_Down && event->modifiers() == Qt::ShiftModifier) {
669 m_lastShiftArrowSelectedCell->first : m_activeCell->first;
671 int prevCellIndex =
getModel()->indexOfVisibleItem(prevCell);
673 if (prevCellIndex >= 0 &&
674 prevCellIndex <
getModel()->getVisibleRowCount() - 1) {
676 getModel()->getItems(prevCellIndex + 1, prevCellIndex + 2);
678 if (itemList.size()) {
682 curItem == m_activeCell->first)
687 if (curItem == m_activeCell->first)
688 m_lastShiftArrowSelectedCell->first = NULL;
690 m_lastShiftArrowSelectedCell->first = curItem;
691 m_lastShiftArrowSelectedCell->second = m_activeCell->second;
692 viewport()->update();
695 int m_itemsPrevIndex = m_items->indexOf(prevCell);
696 int m_itemsCurIndex = m_items->indexOf(curItem);
697 if (m_itemsCurIndex == -1) {
698 if (m_itemsPrevIndex == m_items->size() - 1) {
699 int visibleItemCount =
getModel()->getVisibleRowCount();
700 verticalScrollBar()->setValue(qMin(visibleItemCount,
701 getModel()->indexOfVisibleItem(m_items->at(1))));
714 else if (key == Qt::Key_Up) {
719 else if (key == Qt::Key_Down) {
724 else if (key == Qt::Key_Left) {
729 else if (key == Qt::Key_Right) {
739 if (!m_items->contains(m_activeCell->first))
742 ASSERT(m_items->contains(m_activeCell->first));
744 if (m_items->contains(m_activeCell->first) &&
746 m_activeCell->second)) {
749 m_columns->getVisibleColumns()[m_activeCell->second];
753 m_editWidget = delegate->getWidget(col);
754 delegate->readData(m_editWidget, m_activeCell->first, col, event->text());
755 m_editWidget->setParent(
this);
756 m_editWidget->setFocus(Qt::OtherFocusReason);
759 viewport()->update();
771 m_columns->getVisibleColumns()[m_activeCell->second];
772 getModel()->getDelegate()->saveData(
773 m_editWidget, m_activeCell->first, col);
778 setFocus(Qt::ActiveWindowFocusReason);
787 int activeIndex = m_items->indexOf(m_activeCell->first);
788 if (activeIndex != -1) {
789 if (activeIndex == 0) {
790 int row = qMax(0,
getModel()->indexOfVisibleItem(
791 m_activeCell->first) - 1);
793 verticalScrollBar()->setValue(row);
796 m_activeCell->first = (*m_items)[qMax(0, activeIndex - 1)];
798 viewport()->update();
807 int activeIndex = m_items->indexOf(m_activeCell->first);
808 if (activeIndex != -1) {
809 if (activeIndex == m_items->size() - 1) {
810 int row = qMin(
getModel()->getVisibleRowCount() - 1,
811 getModel()->indexOfVisibleItem(m_items->at(0)));
813 verticalScrollBar()->setValue(row + 1);
814 activeIndex = m_items->indexOf(m_activeCell->first);
817 m_activeCell->first = (*m_items)[qMin(activeIndex + 1, m_items->size() - 1)];
819 viewport()->update();
828 m_activeCell->second = qMax(1, m_activeCell->second - 1);
830 if (leftMostVisibleCol == m_activeCell->second) {
831 horizontalScrollBar()->setValue(horizontalScrollBar()->value() -
832 m_columns->getVisibleColumns()[m_activeCell->second]->getWidth());
836 viewport()->update();
844 m_activeCell->second = qMin(m_columns->getVisibleColumns().size() - 1,
845 m_activeCell->second + 1);
847 if (rightMostVisibleCol == m_activeCell->second) {
848 horizontalScrollBar()->setValue(horizontalScrollBar()->value() +
849 m_columns->getVisibleColumns()[m_activeCell->second]->getWidth());
853 viewport()->update();
865 if (m_model && m_columns) {
867 int rowCount = (int) ceil(viewport()->height() / (double) m_rowHeight);
869 QPainter painter(viewport());
870 painter.setRenderHint(QPainter::Antialiasing,
false);
873 bool editWidgetVisible =
false;
874 for (
int i = 0; i < rowCount; i++) {
876 QPoint relativeTopLeft(0, i * m_rowHeight);
877 QPoint scrollBarPos(horizontalScrollBar()->value(),
878 verticalScrollBar()->value());
879 QPoint absoluteTopLeft(relativeTopLeft + scrollBarPos);
880 QSize rowSize(viewport()->width(), (
int) m_rowHeight);
883 painter.fillRect(QRect(relativeTopLeft, rowSize), palette().base());
885 if (i < m_items->size()) {
925 if (m_items->at(i)->isSelected()) {
926 QPoint selectionTopLeft(-absoluteTopLeft.x(), relativeTopLeft.y());
927 QSize selectionSize(m_columns->getVisibleWidth(), (
int) m_rowHeight);
929 QRect selectionRect(selectionTopLeft, selectionSize);
930 painter.fillRect(selectionRect, palette().highlight().color());
933 paintRow(&painter, i, absoluteTopLeft, relativeTopLeft);
937 for (
int i = 0; i < rowCount; i++) {
938 if (i < m_items->size()) {
939 QPoint relativeTopLeft(0, i * m_rowHeight);
940 if (m_editWidget && m_activeCell->first == m_items->at(i)) {
945 QPoint(xRange.first - horizontalScrollBar()->value() - 1,
946 relativeTopLeft.y() + 1));
947 m_editWidget->resize(xRange.second - xRange.first, m_rowHeight + 1);
948 m_editWidget->setVisible(
true);
949 editWidgetVisible =
true;
952 if (m_activeCell->first == m_items->at(i)) {
956 QRect activeArea(activeXArea.first, relativeTopLeft.y(),
957 activeXArea.second - activeXArea.first, m_rowHeight);
960 activeArea.left() - horizontalScrollBar()->value());
961 activeArea.adjust(-1, -1, -2, -1);
965 painter.drawRect(activeArea);
971 if (m_editWidget && !editWidgetVisible)
972 m_editWidget->setVisible(
false);
975 QWidget::paintEvent(event);
986 QAbstractScrollArea::resizeEvent(event);
999 QAbstractScrollArea::scrollContentsBy(dx, dy);
1008 m_parentView = NULL;
1011 m_activeCell = NULL;
1012 m_lastShiftArrowSelectedCell = NULL;
1013 m_editWidget = NULL;
1015 m_lastShiftSelection = NULL;
1031 if (col->hasNetworkStructureEffect())
1034 emit modelDataChanged();
1042 m_activeCell->first = NULL;
1043 m_activeCell->second = -1;
1051 ASSERT(m_lastShiftArrowSelectedCell);
1052 m_lastShiftArrowSelectedCell->first = NULL;
1064 TableColumn *col = m_columns->getVisibleColumns()[m_activeCell->second];
1066 QString colTitle = col->getTitle();
1067 ASSERT(colTitle.size());
1071 QString cellData = m_activeCell->first->getFormattedData(colTitle);
1075 ASSERT(selection.size());
1077 bool needsDialog =
true;
1079 for (
int i = 0; !done && i < selection.size(); i++) {
1081 bool changeData =
true;
1083 QString warningText =
1084 m_model->getWarningMessage(row, col, cellData);
1085 if (needsDialog && warningText.size()) {
1086 QMessageBox::StandardButton status = QMessageBox::warning(
1087 this,
"Change cells?", warningText, QMessageBox::Yes |
1088 QMessageBox::No | QMessageBox::YesToAll | QMessageBox::NoToAll);
1091 case QMessageBox::YesToAll:
1092 needsDialog =
false;
1094 case QMessageBox::NoToAll:
1096 case QMessageBox::No:
1104 row->setData(colTitle, cellData);
1107 viewport()->update();
1124 m_applyToAllAct->setStatusTip(tr(
"Copy the contents of this cell to all"
1125 "cells in the current column"));
1131 tr(
"Delete the currently selected rows"));
1137 tr(
"Edit the selected control point or the parent control point of control measure"));
1152 for (
int i = 0; i < m_columns->getVisibleColumns().size(); i++) {
1154 int deltaX = -horizontalScrollBar()->value();
1156 if (cellXRange.first + deltaX < screenX &&
1157 cellXRange.second + deltaX > screenX) {
1174 int calculatedRowNum = screenY / m_rowHeight;
1176 if (calculatedRowNum >= 0 && calculatedRowNum < m_items->size() &&
1177 screenY >= 0 && screenY <= viewport()->height())
1178 return calculatedRowNum;
1190 return (m_activeCell->first && m_activeCell->second >= 0);
1200 return (m_model->getSelectedItems().size());
1216 m_activeCell->second == colNum);
1230 return (m_model->getSelectedItems().contains(row));
1244 if (rowNum >= 0 && rowNum < m_items->size())
1261 if (colNum >= 0 && colNum < m_columns->getVisibleColumns().size())
1277 ASSERT(rowNum >= 0 && rowNum < m_items->size());
1278 ASSERT(colNum >= 0 && colNum < m_columns->getVisibleColumns().size());
1280 bool editable =
false;
1282 QString colName = m_columns->getVisibleColumns()[colNum]->getTitle();
1283 if (m_items->at(rowNum)->isSelectable() &&
1284 m_items->at(rowNum)->isDataEditable(colName) &&
1285 !m_columns->getVisibleColumns()[colNum]->isReadOnly())
1301 return m_columns->getVisibleColumns()[colNum]->getTitle().size();
1314 QPoint absolutePosition, QPoint relativePosition) {
1316 ASSERT(rowNum >= 0 && rowNum < m_items->size());
1318 QPoint point(-absolutePosition.x(), relativePosition.y());
1324 QPen originalPen = painter->pen();
1326 QPoint textPoint(point.x() + ITEM_INDENTATION,
1327 point.y() + ITEM_PADDING / 2);
1330 int textHeight = m_rowHeight - ITEM_PADDING;
1332 QFontMetrics metrics(font());
1334 QPen gridPen(Qt::gray);
1338 for (
int i = 0; i < visibleCols.size(); i++) {
1341 QRect cellRect(cellXRange.first, point.y(),
1342 cellXRange.second - cellXRange.first, m_rowHeight);
1343 cellRect.moveLeft(cellRect.left() - horizontalScrollBar()->value() - 1);
1345 QString columnTitle = visibleCols[i]->getTitle();
1346 QRect textRect(textPoint, QSize(cellRect.right() - textPoint.x(),
1349 bool textCentered =
false;
1350 if (!columnTitle.isEmpty()) {
1351 text = item->getFormattedData(columnTitle);
1354 m_activeCell->second == i) {
1356 if (m_activeCell->first != item) {
1357 painter->fillRect(cellRect,
1358 QBrush(palette().highlight().color()));
1359 painter->setPen(palette().highlightedText().color());
1362 painter->setPen(palette().text().color());
1366 if (item->isSelected()) {
1367 painter->setPen(palette().highlightedText().color());
1380 painter->setPen(palette().color(QPalette::Disabled,
1384 painter->setPen(palette().text().color());
1391 text = QString::number(rowNum + verticalScrollBar()->value() + 1);
1392 textCentered =
true;
1395 int x = cellRect.center().x();
1396 QLinearGradient gradient(x, cellRect.top(), x, cellRect.bottom());
1398 bool selected = item->isSelected();
1399 QColor color = selected ? palette().highlight().color() :
1400 palette().button().color();
1402 int adjustment = 110;
1403 gradient.setColorAt(0, color.lighter(adjustment));
1404 gradient.setColorAt(1, color.darker(adjustment));
1405 painter->fillRect(cellRect, gradient);
1407 painter->setPen(palette().highlightedText().color());
1409 painter->setPen(palette().text().color());
1412 int flags = Qt::TextDontClip;
1414 flags |= Qt::AlignCenter;
1418 QFont normalFont = painter->font();
1420 if (item->getPointerType() == AbstractTreeItem::Measure) {
1422 if (cm && cm->Parent() && cm->Parent()->
GetRefMeasure() == cm) {
1423 QFont boldFont(normalFont);
1424 boldFont.setBold(
true);
1425 painter->setFont(boldFont);
1429 painter->drawText(textRect, flags,
1430 metrics.elidedText(text, Qt::ElideRight,
1431 textRect.width() - ITEM_INDENTATION));
1432 painter->setFont(normalFont);
1434 textPoint.setX(cellRect.right() + ITEM_INDENTATION);
1435 painter->setPen(originalPen);
1437 painter->setPen(gridPen);
1438 painter->drawLine(QPoint(cellRect.right(), point.y()),
1439 QPoint(cellRect.right(), point.y() + m_rowHeight));
1440 painter->setPen(originalPen);
1443 int left = -horizontalScrollBar()->value() - 1;
1444 int right = m_columns->getVisibleWidth();
1446 gridPen.setWidth(2);
1447 painter->setPen(gridPen);
1448 painter->drawLine(QPoint(left, point.y() + m_rowHeight),
1449 QPoint(right, point.y() + m_rowHeight));
1450 painter->setPen(originalPen);
1461 if (m_editWidget && m_activeCell->first && m_activeCell->second >= 0) {
1464 m_columns->getVisibleColumns()[m_activeCell->second];
1465 m_model->getDelegate()->saveData(m_editWidget, m_activeCell->first,
1471 QMessageBox::critical(
this,
"Failed to Set Data", e.
what());
1476 int oldActiveColumn = m_activeCell->second;
1484 for (
int i = 0; i < visibleCols.size(); i++) {
1486 QRect cellRect(cellXRange.first, m_rowHeight * rowNum,
1487 cellXRange.second - cellXRange.first, m_rowHeight);
1489 cellRect.moveLeft(cellRect.left() - horizontalScrollBar()->value());
1491 if (cellRect.contains(screenPos) &&
1492 (oldActiveColumn != -1 || !visibleCols[i]->getTitle().isEmpty())) {
1493 m_activeCell->first = item;
1494 m_activeCell->second = i;
1499 if (oldActiveColumn != m_activeCell->second) {
1522 *m_lastShiftSelection = m_model->getItems(
1526 m_lastShiftSelection->clear();
1545 if (row->getPointerType() == AbstractTreeItem::Point)
1547 child->setSelected(
false);
1549 if (row->getPointerType() == AbstractTreeItem::Measure)
1550 row->parent()->setSelected(
false);
1552 row->setSelected(
false);
1556 *m_lastShiftSelection = m_model->getItems(
1560 m_lastShiftSelection->clear();
1565 row->setSelected(
true);
1567 if (row->getPointerType() == AbstractTreeItem::Measure)
1568 row->parent()->setSelected(
true);
1570 newlySelectedItems.append(row);
1573 return newlySelectedItems;
1602 QMessageBox::StandardButton status = QMessageBox::warning(
1603 this,
"Delete row(s)?",
"Delete selected row(s)?",
1604 QMessageBox::Yes | QMessageBox::No);
1606 if (status == QMessageBox::Yes) {
1610 emit rebuildModels(selectedRows);
1611 emit modelDataChanged();
1613 m_lastShiftSelection->clear();
1624 QString serialNumber;
1628 if (m_model->getSelectedItems().count() == 0) {
1629 item = m_activeCell->first;
1637 if (item->getPointerType() == AbstractTreeItem::Point) {
1643 serialNumber = item->getData(
"Image ID").toString();
1659 int startRow = verticalScrollBar()->value();
1660 int rowCount = (int) ceil(viewport()->height() / (double) m_rowHeight);
1663 m_model->getItems(startRow, startRow + rowCount)) {
1664 m_items->append(item);
1667 viewport()->update();
1678 QMenu contextMenu(
this);
1689 if (QApplication::applicationName() !=
"cneteditor") {
1690 if (m_activeControlNet) {
1736 contextMenu.exec(mapToGlobal(mouseLocation));