Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change layout from QGridLayout to QHBoxLayout to add QSplitter #1541

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ DeviceEditDialog::DeviceEditDialog(
, m_protocolWidget{nullptr}
, m_index{-1}
{

const auto& skin = score::Skin::instance();
const QColor textHeaderColor = QColor("#D5D5D5");
auto setHeaderTextFormat = [&](QLabel* label) {
Expand All @@ -58,65 +59,88 @@ DeviceEditDialog::DeviceEditDialog(
};

setWindowTitle(tr("Add device"));
auto gridLayout = new QGridLayout{};
setLayout(gridLayout);
auto base_layout = new QHBoxLayout{this};
setLayout(base_layout);
setModal(true);

m_invalidLabel = new QLabel{
tr("Cannot add device.\n Try changing the name to make it unique, \nor "
"check that the ports aren't already used")};
m_invalidLabel->setAlignment(Qt::AlignRight);
m_invalidLabel->setTextFormat(Qt::PlainText);
m_buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
m_okButton = m_buttonBox->addButton(tr("Add"), QDialogButtonBox::AcceptRole);
m_buttonBox->addButton(QDialogButtonBox::Cancel);

connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
splitter = new QSplitter{this};
base_layout->addWidget(splitter);

auto column1 = new QWidget;
auto column1_layout = new QVBoxLayout{column1};
m_protocolsLabel = new QLabel{tr("Protocols"), this};
setHeaderTextFormat(m_protocolsLabel);
column1_layout->addWidget(m_protocolsLabel);
m_protocolsLabel->setAlignment(Qt::AlignTop);
m_protocolsLabel->setAlignment(Qt::AlignHCenter);
m_protocols = new QTreeWidget{this};
m_protocols->header()->hide();
m_protocols->setSelectionMode(QAbstractItemView::SingleSelection);
column1_layout->addWidget(m_protocols);
column1->setLayout(column1_layout);
column1->setFixedWidth(200);
base_layout->addWidget(column1);

if(m_mode == Mode::Editing)
{
m_protocols->setVisible(false);
column1->setVisible(false);
}
else
{
m_protocols->setFixedWidth(150);

{
m_protocolsLabel = new QLabel{tr("Protocols"), this};
setHeaderTextFormat(m_protocolsLabel);
gridLayout->addWidget(m_protocolsLabel, 0, 0, Qt::AlignHCenter);
}
gridLayout->addWidget(m_protocols, 1, 0);
}
base_layout->addWidget(splitter);

// Column 2: Devices
auto column2 = new QWidget;
auto column2_layout = new QVBoxLayout{column2};
m_devicesLabel = new QLabel{tr("Devices"), this};
setHeaderTextFormat(m_devicesLabel);
column2_layout->addWidget(m_devicesLabel);
m_devicesLabel->setAlignment(Qt::AlignTop);
m_devicesLabel->setAlignment(Qt::AlignHCenter);
m_devices = new QTreeWidget{this};
m_devices->header()->hide();
m_devices->setSelectionMode(QAbstractItemView::SingleSelection);
// m_devices->setFixedWidth(140);
gridLayout->addWidget(m_devices, 1, 1);
{
m_devicesLabel = new QLabel{tr("Devices"), this};
setHeaderTextFormat(m_devicesLabel);
gridLayout->addWidget(m_devicesLabel, 0, 1, Qt::AlignHCenter);
}

{
m_protocolNameLabel = new QLabel{tr("Settings"), this};
setHeaderTextFormat(m_protocolNameLabel);
gridLayout->addWidget(
m_protocolNameLabel, 0, 2, -1, -1, Qt::AlignLeft | Qt::AlignTop);
}
column2_layout->addWidget(m_devices);
column2->setLayout(column2_layout);
splitter->addWidget(column2);

// Column 3: Settings
auto column3 = new QWidget;
auto column3_layout = new QVBoxLayout{column3};
m_protocolNameLabel = new QLabel{tr("Settings"), this};
setHeaderTextFormat(m_protocolNameLabel);
column3_layout->addWidget(m_protocolNameLabel);
m_protocolNameLabel->setAlignment(Qt::AlignTop);
m_protocolNameLabel->setAlignment(Qt::AlignHCenter);
m_main = new QWidget{this};
gridLayout->addWidget(m_main, 1, 2, -1, -1);
m_layout = new QFormLayout;
m_layout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
m_main->setLayout(m_layout);
column3_layout->addWidget(m_main);
m_invalidLabel = new QLabel{
tr("Cannot add device.\n Try changing the name to make it unique, \nor "
"check that the ports aren't already used")};
m_invalidLabel->setAlignment(Qt::AlignRight);
m_invalidLabel->setTextFormat(Qt::PlainText);
m_buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
m_okButton = m_buttonBox->addButton(tr("Add"), QDialogButtonBox::AcceptRole);
m_buttonBox->addButton(QDialogButtonBox::Cancel);
m_layout->addRow(m_invalidLabel);
m_layout->addRow(m_buttonBox);
column3->setLayout(column3_layout);
splitter->addWidget(column3);

m_devices->setMinimumWidth(40);
m_main->setMinimumWidth(100);

splitter->setCollapsible(0, false);
splitter->setCollapsible(1, false);

splitter->setStretchFactor(0, 1);
splitter->setStretchFactor(1, 2);


connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

initAvailableProtocols();

Expand All @@ -126,13 +150,11 @@ DeviceEditDialog::DeviceEditDialog(

if(m_protocols->topLevelItemCount() > 0)
{
// m_protocols->setCurrentItem();
// m_index = m_protocols->currentRow();
selectedProtocolChanged();
}

setMinimumWidth(700);
setMinimumHeight(400);
setMinimumWidth(850);
setMinimumHeight(550);

setAcceptEnabled(false);
}
Expand Down Expand Up @@ -201,7 +223,6 @@ void DeviceEditDialog::selectedDeviceChanged()
{
if(!m_devices->isVisible())
return;

if(m_devices->selectedItems().isEmpty())
return;

Expand All @@ -211,6 +232,7 @@ void DeviceEditDialog::selectedDeviceChanged()

auto name = item->text(0);
auto data = item->data(0, Qt::UserRole).value<Device::DeviceSettings>();

if(m_protocolWidget)
m_protocolWidget->setSettings(data);

Expand All @@ -226,8 +248,6 @@ void DeviceEditDialog::selectedProtocolChanged()
// Recreate
if(m_protocols->selectedItems().isEmpty())
{
//m_devices->setVisible(false);
//m_devicesLabel->setVisible(false);
return;
}
auto selected_item = m_protocols->selectedItems().first();
Expand All @@ -246,7 +266,6 @@ void DeviceEditDialog::selectedProtocolChanged()
if(m_protocolWidget)
{
SCORE_ASSERT(m_index < m_previousSettings.count());

m_previousSettings[m_index] = m_protocolWidget->getSettings();
delete m_protocolWidget;
m_protocolWidget = nullptr;
Expand All @@ -261,6 +280,8 @@ void DeviceEditDialog::selectedProtocolChanged()
m_devicesLabel->setVisible(true);
m_devices->setRootIsDecorated(false);
m_devices->setExpandsOnDoubleClick(false);
splitter->widget(0)->show();
splitter->widget(0)->setMinimumWidth(200);

for(auto& [name, e] : m_enumerators)
{
Expand Down Expand Up @@ -304,6 +325,7 @@ void DeviceEditDialog::selectedProtocolChanged()
{
m_devices->setVisible(false);
m_devicesLabel->setVisible(false);
splitter->widget(0)->hide();
}
m_protocolNameLabel->setText(tr("Settings (%1)").arg(protocol->prettyName()));
m_protocolWidget = protocol->makeSettingsWidget();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <QDialog>
#include <QList>
#include <QSplitter>

#include <score_plugin_deviceexplorer_export.h>

Expand Down Expand Up @@ -82,5 +83,6 @@ class SCORE_PLUGIN_DEVICEEXPLORER_EXPORT DeviceEditDialog final : public QDialog

QString m_originalName{};
int m_index{};
QSplitter* splitter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In C++ when we create some pointer member / pointer variable, it's very important to directly initialize it to nullptr, otherwise it will point to garbage data:

QSplitter* splitter{};

};
}
Loading