Qt-Advanced-Docking-System/AdvancedDockingSystem/src/ContainerWidget.cpp

839 lines
21 KiB
C++
Raw Normal View History

#include "ads/ContainerWidget.h"
#include "ads/Internal.h"
#include "ads/SectionTitleWidget.h"
#include "ads/SectionContentWidget.h"
2015-12-09 19:21:38 +08:00
#include <QPaintEvent>
#include <QPainter>
#include <QContextMenuEvent>
#include <QMenu>
#include <QSplitter>
#include <QDataStream>
#include <QtGlobal>
2015-12-09 19:21:38 +08:00
ADS_NAMESPACE_BEGIN
// Static Helper //////////////////////////////////////////////////////
static QSplitter* newSplitter(Qt::Orientation orientation = Qt::Horizontal, QWidget* parent = 0)
2015-12-09 19:21:38 +08:00
{
QSplitter* s = new QSplitter(orientation, parent);
2015-12-09 19:21:38 +08:00
s->setChildrenCollapsible(false);
s->setOpaqueResize(false);
return s;
}
///////////////////////////////////////////////////////////////////////
ContainerWidget::ContainerWidget(QWidget *parent) :
QFrame(parent),
_mainLayout(NULL),
_orientation(Qt::Horizontal),
_splitter(NULL)
{
_mainLayout = new QGridLayout();
_mainLayout->setContentsMargins(9, 9, 9, 9);
_mainLayout->setSpacing(0);
2015-12-09 19:21:38 +08:00
setLayout(_mainLayout);
}
Qt::Orientation ContainerWidget::orientation() const
{
return _orientation;
}
void ContainerWidget::setOrientation(Qt::Orientation orientation)
{
if (_orientation != orientation)
{
_orientation = orientation;
emit orientationChanged();
}
}
SectionWidget* ContainerWidget::addSectionContent(const SectionContent::RefPtr& sc, SectionWidget* sw, DropArea area)
2015-12-09 19:21:38 +08:00
{
if (!sw)
{
if (_sections.isEmpty())
{ // Create default section
sw = newSectionWidget();
addSection(sw);
}
else if (area == CenterDropArea)
// Use existing default section
sw = _sections.first();
}
// Drop it based on "area"
InternalContentData data;
data.content = sc;
data.titleWidget = new SectionTitleWidget(sc, NULL);
data.contentWidget = new SectionContentWidget(sc, NULL);
return dropContent(data, sw, area, false);
}
bool ContainerWidget::showSectionContent(const SectionContent::RefPtr& sc)
{
// Search SC in floatings
for (int i = 0; i < _floatings.count(); ++i)
{
const bool found = _floatings.at(i)->content()->uid() == sc->uid();
if (!found)
continue;
_floatings.at(i)->setVisible(true);
return true;
}
// Search SC in hidden sections
// Try to show them in the last position, otherwise simply append
// it to the first section (or create a new section?)
if (_hiddenSectionContents.contains(sc->uid()))
{
const HiddenSectionItem hsi = _hiddenSectionContents.take(sc->uid());
hsi.data.titleWidget->setVisible(true);
hsi.data.contentWidget->setVisible(true);
SectionWidget* sw = NULL;
if (hsi.preferredSectionId > 0 && (sw = SectionWidget::LookupMap.value(hsi.preferredSectionId)) != NULL)
{
sw->addContent(hsi.data, true);
return true;
}
else if (_sections.size() > 0 && (sw = _sections.first()) != NULL)
{
sw->addContent(hsi.data, true);
return true;
}
else
{
qDebug() << "TODO Create new SW here and add SC";
return true;
}
}
qFatal("Unable to show SectionContent, don't know where 8-/");
return false;
}
bool ContainerWidget::hideSectionContent(const SectionContent::RefPtr& sc)
{
// Search SC in floatings
// We can simply hide floatings, nothing else required.
for (int i = 0; i < _floatings.count(); ++i)
{
const bool found = _floatings.at(i)->content()->uid() == sc->uid();
if (!found)
continue;
_floatings.at(i)->setVisible(false);
return true;
}
// Search SC in sections
// It's required to remove the SC from SW completely and hold it in a
// separate list as long as a "showSectionContent" gets called for the SC again.
// In case that the SW does not have any other SCs, we need to delete it.
for (int i = 0; i < _sections.count(); ++i)
{
SectionWidget* sw = _sections.at(i);
const bool found = sw->indexOfContent(sc) >= 0;
if (!found)
continue;
HiddenSectionItem hsi;
hsi.preferredSectionId = sw->uid();
hsi.preferredSectionIndex = sw->indexOfContent(sc);
if (!sw->takeContent(sc->uid(), hsi.data))
return false;
hsi.data.titleWidget->setVisible(false);
hsi.data.contentWidget->setVisible(false);
_hiddenSectionContents.insert(sc->uid(), hsi);
if (sw->contents().isEmpty())
{
delete sw;
sw = NULL;
deleteEmptySplitter(this);
}
return true;
}
qFatal("Unable to hide SectionContent, don't know this one 8-/");
return false;
}
QMenu* ContainerWidget::createContextMenu() const
{
// Fill map with actions (sorted by key!)
QMap<QString, QAction*> actions;
// Visible contents of sections
for (int i = 0; i < _sections.size(); ++i)
{
const SectionWidget* sw = _sections.at(i);
const QList<SectionContent::RefPtr>& contents = sw->contents();
foreach (const SectionContent::RefPtr& sc, contents)
{
QAction* a = new QAction(QIcon(), sc->uniqueName(), NULL);
a->setObjectName(QString("ads-action-sc-%1").arg(QString::number(sc->uid())));
a->setProperty("uid", sc->uid());
a->setProperty("type", "section");
a->setCheckable(true);
a->setChecked(true);
#if QT_VERSION >= 0x050000
QObject::connect(a, &QAction::toggled, this, &ContainerWidget::onActionToggleSectionContentVisibility);
#else
QObject::connect(a, SIGNAL(toggled(bool)), this, SLOT(onActionToggleSectionContentVisibility(bool)));
#endif
actions.insert(a->text(), a);
}
}
// Hidden contents of sections
QHashIterator<int, HiddenSectionItem> hiddenIter(_hiddenSectionContents);
while (hiddenIter.hasNext())
{
hiddenIter.next();
const SectionContent::RefPtr sc = hiddenIter.value().data.content;
QAction* a = new QAction(QIcon(), sc->uniqueName(), NULL);
a->setObjectName(QString("ads-action-sc-%1").arg(QString::number(sc->uid())));
a->setProperty("uid", sc->uid());
a->setProperty("type", "section");
a->setCheckable(true);
a->setChecked(false);
#if QT_VERSION >= 0x050000
QObject::connect(a, &QAction::toggled, this, &ContainerWidget::onActionToggleSectionContentVisibility);
#else
QObject::connect(a, SIGNAL(toggled(bool)), this, SLOT(onActionToggleSectionContentVisibility(bool)));
#endif
actions.insert(a->text(), a);
}
// Floating contents
for (int i = 0; i < _floatings.size(); ++i)
{
const FloatingWidget* fw = _floatings.at(i);
const SectionContent::RefPtr sc = fw->content();
QAction* a = new QAction(QIcon(), sc->uniqueName(), NULL);
a->setObjectName(QString("ads-action-sc-%1").arg(QString::number(sc->uid())));
a->setProperty("uid", sc->uid());
a->setProperty("type", "floating");
a->setCheckable(true);
a->setChecked(fw->isVisible());
#if QT_VERSION >= 0x050000
QObject::connect(a, &QAction::toggled, fw, &FloatingWidget::setVisible);
#else
QObject::connect(a, SIGNAL(toggled(bool)), fw, SLOT(setVisible(bool)));
#endif
actions.insert(a->text(), a);
}
// Create menu from "actions"
QMenu* m = new QMenu(NULL);
m->addActions(actions.values());
return m;
}
QByteArray ContainerWidget::saveState() const
{
QByteArray ba;
QDataStream out(&ba, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_5);
out << (quint32) 0x00001337; // Magic
out << (quint32) 1; // Version
// Save state of floating contents
saveFloatingWidgets(out);
// Walk through layout for splitters
// Well.. there actually shouldn't be more than one
for (int i = 0; i < _mainLayout->count(); ++i)
{
QLayoutItem* li = _mainLayout->itemAt(i);
if (!li->widget())
continue;
saveSectionWidgets(out, li->widget());
}
return ba;
}
bool ContainerWidget::restoreState(const QByteArray& data)
{
QDataStream in(data);
in.setVersion(QDataStream::Qt_4_5);
quint32 magic = 0;
in >> magic;
if (magic != 0x00001337)
return false;
quint32 version = 0;
in >> version;
if (version != 1)
return false;
QList<FloatingWidget*> oldFloatings = _floatings;
QList<SectionWidget*> oldSections = _sections;
// Restore floating widgets
QList<FloatingWidget*> floatings;
bool success = restoreFloatingWidgets(in, floatings);
if (!success)
{
qWarning() << "Could not restore floatings completely";
}
// Restore splitters and section widgets
QList<SectionWidget*> sections;
success = restoreSectionWidgets(in, NULL, sections);
if (!success)
{
qWarning() << "Could not restore sections completely";
}
// Handle SectionContent which is not mentioned by deserialized data.
// What shall we do with it? For now: Simply drop them into the first SectionWidget.
if (true)
{
QList<SectionContent::RefPtr> leftContents;
// Collect all contents which has been restored
QList<SectionContent::RefPtr> contents;
for (int i = 0; i < floatings.count(); ++i)
contents.append(floatings.at(i)->content());
for (int i = 0; i < sections.count(); ++i)
for (int j = 0; j < sections.at(i)->contents().count(); ++j)
contents.append(sections.at(i)->contents().at(j));
// Compare restored contents with available contents
const QList<SectionContent::WeakPtr> allContents = SectionContent::LookupMap.values();
for (int i = 0; i < allContents.count(); ++i)
{
const SectionContent::RefPtr sc = allContents.at(i).toStrongRef();
if (sc.isNull() || sc->containerWidget() != this)
continue;
if (contents.contains(sc))
continue;
leftContents.append(sc);
}
// What should we do with a drunken sailor.. what should.. erm..
// .. we do with the left-contents?
// We might need to add them into the hidden-list, if no other sections are available
for (int i = 0; i < leftContents.count(); ++i)
{
const SectionContent::RefPtr sc = leftContents.at(i);
InternalContentData data;
this->takeContent(sc, data);
if (sections.isEmpty())
{
HiddenSectionItem hsi;
hsi.data = data;
_hiddenSectionContents.insert(sc->uid(), hsi);
}
else
sections.first()->addContent(sc);
}
}
_floatings = floatings;
_sections = sections;
// Delete old objects
QLayoutItem* old = _mainLayout->takeAt(0);
_mainLayout->addWidget(_splitter);
delete old;
qDeleteAll(oldFloatings);
qDeleteAll(oldSections);
return success;
}
QRect ContainerWidget::outerTopDropRect() const
{
QRect r = rect();
int h = r.height() / 100 * 5;
return QRect(r.left(), r.top(), r.width(), h);
}
QRect ContainerWidget::outerRightDropRect() const
{
QRect r = rect();
int w = r.width() / 100 * 5;
return QRect(r.right() - w, r.top(), w, r.height());
}
QRect ContainerWidget::outerBottomDropRect() const
{
QRect r = rect();
int h = r.height() / 100 * 5;
return QRect(r.left(), r.bottom() - h, r.width(), h);
}
QRect ContainerWidget::outerLeftDropRect() const
{
QRect r = rect();
int w = r.width() / 100 * 5;
return QRect(r.left(), r.top(), w, r.height());
}
///////////////////////////////////////////////////////////////////////
// PRIVATE API BEGINS HERE
///////////////////////////////////////////////////////////////////////
SectionWidget* ContainerWidget::newSectionWidget()
{
SectionWidget* sw = new SectionWidget(this);
_sections.append(sw);
return sw;
}
SectionWidget* ContainerWidget::dropContent(const InternalContentData& data, SectionWidget* targetSection, DropArea area, bool autoActive)
{
SectionWidget* ret = NULL;
// Drop on outer area
2015-12-09 19:21:38 +08:00
if (!targetSection)
{
switch (area)
{
case TopDropArea:
ret = dropContentOuterHelper(_mainLayout, data, Qt::Vertical, false);
break;
case RightDropArea:
ret = dropContentOuterHelper(_mainLayout, data, Qt::Horizontal, true);
break;
case BottomDropArea:
ret = dropContentOuterHelper(_mainLayout, data, Qt::Vertical, true);
break;
case LeftDropArea:
ret = dropContentOuterHelper(_mainLayout, data, Qt::Horizontal, false);
break;
default:
return NULL;
}
return NULL;
2015-12-09 19:21:38 +08:00
}
QSplitter* targetSectionSplitter = findParentSplitter(targetSection);
2015-12-09 19:21:38 +08:00
// Drop logic based on area.
switch (area)
{
case TopDropArea:
{
SectionWidget* sw = newSectionWidget();
2015-12-09 19:21:38 +08:00
sw->addContent(data, true);
if (targetSectionSplitter->orientation() == Qt::Vertical)
{
const int index = targetSectionSplitter->indexOf(targetSection);
2015-12-09 19:21:38 +08:00
targetSectionSplitter->insertWidget(index, sw);
}
else
{
const int index = targetSectionSplitter->indexOf(targetSection);
QSplitter* s = newSplitter(Qt::Vertical);
2015-12-09 19:21:38 +08:00
s->addWidget(sw);
s->addWidget(targetSection);
targetSectionSplitter->insertWidget(index, s);
}
ret = sw;
2015-12-09 19:21:38 +08:00
break;
}
case RightDropArea:
{
SectionWidget* sw = newSectionWidget();
2015-12-09 19:21:38 +08:00
sw->addContent(data, true);
if (targetSectionSplitter->orientation() == Qt::Horizontal)
{
const int index = targetSectionSplitter->indexOf(targetSection);
2015-12-09 19:21:38 +08:00
targetSectionSplitter->insertWidget(index + 1, sw);
}
else
{
const int index = targetSectionSplitter->indexOf(targetSection);
QSplitter* s = newSplitter(Qt::Horizontal);
2015-12-09 19:21:38 +08:00
s->addWidget(targetSection);
s->addWidget(sw);
targetSectionSplitter->insertWidget(index, s);
}
ret = sw;
2015-12-09 19:21:38 +08:00
break;
}
case BottomDropArea:
{
SectionWidget* sw = newSectionWidget();
2015-12-09 19:21:38 +08:00
sw->addContent(data, true);
if (targetSectionSplitter->orientation() == Qt::Vertical)
{
2016-02-11 22:02:42 +08:00
int index = targetSectionSplitter->indexOf(targetSection);
2015-12-09 19:21:38 +08:00
targetSectionSplitter->insertWidget(index + 1, sw);
}
else
{
2016-02-11 22:02:42 +08:00
int index = targetSectionSplitter->indexOf(targetSection);
QSplitter* s = newSplitter(Qt::Vertical);
2015-12-09 19:21:38 +08:00
s->addWidget(targetSection);
s->addWidget(sw);
targetSectionSplitter->insertWidget(index, s);
}
ret = sw;
2015-12-09 19:21:38 +08:00
break;
}
case LeftDropArea:
{
SectionWidget* sw = newSectionWidget();
2015-12-09 19:21:38 +08:00
sw->addContent(data, true);
if (targetSectionSplitter->orientation() == Qt::Horizontal)
{
2016-02-11 22:02:42 +08:00
int index = targetSectionSplitter->indexOf(targetSection);
2015-12-09 19:21:38 +08:00
targetSectionSplitter->insertWidget(index, sw);
}
else
{
2016-02-11 22:02:42 +08:00
QSplitter* s = newSplitter(Qt::Horizontal);
2015-12-09 19:21:38 +08:00
s->addWidget(sw);
2016-02-11 22:02:42 +08:00
int index = targetSectionSplitter->indexOf(targetSection);
2015-12-09 19:21:38 +08:00
targetSectionSplitter->insertWidget(index, s);
s->addWidget(targetSection);
}
ret = sw;
2015-12-09 19:21:38 +08:00
break;
}
case CenterDropArea:
{
targetSection->addContent(data, autoActive);
ret = targetSection;
2015-12-09 19:21:38 +08:00
break;
}
default:
break;
2015-12-09 19:21:38 +08:00
}
return ret;
2015-12-09 19:21:38 +08:00
}
void ContainerWidget::addSection(SectionWidget* section)
{
// Create default splitter.
if (!_splitter)
{
_splitter = newSplitter(_orientation);
2015-12-09 19:21:38 +08:00
_mainLayout->addWidget(_splitter, 0, 0);
}
if (_splitter->indexOf(section) != -1)
{
qWarning() << Q_FUNC_INFO << QString("Section has already been added");
return;
}
_splitter->addWidget(section);
}
SectionWidget* ContainerWidget::sectionAt(const QPoint& pos) const
{
const QPoint gpos = mapToGlobal(pos);
2015-12-09 19:21:38 +08:00
for (int i = 0; i < _sections.size(); ++i)
{
2016-02-11 22:02:42 +08:00
SectionWidget* sw = _sections[i];
2015-12-09 19:21:38 +08:00
if (sw->rect().contains(sw->mapFromGlobal(gpos)))
{
return sw;
}
}
return 0;
}
SectionWidget* ContainerWidget::dropContentOuterHelper(QLayout* l, const InternalContentData& data, Qt::Orientation orientation, bool append)
{
SectionWidget* sw = newSectionWidget();
sw->addContent(data, true);
QSplitter* oldsp = findImmediateSplitter(this);
if (oldsp->orientation() == orientation
|| oldsp->count() == 1)
{
oldsp->setOrientation(orientation);
if (append)
oldsp->addWidget(sw);
else
oldsp->insertWidget(0, sw);
}
else
{
QSplitter* sp = newSplitter(orientation);
if (append)
{
#if QT_VERSION >= 0x050000
QLayoutItem* li = l->replaceWidget(oldsp, sp);
sp->addWidget(oldsp);
sp->addWidget(sw);
delete li;
#else
int index = l->indexOf(oldsp);
QLayoutItem* li = l->takeAt(index);
l->addWidget(sp);
sp->addWidget(oldsp);
sp->addWidget(sw);
delete li;
#endif
}
else
{
#if QT_VERSION >= 0x050000
sp->addWidget(sw);
QLayoutItem* li = l->replaceWidget(oldsp, sp);
sp->addWidget(oldsp);
delete li;
#else
sp->addWidget(sw);
int index = l->indexOf(oldsp);
QLayoutItem* li = l->takeAt(index);
l->addWidget(sp);
sp->addWidget(oldsp);
delete li;
#endif
}
}
return sw;
2015-12-09 19:21:38 +08:00
}
void ContainerWidget::saveFloatingWidgets(QDataStream& out) const
{
out << _floatings.count();
for (int i = 0; i < _floatings.count(); ++i)
{
FloatingWidget* fw = _floatings.at(i);
out << fw->content()->uniqueName();
out << fw->saveGeometry();
out << fw->isVisible();
}
}
void ContainerWidget::saveSectionWidgets(QDataStream& out, QWidget* widget) const
{
QSplitter* sp = NULL;
SectionWidget* sw = NULL;
if (!widget)
{
out << 0;
}
else if ((sp = dynamic_cast<QSplitter*>(widget)) != NULL)
{
out << 1; // Type = QSplitter
out << ((sp->orientation() == Qt::Horizontal) ? (int) 1 : (int) 2);
out << sp->count();
2016-02-11 21:42:55 +08:00
out << sp->sizes();
for (int i = 0; i < sp->count(); ++i)
{
saveSectionWidgets(out, sp->widget(i));
}
}
else if ((sw = dynamic_cast<SectionWidget*>(widget)) != NULL)
{
const QList<SectionContent::RefPtr>& contents = sw->contents();
QList<HiddenSectionItem> hiddenContents;
QHashIterator<int, HiddenSectionItem> iter(_hiddenSectionContents);
while (iter.hasNext())
{
iter.next();
const HiddenSectionItem& hsi = iter.value();
if (hsi.preferredSectionId != sw->uid())
continue;
hiddenContents.append(hsi);
}
out << 2; // Type = SectionWidget
out << sw->currentIndex();
out << contents.count() + hiddenContents.count();
for (int i = 0; i < contents.count(); ++i)
{
out << contents[i]->uniqueName(); // Unique name
out << (bool) true; // Visiblity
out << i; // Index
}
for (int i = 0; i < hiddenContents.count(); ++i)
{
out << hiddenContents.at(i).data.content->uniqueName();
out << (bool) false;
out << hiddenContents.at(i).preferredSectionIndex;
}
}
}
bool ContainerWidget::restoreFloatingWidgets(QDataStream& in, QList<FloatingWidget*>& floatings)
{
int fwCount = 0;
in >> fwCount;
if (fwCount <= 0)
return true;
for (int i = 0; i < fwCount; ++i)
{
QString uname;
in >> uname;
QByteArray geom;
in >> geom;
bool visible = false;
in >> visible;
qDebug() << "Restore FloatingWidget" << uname << geom << visible;
const SectionContent::RefPtr sc = SectionContent::LookupMapByName.value(uname).toStrongRef();
if (!sc)
{
qWarning() << "Can not find SectionContent:" << uname;
continue;
}
InternalContentData data;
if (!this->takeContent(sc, data))
continue;
FloatingWidget* fw = new FloatingWidget(this, sc, data.titleWidget, data.contentWidget, this);
fw->restoreGeometry(geom);
fw->setVisible(visible);
floatings.append(fw);
data.titleWidget->_fw = fw; // $mfreiholz: Don't look at it :-< It's more than ugly...
}
return true;
}
bool ContainerWidget::restoreSectionWidgets(QDataStream& in, QSplitter* currentSplitter, QList<SectionWidget*>& sections)
{
int type;
in >> type;
// Splitter
if (type == 1)
{
int orientation, count;
2016-02-11 21:42:55 +08:00
QList<int> sizes;
in >> orientation >> count >> sizes;
QSplitter* sp = newSplitter((Qt::Orientation) orientation);
for (int i = 0; i < count; ++i)
{
if (!restoreSectionWidgets(in, sp, sections))
return false;
}
if (sp->count() <= 0)
{
delete sp;
sp = NULL;
}
else if (sp)
{
sp->setSizes(sizes);
2016-02-11 21:42:55 +08:00
if (!currentSplitter)
_splitter = sp;
else
currentSplitter->addWidget(sp);
}
}
// Section
else if (type == 2)
{
if (!currentSplitter)
{
qWarning() << "Missing splitter object for section";
return false;
}
int currentIndex, count;
in >> currentIndex >> count;
SectionWidget* sw = new SectionWidget(this);
for (int i = 0; i < count; ++i)
{
QString uname;
in >> uname;
bool visible = false;
in >> visible;
int preferredIndex = -1;
in >> preferredIndex;
const SectionContent::RefPtr sc = SectionContent::LookupMapByName.value(uname).toStrongRef();
if (!sc)
{
qWarning() << "Can not find SectionContent:" << uname;
continue;
}
InternalContentData data;
this->takeContent(sc, data);
if (visible)
sw->addContent(sc);
else
{
HiddenSectionItem hsi;
hsi.preferredSectionId = sw->uid();
hsi.preferredSectionIndex = preferredIndex;
hsi.data = data;
_hiddenSectionContents.insert(sc->uid(), hsi);
}
}
if (sw->contents().isEmpty())
{
delete sw;
sw = NULL;
}
else if (sw)
{
sw->setCurrentIndex(currentIndex);
currentSplitter->addWidget(sw);
sections.append(sw);
}
}
// Unknown
else
{
qDebug() << QString();
}
return true;
}
bool ContainerWidget::takeContent(const SectionContent::RefPtr& sc, InternalContentData& data)
{
// Search in sections
bool found = false;
for (int i = 0; i < _sections.count() && !found; ++i)
{
found = _sections.at(i)->takeContent(sc->uid(), data);
}
// Search in floating widgets
for (int i = 0; i < _floatings.count() && !found; ++i)
{
found = _floatings.at(i)->content()->uid() == sc->uid();
if (found)
_floatings.at(i)->takeContent(data);
}
return found;
}
void ContainerWidget::onActionToggleSectionContentVisibility(bool visible)
{
QAction* a = qobject_cast<QAction*>(sender());
if (!a)
return;
const int uid = a->property("uid").toInt();
const SectionContent::RefPtr sc = SectionContent::LookupMap.value(uid).toStrongRef();
if (sc.isNull())
{
qCritical() << "Can not find content by ID" << uid;
return;
}
if (visible)
showSectionContent(sc);
else
hideSectionContent(sc);
}
ADS_NAMESPACE_END