ProjectExplorer: Prepare more flexibility to aspect layouting

This hides the explicit use of a QFormLayout from the aspect
interface in a new LayoutBuilder class. That currently works
only on a QFormLayout in the back, but opens the possibility
to use e.g. a QGridLayout as use on the Kits and some option
pages.

The aspects now only announce sub-widgets they like to add,
actuall positioning is does by a new LayoutBuilder class,
also cramming several widgets in an hbox in the right column
of the QFormLayout is done there.

Change-Id: I2b788192c465f2ab82261849d34e514697c5a491
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2019-10-15 17:20:51 +02:00
parent 02350520c2
commit 6eaf239777
20 changed files with 251 additions and 140 deletions

View File

@@ -29,6 +29,9 @@
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <QFormLayout>
#include <QWidget>
using namespace ProjectExplorer;
const char CONFIGURATION_ID_KEY[] = "ProjectExplorer.ProjectConfiguration.Id";
@@ -51,6 +54,92 @@ QWidget *ProjectConfigurationAspect::createConfigWidget() const
return m_configWidgetCreator ? m_configWidgetCreator() : nullptr;
}
void ProjectConfigurationAspect::addToLayout(LayoutBuilder &)
{
}
// LayoutBuilder
LayoutBuilder::LayoutBuilder(QWidget *parent)
: m_layout(new QFormLayout(parent))
{
m_layout->setContentsMargins(0, 0, 0, 0);
if (auto fl = qobject_cast<QFormLayout *>(m_layout))
fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
}
LayoutBuilder::~LayoutBuilder()
{
flushPendingItems();
}
void LayoutBuilder::startNewRow()
{
flushPendingItems();
}
void LayoutBuilder::flushPendingItems()
{
if (m_pendingItems.isEmpty())
return;
if (auto fl = qobject_cast<QFormLayout *>(m_layout)) {
// If there are more than two items, we cram the last ones in one hbox.
if (m_pendingItems.size() > 2) {
auto hbox = new QHBoxLayout;
for (int i = 1; i < m_pendingItems.size(); ++i) {
if (QWidget *w = m_pendingItems.at(i).widget)
hbox->addWidget(w);
else if (QLayout *l = m_pendingItems.at(i).layout)
hbox->addItem(l);
else
QTC_CHECK(false);
}
while (m_pendingItems.size() >= 2)
m_pendingItems.takeLast();
m_pendingItems.append(LayoutItem(hbox));
}
if (m_pendingItems.size() == 1) { // One one item given, so this spans both columns.
if (auto layout = m_pendingItems.at(0).layout)
fl->addRow(layout);
else if (auto widget = m_pendingItems.at(0).widget)
fl->addRow(widget);
} else if (m_pendingItems.size() == 2) { // Normal case, both columns used.
if (auto label = m_pendingItems.at(0).widget) {
if (auto layout = m_pendingItems.at(1).layout)
fl->addRow(label, layout);
else if (auto widget = m_pendingItems.at(1).widget)
fl->addRow(label, widget);
} else {
if (auto layout = m_pendingItems.at(1).layout)
fl->addRow(m_pendingItems.at(0).text, layout);
else if (auto widget = m_pendingItems.at(1).widget)
fl->addRow(m_pendingItems.at(0).text, widget);
}
} else {
QTC_CHECK(false);
}
} else {
QTC_CHECK(false);
}
m_pendingItems.clear();
}
QLayout *LayoutBuilder::layout() const
{
return m_layout;
}
void LayoutBuilder::addItem(LayoutItem item)
{
if (item.widget && !item.widget->parent())
item.widget->setParent(m_layout->parentWidget());
m_pendingItems.append(item);
}
// ProjectConfigurationAspects