src/gui/widgets/qabstractscrollarea.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    49 #include "qstyleoption.h"
    49 #include "qstyleoption.h"
    50 #include "qevent.h"
    50 #include "qevent.h"
    51 #include "qdebug.h"
    51 #include "qdebug.h"
    52 #include "qboxlayout.h"
    52 #include "qboxlayout.h"
    53 #include "qpainter.h"
    53 #include "qpainter.h"
       
    54 #include "qmargins.h"
    54 
    55 
    55 #include "qabstractscrollarea_p.h"
    56 #include "qabstractscrollarea_p.h"
    56 #include <qwidget.h>
    57 #include <qwidget.h>
    57 
    58 
    58 #include <private/qapplication_p.h>
    59 #include <private/qapplication_p.h>
   291     viewport->setFocusProxy(q);
   292     viewport->setFocusProxy(q);
   292     q->setFocusPolicy(Qt::WheelFocus);
   293     q->setFocusPolicy(Qt::WheelFocus);
   293     q->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
   294     q->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
   294     q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
   295     q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
   295     layoutChildren();
   296     layoutChildren();
       
   297 #ifndef Q_WS_MAC
       
   298     viewport->grabGesture(Qt::PanGesture);
       
   299 #endif
   296 }
   300 }
   297 
   301 
   298 #ifdef Q_WS_WIN
   302 #ifdef Q_WS_WIN
   299 void QAbstractScrollAreaPrivate::setSingleFingerPanEnabled(bool on)
   303 void QAbstractScrollAreaPrivate::setSingleFingerPanEnabled(bool on)
   300 {
   304 {
   301     singleFingerPanEnabled = on;
   305     singleFingerPanEnabled = on;
   302     winSetupGestures();
   306     QWidgetPrivate *dd = static_cast<QWidgetPrivate *>(QObjectPrivate::get(viewport));
       
   307     if (dd)
       
   308         dd->winSetupGestures();
   303 }
   309 }
   304 #endif // Q_WS_WIN
   310 #endif // Q_WS_WIN
   305 
   311 
   306 void QAbstractScrollAreaPrivate::layoutChildren()
   312 void QAbstractScrollAreaPrivate::layoutChildren()
   307 {
   313 {
   537             widget = new QWidget;
   543             widget = new QWidget;
   538         d->viewport = widget;
   544         d->viewport = widget;
   539         d->viewport->setParent(this);
   545         d->viewport->setParent(this);
   540         d->viewport->setFocusProxy(this);
   546         d->viewport->setFocusProxy(this);
   541         d->viewport->installEventFilter(d->viewportFilter.data());
   547         d->viewport->installEventFilter(d->viewportFilter.data());
       
   548 #ifndef Q_WS_MAC
       
   549         d->viewport->grabGesture(Qt::PanGesture);
       
   550 #endif
   542         d->layoutChildren();
   551         d->layoutChildren();
   543         if (isVisible())
   552         if (isVisible())
   544             d->viewport->show();
   553             d->viewport->show();
   545         QMetaObject::invokeMethod(this, "setupViewport", Q_ARG(QWidget *, widget));
   554         QMetaObject::invokeMethod(this, "setupViewport", Q_ARG(QWidget *, widget));
   546         delete oldViewport;
   555         delete oldViewport;
   859     d->left = left;
   868     d->left = left;
   860     d->top = top;
   869     d->top = top;
   861     d->right = right;
   870     d->right = right;
   862     d->bottom = bottom;
   871     d->bottom = bottom;
   863     d->layoutChildren();
   872     d->layoutChildren();
       
   873 }
       
   874 
       
   875 /*!
       
   876     \since 4.6
       
   877     Sets \a margins around the scrolling area. This is useful for
       
   878     applications such as spreadsheets with "locked" rows and columns.
       
   879     The marginal space is is left blank; put widgets in the unused
       
   880     area.
       
   881 
       
   882     By default all margins are zero.
       
   883 
       
   884 */
       
   885 void QAbstractScrollArea::setViewportMargins(const QMargins &margins)
       
   886 {
       
   887     setViewportMargins(margins.left(), margins.top(),
       
   888                        margins.right(), margins.bottom());
   864 }
   889 }
   865 
   890 
   866 /*!
   891 /*!
   867     \fn bool QAbstractScrollArea::event(QEvent *event)
   892     \fn bool QAbstractScrollArea::event(QEvent *event)
   868 
   893 
   933         // ignore touch events in case they have been propagated from the viewport
   958         // ignore touch events in case they have been propagated from the viewport
   934     case QEvent::TouchBegin:
   959     case QEvent::TouchBegin:
   935     case QEvent::TouchUpdate:
   960     case QEvent::TouchUpdate:
   936     case QEvent::TouchEnd:
   961     case QEvent::TouchEnd:
   937         return false;
   962         return false;
       
   963     case QEvent::Gesture:
       
   964     {
       
   965         QGestureEvent *ge = static_cast<QGestureEvent *>(e);
       
   966         QPanGesture *g = static_cast<QPanGesture *>(ge->gesture(Qt::PanGesture));
       
   967         if (g) {
       
   968             QScrollBar *hBar = horizontalScrollBar();
       
   969             QScrollBar *vBar = verticalScrollBar();
       
   970             QPointF delta = g->delta();
       
   971             if (!delta.isNull()) {
       
   972                 if (QApplication::isRightToLeft())
       
   973                     delta.rx() *= -1;
       
   974                 int newX = hBar->value() - delta.x();
       
   975                 int newY = vBar->value() - delta.y();
       
   976                 hBar->setValue(newX);
       
   977                 vBar->setValue(newY);
       
   978             }
       
   979             return true;
       
   980         }
       
   981         return false;
       
   982     }
   938     case QEvent::StyleChange:
   983     case QEvent::StyleChange:
   939     case QEvent::LayoutDirectionChange:
   984     case QEvent::LayoutDirectionChange:
   940     case QEvent::ApplicationLayoutDirectionChange:
   985     case QEvent::ApplicationLayoutDirectionChange:
   941         d->layoutChildren();
   986         d->layoutChildren();
   942         // fall through
   987         // fall through
   988     case QEvent::DragMove:
  1033     case QEvent::DragMove:
   989     case QEvent::DragLeave:
  1034     case QEvent::DragLeave:
   990 #endif
  1035 #endif
   991         return QFrame::event(e);
  1036         return QFrame::event(e);
   992     case QEvent::LayoutRequest:
  1037     case QEvent::LayoutRequest:
       
  1038     case QEvent::Gesture:
       
  1039     case QEvent::GestureOverride:
   993         return event(e);
  1040         return event(e);
   994     default:
  1041     default:
   995         break;
  1042         break;
   996     }
  1043     }
   997     return false; // let the viewport widget handle the event
  1044     return false; // let the viewport widget handle the event
  1085 */
  1132 */
  1086 #ifndef QT_NO_WHEELEVENT
  1133 #ifndef QT_NO_WHEELEVENT
  1087 void QAbstractScrollArea::wheelEvent(QWheelEvent *e)
  1134 void QAbstractScrollArea::wheelEvent(QWheelEvent *e)
  1088 {
  1135 {
  1089     Q_D(QAbstractScrollArea);
  1136     Q_D(QAbstractScrollArea);
  1090     if (static_cast<QWheelEvent*>(e)->orientation() == Qt::Horizontal)
  1137     QScrollBar *const bars[2] = { d->hbar, d->vbar };
  1091         QApplication::sendEvent(d->hbar, e);
  1138     int idx = (e->orientation() == Qt::Vertical) ? 1 : 0;
  1092     else
  1139     int other = (idx + 1) % 2;
  1093         QApplication::sendEvent(d->vbar, e);
  1140     if (!bars[idx]->isVisible() && bars[other]->isVisible())
       
  1141         idx = other;   // If the scrollbar of the event orientation is hidden, fallback to the other.
       
  1142 
       
  1143     QApplication::sendEvent(bars[idx], e);
  1094 }
  1144 }
  1095 #endif
  1145 #endif
  1096 
  1146 
  1097 #ifndef QT_NO_CONTEXTMENU
  1147 #ifndef QT_NO_CONTEXTMENU
  1098 /*!
  1148 /*!
  1264 }
  1314 }
  1265 
  1315 
  1266 void QAbstractScrollAreaPrivate::_q_showOrHideScrollBars()
  1316 void QAbstractScrollAreaPrivate::_q_showOrHideScrollBars()
  1267 {
  1317 {
  1268     layoutChildren();
  1318     layoutChildren();
  1269 #ifdef Q_OS_WIN
  1319 #ifdef Q_WS_WIN
  1270     // Need to re-subscribe to gestures as the content changes to make sure we
  1320     // Need to re-subscribe to gestures as the content changes to make sure we
  1271     // enable/disable panning when needed.
  1321     // enable/disable panning when needed.
  1272     winSetupGestures();
  1322     QWidgetPrivate *dd = static_cast<QWidgetPrivate *>(QObjectPrivate::get(viewport));
  1273 #endif // Q_OS_WIN
  1323     if (dd)
       
  1324         dd->winSetupGestures();
       
  1325 #endif // Q_WS_WIN
  1274 }
  1326 }
  1275 
  1327 
  1276 QPoint QAbstractScrollAreaPrivate::contentsOffset() const
  1328 QPoint QAbstractScrollAreaPrivate::contentsOffset() const
  1277 {
  1329 {
  1278     Q_Q(const QAbstractScrollArea);
  1330     Q_Q(const QAbstractScrollArea);
  1333 void QAbstractScrollArea::setupViewport(QWidget *viewport)
  1385 void QAbstractScrollArea::setupViewport(QWidget *viewport)
  1334 {
  1386 {
  1335     Q_UNUSED(viewport);
  1387     Q_UNUSED(viewport);
  1336 }
  1388 }
  1337 
  1389 
  1338 //void QAbstractScrollAreaPrivate::_q_gestureTriggered()
       
  1339 //{
       
  1340 //    Q_Q(QAbstractScrollArea);
       
  1341 //    QPanGesture *g = qobject_cast<QPanGesture*>(q->sender());
       
  1342 //    if (!g)
       
  1343 //        return;
       
  1344 //    QScrollBar *hBar = q->horizontalScrollBar();
       
  1345 //    QScrollBar *vBar = q->verticalScrollBar();
       
  1346 //    QSizeF delta = g->lastOffset();
       
  1347 //    if (!delta.isNull()) {
       
  1348 //        if (QApplication::isRightToLeft())
       
  1349 //            delta.rwidth() *= -1;
       
  1350 //        int newX = hBar->value() - delta.width();
       
  1351 //        int newY = vBar->value() - delta.height();
       
  1352 //        hbar->setValue(newX);
       
  1353 //        vbar->setValue(newY);
       
  1354 //    }
       
  1355 //}
       
  1356 
       
  1357 QT_END_NAMESPACE
  1390 QT_END_NAMESPACE
  1358 
  1391 
  1359 #include "moc_qabstractscrollarea.cpp"
  1392 #include "moc_qabstractscrollarea.cpp"
  1360 #include "moc_qabstractscrollarea_p.cpp"
  1393 #include "moc_qabstractscrollarea_p.cpp"
  1361 
  1394