src/gui/itemviews/qtableview.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
  1844             QModelIndex topLeft = d->model->index(row, tl.column(), d->root);
  1844             QModelIndex topLeft = d->model->index(row, tl.column(), d->root);
  1845             QModelIndex bottomRight = d->model->index(row, br.column(), d->root);
  1845             QModelIndex bottomRight = d->model->index(row, br.column(), d->root);
  1846             selection.append(QItemSelectionRange(topLeft, bottomRight));
  1846             selection.append(QItemSelectionRange(topLeft, bottomRight));
  1847         }
  1847         }
  1848     } else { // nothing moved
  1848     } else { // nothing moved
  1849         selection.append(QItemSelectionRange(tl, br));
  1849         QItemSelectionRange range(tl, br);
       
  1850         if (!range.isEmpty())
       
  1851             selection.append(range);
  1850     }
  1852     }
  1851 
  1853 
  1852     d->selectionModel->select(selection, command);
  1854     d->selectionModel->select(selection, command);
  1853 }
  1855 }
  1854 
  1856 
  1855 /*!
  1857 /*!
  1856     \internal
  1858     \internal
  1857 
  1859 
  1858     Returns the rectangle from the viewport of the items in the given
  1860     Returns the rectangle from the viewport of the items in the given
  1859     \a selection.
  1861     \a selection.
       
  1862 
       
  1863     Since 4.7, the returned region only contains rectangles intersecting
       
  1864     (or included in) the viewport.
  1860 */
  1865 */
  1861 QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) const
  1866 QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) const
  1862 {
  1867 {
  1863     Q_D(const QTableView);
  1868     Q_D(const QTableView);
  1864 
  1869 
  1865     if (selection.isEmpty())
  1870     if (selection.isEmpty())
  1866         return QRegion();
  1871         return QRegion();
  1867 
  1872 
  1868     QRegion selectionRegion;
  1873     QRegion selectionRegion;
       
  1874     const QRect &viewportRect = d->viewport->rect();
  1869     bool verticalMoved = verticalHeader()->sectionsMoved();
  1875     bool verticalMoved = verticalHeader()->sectionsMoved();
  1870     bool horizontalMoved = horizontalHeader()->sectionsMoved();
  1876     bool horizontalMoved = horizontalHeader()->sectionsMoved();
  1871 
  1877 
  1872     if ((verticalMoved && horizontalMoved) || (d->hasSpans() && (verticalMoved || horizontalMoved))) {
  1878     if ((verticalMoved && horizontalMoved) || (d->hasSpans() && (verticalMoved || horizontalMoved))) {
  1873         for (int i = 0; i < selection.count(); ++i) {
  1879         for (int i = 0; i < selection.count(); ++i) {
  1874             QItemSelectionRange range = selection.at(i);
  1880             QItemSelectionRange range = selection.at(i);
  1875             if (range.parent() != d->root || !range.isValid())
  1881             if (range.parent() != d->root || !range.isValid())
  1876                 continue;
  1882                 continue;
  1877             for (int r = range.top(); r <= range.bottom(); ++r)
  1883             for (int r = range.top(); r <= range.bottom(); ++r)
  1878                 for (int c = range.left(); c <= range.right(); ++c)
  1884                 for (int c = range.left(); c <= range.right(); ++c) {
  1879                     selectionRegion += QRegion(visualRect(d->model->index(r, c, d->root)));
  1885                     const QRect &rangeRect = visualRect(d->model->index(r, c, d->root));
       
  1886                     if (viewportRect.intersects(rangeRect))
       
  1887                         selectionRegion += rangeRect;
       
  1888                 }
  1880         }
  1889         }
  1881     } else if (horizontalMoved) {
  1890     } else if (horizontalMoved) {
  1882         for (int i = 0; i < selection.count(); ++i) {
  1891         for (int i = 0; i < selection.count(); ++i) {
  1883             QItemSelectionRange range = selection.at(i);
  1892             QItemSelectionRange range = selection.at(i);
  1884             if (range.parent() != d->root || !range.isValid())
  1893             if (range.parent() != d->root || !range.isValid())
  1886             int top = rowViewportPosition(range.top());
  1895             int top = rowViewportPosition(range.top());
  1887             int bottom = rowViewportPosition(range.bottom()) + rowHeight(range.bottom());
  1896             int bottom = rowViewportPosition(range.bottom()) + rowHeight(range.bottom());
  1888             if (top > bottom)
  1897             if (top > bottom)
  1889                 qSwap<int>(top, bottom);
  1898                 qSwap<int>(top, bottom);
  1890             int height = bottom - top;
  1899             int height = bottom - top;
  1891             for (int c = range.left(); c <= range.right(); ++c)
  1900             for (int c = range.left(); c <= range.right(); ++c) {
  1892                 selectionRegion += QRegion(QRect(columnViewportPosition(c), top,
  1901                 const QRect rangeRect(columnViewportPosition(c), top, columnWidth(c), height);
  1893                                                  columnWidth(c), height));
  1902                 if (viewportRect.intersects(rangeRect))
       
  1903                     selectionRegion += rangeRect;
       
  1904             }
  1894         }
  1905         }
  1895     } else if (verticalMoved) {
  1906     } else if (verticalMoved) {
  1896         for (int i = 0; i < selection.count(); ++i) {
  1907         for (int i = 0; i < selection.count(); ++i) {
  1897             QItemSelectionRange range = selection.at(i);
  1908             QItemSelectionRange range = selection.at(i);
  1898             if (range.parent() != d->root || !range.isValid())
  1909             if (range.parent() != d->root || !range.isValid())
  1900             int left = columnViewportPosition(range.left());
  1911             int left = columnViewportPosition(range.left());
  1901             int right = columnViewportPosition(range.right()) + columnWidth(range.right());
  1912             int right = columnViewportPosition(range.right()) + columnWidth(range.right());
  1902             if (left > right)
  1913             if (left > right)
  1903                 qSwap<int>(left, right);
  1914                 qSwap<int>(left, right);
  1904             int width = right - left;
  1915             int width = right - left;
  1905             for (int r = range.top(); r <= range.bottom(); ++r)
  1916             for (int r = range.top(); r <= range.bottom(); ++r) {
  1906                 selectionRegion += QRegion(QRect(left, rowViewportPosition(r),
  1917                 const QRect rangeRect(left, rowViewportPosition(r), width, rowHeight(r));
  1907                                                  width, rowHeight(r)));
  1918                 if (viewportRect.intersects(rangeRect))
       
  1919                     selectionRegion += rangeRect;
       
  1920             }
  1908         }
  1921         }
  1909     } else { // nothing moved
  1922     } else { // nothing moved
  1910         const int gridAdjust = showGrid() ? 1 : 0;
  1923         const int gridAdjust = showGrid() ? 1 : 0;
  1911         for (int i = 0; i < selection.count(); ++i) {
  1924         for (int i = 0; i < selection.count(); ++i) {
  1912             QItemSelectionRange range = selection.at(i);
  1925             QItemSelectionRange range = selection.at(i);
  1923                 rright = columnViewportPosition(range.right()) + columnWidth(range.right());
  1936                 rright = columnViewportPosition(range.right()) + columnWidth(range.right());
  1924             } else {
  1937             } else {
  1925                 rleft = columnViewportPosition(range.right());
  1938                 rleft = columnViewportPosition(range.right());
  1926                 rright = columnViewportPosition(range.left()) + columnWidth(range.left());
  1939                 rright = columnViewportPosition(range.left()) + columnWidth(range.left());
  1927             }
  1940             }
  1928             selectionRegion += QRect(QPoint(rleft, rtop), QPoint(rright - 1 - gridAdjust, rbottom - 1 - gridAdjust));
  1941             const QRect rangeRect(QPoint(rleft, rtop), QPoint(rright - 1 - gridAdjust, rbottom - 1 - gridAdjust));
       
  1942             if (viewportRect.intersects(rangeRect))
       
  1943                 selectionRegion += rangeRect;
  1929             if (d->hasSpans()) {
  1944             if (d->hasSpans()) {
  1930                 foreach (QSpanCollection::Span *s,
  1945                 foreach (QSpanCollection::Span *s,
  1931                          d->spans.spansInRect(range.left(), range.top(), range.width(), range.height())) {
  1946                          d->spans.spansInRect(range.left(), range.top(), range.width(), range.height())) {
  1932                     if (range.contains(s->top(), s->left(), range.parent()))
  1947                     if (range.contains(s->top(), s->left(), range.parent())) {
  1933                         selectionRegion += d->visualSpanRect(*s);
  1948                         const QRect &visualSpanRect = d->visualSpanRect(*s);
       
  1949                         if (viewportRect.intersects(visualSpanRect))
       
  1950                             selectionRegion += visualSpanRect;
       
  1951                     }
  1934                 }
  1952                 }
  1935             }
  1953             }
  1936         }
  1954         }
  1937     }
  1955     }
  1938 
  1956 
  1965     number of rows is specified by \a newCount.
  1983     number of rows is specified by \a newCount.
  1966 */
  1984 */
  1967 void QTableView::rowCountChanged(int /*oldCount*/, int /*newCount*/ )
  1985 void QTableView::rowCountChanged(int /*oldCount*/, int /*newCount*/ )
  1968 {
  1986 {
  1969     Q_D(QTableView);
  1987     Q_D(QTableView);
  1970     updateGeometries();
  1988     d->doDelayedItemsLayout();
  1971     if (verticalScrollMode() == QAbstractItemView::ScrollPerItem)
       
  1972         d->verticalHeader->setOffsetToSectionPosition(verticalScrollBar()->value());
       
  1973     else
       
  1974         d->verticalHeader->setOffset(verticalScrollBar()->value());
       
  1975     d->viewport->update();
       
  1976 }
  1989 }
  1977 
  1990 
  1978 /*!
  1991 /*!
  1979     This slot is called whenever columns are added or deleted. The
  1992     This slot is called whenever columns are added or deleted. The
  1980     previous number of columns is specified by \a oldCount, and the new
  1993     previous number of columns is specified by \a oldCount, and the new
  2130     if (!model())
  2143     if (!model())
  2131         return -1;
  2144         return -1;
  2132 
  2145 
  2133     ensurePolished();
  2146     ensurePolished();
  2134 
  2147 
  2135     int left = qMax(0, columnAt(0));
  2148     int left = qMax(0, d->horizontalHeader->visualIndexAt(0));
  2136     int right = columnAt(d->viewport->width());
  2149     int right = d->horizontalHeader->visualIndexAt(d->viewport->width());
  2137     if (right == -1) // the table don't have enough columns to fill the viewport
  2150     if (right == -1) // the table don't have enough columns to fill the viewport
  2138         right = d->model->columnCount(d->root) - 1;
  2151         right = d->model->columnCount(d->root) - 1;
  2139 
  2152 
  2140     QStyleOptionViewItemV4 option = d->viewOptionsV4();
  2153     QStyleOptionViewItemV4 option = d->viewOptionsV4();
  2141 
  2154 
  2189     if (!model())
  2202     if (!model())
  2190         return -1;
  2203         return -1;
  2191 
  2204 
  2192     ensurePolished();
  2205     ensurePolished();
  2193 
  2206 
  2194     int top = qMax(0, rowAt(0));
  2207     int top = qMax(0, d->verticalHeader->visualIndexAt(0));
  2195     int bottom = rowAt(d->viewport->height());
  2208     int bottom = d->verticalHeader->visualIndexAt(d->viewport->height());
  2196     if (!isVisible() || bottom == -1) // the table don't have enough rows to fill the viewport
  2209     if (!isVisible() || bottom == -1) // the table don't have enough rows to fill the viewport
  2197         bottom = d->model->rowCount(d->root) - 1;
  2210         bottom = d->model->rowCount(d->root) - 1;
  2198 
  2211 
  2199     QStyleOptionViewItemV4 option = d->viewOptionsV4();
  2212     QStyleOptionViewItemV4 option = d->viewOptionsV4();
  2200 
  2213