File indexing completed on 2024-05-12 04:41:11

0001 /* AtCore KDE Libary for 3D Printers
0002     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003     SPDX-FileCopyrightText: 2018-2020 Chris Rizzitello <rizzitello@kde.org>
0004     SPDX-FileCopyrightText: 2018 Patrick José Pereira <patrickjp@kde.org>
0005 */
0006 
0007 #include "commandwidget.h"
0008 #include <QHBoxLayout>
0009 #include <QLineEdit>
0010 #include <QPushButton>
0011 #include <QVBoxLayout>
0012 
0013 CommandWidget::CommandWidget(QWidget *parent)
0014     : QWidget(parent)
0015 {
0016     // Expose the least amount of object outside the creation function.
0017 
0018     // First we make our mainLayout
0019     auto mainLayout = new QVBoxLayout;
0020 
0021     // Begin making content from top to bottom or left to right.
0022     // Making child layouts in the order you want to put them
0023     // onto the mainLayout
0024     lineCommand = new QLineEdit(this);
0025     lineCommand->setPlaceholderText(tr("Send Command"));
0026 
0027     // we have a few buttons to make here. Lets name this newButton so its easier to reuse
0028     auto newButton = new QPushButton(tr("Send"), this);
0029     connect(newButton, &QPushButton::clicked, this, [this] {
0030         if (!lineCommand->text().isEmpty()) {
0031             if (lineCommand->text().startsWith(QStringLiteral("g"))) {
0032                 lineCommand->setText(lineCommand->text().replace(0, 1, QStringLiteral("G")));
0033             } else if (lineCommand->text().startsWith(QStringLiteral("m"))) {
0034                 lineCommand->setText(lineCommand->text().replace(0, 1, QStringLiteral("M")));
0035             }
0036             Q_EMIT commandPressed(lineCommand->text());
0037             lineCommand->clear();
0038         }
0039     });
0040     // When you have created a Row put the items into layout.
0041     auto hBoxLayout = new QHBoxLayout;
0042     hBoxLayout->addWidget(lineCommand);
0043     hBoxLayout->addWidget(newButton);
0044     // Put the Layout or Widget on the mainLayout when its finished.
0045     // This will free your pointers for reuse.
0046     mainLayout->addLayout(hBoxLayout);
0047 
0048     // Start making items for the next layout to place onto the mainLayout.
0049     lineMessage = new QLineEdit(this);
0050     lineMessage->setPlaceholderText(tr("Show Message"));
0051 
0052     // Reuse our button pointer.
0053     newButton = new QPushButton(tr("Send"), this);
0054 
0055     connect(newButton, &QPushButton::clicked, this, [this] {
0056         if (!lineMessage->text().isEmpty()) {
0057             Q_EMIT messagePressed(lineMessage->text());
0058             lineMessage->clear();
0059         }
0060     });
0061 
0062     // We reuse the hBoxLayout pointer in the same way as the button pointer.
0063     hBoxLayout = new QHBoxLayout;
0064     hBoxLayout->addWidget(lineMessage);
0065     hBoxLayout->addWidget(newButton);
0066     mainLayout->addLayout(hBoxLayout);
0067 
0068     setLayout(mainLayout);
0069 }