From 117b9bb3d576d9974b3d05174d1fccf98e47cb22 Mon Sep 17 00:00:00 2001 From: Thierry Bastian Date: Wed, 24 Jun 2009 18:03:28 +0200 Subject: QDockWidget/Toolbars refactor to avoid list copies This also helps understanding the code better. --- src/gui/widgets/qdockarealayout.cpp | 125 ++++++++++++++++----------------- src/gui/widgets/qdockarealayout_p.h | 38 +++++----- src/gui/widgets/qtoolbararealayout.cpp | 36 +++++----- src/gui/widgets/qtoolbararealayout_p.h | 20 +++--- 4 files changed, 110 insertions(+), 109 deletions(-) diff --git a/src/gui/widgets/qdockarealayout.cpp b/src/gui/widgets/qdockarealayout.cpp index cbfa5bf..ea5e323 100644 --- a/src/gui/widgets/qdockarealayout.cpp +++ b/src/gui/widgets/qdockarealayout.cpp @@ -994,15 +994,15 @@ void QDockAreaLayoutInfo::unnest(int index) } } -void QDockAreaLayoutInfo::remove(QList path) +void QDockAreaLayoutInfo::remove(const QList &path) { Q_ASSERT(!path.isEmpty()); if (path.count() > 1) { - int index = path.takeFirst(); + const int index = path.first(); QDockAreaLayoutItem &item = item_list[index]; Q_ASSERT(item.subinfo != 0); - item.subinfo->remove(path); + item.subinfo->remove(path.mid(1)); unnest(index); } else { int index = path.first(); @@ -1010,18 +1010,18 @@ void QDockAreaLayoutInfo::remove(QList path) } } -QLayoutItem *QDockAreaLayoutInfo::plug(QList path) +QLayoutItem *QDockAreaLayoutInfo::plug(const QList &path) { Q_ASSERT(!path.isEmpty()); - int index = path.takeFirst(); + int index = path.first(); if (index < 0) index = -index - 1; - if (!path.isEmpty()) { + if (path.count() > 1) { const QDockAreaLayoutItem &item = item_list.at(index); Q_ASSERT(item.subinfo != 0); - return item.subinfo->plug(path); + return item.subinfo->plug(path.mid(1)); } QDockAreaLayoutItem &item = item_list[index]; @@ -1059,18 +1059,17 @@ QLayoutItem *QDockAreaLayoutInfo::plug(QList path) return item.widgetItem; } -QLayoutItem *QDockAreaLayoutInfo::unplug(QList path) +QLayoutItem *QDockAreaLayoutInfo::unplug(const QList &path) { Q_ASSERT(!path.isEmpty()); + const int index = path.first(); if (path.count() > 1) { - int index = path.takeFirst(); const QDockAreaLayoutItem &item = item_list.at(index); Q_ASSERT(item.subinfo != 0); - return item.subinfo->unplug(path); + return item.subinfo->unplug(path.mid(1)); } - int index = path.first(); QDockAreaLayoutItem &item = item_list[index]; int prev = this->prev(index); int next = this->next(index); @@ -1142,12 +1141,12 @@ static QRect dockedGeometry(QWidget *widget) return result; } -bool QDockAreaLayoutInfo::insertGap(QList path, QLayoutItem *dockWidgetItem) +bool QDockAreaLayoutInfo::insertGap(const QList &path, QLayoutItem *dockWidgetItem) { Q_ASSERT(!path.isEmpty()); bool insert_tabbed = false; - int index = path.takeFirst(); + int index = path.first(); if (index < 0) { insert_tabbed = true; index = -index - 1; @@ -1155,7 +1154,7 @@ bool QDockAreaLayoutInfo::insertGap(QList path, QLayoutItem *dockWidgetItem // dump(qDebug() << "insertGap() before:" << index << tabIndex, *this, QString()); - if (!path.isEmpty()) { + if (path.count() > 1) { QDockAreaLayoutItem &item = item_list[index]; if (item.subinfo == 0 @@ -1194,8 +1193,7 @@ bool QDockAreaLayoutInfo::insertGap(QList path, QLayoutItem *dockWidgetItem #endif } - bool result = item.subinfo->insertGap(path, dockWidgetItem); - return result; + return item.subinfo->insertGap(path.mid(1), dockWidgetItem); } // create the gap item @@ -1295,16 +1293,16 @@ QDockAreaLayoutInfo *QDockAreaLayoutInfo::info(QWidget *widget) return 0; } -QDockAreaLayoutInfo *QDockAreaLayoutInfo::info(QList path) +QDockAreaLayoutInfo *QDockAreaLayoutInfo::info(const QList &path) { - int index = path.takeFirst(); + int index = path.first(); if (index < 0) index = -index - 1; if (index >= item_list.count()) return this; - if (path.isEmpty() || item_list.at(index).subinfo == 0) + if (path.count() == 1 || item_list.at(index).subinfo == 0) return this; - return item_list.at(index).subinfo->info(path); + return item_list.at(index).subinfo->info(path.mid(1)); } QRect QDockAreaLayoutInfo::itemRect(int index) const @@ -1335,17 +1333,18 @@ QRect QDockAreaLayoutInfo::itemRect(int index) const return result; } -QRect QDockAreaLayoutInfo::itemRect(QList path) const +QRect QDockAreaLayoutInfo::itemRect(const QList &path) const { Q_ASSERT(!path.isEmpty()); + const int index = path.first(); if (path.count() > 1) { - const QDockAreaLayoutItem &item = item_list.at(path.takeFirst()); + const QDockAreaLayoutItem &item = item_list.at(index); Q_ASSERT(item.subinfo != 0); - return item.subinfo->itemRect(path); + return item.subinfo->itemRect(path.mid(1)); } - return itemRect(path.first()); + return itemRect(index); } QRect QDockAreaLayoutInfo::separatorRect(int index) const @@ -1367,16 +1366,17 @@ QRect QDockAreaLayoutInfo::separatorRect(int index) const return QRect(pos, s); } -QRect QDockAreaLayoutInfo::separatorRect(QList path) const +QRect QDockAreaLayoutInfo::separatorRect(const QList &path) const { Q_ASSERT(!path.isEmpty()); + const int index = path.first(); if (path.count() > 1) { - const QDockAreaLayoutItem &item = item_list.at(path.takeFirst()); + const QDockAreaLayoutItem &item = item_list.at(index); Q_ASSERT(item.subinfo != 0); - return item.subinfo->separatorRect(path); + return item.subinfo->separatorRect(path.mid(1)); } - return separatorRect(path.first()); + return separatorRect(index); } QList QDockAreaLayoutInfo::findSeparator(const QPoint &_pos) const @@ -1702,15 +1702,16 @@ void QDockAreaLayoutInfo::split(int index, Qt::Orientation orientation, } } -QDockAreaLayoutItem &QDockAreaLayoutInfo::item(QList path) +QDockAreaLayoutItem &QDockAreaLayoutInfo::item(const QList &path) { Q_ASSERT(!path.isEmpty()); + const int index = path.first(); if (path.count() > 1) { - QDockAreaLayoutItem &item = item_list[path.takeFirst()]; + const QDockAreaLayoutItem &item = item_list.at(index); Q_ASSERT(item.subinfo != 0); - return item.subinfo->item(path); + return item.subinfo->item(path.mid(1)); } - return item_list[path.first()]; + return item_list[index]; } QLayoutItem *QDockAreaLayoutInfo::itemAt(int *x, int index) const @@ -2474,37 +2475,37 @@ QDockAreaLayoutInfo *QDockAreaLayout::info(QWidget *widget) return 0; } -QDockAreaLayoutInfo *QDockAreaLayout::info(QList path) +QDockAreaLayoutInfo *QDockAreaLayout::info(const QList &path) { Q_ASSERT(!path.isEmpty()); - int index = path.takeFirst(); + const int index = path.first(); Q_ASSERT(index >= 0 && index < QInternal::DockCount); - if (path.isEmpty()) + if (path.count() == 1) return &docks[index]; - return docks[index].info(path); + return docks[index].info(path.mid(1)); } -const QDockAreaLayoutInfo *QDockAreaLayout::info(QList path) const +const QDockAreaLayoutInfo *QDockAreaLayout::info(const QList &path) const { return const_cast(this)->info(path); } -QDockAreaLayoutItem &QDockAreaLayout::item(QList path) +QDockAreaLayoutItem &QDockAreaLayout::item(const QList &path) { Q_ASSERT(!path.isEmpty()); - int index = path.takeFirst(); + const int index = path.first(); Q_ASSERT(index >= 0 && index < QInternal::DockCount); - return docks[index].item(path); + return docks[index].item(path.mid(1)); } -QRect QDockAreaLayout::itemRect(QList path) const +QRect QDockAreaLayout::itemRect(const QList &path) const { Q_ASSERT(!path.isEmpty()); - int index = path.takeFirst(); + const int index = path.first(); Q_ASSERT(index >= 0 && index < QInternal::DockCount); - return docks[index].itemRect(path); + return docks[index].itemRect(path.mid(1)); } QRect QDockAreaLayout::separatorRect(int index) const @@ -2528,49 +2529,49 @@ QRect QDockAreaLayout::separatorRect(int index) const return QRect(); } -QRect QDockAreaLayout::separatorRect(QList path) const +QRect QDockAreaLayout::separatorRect(const QList &path) const { Q_ASSERT(!path.isEmpty()); - int index = path.takeFirst(); + const int index = path.first(); Q_ASSERT(index >= 0 && index < QInternal::DockCount); - if (path.isEmpty()) + if (path.count() == 1) return separatorRect(index); else - return docks[index].separatorRect(path); + return docks[index].separatorRect(path.mid(1)); } -bool QDockAreaLayout::insertGap(QList path, QLayoutItem *dockWidgetItem) +bool QDockAreaLayout::insertGap(const QList &path, QLayoutItem *dockWidgetItem) { Q_ASSERT(!path.isEmpty()); - int index = path.takeFirst(); + const int index = path.first(); Q_ASSERT(index >= 0 && index < QInternal::DockCount); - return docks[index].insertGap(path, dockWidgetItem); + return docks[index].insertGap(path.mid(1), dockWidgetItem); } -QLayoutItem *QDockAreaLayout::plug(QList path) +QLayoutItem *QDockAreaLayout::plug(const QList &path) { Q_ASSERT(!path.isEmpty()); - int index = path.takeFirst(); + const int index = path.first(); Q_ASSERT(index >= 0 && index < QInternal::DockCount); - return docks[index].plug(path); + return docks[index].plug(path.mid(1)); } -QLayoutItem *QDockAreaLayout::unplug(QList path) +QLayoutItem *QDockAreaLayout::unplug(const QList &path) { Q_ASSERT(!path.isEmpty()); - int index = path.takeFirst(); + const int index = path.first(); Q_ASSERT(index >= 0 && index < QInternal::DockCount); - return docks[index].unplug(path); + return docks[index].unplug(path.mid(1)); } -void QDockAreaLayout::remove(QList path) +void QDockAreaLayout::remove(const QList &path) { Q_ASSERT(!path.isEmpty()); - int index = path.takeFirst(); + const int index = path.first(); Q_ASSERT(index >= 0 && index < QInternal::DockCount); - docks[index].remove(path); + docks[index].remove(path.mid(1)); } static inline int qMin(int i1, int i2, int i3) { return qMin(i1, qMin(i2, i3)); } @@ -3111,14 +3112,12 @@ QRegion QDockAreaLayout::separatorRegion() const return result; } -int QDockAreaLayout::separatorMove(QList separator, const QPoint &origin, +int QDockAreaLayout::separatorMove(const QList &separator, const QPoint &origin, const QPoint &dest) { int delta = 0; int index = separator.last(); - - if (separator.count() > 1) { QDockAreaLayoutInfo *info = this->info(separator); delta = pick(info->o, dest - origin); @@ -3255,7 +3254,7 @@ QSet QDockAreaLayout::usedSeparatorWidgets() const return result; } -QRect QDockAreaLayout::gapRect(QList path) const +QRect QDockAreaLayout::gapRect(const QList &path) const { const QDockAreaLayoutInfo *info = this->info(path); if (info == 0) diff --git a/src/gui/widgets/qdockarealayout_p.h b/src/gui/widgets/qdockarealayout_p.h index 771aa52..543a201 100644 --- a/src/gui/widgets/qdockarealayout_p.h +++ b/src/gui/widgets/qdockarealayout_p.h @@ -136,18 +136,18 @@ public: QSize sizeHint() const; QSize size() const; - bool insertGap(QList path, QLayoutItem *dockWidgetItem); - QLayoutItem *plug(QList path); - QLayoutItem *unplug(QList path); + bool insertGap(const QList &path, QLayoutItem *dockWidgetItem); + QLayoutItem *plug(const QList &path); + QLayoutItem *unplug(const QList &path); enum TabMode { NoTabs, AllowTabs, ForceTabs }; QList gapIndex(const QPoint &pos, bool nestingEnabled, TabMode tabMode) const; - void remove(QList path); + void remove(const QList &path); void unnest(int index); void split(int index, Qt::Orientation orientation, QLayoutItem *dockWidgetItem); void tab(int index, QLayoutItem *dockWidgetItem); - QDockAreaLayoutItem &item(QList path); - QDockAreaLayoutInfo *info(QList path); + QDockAreaLayoutItem &item(const QList &path); + QDockAreaLayoutInfo *info(const QList &path); QDockAreaLayoutInfo *info(QWidget *widget); enum { // sentinel values used to validate state data @@ -162,9 +162,9 @@ public: bool expansive(Qt::Orientation o) const; int changeSize(int index, int size, bool below); QRect itemRect(int index) const; - QRect itemRect(QList path) const; + QRect itemRect(const QList &path) const; QRect separatorRect(int index) const; - QRect separatorRect(QList path) const; + QRect separatorRect(const QList &path) const; void clear(); bool isEmpty() const; @@ -246,18 +246,18 @@ public: QList gapIndex(const QPoint &pos) const; QList findSeparator(const QPoint &pos) const; - QDockAreaLayoutItem &item(QList path); - QDockAreaLayoutInfo *info(QList path); - const QDockAreaLayoutInfo *info(QList path) const; + QDockAreaLayoutItem &item(const QList &path); + QDockAreaLayoutInfo *info(const QList &path); + const QDockAreaLayoutInfo *info(const QList &path) const; QDockAreaLayoutInfo *info(QWidget *widget); - QRect itemRect(QList path) const; + QRect itemRect(const QList &path) const; QRect separatorRect(int index) const; - QRect separatorRect(QList path) const; + QRect separatorRect(const QList &path) const; - bool insertGap(QList path, QLayoutItem *dockWidgetItem); - QLayoutItem *plug(QList path); - QLayoutItem *unplug(QList path); - void remove(QList path); + bool insertGap(const QList &path, QLayoutItem *dockWidgetItem); + QLayoutItem *plug(const QList &path); + QLayoutItem *unplug(const QList &path); + void remove(const QList &path); void fitLayout(); @@ -277,7 +277,7 @@ public: void paintSeparators(QPainter *p, QWidget *widget, const QRegion &clip, const QPoint &mouse) const; QRegion separatorRegion() const; - int separatorMove(QList separator, const QPoint &origin, const QPoint &dest); + int separatorMove(const QList &separator, const QPoint &origin, const QPoint &dest); void updateSeparatorWidgets() const; QLayoutItem *itemAt(int *x, int index) const; @@ -289,7 +289,7 @@ public: void setGrid(QVector *ver_struct_list, QVector *hor_struct_list); - QRect gapRect(QList path) const; + QRect gapRect(const QList &path) const; void keepSize(QDockWidget *w); diff --git a/src/gui/widgets/qtoolbararealayout.cpp b/src/gui/widgets/qtoolbararealayout.cpp index 8a10355..db2afd6 100644 --- a/src/gui/widgets/qtoolbararealayout.cpp +++ b/src/gui/widgets/qtoolbararealayout.cpp @@ -526,9 +526,10 @@ QList QToolBarAreaLayoutInfo::gapIndex(const QPoint &pos) const return QList(); } -bool QToolBarAreaLayoutInfo::insertGap(QList path, QLayoutItem *item) +bool QToolBarAreaLayoutInfo::insertGap(const QList &path, QLayoutItem *item) { - int j = path.at(0); + Q_ASSERT(path.count() == 2); + int j = path.first(); if (j == lines.count()) lines.append(QToolBarAreaLayoutLine(o)); @@ -570,8 +571,9 @@ void QToolBarAreaLayoutInfo::clear() rect = QRect(); } -QRect QToolBarAreaLayoutInfo::itemRect(QList path) const +QRect QToolBarAreaLayoutInfo::itemRect(const QList &path) const { + Q_ASSERT(path.count() == 2); int j = path.at(0); int k = path.at(1); @@ -623,10 +625,8 @@ QRect QToolBarAreaLayoutInfo::appendLineDropRect() const ** QToolBarAreaLayout */ -QToolBarAreaLayout::QToolBarAreaLayout(QMainWindow *win) +QToolBarAreaLayout::QToolBarAreaLayout(const QMainWindow *win) : mainWindow(win), visible(true) { - visible = true; - mainWindow = win; for (int i = 0; i < QInternal::DockCount; ++i) { QInternal::DockPosition pos = static_cast(i); docks[i] = QToolBarAreaLayoutInfo(pos); @@ -1064,16 +1064,17 @@ QList QToolBarAreaLayout::currentGapIndex() const return QList(); } -bool QToolBarAreaLayout::insertGap(QList path, QLayoutItem *item) +bool QToolBarAreaLayout::insertGap(const QList &path, QLayoutItem *item) { - Q_ASSERT(!path.isEmpty()); - int i = path.takeFirst(); + Q_ASSERT(path.count() == 3); + const int i = path.first(); Q_ASSERT(i >= 0 && i < QInternal::DockCount); - return docks[i].insertGap(path, item); + return docks[i].insertGap(path.mid(1), item); } -void QToolBarAreaLayout::remove(QList path) +void QToolBarAreaLayout::remove(const QList &path) { + Q_ASSERT(path.count() == 3); docks[path.at(0)].lines[path.at(1)].toolBarItems.removeAt(path.at(2)); } @@ -1104,7 +1105,7 @@ void QToolBarAreaLayout::clear() rect = QRect(); } -QToolBarAreaLayoutItem &QToolBarAreaLayout::item(QList path) +QToolBarAreaLayoutItem &QToolBarAreaLayout::item(const QList &path) { Q_ASSERT(path.count() == 3); @@ -1116,18 +1117,18 @@ QToolBarAreaLayoutItem &QToolBarAreaLayout::item(QList path) return line.toolBarItems[path.at(2)]; } -QRect QToolBarAreaLayout::itemRect(QList path) const +QRect QToolBarAreaLayout::itemRect(const QList &path) const { - int i = path.takeFirst(); + const int i = path.first(); - QRect r = docks[i].itemRect(path); + QRect r = docks[i].itemRect(path.mid(1)); if (docks[i].o == Qt::Horizontal) r = QStyle::visualRect(mainWindow->layoutDirection(), docks[i].rect, r); return r; } -QLayoutItem *QToolBarAreaLayout::plug(QList path) +QLayoutItem *QToolBarAreaLayout::plug(const QList &path) { QToolBarAreaLayoutItem &item = this->item(path); Q_ASSERT(item.gap); @@ -1136,9 +1137,10 @@ QLayoutItem *QToolBarAreaLayout::plug(QList path) return item.widgetItem; } -QLayoutItem *QToolBarAreaLayout::unplug(QList path, QToolBarAreaLayout *other) +QLayoutItem *QToolBarAreaLayout::unplug(const QList &path, QToolBarAreaLayout *other) { //other needs to be update as well + Q_ASSERT(path.count() == 3); QToolBarAreaLayoutItem &item = this->item(path); //update the leading space here diff --git a/src/gui/widgets/qtoolbararealayout_p.h b/src/gui/widgets/qtoolbararealayout_p.h index a44fbc7..636c9ca 100644 --- a/src/gui/widgets/qtoolbararealayout_p.h +++ b/src/gui/widgets/qtoolbararealayout_p.h @@ -174,9 +174,9 @@ public: void moveToolBar(QToolBar *toolbar, int pos); QList gapIndex(const QPoint &pos) const; - bool insertGap(QList path, QLayoutItem *item); + bool insertGap(const QList &path, QLayoutItem *item); void clear(); - QRect itemRect(QList path) const; + QRect itemRect(const QList &path) const; QRect appendLineDropRect() const; QRect rect; @@ -194,11 +194,11 @@ public: }; QRect rect; - QMainWindow *mainWindow; + const QMainWindow *mainWindow; QToolBarAreaLayoutInfo docks[4]; bool visible; - QToolBarAreaLayout(QMainWindow *win); + QToolBarAreaLayout(const QMainWindow *win); QRect fitLayout(); @@ -230,14 +230,14 @@ public: QList indexOf(QWidget *toolBar) const; QList gapIndex(const QPoint &pos) const; QList currentGapIndex() const; - bool insertGap(QList path, QLayoutItem *item); - void remove(QList path); + bool insertGap(const QList &path, QLayoutItem *item); + void remove(const QList &path); void remove(QLayoutItem *item); void clear(); - QToolBarAreaLayoutItem &item(QList path); - QRect itemRect(QList path) const; - QLayoutItem *plug(QList path); - QLayoutItem *unplug(QList path, QToolBarAreaLayout *other); + QToolBarAreaLayoutItem &item(const QList &path); + QRect itemRect(const QList &path) const; + QLayoutItem *plug(const QList &path); + QLayoutItem *unplug(const QList &path, QToolBarAreaLayout *other); void saveState(QDataStream &stream) const; bool restoreState(QDataStream &stream, const QList &toolBars, uchar tmarker, bool pre43, bool testing = false); -- cgit v0.12