[Companion] Radio conversion improvements (#5534)

* Move MultiposPots & MultiposPotsPositions capability lookups and getAnalogInputName() to Boards class.

* Add RawSource::isAvailable() and RawSwitch::isAvailable(); Add board type param to various RawSource lookup functions.

* Add CustomFunctionData::toString(), FlightModeData::toString(), FactoryInstalledPots board capability, MAX_SWITCH_TYPE, & break out GeneralSettings source/switch defaults to own function.

* Add RadioDataConversionState class for tracking conversion actions.

* Implement RadioDataConversionState handler, add conversion process report, and improve/fix conversion issues:
    * Make sure all switches/pots/knobs have default configs for destination board type;
    * Try to move more custom control names, intelligently;
    * Convert model timers and throttle source;
    * Adjust for Horus 6P switch in place of Taranis S2 knob;
    * Adjust for extra sliders on X12 and X9E to move LS and RS to proper slots as needed;
    * Make sure ALL controls are validated, eg. to always account for extra trims/analogs/switches/etc which don't exist on destination;
    * Properly indicate invalid RawSource::toString() items with "???".

* Allow user to cancel out of file conversions & provide ample warnings if switching radio type/profile with unsaved file(s) still open;
Also: Fix bug with some orphaned MdiChild windows never being deleted; Create static bool Storage::isBoardCompatible(b1, b2).

* Add maxLibQt "stub" (will not compile).

* Add ExportableTableView for better presentation of conversion results table.

* Move getFourCC() and isBoardCompatible() to Boards class.
This commit is contained in:
Max Paperno
2017-12-18 22:56:39 +01:00
committed by Bertrand Songis
parent e63a877ed6
commit 0dbad182e8
25 changed files with 1536 additions and 420 deletions
@@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 2.8.12)
cmake_policy(SET CMP0020 NEW)
set(CMAKE_CXX_STANDARD 11)
if (CMAKE_VERSION VERSION_LESS "3.1" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND CMAKE_CXX_FLAGS --std=gnu++11)
endif ()
find_package(Qt5Core)
find_package(Qt5Widgets)
project(maxLibQtWidgets)
set(SRCS
"${CMAKE_CURRENT_LIST_DIR}/ExportableTableView.cpp"
# "${CMAKE_CURRENT_LIST_DIR}/ScrollableMessageBox.cpp"
# "${CMAKE_CURRENT_LIST_DIR}/TimerEdit.cpp"
# "${CMAKE_CURRENT_LIST_DIR}/TreeComboBox.cpp"
)
set(HDRS
"${CMAKE_CURRENT_LIST_DIR}/ExportableTableView.h"
# "${CMAKE_CURRENT_LIST_DIR}/ScrollableMessageBox.h"
# "${CMAKE_CURRENT_LIST_DIR}/TimerEdit.h"
# "${CMAKE_CURRENT_LIST_DIR}/TreeComboBox.h"
)
qt5_wrap_cpp(SRCS ${HDRS})
add_library(${PROJECT_NAME} ${SRCS} ${HDRS})
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Widgets)
target_compile_definitions(${PROJECT_NAME} PRIVATE QT_USE_QSTRINGBUILDER)
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC "${CMAKE_CURRENT_LIST_DIR}")
# need to push this upstream
set(BUILT_LIBRARIES ${BUILT_LIBRARIES} ${PROJECT_NAME} PARENT_SCOPE)
@@ -0,0 +1 @@
#include "ExportableTableView.h"
@@ -0,0 +1,242 @@
/*
ExportableTableView
COPYRIGHT: (c)2017 Maxim Paperno; All Right Reserved.
Contact: http://www.WorldDesign.com/contact
LICENSE:
Commercial License Usage
Licensees holding valid commercial licenses may use this file in
accordance with the terms contained in a written agreement between
you and the copyright holder.
GNU General Public License Usage
Alternatively, this file may be used 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.
A copy of the GNU General Public License is available at <http://www.gnu.org/licenses/>.
*/
#include "ExportableTableView.h"
ExportableTableView::ExportableTableView(QWidget *parent) : QTableView(parent)
{
setContextMenuPolicy(Qt::CustomContextMenu);
setHtmlTemplate(getDefaultHtmlTemplate());
QAction * selAll = new QAction(tr("Select All"), this);
selAll->setShortcut(QKeySequence::SelectAll);
addAction(selAll);
QAction * cpyTab = new QAction(tr("Copy selection as TAB-delimited text"), this);
cpyTab->setShortcut(QKeySequence::Copy);
cpyTab->setProperty("delim", "\t");
addAction(cpyTab);
QAction * cpyCsv = new QAction(tr("Copy selection as cooma-delimited text (CSV)"), this);
cpyCsv->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_C);
cpyCsv->setProperty("delim", ", ");
addAction(cpyCsv);
QAction * cpyPipe = new QAction(tr("Copy selection as pipe-delimited text"), this);
cpyPipe->setShortcut(Qt::CTRL + Qt::ALT + Qt::Key_C);
cpyPipe->setProperty("delim", " | ");
addAction(cpyPipe);
QAction * cpyHtml = new QAction(tr("Copy selection as HTML"), this);
cpyHtml->setProperty("delim", "html");
addAction(cpyHtml);
QAction * saveFile = new QAction(tr("Save selection to file"), this);
saveFile->setShortcut(QKeySequence::Save);
addAction(saveFile);
connect(selAll, &QAction::triggered, this, &QTableView::selectAll);
connect(cpyTab, &QAction::triggered, this, &ExportableTableView::copy);
connect(cpyCsv, &QAction::triggered, this, &ExportableTableView::copy);
connect(cpyPipe, &QAction::triggered, this, &ExportableTableView::copy);
connect(cpyHtml, &QAction::triggered, this, &ExportableTableView::copy);
connect(saveFile, &QAction::triggered, this, &ExportableTableView::save);
connect(this, &QTableView::customContextMenuRequested, this, &ExportableTableView::onCustomContextMenuRequested);
}
QString ExportableTableView::getDefaultHtmlTemplate()
{
return "<!DOCTYPE html>\n<html>\n<head><meta charset='utf-8'/><style>" \
"th, td {font-family: sans-serif; padding: 3px 15px 3px 3px;}" \
"</style></head>\n<body>\n" \
"<table border=0 cellspacing=2>\n" \
"%1" \
"</table>\n" \
"</body></html>";
}
void ExportableTableView::setHtmlTemplate(const QString &value)
{
m_htmlTemplate = value;
}
QString ExportableTableView::toPlainText(const QModelIndexList &indexList, const QString &delim) const
{
QString ret, header;
const QChar rowDelim = '\n';
bool firstRow = true;
for (int i = 0; i < indexList.count(); ++i) {
const QModelIndex & idx = indexList.at(i);
if (firstRow)
header.append(model()->headerData(idx.column(), Qt::Horizontal).toString());
ret.append(idx.data(Qt::DisplayRole).toString());
if (i + 1 == indexList.count() || indexList.at(i+1).row() != idx.row()) {
ret.append(rowDelim);
if (firstRow && !header.isEmpty())
header.append(rowDelim);
firstRow = false;
}
else {
ret.append(delim);
if (firstRow && !header.isEmpty())
header.append(delim);
}
}
if (!header.isEmpty())
ret.prepend(header);
return ret;
}
QString ExportableTableView::toHtml(const QModelIndexList &indexList) const
{
QString ret, header, row;
bool firstRow = true;
for (int i = 0; i < indexList.count(); ++i) {
const QModelIndex & idx = indexList.at(i);
const int algn = (idx.data(Qt::TextAlignmentRole).isValid() ? idx.data(Qt::TextAlignmentRole).toInt() : (Qt::AlignLeft | Qt::AlignVCenter));
const QString fg = (idx.data(Qt::ForegroundRole).isValid() ? idx.data(Qt::ForegroundRole).value<QBrush>().color().name(QColor::HexRgb) : "initial");
const QString bg = (idx.data(Qt::BackgroundRole).isValid() ? idx.data(Qt::BackgroundRole).value<QBrush>().color().name(QColor::HexRgb) : "initial");
const QString ttl = (idx.data(Qt::ToolTipRole).isValid() ? idx.data(Qt::ToolTipRole).toString().replace("\"", "\"\"") : QString());
QString fnt;
if (idx.data(Qt::FontRole).isValid()) {
const QFont font = idx.data(Qt::FontRole).value<QFont>();
fnt = "font: \"" % font.family() % "\" " % QString::number(font.pointSize()) % "pt;";
}
if (firstRow)
header.append(QString("<th align='left'>%1</th>\n").arg(model()->headerData(idx.column(), Qt::Horizontal).toString()));
row.append("<td style='color: %2; background-color: %3; %4' align='%5' valign='%6' %7>%1</td>\n"); // cell template
row = row.arg(idx.data(Qt::DisplayRole).toString()).arg(fg).arg(bg).arg(fnt);
row = row.arg((algn & Qt::AlignRight) ? "right" : (algn & Qt::AlignHCenter) ? "center" : "left");
row = row.arg((algn & Qt::AlignTop) ? "top" : (algn & Qt::AlignBottom) ? "bottom" : "middle");
row = row.arg(ttl.isEmpty() ? ttl : QString("title=\"%1\"").arg(ttl));
if (i + 1 == indexList.count() || indexList.at(i+1).row() != idx.row()) {
ret.append(QString("<tr>\n%1</tr>\n").arg(row));
row.clear();
firstRow = false;
}
}
ret = QString("<tbody>\n%1</tbody>\n").arg(ret);
if (!header.isEmpty())
ret.prepend(QString("<thead>\n<tr>\n%1</tr>\n</thead>\n").arg(header));
ret = m_htmlTemplate.arg(ret);
return ret;
}
bool ExportableTableView::saveToFile(const QModelIndexList &indexList, const QString &fileName)
{
static QString lastDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
if (indexList.isEmpty())
return false;
QString fname = fileName;
if (fname.isEmpty()) {
QString types = tr("Tab-delimited text") % " (*.tab);;" % tr("Comma-delimited text") % " (*.csv);;" % tr("Pipe-delimited text") % " (*.txt);;" % tr("HTML") % " (*.html)";
fname = QFileDialog::getSaveFileName(this, tr("Save to file"), lastDir, types);
}
if (fname.isEmpty())
return false;
QFile file(fname);
QFileInfo fi(file);
lastDir = fi.absolutePath();
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
if (fi.suffix() == "html")
file.write(toHtml(indexList).toUtf8());
else
file.write(toPlainText(indexList, (fi.suffix() == "tab" ? "\t" : fi.suffix() == "csv" ? ", " : " | ")).toUtf8());
file.close();
return true;
}
QSize ExportableTableView::sizeHint() const
{
int w = 0;
for (int i = 0; i < model()->columnCount(); ++i)
w += sizeHintForColumn(i);
return QSize(w + verticalScrollBar()->sizeHint().width() + 60, sizeHintForRow(0) * model()->rowCount());
}
void ExportableTableView::copyText(const QString &delim)
{
QApplication::clipboard()->setText(toPlainText(getSelectedOrAll(), delim));
}
void ExportableTableView::copyHtml()
{
QMimeData * mdata = new QMimeData;
mdata->setHtml(toHtml(getSelectedOrAll()));
QApplication::clipboard()->setMimeData(mdata);
}
void ExportableTableView::copy()
{
QString delim = "\t";
if (sender() && sender()->property("delim").isValid())
delim = sender()->property("delim").toString();
if (delim == "html")
copyHtml();
else
copyText(delim);
}
void ExportableTableView::save()
{
saveToFile(getSelectedOrAll());
}
QModelIndexList ExportableTableView::getSelectedOrAll()
{
QModelIndexList sel = selectionModel()->selectedIndexes();
if (sel.isEmpty()) {
selectAll();
sel = selectionModel()->selectedIndexes();
}
return sel;
}
void ExportableTableView::onCustomContextMenuRequested(const QPoint & pos)
{
QMenu menu;
menu.addActions(actions());
menu.exec(mapToGlobal(pos));
}
@@ -0,0 +1,99 @@
/*
ExportableTableView
https://github.com/mpaperno/maxLibQt
COPYRIGHT: (c)2017 Maxim Paperno; All Right Reserved.
Contact: http://www.WorldDesign.com/contact
LICENSE:
Commercial License Usage
Licensees holding valid commercial licenses may use this file in
accordance with the terms contained in a written agreement between
you and the copyright holder.
GNU General Public License Usage
Alternatively, this file may be used 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.
A copy of the GNU General Public License is available at <http://www.gnu.org/licenses/>.
*/
#ifndef EXPORTABLETABLEVIEW_H
#define EXPORTABLETABLEVIEW_H
#include <QAction>
#include <QApplication>
#include <QClipboard>
#include <QFile>
#include <QFileDialog>
#include <QMenu>
#include <QMimeData>
#include <QScrollBar>
#include <QStandardPaths>
#include <QTableView>
/**
\class ExportableTableView
\version 1.0.0
\brief The ExportableTableView class provides a regular QTableView but with features to export the data
as plain text or HTML.
Any selection of data can be exported. The horizontal headings, if any, are included in the export.
The export functions are available to the user via a custom context menu or keyboard shortcuts.
3 field delimiter choices are available for plain-text export (tab, comma, or pipe).
Data can be saved to the clipboard or to a file.
The export functions can also be accessed programmatically via \c toPlainText(), \c toHtml(),
and \c saveToFile().
The overall HTML template can be customized by setting \c setHtmlTemplate(). The data itself is always
formatted as a basic HTML table and then inserted into the template at the \c %1 placeholder.
HTML version tries to preserve many data role attributes of the model items:
\li \c Qt::FontRole
\li \c Qt::ForegroundRole
\li \c Qt::BackgroundRole
\li \c Qt::TextAlignmentRole
\li \c Qt::ToolTipRole
Note that \c Qt::EditRole data is not specifically preserved (unless it already matches \c Qt::DisplayRole ).
*/
class ExportableTableView : public QTableView
{
Q_OBJECT
public:
ExportableTableView(QWidget *parent = Q_NULLPTR);
static QString getDefaultHtmlTemplate();
void setHtmlTemplate(const QString &value);
QString toPlainText(const QModelIndexList &indexList, const QString &delim = "\t") const;
QString toHtml(const QModelIndexList &indexList) const;
bool saveToFile(const QModelIndexList &indexList, const QString &fileName = QString());
QSize sizeHint() const Q_DECL_OVERRIDE;
public slots:
void copyText(const QString &delim = QString("\t"));
void copyHtml();
void copy();
void save();
QModelIndexList getSelectedOrAll();
protected:
void onCustomContextMenuRequested(const QPoint &pos);
QString m_htmlTemplate;
};
#endif // EXPORTABLETABLEVIEW_H