📄 q3dockarea.cpp
字号:
}/*! \class Q3DockArea q3dockarea.h \brief The Q3DockArea class manages and lays out Q3DockWindows. \compat A Q3DockArea is a container which manages a list of \l{Q3DockWindow}s which it lays out within its area. In cooperation with the \l{Q3DockWindow}s it is responsible for the docking and undocking of \l{Q3DockWindow}s and moving them inside the dock area. Q3DockAreas also handle the wrapping of \l{Q3DockWindow}s to fill the available space as compactly as possible. Q3DockAreas can contain Q3ToolBars since Q3ToolBar is a Q3DockWindow subclass. QMainWindow contains four Q3DockAreas which you can use for your Q3ToolBars and Q3DockWindows, so in most situations you do not need to use the Q3DockArea class directly. Although QMainWindow contains support for its own dock areas it isn't convenient for adding new Q3DockAreas. If you need to create your own dock areas we suggest that you create a subclass of QWidget and add your Q3DockAreas to your subclass. \img qmainwindow-qdockareas.png QMainWindow's Q3DockAreas \target lines \e Lines. Q3DockArea uses the concept of lines. A line is a horizontal region which may contain dock windows side-by-side. A dock area may have room for more than one line. When dock windows are docked into a dock area they are usually added at the right hand side of the top-most line that has room (unless manually placed by the user). When users move dock windows they may leave empty lines or gaps in non-empty lines. Qt::Dock windows can be lined up to minimize wasted space using the lineUp() function. The Q3DockArea class maintains a position list of all its child dock windows. Qt::Dock windows are added to a dock area from position 0 onwards. Qt::Dock windows are laid out sequentially in position order from left to right, and in the case of multiple lines of dock windows, from top to bottom. If a dock window is floated it still retains its position since this is where the window will return if the user double clicks its caption. A dock window's position can be determined with hasDockWindow(). The position can be changed with moveDockWindow(). To dock or undock a dock window use Q3DockWindow::dock() and Q3DockWindow::undock() respectively. If you want to control which dock windows can dock in a dock area use setAcceptDockWindow(). To see if a dock area contains a particular dock window use \l{hasDockWindow()}; to see how many dock windows a dock area contains use count(). The streaming operators can write the positions of the dock windows in the dock area to a QTextStream. The positions can be read back later to restore the saved positions. Save the positions to a QTextStream: \code ts << *myDockArea; \endcode Restore the positions from a QTextStream: \code ts >> *myDockArea; \endcode*//*! \property Q3DockArea::handlePosition \brief where the dock window splitter handle is placed in the dock area The default position is \c Normal.*//*! \property Q3DockArea::orientation \brief the dock area's orientation There is no default value; the orientation is specified in the constructor.*//*! \enum Q3DockArea::HandlePosition A dock window has two kinds of handles, the dock window handle used for dragging the dock window, and the splitter handle used to resize the dock window in relation to other dock windows using a splitter. (The splitter handle is only visible for docked windows.) This enum specifies where the dock window splitter handle is placed in the dock area. \value Normal The splitter handles of dock windows are placed at the right or bottom. \value Reverse The splitter handles of dock windows are placed at the left or top.*//*! Constructs a Q3DockArea with orientation \a o, HandlePosition \a h, parent \a parent and called \a name.*/Q3DockArea::Q3DockArea(Qt::Orientation o, HandlePosition h, QWidget *parent, const char *name) : QWidget(parent, name), orient(o), layout(0), hPos(h){ layout = new Q3DockAreaLayout(this, o, &dockWindows, 0, 0, "toollayout"); installEventFilter(this);}/*! Destroys the dock area and all the dock windows docked in the dock area. Does not affect any floating dock windows or dock windows in other dock areas, even if they first appeared in this dock area. Floating dock windows are effectively top level windows and are not child windows of the dock area. When a floating dock window is docked (dragged into a dock area) its parent becomes the dock area.*/Q3DockArea::~Q3DockArea(){ while (!dockWindows.isEmpty()) delete dockWindows.takeFirst();}/*! Moves the Q3DockWindow \a w within the dock area. If \a w is not already docked in this area, \a w is docked first. If \a index is -1 or larger than the number of docked widgets, \a w is appended at the end, otherwise it is inserted at the position \a index.*/void Q3DockArea::moveDockWindow(Q3DockWindow *w, int index){ invalidateFixedSizes(); Q3DockWindow *dockWindow = 0; int dockWindowIndex = findDockWindow(w); if (dockWindowIndex == -1) { dockWindow = w; bool vis = dockWindow->isVisible(); dockWindow->setParent(this); dockWindow->move(0, 0); if(vis) dockWindow->show(); w->installEventFilter(this); updateLayout(); setSizePolicy(QSizePolicy(orientation() == Qt::Horizontal ? QSizePolicy::Expanding : QSizePolicy::Minimum, orientation() == Qt::Vertical ? QSizePolicy::Expanding : QSizePolicy::Minimum)); dockWindows.append(w); } else { if (w->parent() != this) { bool vis = w->isVisible(); w->setParent(this); w->move(0, 0); if(vis) w->show(); } if (index == -1) { dockWindows.removeAll(w); dockWindows.append(w); } } w->dockArea = this; w->curPlace = Q3DockWindow::InDock; w->updateGui(); if (index != -1 && index < (int)dockWindows.count()) { dockWindows.removeAll(w); dockWindows.insert(index, w); }}/*! Returns true if the dock area contains the dock window \a w; otherwise returns false. If \a index is not 0 it will be set as follows: if the dock area contains the dock window *\a{index} is set to \a w's index position; otherwise *\a{index} is set to -1.*/bool Q3DockArea::hasDockWindow(Q3DockWindow *w, int *index){ int i = dockWindows.indexOf(w); if (index) *index = i; return i != -1;}int Q3DockArea::lineOf(int index){ QList<Q3DockWindow *> lineStarts = layout->lineStarts(); int i = 0; for (; i < lineStarts.size(); ++i) { Q3DockWindow *w = lineStarts.at(i); if (dockWindows.indexOf(w) >= index) return i; } return i;}/*! \overload Moves the dock window \a w inside the dock area where \a p is the new position (in global screen coordinates), \a r is the suggested rectangle of the dock window and \a swap specifies whether or not the orientation of the docked widget needs to be changed. This function is used internally by Q3DockWindow. You shouldn't need to call it yourself.*/void Q3DockArea::moveDockWindow(Q3DockWindow *w, const QPoint &p, const QRect &r, bool swap){ invalidateFixedSizes(); int mse = -10; bool hasResizable = false; for (int i = 0; i < dockWindows.size(); ++i) { Q3DockWindow *dw = dockWindows.at(i); if (dw->isHidden()) continue; if (dw->isResizeEnabled()) hasResizable = true; if (orientation() != Qt::Horizontal) mse = qMax(qMax(dw->fixedExtent().width(), dw->width()), mse); else mse = qMax(qMax(dw->fixedExtent().height(), dw->height()), mse); } if (!hasResizable && w->isResizeEnabled()) { if (orientation() != Qt::Horizontal) mse = qMax(w->fixedExtent().width(), mse); else mse = qMax(w->fixedExtent().height(), mse); } Q3DockWindow *dockWindow = 0; int dockWindowIndex = findDockWindow(w); QList<Q3DockWindow *> lineStarts = layout->lineStarts(); QList<QRect> lines = layout->lineList(); bool wasAloneInLine = false; QPoint pos = mapFromGlobal(p); int line = lineOf(dockWindowIndex); QRect lr; if (line < lines.size()) lr = lines.at(line); if (dockWindowIndex != -1) { if (lineStarts.contains(w) && ((dockWindowIndex < dockWindows.count() - 1 && lineStarts.contains(dockWindows.at(dockWindowIndex + 1))) || dockWindowIndex == dockWindows.count() - 1)) wasAloneInLine = true; dockWindow = dockWindows.takeAt(dockWindowIndex); if (!wasAloneInLine) { // only do the pre-layout if the widget isn't the only one in its line if (lineStarts.contains(dockWindow) && dockWindowIndex < dockWindows.count()) dockWindows.at(dockWindowIndex)->setNewLine(true); layout->layoutItems(QRect(0, 0, width(), height()), true); } } else { dockWindow = w; bool vis = dockWindow->isVisible(); dockWindow->setParent(this); dockWindow->move(0, 0); if(vis) dockWindow->show(); if (swap) dockWindow->resize(dockWindow->height(), dockWindow->width()); w->installEventFilter(this); } lineStarts = layout->lineStarts(); lines = layout->lineList(); QRect rect = QRect(mapFromGlobal(r.topLeft()), r.size()); if (orientation() == Qt::Horizontal && QApplication::reverseLayout()) { rect = QRect(width() - rect.x() - rect.width(), rect.y(), rect.width(), rect.height()); pos.rx() = width() - pos.x(); } dockWindow->setOffset(point_pos(rect.topLeft(), orientation())); if (orientation() == Qt::Horizontal) { int offs = dockWindow->offset(); if (width() - offs < dockWindow->minimumWidth()) dockWindow->setOffset(width() - dockWindow->minimumWidth()); } else { int offs = dockWindow->offset(); if (height() - offs < dockWindow->minimumHeight()) dockWindow->setOffset(height() - dockWindow->minimumHeight()); } if (dockWindows.isEmpty()) { dockWindows.append(dockWindow); } else { int dockLine = -1; bool insertLine = false; int i = 0; QRect lineRect; // find the line which we touched with the mouse for (QList<QRect>::Iterator it = lines.begin(); it != lines.end(); ++it, ++i) { if (point_pos(pos, orientation(), true) >= point_pos((*it).topLeft(), orientation(), true) && point_pos(pos, orientation(), true) <= point_pos((*it).topLeft(), orientation(), true) + size_extent((*it).size(), orientation(), true)) { dockLine = i; lineRect = *it; break; } } if (dockLine == -1) { // outside the dock... insertLine = true; if (point_pos(pos, orientation(), true) < 0) // insert as first line dockLine = 0; else dockLine = (int)lines.count(); // insert after the last line ### size_t/int cast } else { // inside the dock (we have found a dockLine) if (point_pos(pos, orientation(), true) < point_pos(lineRect.topLeft(), orientation(), true) + 4) { // mouse was at the very beginning of the line insertLine = true; // insert a new line before that with the docking widget } else if (point_pos(pos, orientation(), true) > point_pos(lineRect.topLeft(), orientation(), true) + size_extent(lineRect.size(), orientation(), true) - 4) { // mouse was at the very and of the line insertLine = true; // insert a line after that with the docking widget dockLine++; } } if (!insertLine && wasAloneInLine && lr.contains(pos)) // if we are alone in a line and just moved in there, re-insert it insertLine = true;#if defined(QDOCKAREA_DEBUG) qDebug("insert in line %d, and insert that line: %d", dockLine, insertLine); qDebug(" (btw, we have %d lines)", lines.count());#endif Q3DockWindow *dw = 0; if (dockLine >= (int)lines.count()) { // insert after last line dockWindows.append(dockWindow); dockWindow->setNewLine(true);#if defined(QDOCKAREA_DEBUG) qDebug("insert at the end");#endif } else if (dockLine == 0 && insertLine) { // insert before first line dockWindows.insert(0, dockWindow); dockWindows.at(1)->setNewLine(true);#if defined(QDOCKAREA_DEBUG) qDebug("insert at the begin");#endif } else { // insert somewhere in between // make sure each line start has a new line for (int i = 0; i < lineStarts.size(); ++i) { dw = lineStarts.at(i); dw->setNewLine(true); } // find the index of the first widget in the search line int searchLine = dockLine;#if defined(QDOCKAREA_DEBUG) qDebug("search line start of %d", searchLine);#endif Q3DockWindow *lsw = lineStarts.at(searchLine); int index = dockWindows.indexOf(lsw); if (index == -1) { // the linestart widget hasn't been found, try to find it harder if (lsw == w && dockWindowIndex <= dockWindows.count()) index = dockWindowIndex; else index = 0; }#if defined(QDOCKAREA_DEBUG) qDebug(" which starts at %d", index);#endif if (!insertLine) { // if we insert the docking widget in the existing line // find the index for the widget bool inc = true; bool firstTime = true; for (int i = index; i < dockWindows.size(); ++i) { dw = dockWindows.at(i); if (orientation() == Qt::Horizontal) dw->setFixedExtentWidth(-1); else dw->setFixedExtentHeight(-1); if (!firstTime && lineStarts.contains(dw)) // we are in the next line, so break break; if (point_pos(pos, orientation()) < point_pos(fix_pos(dw), orientation()) + size_extent(dw->size(), orientation()) / 2) { inc = false; } if (inc) index++; firstTime = false; }#if defined(QDOCKAREA_DEBUG) qDebug("insert at index: %d", index);#endif // if we insert it just before a widget which has a new line, transfer the newline to the docking widget // but not if we didn't only mave a widget in its line which was alone in the line before if (!(wasAloneInLine && lr.contains(pos)) && index >= 0 && index < dockWindows.count() && dockWindows.at(index)->newLine() && lineOf(index) == dockLine) {#if defined(QDOCKAREA_DEBUG) qDebug("get rid of the old newline and get me one");#endif dockWindows.at(index)->setNewLine(false); dockWindow->setNewLine(true); } else if (wasAloneInLine && lr.contains(pos)) { dockWindow->setNewLine(true); } else { // if we are somewhere in a line, get rid of the newline dockWindow->setNewLine(false); } } else { // insert in a new line, so make sure the dock widget and the widget which will be after it have a newline#if defined(QDOCKAREA_DEBUG) qDebug("insert a new line");#endif if (index < dockWindows.count()) {#if defined(QDOCKAREA_DEBUG) qDebug("give the widget at %d a newline", index);#endif Q3DockWindow* nldw = dockWindows.at(index); if (nldw) nldw->setNewLine(true); }#if defined(QDOCKAREA_DEBUG) qDebug("give me a newline");#endif dockWindow->setNewLine(true); } // finally insert the widget dockWindows.insert(index, dockWindow); } } if (mse != -10 && w->isResizeEnabled()) { if (orientation() != Qt::Horizontal) w->setFixedExtentWidth(qMin(qMax(w->minimumWidth(), mse), w->sizeHint().width())); else w->setFixedExtentHeight(qMin(qMax(w->minimumHeight(), mse), w->sizeHint().height())); } updateLayout(); setSizePolicy(QSizePolicy(orientation() == Qt::Horizontal ? QSizePolicy::Expanding : QSizePolicy::Minimum, orientation() == Qt::Vertical ? QSizePolicy::Expanding : QSizePolicy::Minimum));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -