📄 qsplitter.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qsplitter.h"#ifndef QT_NO_SPLITTER#include "qapplication.h"#include "qcursor.h"#include "qdrawutil.h"#include "qevent.h"#include "qlayout.h"#include "qlist.h"#include "qpainter.h"#include "qrubberband.h"#include "qstyle.h"#include "qstyleoption.h"#include "qtextstream.h"#include "qvarlengtharray.h"#include "qvector.h"#include "private/qlayoutengine_p.h"#include "private/qsplitter_p.h"#include "qdebug.h"#include <ctype.h>//#define QSPLITTER_DEBUGstatic int mouseOffset;/*! \class QSplitterHandle \brief The QSplitterHandle class provides handle functionality of the splitter. \ingroup organizers QSplitterHandle is typically what people think about when they think about a splitter. It is the handle that is used to resize the widgets. A typical developer using QSplitter will never have to worry about QSplitterHandle. It is provided for developers who want splitter handles that do more. The typical way one would create splitter handles is to subclass QSplitter and then reimplement QSplitter::createHandle() to instatiate the custom splitter handle. Most of the functions inside QSplitterHandle are forwards to QSplitter or like orientation() and opaqueResize(), controlled by the QSplitter. \sa QSplitter*//*! Creates a QSplitter handle with the given \a orientation and QSplitter \a parent.*/QSplitterHandle::QSplitterHandle(Qt::Orientation orientation, QSplitter *parent) : QWidget(*new QSplitterHandlePrivate, parent, 0){ Q_D(QSplitterHandle); d->s = parent; d->hover = false; setOrientation(orientation);}/*! Sets the orientation of the splitter handle to \a orientation. This is usually propogated from the QSplitter. \sa QSplitter::setOrientation()*/void QSplitterHandle::setOrientation(Qt::Orientation orientation){ Q_D(QSplitterHandle); d->orient = orientation;#ifndef QT_NO_CURSOR setCursor(orientation == Qt::Horizontal ? Qt::SplitHCursor : Qt::SplitVCursor);#endif}/*! Returns the handle's orientation. This is usually propagated from the QSplitter. \sa QSplitter::orientation()*/Qt::Orientation QSplitterHandle::orientation() const{ Q_D(const QSplitterHandle); return d->orient;}/*! Returns true if widgets are resized dynamically (opaquely), otherwise returns false. This value is controlled by the QSplitter. \sa QSplitter::opaqueResize()*/bool QSplitterHandle::opaqueResize() const{ Q_D(const QSplitterHandle); return d->s->opaqueResize();}/*! Returns the splitter associated with this splitter handle. \sa QSplitter::handle()*/QSplitter *QSplitterHandle::splitter() const{ return d_func()->s;}/*! Tells the splitter to move this handle to position \a pos, which is the distance from the left or top edge of the widget. Note that \a pos is also measured from the left (or top) for right-to-left languages. This function will map \a pos to the appropriate position before calling QSplitter::moveSplitter(). \sa QSplitter::moveSplitter() closestLegalPosition()*/void QSplitterHandle::moveSplitter(int pos){ Q_D(QSplitterHandle); if (d->s->isRightToLeft() && d->orient == Qt::Horizontal) pos = d->s->contentsRect().width() - pos; d->s->moveSplitter(pos, d->s->indexOf(this));}/*! Returns the closest legal position to \a pos of the splitter handle. The positions are measured from the left or top edge of the splitter, even for right-to-left languages. \sa QSplitter::closestLegalPosition(), moveSplitter()*/int QSplitterHandle::closestLegalPosition(int pos){ Q_D(QSplitterHandle); QSplitter *s = d->s; if (s->isRightToLeft() && d->orient == Qt::Horizontal) { int w = s->contentsRect().width(); return w - s->closestLegalPosition(w - pos, s->indexOf(this)); } return s->closestLegalPosition(pos, s->indexOf(this));}/*! \reimp*/QSize QSplitterHandle::sizeHint() const{ Q_D(const QSplitterHandle); int hw = d->s->handleWidth(); QStyleOption opt(0); opt.init(d->s); opt.state = QStyle::State_None; return parentWidget()->style()->sizeFromContents(QStyle::CT_Splitter, &opt, QSize(hw, hw), d->s) .expandedTo(QApplication::globalStrut());}/*! \reimp*/bool QSplitterHandle::event(QEvent *event){ Q_D(QSplitterHandle); switch(event->type()) { case QEvent::HoverEnter: d->hover = true; update(); break; case QEvent::HoverLeave: d->hover = false; update(); break; default: break; } return QWidget::event(event);}/*! \reimp*/void QSplitterHandle::mouseMoveEvent(QMouseEvent *e){ Q_D(QSplitterHandle); if (!(e->buttons() & Qt::LeftButton)) return; int pos = d->pick(parentWidget()->mapFromGlobal(e->globalPos())) - mouseOffset; if (opaqueResize()) { moveSplitter(pos); } else { d->s->setRubberBand(closestLegalPosition(pos)); }}/*! \reimp*/void QSplitterHandle::mousePressEvent(QMouseEvent *e){ Q_D(QSplitterHandle); if (e->button() == Qt::LeftButton) mouseOffset = d->pick(e->pos());}/*! \reimp*/void QSplitterHandle::mouseReleaseEvent(QMouseEvent *e){ Q_D(QSplitterHandle); if (!opaqueResize() && e->button() == Qt::LeftButton) { int pos = d->pick(parentWidget()->mapFromGlobal(e->globalPos())) - mouseOffset; d->s->setRubberBand(-1); moveSplitter(pos); }}/*! \reimp*/void QSplitterHandle::paintEvent(QPaintEvent *){ Q_D(QSplitterHandle); QPainter p(this); QStyleOption opt(0); opt.rect = rect(); opt.palette = palette(); if (orientation() == Qt::Horizontal) opt.state = QStyle::State_Horizontal; else opt.state = QStyle::State_None; if (d->hover) opt.state |= QStyle::State_MouseOver; if (isEnabled()) opt.state |= QStyle::State_Enabled; parentWidget()->style()->drawControl(QStyle::CE_Splitter, &opt, &p, d->s);}int QSplitterLayoutStruct::getWidgetSize(Qt::Orientation orient){ if (sizer == -1) { QSize s = widget->sizeHint(); const int presizer = pick(s, orient); const int realsize = pick(widget->size(), orient); if (!s.isValid() || (widget->testAttribute(Qt::WA_Resized) && (realsize > presizer))) { sizer = pick(widget->size(), orient); } else { sizer = presizer; } QSizePolicy p = widget->sizePolicy(); int sf = (orient == Qt::Horizontal) ? p.horizontalStretch() : p.verticalStretch(); if (sf > 1) sizer *= sf; } return sizer;}int QSplitterLayoutStruct::getHandleSize(Qt::Orientation orient){ return pick(handle->sizeHint(), orient);}void QSplitterPrivate::init(){ Q_Q(QSplitter); QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Preferred); if (orient == Qt::Vertical) sp.transpose(); q->setSizePolicy(sp); q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);}void QSplitterPrivate::recalc(bool update){ Q_Q(QSplitter); int n = list.count(); /* Splitter handles before the first visible widget or right before a hidden widget must be hidden. */ bool first = true; for (int i = 0; i < n ; ++i) { QSplitterLayoutStruct *s = list.at(i); s->handle->setHidden(first || s->widget->isHidden()); if (!s->widget->isHidden()) first = false; } int fi = 2 * q->frameWidth(); int maxl = fi; int minl = fi; int maxt = QWIDGETSIZE_MAX; int mint = fi; /* calculate min/max sizes for the whole splitter */ bool empty = true; for (int j = 0; j < n; j++) { QSplitterLayoutStruct *s = list.at(j); if (!s->widget->isHidden()) { empty = false; if (!s->handle->isHidden()) { minl += s->getHandleSize(orient); maxl += s->getHandleSize(orient); } QSize minS = qSmartMinSize(s->widget); minl += pick(minS); maxl += pick(s->widget->maximumSize()); mint = qMax(mint, trans(minS)); int tm = trans(s->widget->maximumSize()); if (tm > 0) maxt = qMin(maxt, tm); } } if (empty) { if (qobject_cast<QSplitter *>(q->parentWidget())) { // nested splitters; be nice maxl = maxt = 0; } else { // QSplitter with no children yet maxl = QWIDGETSIZE_MAX; } } else { maxl = qMin<int>(maxl, QWIDGETSIZE_MAX); } if (maxt < mint) maxt = mint; if (update) { if (orient == Qt::Horizontal) { q->setMaximumSize(maxl, maxt); if (q->isWindow()) q->setMinimumSize(minl,mint); } else { q->setMaximumSize(maxt, maxl); if (q->isWindow()) q->setMinimumSize(mint,minl); } doResize(); q->updateGeometry(); } else { firstShow = true; }}void QSplitterPrivate::doResize(){ Q_Q(QSplitter); QRect r = q->contentsRect(); int n = list.count(); QVector<QLayoutStruct> a(n*2); int i; bool noStretchFactorsSet = true; for (i = 0; i < n; ++i) { QSizePolicy p = list.at(i)->widget->sizePolicy(); int sf = orient == Qt::Horizontal ? p.horizontalStretch() : p.verticalStretch(); if (sf != 0) { noStretchFactorsSet = false; break; } } int j=0; for (i = 0; i < n; ++i) { QSplitterLayoutStruct *s = list.at(i);#ifdef QSPLITTER_DEBUG qDebug("widget %d hidden: %d collapsed: %d handle hidden: %d", i, s->widget->isHidden(), s->collapsed, s->handle->isHidden());#endif a[j].init(); if (s->handle->isHidden()) { a[j].maximumSize = 0; } else { a[j].sizeHint = a[j].minimumSize = a[j].maximumSize = s->getHandleSize(orient); a[j].empty = false; } ++j; a[j].init(); if (s->widget->isHidden() || s->collapsed) { a[j].maximumSize = 0; } else { a[j].minimumSize = pick(qSmartMinSize(s->widget)); a[j].maximumSize = pick(s->widget->maximumSize()); a[j].empty = false; bool stretch = noStretchFactorsSet; if (!stretch) { QSizePolicy p = s->widget->sizePolicy(); int sf = orient == Qt::Horizontal ? p.horizontalStretch() : p.verticalStretch(); stretch = (sf != 0); } if (stretch) { a[j].stretch = s->getWidgetSize(orient); a[j].sizeHint = a[j].minimumSize; a[j].expansive = true; } else { a[j].sizeHint = s->getWidgetSize(orient);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -