This commit is contained in:
Sergio Martins
2020-04-27 23:12:55 +01:00
parent e09d66f20f
commit 4dc0da5e75
2 changed files with 17 additions and 136 deletions

View File

@@ -33,7 +33,6 @@
using namespace KDDockWidgets;
using namespace Layouting;
typedef QWidget QWidgetAdapter ; // TODO
bool Anchor::s_isResizing = false;
@@ -47,13 +46,15 @@ static Separator* createSeparator(Anchor *a, QWidget *parent)
return new Separator(a, parent);
}
Anchor::Anchor(Qt::Orientation orientation, Options options, QWidget *hostWidget)
Anchor::Anchor(ItemContainer *parentContainer, Qt::Orientation orientation,
Options options, QWidget *hostWidget)
: QObject(hostWidget)
, m_orientation(orientation)
, m_hostWidget(hostWidget)
, m_separatorWidget(createSeparator(this, m_hostWidget))
, m_options(options)
, m_lazyResizeRubberBand((options & Option::LazyResize) ? new QRubberBand(QRubberBand::Line, hostWidget) : nullptr)
, m_parentContainer(parentContainer)
{
connect(this, &QObject::objectNameChanged, m_separatorWidget, &QObject::setObjectName);
}
@@ -82,32 +83,6 @@ void Anchor::setGeometry(QRect r)
}
}
void Anchor::debug_updateItemNames()
{
// I call this in the unit-tests, when running them on gammaray
m_debug_side1ItemNames.clear();
m_debug_side2ItemNames.clear();
for (Item *item : qAsConst(m_side1Items))
m_debug_side1ItemNames += item->objectName() + QStringLiteral("; ");
for (Item *item : qAsConst(m_side2Items))
m_debug_side2ItemNames += item->objectName() + QStringLiteral("; ");
Q_EMIT debug_itemNamesChanged();
}
QString Anchor::debug_side1ItemNames() const
{
return m_debug_side1ItemNames;
}
QString Anchor::debug_side2ItemNames() const
{
return m_debug_side2ItemNames;
}
Qt::Orientation Anchor::orientation() const
{
return m_orientation;
@@ -135,44 +110,12 @@ int Anchor::position() const
return isVertical() ? topLeft.y() : topLeft.x();
}
bool Anchor::containsItem(const Item *item, Side side) const
{
switch (side) {
case Side1:
return m_side1Items.contains(const_cast<Item *>(item));
case Side2:
return m_side2Items.contains(const_cast<Item *>(item));
default:
Q_ASSERT(false);
return false;
}
}
const QVector<Item *> Anchor::items(Side side) const
{
switch (side) {
case Side1:
return m_side1Items;
case Side2:
return m_side2Items;
default:
Q_ASSERT(false);
return {};
}
}
bool Anchor::isBeingDragged() const
{
return false; // TODO
//return m_layout->anchorBeingDragged() == this;
}
void Anchor::clear()
{
m_side1Items.clear();
m_side2Items.clear();
}
bool Anchor::lazyResizeEnabled() const
{
return m_options & Option::LazyResize;
@@ -201,7 +144,7 @@ void Anchor::setLazyPosition(int pos)
int Anchor::position(QPoint p) const
{
return isVertical() ? p.x() : p.y();
return isVertical() ? p.y() : p.x();
}
void Anchor::onMousePress()
@@ -227,7 +170,7 @@ void Anchor::onMouseReleased()
// m_layout->setAnchorBeingDragged(nullptr); TODO
}
void Anchor::onMouseMoved(QPoint)
void Anchor::onMouseMoved(QPoint pt)
{
if (!isBeingDragged())
return;
@@ -248,22 +191,22 @@ void Anchor::onMouseMoved(QPoint)
}
#endif
// const int positionToGoTo = position(pt);
/*auto bounds = m_layout->boundPositionsForAnchor(this);
const int positionToGoTo = position(pt);
if (positionToGoTo < bounds.first || positionToGoTo > bounds.second) {
// qDebug() << "Out of bounds" << bounds.first << bounds.second << positionToGoTo << "; currentPos" << position() << "; window size" << window()->size();
const int minPos = m_parentContainer->minPosForSeparator(this);
const int maxPos = m_parentContainer->maxPosForSeparator(this);
if (positionToGoTo < minPos || positionToGoTo > maxPos)
return;
}
m_lastMoveDirection = positionToGoTo < position() ? Side1
: (positionToGoTo > position() ? Side2
: Side_None); // Side_None shouldn't happen though.
: Side2); // Last case shouldn't happen though.
if (m_lazyResize)
if (/*m_lazyResize*/ false) // TODO
setLazyPosition(positionToGoTo);
else
setPosition(positionToGoTo);*/
//else
//setPosition(positionToGoTo);
}
void Anchor::onWidgetMoved(int )

