⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtoolbarlayout.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    if (expanded || animating || ranOutOfSpace) {        Qt::ToolBarArea area = Qt::TopToolBarArea;        if (QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget()))            area = win->toolBarArea(tb);        QSize hint = sizeHint();        QPoint pos;        rpick(o, pos) = pick(o, rect.bottomRight()) - margin - extensionExtent + 2;        if (area == Qt::LeftToolBarArea || area == Qt::TopToolBarArea)            rperp(o, pos) = perp(o, rect.topLeft()) + margin;        else            rperp(o, pos) = perp(o, rect.bottomRight()) - margin - (perp(o, hint) - 2*margin) + 1;        QSize size;        rpick(o, size) = extensionExtent;        rperp(o, size) = perp(o, hint) - 2*margin;        QRect r(pos, size);        if (o == Qt::Horizontal)            r = QStyle::visualRect(parentWidget()->layoutDirection(), rect, r);        extension->setGeometry(r);        if (extension->isHidden())            extension->show();    } else {        if (!extension->isHidden())            extension->hide();    }}bool QToolBarLayout::layoutActions(const QSize &size){    if (dirty)        updateGeomArray();    QRect rect(0, 0, size.width(), size.height());    QList<QWidget*> showWidgets, hideWidgets;    QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());    QStyle *style = tb->style();    QStyleOptionToolBar opt;    tb->initStyleOption(&opt);    const int handleExtent = movable()            ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0;    const int margin = this->margin();    const int spacing = this->spacing();    const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb);    Qt::Orientation o = tb->orientation();    bool extensionMenuContainsOnlyWidgetActions = true;    int space = pick(o, rect.size()) - 2*margin - handleExtent;    if (space <= 0)        return false;  // nothing to do.    bool ranOutOfSpace = false;    int rows = 0;    int rowPos = perp(o, rect.topLeft()) + margin;    int i = 0;    while (i < items.count()) {        QVector<QLayoutStruct> a = geomArray;        int start = i;        int size = 0;        int prev = -1;        int rowHeight = 0;        int count = 0;        bool expansiveRow = false;        for (; i < items.count(); ++i) {            if (a[i].empty)                continue;            int newSize = size + spacing + a[i].minimumSize;            if (prev != -1 && newSize > space) {                if (rows == 0)                    ranOutOfSpace = true;                // do we have to move the previous item to the next line to make space for                // the extension button?                if (count > 1 && size + spacing + extensionExtent > space)                    i = prev;                break;            }            if (expanded)                rowHeight = qMax(rowHeight, perp(o, items.at(i)->sizeHint()));            expansiveRow = expansiveRow || a[i].expansive;            size = newSize;            prev = i;            ++count;        }        // stretch at the end        a[i].sizeHint = 0;        a[i].maximumSize = QWIDGETSIZE_MAX;        a[i].minimumSize = 0;        a[i].expansive = true;        a[i].stretch = 0;        a[i].empty = true;        qGeomCalc(a, start, i - start + (expansiveRow ? 0 : 1), 0,                    space - (ranOutOfSpace ? (extensionExtent + spacing) : 0),                    spacing);        for (int j = start; j < i; ++j) {            QToolBarItem *item = items.at(j);            if (a[j].empty) {                if (!item->widget()->isHidden())                    hideWidgets << item->widget();                continue;            }            QPoint pos;            rpick(o, pos) = margin + handleExtent + spacing + a[j].pos;            rperp(o, pos) = rowPos;            QSize size;            rpick(o, size) = a[j].size;            if (expanded)                rperp(o, size) = rowHeight;            else                rperp(o, size) = perp(o, rect.size()) - 2*margin;            QRect r(pos, size);            if (o == Qt::Horizontal)                r = QStyle::visualRect(parentWidget()->layoutDirection(), rect, r);            item->setGeometry(r);            if (item->widget()->isHidden())                showWidgets << item->widget();            if (popupMenu && !defaultWidgetAction(item->action))                popupMenu->removeAction(item->action);        }        if (!expanded) {            for (int j = i; j < items.count(); ++j) {                QToolBarItem *item = items.at(j);                if (!item->widget()->isHidden())                    hideWidgets << item->widget();                if (popupMenu) {                    if (!defaultWidgetAction(item->action)) {                        popupMenu->addAction(item->action);                        extensionMenuContainsOnlyWidgetActions = false;                    }                }            }            break;        }        rowPos += rowHeight + spacing;        ++rows;    }    // if we are using a popup menu, not the expadning toolbar effect, we cannot move custom    // widgets into the menu. If only custom widget actions are chopped off, the popup menu    // is empty. So we show the little extension button to show something is chopped off,    // but we make it disabled.    extension->setEnabled(popupMenu == 0 || !extensionMenuContainsOnlyWidgetActions);    // we have to do the show/hide here, because it triggers more calls to setGeometry :(    for (int i = 0; i < showWidgets.count(); ++i)        showWidgets.at(i)->show();    for (int i = 0; i < hideWidgets.count(); ++i)        hideWidgets.at(i)->hide();    return ranOutOfSpace;}QSize QToolBarLayout::expandedSize(const QSize &size) const{    if (dirty)        updateGeomArray();    QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());    QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget());    Qt::Orientation o = tb->orientation();    QStyle *style = tb->style();    QStyleOptionToolBar opt;    tb->initStyleOption(&opt);    const int handleExtent = movable()            ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0;    const int margin = this->margin();    const int spacing = this->spacing();    const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb);    int total_w = 0;    int count = 0;    for (int x = 0; x < items.count(); ++x) {        if (!geomArray[x].empty) {            ++count;            total_w += spacing + geomArray[x].minimumSize;        }    }    if (count == 0)        return QSize(0, 0);    int min_w = pick(o, size);    int rows = (int)sqrt(count + 0.0);    if (rows == 1)        ++rows;      // we want to expand to at least two rows    int space = total_w/rows + spacing + extensionExtent;    space = qMax(space, min_w - 2*margin - handleExtent);    if (win != 0)        space = qMin(space, pick(o, win->size()) - 2*margin - handleExtent);    int w = 0;    int h = 0;    int i = 0;    while (i < items.count()) {        int count = 0;        int size = 0;        int prev = -1;        int rowHeight = 0;        for (; i < items.count(); ++i) {            if (geomArray[i].empty)                continue;            int newSize = size + spacing + geomArray[i].minimumSize;            rowHeight = qMax(rowHeight, perp(o, items.at(i)->sizeHint()));            if (prev != -1 && newSize > space) {                if (count > 1 && size + spacing + extensionExtent > space) {                    size -= spacing + geomArray[prev].minimumSize;                    i = prev;                }                break;            }            size = newSize;            prev = i;            ++count;        }        w = qMax(size, w);        h += rowHeight + spacing;    }    w += 2*margin + handleExtent + spacing + extensionExtent;    w = qMax(w, min_w);    if (win != 0)        w = qMin(w, pick(o, win->size()));    h += 2*margin;    QSize result;    rpick(o, result) = w;    rperp(o, result) = h;    return result;}void QToolBarLayout::setExpanded(bool exp){    if (exp == expanded)        return;    expanded = exp;    extension->setChecked(expanded);    QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());    if (QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget())) {        animating = true;        QMainWindowLayout *layout = qobject_cast<QMainWindowLayout*>(win->layout());        if (expanded) {            tb->raise();        } else {            QList<int> path = layout->layoutState.indexOf(tb);            if (!path.isEmpty()) {                QRect rect = layout->layoutState.itemRect(path);                layoutActions(rect.size());            }        }        layout->layoutState.toolBarAreaLayout.apply(true);    }}QSize QToolBarLayout::minimumSize() const{    if (dirty)        updateGeomArray();    return minSize;}QSize QToolBarLayout::sizeHint() const{    if (dirty)        updateGeomArray();    return hint;}QToolBarItem *QToolBarLayout::createItem(QAction *action){    bool customWidget = false;    bool standardButtonWidget = false;    QWidget *widget = 0;    QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());    if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(action)) {        widget = widgetAction->requestWidget(tb);        if (widget != 0)            customWidget = true;    } else if (action->isSeparator()) {        QToolBarSeparator *sep = new QToolBarSeparator(tb);        connect(tb, SIGNAL(orientationChanged(Qt::Orientation)),                sep, SLOT(setOrientation(Qt::Orientation)));        widget = sep;    }    if (!widget) {        QToolButton *button = new QToolButton(tb);        button->setAutoRaise(true);        button->setFocusPolicy(Qt::NoFocus);        button->setIconSize(tb->iconSize());        button->setToolButtonStyle(tb->toolButtonStyle());        QObject::connect(tb, SIGNAL(iconSizeChanged(QSize)),                         button, SLOT(setIconSize(QSize)));        QObject::connect(tb, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),                         button, SLOT(setToolButtonStyle(Qt::ToolButtonStyle)));        button->setDefaultAction(action);        QObject::connect(button, SIGNAL(triggered(QAction*)), tb, SIGNAL(actionTriggered(QAction*)));        widget = button;        standardButtonWidget = true;    }    widget->hide();    QToolBarItem *result = new QToolBarItem(widget);    if (standardButtonWidget)        result->setAlignment(Qt::AlignJustify);    result->customWidget = customWidget;    result->action = action;    return result;}QRect QToolBarLayout::handleRect() const{    return handRect;}#endif // QT_NO_TOOLBAR

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -