Skip to content

Commit

Permalink
Change layout from QGridLayout to QHBoxLayout to add QSplitter
Browse files Browse the repository at this point in the history
  • Loading branch information
samamou authored and Sarah committed Sep 9, 2024
1 parent 9960356 commit fa3a9b7
Showing 1 changed file with 54 additions and 41 deletions.
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,76 @@ DeviceEditDialog::DeviceEditDialog(
};

setWindowTitle(tr("Add device"));
auto gridLayout = new QGridLayout{};
setLayout(gridLayout);
auto base_layout = new QVBoxLayout{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);
auto splitter = new QSplitter{this};
base_layout->addWidget(splitter);

auto column1 = new QWidget;
auto column1_layout = new QVBoxLayout{column1};
auto protocolsLabel = new QLabel{tr("Protocols"), this};
setHeaderTextFormat(protocolsLabel);
column1_layout->addWidget(protocolsLabel);
protocolsLabel->setAlignment(Qt::AlignTop);
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);
splitter->addWidget(column1);

if(m_mode == Mode::Editing)
{
m_protocols->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);
column1->setVisible(false);
}

// Column 2: Devices
auto column2 = new QWidget;
auto column2_layout = new QVBoxLayout{column2};
auto devicesLabel = new QLabel{tr("Devices"), this};
setHeaderTextFormat(devicesLabel);
column2_layout->addWidget(devicesLabel);
devicesLabel->setAlignment(Qt::AlignTop);
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);
}
qDebug() << "m_devices initialized : " << m_devices;
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};
auto settingsLabel = new QLabel{tr("Settings"), this};
setHeaderTextFormat(settingsLabel);
column3_layout->addWidget(settingsLabel);
settingsLabel->setAlignment(Qt::AlignTop);
settingsLabel->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);

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

initAvailableProtocols();

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

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

Expand All @@ -211,6 +222,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 Down Expand Up @@ -302,6 +314,7 @@ void DeviceEditDialog::selectedProtocolChanged()
}
else
{
qDebug() << "m_devices is not initialized!";
m_devices->setVisible(false);
m_devicesLabel->setVisible(false);
}
Expand Down

0 comments on commit fa3a9b7

Please sign in to comment.