View File

@@ -38,51 +38,10 @@ class MultiSplitterLayout;
namespace Layouting {
class Item;
/**
* @brief An anchor is the vertical or horizontal (@ref orientation()) line that has an handle
* so you can resize widgets with your mouse.
* *
* Each anchor has two properties indicating in which anchor it starts and where it ends, @ref from(), to().
* For example, the top static horizontal anchor starts at the left anchor and ends at the right static anchor.
* If this anchor is vertical, then from()/to() return horizontal anchors, and vice-versa.
*
* An anchor has a length, which is to()->pos() - from()->pos(). The length of a vertical anchor is,
* thus, its vertical extent (Likewise for horizontal anchors).
*
* An anchor controls two groups of widgets: side1 and side2 widgets. When an anchor is dragged with mouse
* it will resize those widgets. The widgets always start or end at the position where the anchor lives.
* For vertical anchors, side1 means "the widgets at its left" and side2 means "the widgets at its right",
* Same principle for horizontal anchors, but for top/bottom instead.
* Static anchors only have 1 side with widgets. For example the left static anchor only has widgets at its
* right, so side1Widgets is empty.
* Non-static anchors, always have side1 and side2 widgets. If not then they are considered unneeded
* and are deleted.
*
* Example:
*
* +--------------------+
* | | |
* | | |
* | | |
* | Foo | Bar |
* | | |
* | | |
* +--------------------+
*
* In the above example we have 5 anchors. 4 of them are static (left, right, top, bottom) and there's
* a non-static one, in the middle. It's vertical, and can be dragged left and right, resizing its
* side1Widgets (Foo) and side2Widgets (Bar). This non-static anchors has from=top anchor, and to=bottom anchor.
*
*/
class Anchor : public QObject // clazy:exclude=ctor-missing-parent-argument
{
Q_OBJECT
Q_PROPERTY(QString debug_side1ItemNames READ debug_side1ItemNames NOTIFY debug_itemNamesChanged)
Q_PROPERTY(QString debug_side2ItemNames READ debug_side2ItemNames NOTIFY debug_itemNamesChanged)
Q_PROPERTY(QRect geometry READ geometry NOTIFY geometryChanged)
Q_PROPERTY(Qt::Orientation orientation READ orientation CONSTANT)
public:
@@ -94,16 +53,14 @@ public:
Q_DECLARE_FLAGS(Options, Option);
typedef QVector<Anchor *> List;
explicit Anchor(Qt::Orientation orientation, Options options, QWidget *hostWidget);
explicit Anchor(ItemContainer *parentContainer, Qt::Orientation orientation,
Options options, QWidget *hostWidget);
~Anchor() override;
QWidget *hostWidget() const;
Qt::Orientation orientation() const;
void addItem(Item *, Side);
void addItems(const QVector<Item*> &list, Side);
void removeItem(Item *w);
bool isVertical() const { return m_orientation == Qt::Vertical; }
void setGeometry(int pos, int pos2, int length);
void updatePositionPercentage();
@@ -114,19 +71,9 @@ public:
///@brief returns the separator widget
Separator* separatorWidget() const;
bool isEmpty() const { return !hasItems(Side1) && !hasItems(Side2); }
bool hasItems(Side) const;
bool containsItem(const Item *w, Side side) const;
const QVector<Item*> items(Side side) const;
void setPositionOffset(int);
bool isBeingDragged() const;
///@brief removes the side1 and side2 items. Doesn't delete them
void clear();
bool lazyResizeEnabled() const;
void onMousePress();
@@ -143,21 +90,13 @@ private:
void setGeometry(QRect);
Q_SIGNALS:
void geometryChanged(QRect);
void itemsChanged(Side);
void debug_itemNamesChanged();
public:
int position(QPoint) const;
void updateSize();
void updateItemSizes();
void debug_updateItemNames();
QString debug_side1ItemNames() const;
QString debug_side2ItemNames() const;
QRect geometry() const { return m_geometry; }
const Qt::Orientation m_orientation;
QVector<Item*> m_side1Items;
QVector<Item*> m_side2Items;
// Only set when anchor is moved through mouse. Side1 if going towards left or top, Side2 otherwise.
Layouting::Side m_lastMoveDirection = Side1;
@@ -168,13 +107,12 @@ public:
bool m_initialized = false;
static bool s_isResizing;
QString m_debug_side1ItemNames;
QString m_debug_side2ItemNames;
Separator *const m_separatorWidget;
QRect m_geometry;
int m_lazyPosition = 0;
const Options m_options;
QRubberBand *const m_lazyResizeRubberBand;
ItemContainer *const m_parentContainer;
};
}