diff --git a/src/core/seriallayer.cpp b/src/core/seriallayer.cpp index c8fb913..f0c4273 100644 --- a/src/core/seriallayer.cpp +++ b/src/core/seriallayer.cpp @@ -1,147 +1,147 @@ /* AtCore Copyright (C) <2016> Authors: Patrick José Pereira Chris Rizzitello Tomaz Canabrava This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include #include "seriallayer.h" Q_LOGGING_CATEGORY(SERIAL_LAYER, "org.kde.atelier.core.serialLayer") namespace { QByteArray _return = QByteArray("\r"); QByteArray _newLine = QByteArray("\n"); QByteArray _newLineReturn = QByteArray("\n\r"); QStringList _validBaudRates = { QStringLiteral("9600"), QStringLiteral("14400"), QStringLiteral("19200"), QStringLiteral("28800"), QStringLiteral("38400"), QStringLiteral("57600"), QStringLiteral("76800"), QStringLiteral("115200"), QStringLiteral("230400"), QStringLiteral("250000"), QStringLiteral("500000"), QStringLiteral("1000000") }; } /** * @brief The SerialLayerPrivate class */ class SerialLayerPrivate { public: bool _serialOpened; //!< @param _serialOpened: is serial port opened QByteArray _rawData; //!< @param _rawData: the raw serial data QVector _rByteCommands; //!< @param _rByteCommand: received Messages QVector _sByteCommands; //!< @param _sByteCommand: sent Messages }; SerialLayer::SerialLayer(const QString &port, uint baud, QObject *parent) : QSerialPort(parent), d(new SerialLayerPrivate()) { setPortName(port); setBaudRate(baud); if (open(QIODevice::ReadWrite)) { d->_serialOpened = true; connect(this, &QSerialPort::readyRead, this, &SerialLayer::readAllData); } }; void SerialLayer::readAllData() { d->_rawData.append(readAll()); //Remove any \r in the string, then split by \n. //This removes any trailing \r or \n from the commands // Proper line endings are added when the command is pushed. d->_rawData = d->_rawData.replace(_return, QByteArray()); QList tempList = d->_rawData.split(_newLine.at(0)); for (auto i = tempList.begin(); i != tempList.end(); ++i) { // Get finished line to _byteCommands if (i < tempList.end() - 1) { d->_rByteCommands.append(*i); emit receivedCommand(*i); } else { d->_rawData.clear(); d->_rawData.append(*i); } } } void SerialLayer::pushCommand(const QByteArray &comm, const QByteArray &term) { if (!isOpen()) { qCDebug(SERIAL_LAYER) << "Serial not connected !"; return; } QByteArray tmp = comm + term; write(tmp); emit pushedCommand(tmp); } void SerialLayer::pushCommand(const QByteArray &comm) { pushCommand(comm, _newLineReturn); } void SerialLayer::add(const QByteArray &comm, const QByteArray &term) { QByteArray tmp = comm + term; d->_sByteCommands.append(tmp); } void SerialLayer::add(const QByteArray &comm) { add(comm, _newLineReturn); } void SerialLayer::push() { if (!isOpen()) { qCDebug(SERIAL_LAYER) << "Serial not connected !"; return; } - for (const auto &comm : d->_sByteCommands) { + for (const auto &comm : qAsConst(d->_sByteCommands)) { write(comm); emit pushedCommand(comm); } d->_sByteCommands.clear(); } bool SerialLayer::commandAvailable() const { return !d->_rByteCommands.isEmpty(); } QStringList SerialLayer::validBaudRates() const { return _validBaudRates; } diff --git a/src/widgets/logwidget.cpp b/src/widgets/logwidget.cpp index d7dd6b8..4ce887e 100644 --- a/src/widgets/logwidget.cpp +++ b/src/widgets/logwidget.cpp @@ -1,161 +1,161 @@ /* AtCore Test Client Copyright (C) <2018> Author: Chris Rizzitello - rizzitello@kde.org Patrick José Pereira This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "logwidget.h" #include #include #include #include #include #include #include LogWidget::LogWidget(QTemporaryFile *tempFile, QWidget *parent) : QWidget(parent), logFile(tempFile) { QSize iconSize = QSize(fontMetrics().height(), fontMetrics().height()); auto *page = new QWidget; textLog = new QPlainTextEdit; textLog->setReadOnly(true); textLog->setMaximumBlockCount(1000); auto pageLayout = new QVBoxLayout; pageLayout->addWidget(textLog); page->setLayout(pageLayout); QStackedWidget *mainStack = new QStackedWidget; mainStack->insertWidget(0, page); page = new QWidget; auto textbox = new QTextEdit; textbox->setReadOnly(true); textbox->setHtml(tr("\

Special Log Entries

\

Failed to open device in read/write mode.

\

    The Device was not able to be opened.

\

       Check the device is not opened by another program.

\

       Check you have the correct permissions to open the device.

\

No plugin found for <Detected Firmware>

\

    Firmware plugins are missing or your firmware is not currently supported.

\

      Manually select the Marlin or Repetier plugin.

\

      If your firmware does not have a plugin please let us know.

\

Lots of “Waiting for firmware detect”

\

    Unable to send the firmware detect waiting for printer to restart

\

      Push the restart button on your printer or turn it on and off.

\

      Select a firmware plugin in place of auto detect.

")); pageLayout = new QVBoxLayout; pageLayout->addWidget(textbox); page->setLayout(pageLayout); mainStack->insertWidget(1, page); auto saveButton = new QPushButton(QIcon::fromTheme(QStringLiteral("document-save"), style()->standardIcon(QStyle::SP_DialogSaveButton)), tr("Save Session Log")); saveButton->setIconSize(iconSize); connect(saveButton, &QPushButton::clicked, this, &LogWidget::savePressed); auto helpButton = new QToolButton(); helpButton->setCheckable(true); helpButton->setChecked(false); helpButton->setIconSize(iconSize); helpButton->setIcon(QIcon::fromTheme(QStringLiteral("help-about"), style()->standardIcon(QStyle::SP_DialogHelpButton))); connect(helpButton, &QToolButton::clicked, this, [this, mainStack](bool checked) { mainStack->setCurrentIndex(checked); }); auto buttonLayout = new QHBoxLayout; buttonLayout->addWidget(saveButton); buttonLayout->addWidget(helpButton); auto mainLayout = new QVBoxLayout; mainLayout->addWidget(mainStack); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); } QString LogWidget::getTime() { return QTime::currentTime().toString(QStringLiteral("hh:mm:ss:zzz")); } void LogWidget::appendLog(const QString &msg) { QString message(QStringLiteral("[%1] %2").arg(getTime(), msg)); textLog->appendPlainText(message); writeTempFile(message); } void LogWidget::appendRLog(const QByteArray &bmsg) { QString message(QStringLiteral("[%1]< %2").arg(getTime(), QString::fromUtf8(bmsg))); textLog->appendPlainText(message); writeTempFile(message); } void LogWidget::appendSLog(const QByteArray &bmsg) { QString msg = QString::fromUtf8(bmsg); QRegExp _newLine(QChar::fromLatin1('\n')); QRegExp _return(QChar::fromLatin1('\r')); msg.replace(_newLine, QStringLiteral("\\n")); msg.replace(_return, QStringLiteral("\\r")); QString message(QStringLiteral("[%1]> %2").arg(getTime(), msg)); textLog->appendPlainText(message); writeTempFile(message); } void LogWidget::writeTempFile(QString text) { //Add text to our unsynced string list when that hits 100 sync to the temp file. unsyncedStrings.append(text); if (unsyncedStrings.count() > 100) { flushTemp(); } } void LogWidget::flushTemp() { /* A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, this allows easy access to the data in the file. This function will return true upon success and will set the fileName() to the unique filename used. */ logFile->open(); logFile->seek(logFile->size()); - for (const auto &string : unsyncedStrings) { + for (const auto &string : qAsConst(unsyncedStrings)) { logFile->write(string.toLatin1()); logFile->putChar('\n'); } logFile->close(); unsyncedStrings.clear(); } void LogWidget::savePressed() { //If saving be sure we have flushed to temp. flushTemp(); // Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it). QString fileName = QDir::homePath() + QChar::fromLatin1('/') + QFileInfo(logFile->fileName()).fileName() + QStringLiteral(".txt"); QString saveFileName = QFileDialog::getSaveFileName(this, tr("Save Log to file"), fileName); QFile::copy(logFile->fileName(), saveFileName); logFile->close(); } bool LogWidget::endsWith(const QString &string) { return textLog->toPlainText().endsWith(string); }