Initial commit (project based on widgets)

This commit is contained in:
Yury Shuvakin
2022-08-01 21:53:36 +03:00
parent d9396cdc2f
commit 14a7aa699f
411 changed files with 95119 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "aspectimglabel.h"
AspectImgLabel::AspectImgLabel(Qt::Orientation orientation, QWidget *parent) :
QLabel(parent), mOrientation(orientation)
{
}
void AspectImgLabel::resizeEvent(QResizeEvent *event)
{
QLabel::resizeEvent(event);
const QPixmap *pix = pixmap();
if (pix) {
int wLabel = width();
int hLabel = height();
int wImg = pix->width();
int hImg = pix->height();
if (mOrientation == Qt::Horizontal) {
setMaximumHeight((wLabel * hImg) / wImg);
} else if (hLabel != hImg) {
setMaximumWidth((hLabel * wImg) / hImg);
}
}
}

43
widgets/aspectimglabel.h Normal file
View File

@@ -0,0 +1,43 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef ASPECTIMGLABEL_H
#define ASPECTIMGLABEL_H
#include <QObject>
#include <QWidget>
#include <QLabel>
class AspectImgLabel : public QLabel
{
Q_OBJECT
public:
AspectImgLabel(Qt::Orientation orientatio = Qt::Vertical,
QWidget *parent = 0);
protected:
void resizeEvent(QResizeEvent *event);
private:
Qt::Orientation mOrientation;
};
#endif // ASPECTIMGLABEL_H

142
widgets/displaybar.cpp Normal file
View File

@@ -0,0 +1,142 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "displaybar.h"
#include <QPainter>
#include <cmath>
DisplayBar::DisplayBar(QWidget *parent) : QWidget(parent)
{
mName = "Current";
mRange = 60.0;
mVal = 0.0;
mUnit = " A";
mDecimals = 2;
}
void DisplayBar::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
int w = width();
int h = height();
double f_disp = 0.3;
double f_val = 1.0 - f_disp;
painter.fillRect(event->rect(), Qt::transparent);
painter.setBrush(Qt::black);
painter.drawRoundedRect(event->rect(), 5.0, 5.0);
painter.setBrush(QBrush(Qt::red));
painter.fillRect(w / 2 - 1, 0, 2, h, Qt::darkRed);
painter.fillRect(0, h * f_disp - 1, w, 2, Qt::darkGreen);
QPen pen;
QFont font;
// Name
pen.setColor(Qt::white);
font.setFamily("Monospace");
font.setBold(true);
font.setPixelSize(h * f_val - 2);
painter.setPen(pen);
painter.setFont(font);
painter.drawText(QRect(0, h * f_disp + 1, w / 2 - 2, h * f_val - 1),
Qt::AlignCenter, mName);
// Value
pen.setColor(Qt::white);
font.setFamily("Monospace");
font.setBold(true);
font.setPixelSize(h * f_val - 2);
painter.setPen(pen);
painter.setFont(font);
QString str = QString("%1%2").arg(mVal, 0, 'f', mDecimals).arg(mUnit);
painter.drawText(QRect(w / 2 + 1, h * f_disp + 1, w / 2 - 2, h * f_val - 1),
Qt::AlignCenter, str);
double xsp = (double)w / 2.0 + 1.0;
double xsm = (double)w / 2.0 - 1.0;
double valw = (mVal / mRange) * ((double)w / 2.0 - 2.0);
double valh = (double)h * f_disp - 2.0;
if (fabs(valw) > 0.1) {
if (valw >= 0.0) {
painter.setBrush(Qt::green);
painter.drawRect(xsp, 1, valw, valh);
} else {
painter.setBrush(Qt::red);
painter.drawRect(xsm + valw, 1, -valw, valh);
}
}
}
int DisplayBar::decimals() const
{
return mDecimals;
}
void DisplayBar::setDecimals(int decimals)
{
mDecimals = decimals;
}
QString DisplayBar::unit() const
{
return mUnit;
}
void DisplayBar::setUnit(const QString &unit)
{
mUnit = unit;
}
double DisplayBar::val() const
{
return mVal;
}
void DisplayBar::setVal(double val)
{
mVal = val;
update();
}
double DisplayBar::range() const
{
return mRange;
}
void DisplayBar::setRange(double range)
{
mRange = range;
update();
}
QString DisplayBar::name() const
{
return mName;
}
void DisplayBar::setName(const QString &name)
{
mName = name;
update();
}

59
widgets/displaybar.h Normal file
View File

@@ -0,0 +1,59 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef DISPLAYBAR_H
#define DISPLAYBAR_H
#include <QWidget>
#include <QPaintEvent>
class DisplayBar : public QWidget
{
Q_OBJECT
public:
explicit DisplayBar(QWidget *parent = 0);
QString name() const;
void setName(const QString &name);
double range() const;
void setRange(double range);
double val() const;
void setVal(double val);
QString unit() const;
void setUnit(const QString &unit);
int decimals() const;
void setDecimals(int decimals);
signals:
public slots:
protected:
void paintEvent(QPaintEvent *event);
private:
QString mName;
double mRange;
double mVal;
QString mUnit;
int mDecimals;
};
#endif // DISPLAYBAR_H

View File

@@ -0,0 +1,147 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "displaypercentage.h"
#include <QPainter>
#include <QPaintEvent>
#include <QFont>
#include <cmath>
DisplayPercentage::DisplayPercentage(QWidget *parent) : QWidget(parent)
{
mText = "";
mIsDual = false;
mValue = 0.0;
}
QString DisplayPercentage::text() const
{
return mText;
}
void DisplayPercentage::setText(const QString &text)
{
mText = text;
update();
}
bool DisplayPercentage::isDual() const
{
return mIsDual;
}
void DisplayPercentage::setDual(bool hasNegative)
{
mIsDual = hasNegative;
update();
}
double DisplayPercentage::value() const
{
return mValue;
}
void DisplayPercentage::setValue(double value)
{
mValue = value;
update();
}
void DisplayPercentage::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QColor c_bg = Qt::black;
QColor c_hl = Qt::darkGreen;
QColor c_vl = Qt::darkRed;
QColor c_neg = Qt::red;
QColor c_pos = Qt::green;
QColor c_text = Qt::white;
if (!isEnabled()) {
int c = 80;
c_bg = QColor(c, c, c);
c = 120;
c_hl = QColor(c, c, c);
c = 120;
c_vl = QColor(c, c, c);
c = 120;
c_neg = QColor(c, c, c);
c = 180;
c_pos = QColor(c, c, c);
c = 180;
c_text = QColor(c, c, c);
}
int w = width();
int h = height();
painter.fillRect(event->rect(), Qt::transparent);
painter.setBrush(c_bg);
painter.drawRoundedRect(event->rect(), 5.0, 5.0);
if (mIsDual) {
painter.fillRect(w / 2 - 1, 0, 2, h / 3, c_vl);
}
painter.fillRect(0, h / 3 - 1, w, 2, c_hl);
QPen pen;
QFont font;
// Text
pen.setColor(c_text);
font.setFamily("Monospace");
font.setBold(true);
font.setPixelSize(2 * h / 3 - 2);
painter.setPen(pen);
painter.setFont(font);
painter.drawText(QRect(0, h / 3 + 1, w - 2, 2 * h / 3 - 1), Qt::AlignCenter, mText);
double valh = (double)h / 3.0 - 2.0;
if (mIsDual) {
double xsp = (double)w / 2.0 + 1.0;
double xsm = (double)w / 2.0 - 1.0;
double valw = (mValue / 100.0) * ((double)w / 2.0 - 2.0);
if (fabs(valw) > 0.1) {
if (valw >= 0.0) {
painter.setBrush(c_pos);
painter.drawRect(xsp, 0, valw, valh);
} else {
painter.setBrush(c_neg);
painter.drawRect(xsm + valw, 1, -valw, valh);
}
}
} else {
if (mValue >= 0.0) {
painter.setBrush(c_pos);
painter.drawRect(0, 1, (w * mValue) / 100.0, valh);
} else {
painter.setBrush(c_neg);
painter.drawRect(w + w * (mValue / 100.0), 1, w, valh);
}
}
}

View File

@@ -0,0 +1,54 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef DISPLAYPERCENTAGE_H
#define DISPLAYPERCENTAGE_H
#include <QWidget>
class DisplayPercentage : public QWidget
{
Q_OBJECT
public:
explicit DisplayPercentage(QWidget *parent = 0);
QString text() const;
void setText(const QString &text);
bool isDual() const;
void setDual(bool isDual);
double value() const;
void setValue(double value);
signals:
public slots:
protected:
void paintEvent(QPaintEvent *event);
private:
QString mText;
bool mIsDual;
double mValue;
};
#endif // DISPLAYPERCENTAGE_H

86
widgets/helpdialog.cpp Normal file
View File

@@ -0,0 +1,86 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "helpdialog.h"
#include "ui_helpdialog.h"
#include <QDebug>
#include <QMessageBox>
HelpDialog::HelpDialog(QString title, QString text, QWidget *parent) :
QDialog(parent),
ui(new Ui::HelpDialog)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(title);
ui->textEdit->setText(text);
ui->textEdit->viewport()->setAutoFillBackground(false);
}
HelpDialog::~HelpDialog()
{
delete ui;
}
void HelpDialog::showHelp(QWidget *parent, ConfigParams *params, QString name, bool modal)
{
ConfigParam *param = params->getParam(name);
if (param) {
HelpDialog *h = new HelpDialog(param->longName,
param->description,
parent);
if (modal) {
h->exec();
} else {
h->show();
}
} else {
QMessageBox::warning(parent,
tr("Show help"),
tr("Help text for %1 not found.").arg(name));
}
}
void HelpDialog::showHelp(QWidget *parent, QString title, QString text)
{
HelpDialog *h = new HelpDialog(title, text, parent);
h->exec();
}
void HelpDialog::showEvent(QShowEvent *event)
{
QSize s = ui->textEdit->document()->size().toSize();
int tot = (this->height() - ui->textEdit->height()) + s.height() + 5;
if (tot < 140) {
this->resize(this->width(), 140);
} else if (tot > 450) {
this->resize(this->width(), 450);
} else {
this->resize(this->width(), tot);
}
QDialog::showEvent(event);
}
void HelpDialog::on_okButton_clicked()
{
close();
}

50
widgets/helpdialog.h Normal file
View File

@@ -0,0 +1,50 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef HELPDIALOG_H
#define HELPDIALOG_H
#include <QDialog>
#include "configparams.h"
namespace Ui {
class HelpDialog;
}
class HelpDialog : public QDialog
{
Q_OBJECT
public:
explicit HelpDialog(QString title, QString text, QWidget *parent = 0);
~HelpDialog();
void showEvent(QShowEvent *event) override;
static void showHelp(QWidget *parent, ConfigParams *params, QString name, bool modal = true);
static void showHelp(QWidget *parent, QString title, QString text);
private slots:
void on_okButton_clicked();
private:
Ui::HelpDialog *ui;
};
#endif // HELPDIALOG_H

94
widgets/helpdialog.ui Normal file
View File

@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>HelpDialog</class>
<widget class="QDialog" name="HelpDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>613</width>
<height>236</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<property name="windowIcon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Help-96.png</normaloff>:/res/icons/Help-96.png</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="minimumSize">
<size>
<width>40</width>
<height>40</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>40</width>
<height>40</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="../res.qrc">:/res/icons/About-96.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="VTextBrowser" name="textEdit">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="okButton">
<property name="text">
<string>Ok</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>VTextBrowser</class>
<extends>QTextEdit</extends>
<header>widgets/vtextbrowser.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../res.qrc"/>
</resources>
<connections/>
</ui>

109
widgets/historylineedit.cpp Normal file
View File

@@ -0,0 +1,109 @@
/*
Copyright 2018 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "historylineedit.h"
#include <QDebug>
//#if 1
HistoryLineEdit::HistoryLineEdit(QWidget *parent) :
QLineEdit(parent)
{
mIndexNow = 0;
installEventFilter(this);
}
bool HistoryLineEdit::eventFilter(QObject *object, QEvent *e)
{
Q_UNUSED(object);
if (e->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
switch(keyEvent->key()) {
case Qt::Key_Up:
case Qt::Key_Down:
//case Qt::Key_Return:
//case Qt::Key_Enter:
// break;
case Qt::Key_Return:
case Qt::Key_Enter:
/// TO DO /// emit enterClicked();
break;
default:
return false;
}
bool retval = true;
switch(keyEvent->key()) {
case Qt::Key_Up: {
if (mIndexNow == 0) {
mCurrentText = text();
}
mIndexNow--;
int ind = mHistory.size() + mIndexNow;
if (ind >= 0 && ind < mHistory.size()) {
setText(mHistory.at(ind));
} else {
mIndexNow++;
}
} break;
case Qt::Key_Down:
mIndexNow++;
if (mIndexNow >= 0) {
if (mIndexNow == 0) {
setText(mCurrentText);
}
mIndexNow = 0;
} else {
int ind = mHistory.size() + mIndexNow;
if (ind >= 0 && ind < mHistory.size()) {
setText(mHistory.at(ind));
}
}
break;
case Qt::Key_Return:
case Qt::Key_Enter:
if (!text().isEmpty()) {
if (mHistory.isEmpty() || mHistory.last() != text()) {
mHistory.append(text());
}
}
mIndexNow = 0;
mCurrentText.clear();
retval = false;
break;
default:
break;
}
return retval;
}
return false;
}
//#endif

47
widgets/historylineedit.h Normal file
View File

@@ -0,0 +1,47 @@
/*
Copyright 2018 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef HISTORYLINEEDIT_H
#define HISTORYLINEEDIT_H
#include <QObject>
#include <QWidget>
#include <QLineEdit>
#include <QStringList>
#include <QEvent>
#include <QKeyEvent>
//#if 1
class HistoryLineEdit : public QLineEdit
{
//Q_OBJECT
public:
HistoryLineEdit(QWidget *parent = 0);
bool eventFilter(QObject *object, QEvent *e);
//signals:
// void enterClicked();
private:
QStringList mHistory;
int mIndexNow;
QString mCurrentText;
};
//#endif
#endif // HISTORYLINEEDIT_H

54
widgets/imagewidget.cpp Normal file
View File

@@ -0,0 +1,54 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "imagewidget.h"
#include <QPainter>
#include <QPaintEvent>
#include <QDebug>
ImageWidget::ImageWidget(QWidget *parent) : QWidget(parent)
{
}
void ImageWidget::paintEvent(QPaintEvent *event)
{
(void)event;
if (!mPixmap.isNull()) {
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
painter.fillRect(rect(), Qt::transparent);
int pw = mPixmap.width();
int ph = mPixmap.height();
int h = height();
painter.drawPixmap(0, 0, (pw * h) / ph, h, mPixmap);
}
}
QPixmap ImageWidget::pixmap() const
{
return mPixmap;
}
void ImageWidget::setPixmap(const QPixmap &pixmap)
{
mPixmap = pixmap;
}

46
widgets/imagewidget.h Normal file
View File

@@ -0,0 +1,46 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H
#include <QWidget>
class ImageWidget : public QWidget
{
Q_OBJECT
public:
explicit ImageWidget(QWidget *parent = 0);
QPixmap pixmap() const;
void setPixmap(const QPixmap &pixmap);
signals:
public slots:
protected:
void paintEvent(QPaintEvent *event);
private:
QPixmap mPixmap;
};
#endif // IMAGEWIDGET_H

707
widgets/mrichtextedit.cpp Normal file
View File

@@ -0,0 +1,707 @@
/*
** Copyright (C) 2013 Jiří Procházka (Hobrasoft)
** Contact: http://www.hobrasoft.cz/
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file is under the terms of the GNU Lesser General Public License
** version 2.1 as published by the Free Software Foundation and appearing
** in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the
** GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
*/
#include "mrichtextedit.h"
#include <QApplication>
#include <QClipboard>
#include <QMimeData>
#include <QFontDatabase>
#include <QInputDialog>
#include <QColorDialog>
#include <QTextList>
#include <QtDebug>
#include <QFileDialog>
#include <QImageReader>
#include <QSettings>
#include <QBuffer>
#include <QUrl>
#include <QPlainTextEdit>
#include <QMenu>
#include <QDialog>
MRichTextEdit::MRichTextEdit(QWidget *parent) : QWidget(parent) {
setupUi(this);
m_lastBlockList = 0;
f_textedit->setTabStopWidth(40);
connect(f_textedit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
this, SLOT(slotCurrentCharFormatChanged(QTextCharFormat)));
connect(f_textedit, SIGNAL(cursorPositionChanged()),
this, SLOT(slotCursorPositionChanged()));
m_fontsize_h1 = 18;
m_fontsize_h2 = 16;
m_fontsize_h3 = 14;
m_fontsize_h4 = 12;
fontChanged(f_textedit->font());
bgColorChanged(f_textedit->textColor());
fgColorChanged(f_textedit->textColor());
// paragraph formatting
m_paragraphItems << tr("Standard")
<< tr("Heading 1")
<< tr("Heading 2")
<< tr("Heading 3")
<< tr("Heading 4")
<< tr("Monospace");
f_paragraph->addItems(m_paragraphItems);
connect(f_paragraph, SIGNAL(activated(int)),
this, SLOT(textStyle(int)));
// undo & redo
f_undo->setShortcut(QKeySequence::Undo);
f_redo->setShortcut(QKeySequence::Redo);
connect(f_textedit->document(), SIGNAL(undoAvailable(bool)),
f_undo, SLOT(setEnabled(bool)));
connect(f_textedit->document(), SIGNAL(redoAvailable(bool)),
f_redo, SLOT(setEnabled(bool)));
f_undo->setEnabled(f_textedit->document()->isUndoAvailable());
f_redo->setEnabled(f_textedit->document()->isRedoAvailable());
connect(f_undo, SIGNAL(clicked()), f_textedit, SLOT(undo()));
connect(f_redo, SIGNAL(clicked()), f_textedit, SLOT(redo()));
// cut, copy & paste
f_cut->setShortcut(QKeySequence::Cut);
f_copy->setShortcut(QKeySequence::Copy);
f_paste->setShortcut(QKeySequence::Paste);
f_cut->setEnabled(false);
f_copy->setEnabled(false);
connect(f_cut, SIGNAL(clicked()), f_textedit, SLOT(cut()));
connect(f_copy, SIGNAL(clicked()), f_textedit, SLOT(copy()));
connect(f_paste, SIGNAL(clicked()), f_textedit, SLOT(paste()));
connect(f_textedit, SIGNAL(copyAvailable(bool)), f_cut, SLOT(setEnabled(bool)));
connect(f_textedit, SIGNAL(copyAvailable(bool)), f_copy, SLOT(setEnabled(bool)));
#ifndef QT_NO_CLIPBOARD
connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(slotClipboardDataChanged()));
#endif
// link
f_link->setShortcut(Qt::CTRL + Qt::Key_L);
connect(f_link, SIGNAL(clicked(bool)), this, SLOT(textLink(bool)));
// bold, italic & underline
f_bold->setShortcut(Qt::CTRL + Qt::Key_B);
f_italic->setShortcut(Qt::CTRL + Qt::Key_I);
f_underline->setShortcut(Qt::CTRL + Qt::Key_U);
connect(f_bold, SIGNAL(clicked()), this, SLOT(textBold()));
connect(f_italic, SIGNAL(clicked()), this, SLOT(textItalic()));
connect(f_underline, SIGNAL(clicked()), this, SLOT(textUnderline()));
connect(f_strikeout, SIGNAL(clicked()), this, SLOT(textStrikeout()));
f_align_left->setChecked(true);
connect(f_align_left, SIGNAL(clicked(bool)), this, SLOT(textAlignLeft()));
connect(f_align_center, SIGNAL(clicked(bool)), this, SLOT(textAlignCenter()));
connect(f_align_right, SIGNAL(clicked(bool)), this, SLOT(textAlignRight()));
connect(f_align_justify, SIGNAL(clicked(bool)), this, SLOT(textAlignJustify()));
QAction *removeFormat = new QAction(tr("Remove character formatting"), this);
removeFormat->setShortcut(QKeySequence("CTRL+M"));
connect(removeFormat, SIGNAL(triggered()), this, SLOT(textRemoveFormat()));
f_textedit->addAction(removeFormat);
QAction *removeAllFormat = new QAction(tr("Remove all formatting"), this);
connect(removeAllFormat, SIGNAL(triggered()), this, SLOT(textRemoveAllFormat()));
f_textedit->addAction(removeAllFormat);
QAction *textsource = new QAction(tr("Edit document source"), this);
textsource->setShortcut(QKeySequence("CTRL+O"));
connect(textsource, SIGNAL(triggered()), this, SLOT(textSource()));
f_textedit->addAction(textsource);
QMenu *menu = new QMenu(this);
menu->addAction(removeAllFormat);
menu->addAction(removeFormat);
menu->addAction(textsource);
f_menu->setMenu(menu);
f_menu->setPopupMode(QToolButton::InstantPopup);
// lists
f_list_bullet->setShortcut(Qt::CTRL + Qt::Key_Minus);
f_list_ordered->setShortcut(Qt::CTRL + Qt::Key_Equal);
connect(f_list_bullet, SIGNAL(clicked(bool)), this, SLOT(listBullet(bool)));
connect(f_list_ordered, SIGNAL(clicked(bool)), this, SLOT(listOrdered(bool)));
// indentation
f_indent_dec->setShortcut(Qt::CTRL + Qt::Key_Comma);
f_indent_inc->setShortcut(Qt::CTRL + Qt::Key_Period);
connect(f_indent_inc, SIGNAL(clicked()), this, SLOT(increaseIndentation()));
connect(f_indent_dec, SIGNAL(clicked()), this, SLOT(decreaseIndentation()));
// font size
QFontDatabase db;
foreach(int size, db.standardSizes())
f_fontsize->addItem(QString::number(size));
connect(f_fontsize, SIGNAL(activated(QString)),
this, SLOT(textSize(QString)));
f_fontsize->setCurrentIndex(f_fontsize->findText(QString::number(QApplication::font()
.pointSize())));
// text color
QPixmap pix(16, 16);
pix.fill(QApplication::palette().background().color());
f_bgcolor->setIcon(pix);
connect(f_bgcolor, SIGNAL(clicked()), this, SLOT(textBgColor()));
QPixmap pix2(16, 16);
pix2.fill(QApplication::palette().foreground().color());
f_fgcolor->setIcon(pix2);
connect(f_fgcolor, SIGNAL(clicked()), this, SLOT(textFgColor()));
// images
connect(f_image, SIGNAL(clicked()), this, SLOT(insertImage()));
}
void MRichTextEdit::textSource() {
QDialog *dialog = new QDialog(this);
QPlainTextEdit *pte = new QPlainTextEdit(dialog);
pte->setPlainText( f_textedit->toHtml() );
QGridLayout *gl = new QGridLayout(dialog);
gl->addWidget(pte,0,0,1,1);
dialog->setWindowTitle(tr("Document source"));
dialog->setMinimumWidth (400);
dialog->setMinimumHeight(600);
dialog->exec();
f_textedit->setHtml(pte->toPlainText());
delete dialog;
}
void MRichTextEdit::textRemoveFormat() {
QTextCharFormat fmt;
fmt.setFontWeight(QFont::Normal);
fmt.setFontUnderline (false);
fmt.setFontStrikeOut (false);
fmt.setFontItalic (false);
fmt.setFontPointSize (9);
// fmt.setFontFamily ("Helvetica");
// fmt.setFontStyleHint (QFont::SansSerif);
// fmt.setFontFixedPitch (true);
f_bold ->setChecked(false);
f_underline ->setChecked(false);
f_italic ->setChecked(false);
f_strikeout ->setChecked(false);
f_fontsize ->setCurrentIndex(f_fontsize->findText("9"));
// QTextBlockFormat bfmt = cursor.blockFormat();
// bfmt->setIndent(0);
fmt.clearBackground();
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textRemoveAllFormat() {
f_bold ->setChecked(false);
f_underline ->setChecked(false);
f_italic ->setChecked(false);
f_strikeout ->setChecked(false);
f_fontsize ->setCurrentIndex(f_fontsize->findText("9"));
QString text = f_textedit->toPlainText();
f_textedit->setPlainText(text);
}
void MRichTextEdit::textBold() {
QTextCharFormat fmt;
fmt.setFontWeight(f_bold->isChecked() ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::focusInEvent(QFocusEvent *) {
f_textedit->setFocus(Qt::TabFocusReason);
}
void MRichTextEdit::textUnderline() {
QTextCharFormat fmt;
fmt.setFontUnderline(f_underline->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textItalic() {
QTextCharFormat fmt;
fmt.setFontItalic(f_italic->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textStrikeout() {
QTextCharFormat fmt;
fmt.setFontStrikeOut(f_strikeout->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textSize(const QString &p) {
qreal pointSize = p.toFloat();
if (p.toFloat() > 0) {
QTextCharFormat fmt;
fmt.setFontPointSize(pointSize);
mergeFormatOnWordOrSelection(fmt);
}
}
void MRichTextEdit::textLink(bool checked) {
bool unlink = false;
QTextCharFormat fmt;
if (checked) {
QString url = f_textedit->currentCharFormat().anchorHref();
bool ok;
QString newUrl = QInputDialog::getText(this, tr("Create a link"),
tr("Link URL:"), QLineEdit::Normal,
url,
&ok);
if (ok) {
fmt.setAnchor(true);
fmt.setAnchorHref(newUrl);
fmt.setForeground(QApplication::palette().color(QPalette::Link));
fmt.setFontUnderline(true);
} else {
unlink = true;
}
} else {
unlink = true;
}
if (unlink) {
fmt.setAnchor(false);
fmt.setForeground(QApplication::palette().color(QPalette::Text));
fmt.setFontUnderline(false);
}
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textStyle(int index) {
QTextCursor cursor = f_textedit->textCursor();
cursor.beginEditBlock();
// standard
if (!cursor.hasSelection()) {
cursor.select(QTextCursor::BlockUnderCursor);
}
QTextCharFormat fmt;
cursor.setCharFormat(fmt);
f_textedit->setCurrentCharFormat(fmt);
if (index == ParagraphHeading1
|| index == ParagraphHeading2
|| index == ParagraphHeading3
|| index == ParagraphHeading4 ) {
if (index == ParagraphHeading1) {
fmt.setFontPointSize(m_fontsize_h1);
}
if (index == ParagraphHeading2) {
fmt.setFontPointSize(m_fontsize_h2);
}
if (index == ParagraphHeading3) {
fmt.setFontPointSize(m_fontsize_h3);
}
if (index == ParagraphHeading4) {
fmt.setFontPointSize(m_fontsize_h4);
}
if (index == ParagraphHeading2 || index == ParagraphHeading4) {
fmt.setFontItalic(true);
}
fmt.setFontWeight(QFont::Bold);
}
if (index == ParagraphMonospace) {
fmt = cursor.charFormat();
fmt.setFontFamily("Monospace");
fmt.setFontStyleHint(QFont::Monospace);
fmt.setFontFixedPitch(true);
}
cursor.setCharFormat(fmt);
f_textedit->setCurrentCharFormat(fmt);
cursor.endEditBlock();
}
void MRichTextEdit::textBgColor() {
QColor col = QColorDialog::getColor(f_textedit->textBackgroundColor(), this);
QTextCursor cursor = f_textedit->textCursor();
if (!cursor.hasSelection()) {
cursor.select(QTextCursor::WordUnderCursor);
}
QTextCharFormat fmt = cursor.charFormat();
if (col.isValid()) {
fmt.setBackground(col);
} else {
fmt.clearBackground();
}
cursor.setCharFormat(fmt);
f_textedit->setCurrentCharFormat(fmt);
bgColorChanged(col);
}
void MRichTextEdit::textFgColor()
{
QColor col = QColorDialog::getColor(f_textedit->textColor(), this);
QTextCursor cursor = f_textedit->textCursor();
if (!cursor.hasSelection()) {
cursor.select(QTextCursor::WordUnderCursor);
}
QTextCharFormat fmt = cursor.charFormat();
if (col.isValid()) {
fmt.setForeground(col);
} else {
fmt.clearForeground();
}
cursor.setCharFormat(fmt);
f_textedit->setCurrentCharFormat(fmt);
fgColorChanged(col);
}
void MRichTextEdit::textAlignLeft()
{
if (f_align_left->isChecked()) {
f_align_center->setChecked(false);
f_align_right->setChecked(false);
f_align_justify->setChecked(false);
QTextBlockFormat fmt;
fmt.setAlignment(Qt::AlignLeft);
mergeBlockFormatOnWordOrSelection(fmt);
}
}
void MRichTextEdit::textAlignCenter()
{
if (f_align_center->isChecked()) {
f_align_left->setChecked(false);
f_align_right->setChecked(false);
f_align_justify->setChecked(false);
QTextBlockFormat fmt;
fmt.setAlignment(Qt::AlignHCenter);
mergeBlockFormatOnWordOrSelection(fmt);
}
}
void MRichTextEdit::textAlignRight()
{
if (f_align_right->isChecked()) {
f_align_left->setChecked(false);
f_align_center->setChecked(false);
f_align_justify->setChecked(false);
QTextBlockFormat fmt;
fmt.setAlignment(Qt::AlignRight);
mergeBlockFormatOnWordOrSelection(fmt);
}
}
void MRichTextEdit::textAlignJustify()
{
if (f_align_justify->isChecked()) {
f_align_left->setChecked(false);
f_align_right->setChecked(false);
f_align_center->setChecked(false);
QTextBlockFormat fmt;
fmt.setAlignment(Qt::AlignJustify);
mergeBlockFormatOnWordOrSelection(fmt);
}
}
void MRichTextEdit::listBullet(bool checked) {
if (checked) {
f_list_ordered->setChecked(false);
}
list(checked, QTextListFormat::ListDisc);
}
void MRichTextEdit::listOrdered(bool checked) {
if (checked) {
f_list_bullet->setChecked(false);
}
list(checked, QTextListFormat::ListDecimal);
}
void MRichTextEdit::list(bool checked, QTextListFormat::Style style) {
QTextCursor cursor = f_textedit->textCursor();
cursor.beginEditBlock();
if (!checked) {
QTextBlockFormat obfmt = cursor.blockFormat();
QTextBlockFormat bfmt;
bfmt.setIndent(obfmt.indent());
cursor.setBlockFormat(bfmt);
} else {
QTextListFormat listFmt;
if (cursor.currentList()) {
listFmt = cursor.currentList()->format();
}
listFmt.setStyle(style);
cursor.createList(listFmt);
}
cursor.endEditBlock();
}
void MRichTextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format) {
QTextCursor cursor = f_textedit->textCursor();
if (!cursor.hasSelection()) {
cursor.select(QTextCursor::WordUnderCursor);
}
cursor.mergeCharFormat(format);
f_textedit->mergeCurrentCharFormat(format);
f_textedit->setFocus(Qt::TabFocusReason);
}
void MRichTextEdit::mergeBlockFormatOnWordOrSelection(const QTextBlockFormat &format) {
QTextCursor cursor = f_textedit->textCursor();
if (!cursor.hasSelection()) {
cursor.select(QTextCursor::WordUnderCursor);
}
cursor.mergeBlockFormat(format);
//f_textedit->mergeCurrentCharFormat(format);
f_textedit->setFocus(Qt::TabFocusReason);
}
void MRichTextEdit::slotCursorPositionChanged() {
QTextList *l = f_textedit->textCursor().currentList();
if (m_lastBlockList && (l == m_lastBlockList || (l != 0 && m_lastBlockList != 0
&& l->format().style() == m_lastBlockList->format().style()))) {
return;
}
m_lastBlockList = l;
if (l) {
QTextListFormat lfmt = l->format();
if (lfmt.style() == QTextListFormat::ListDisc) {
f_list_bullet->setChecked(true);
f_list_ordered->setChecked(false);
} else if (lfmt.style() == QTextListFormat::ListDecimal) {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(true);
} else {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(false);
}
} else {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(false);
}
Qt::Alignment alignment = f_textedit->textCursor().blockFormat().alignment();
switch (alignment) {
case Qt::AlignLeft:
if (!f_align_left->isChecked()) {
f_align_left->setChecked(true);
f_align_center->setChecked(false);
f_align_right->setChecked(false);
f_align_justify->setChecked(false);
}
break;
case Qt::AlignHCenter:
if (!f_align_center->isChecked()) {
f_align_left->setChecked(false);
f_align_center->setChecked(true);
f_align_right->setChecked(false);
f_align_justify->setChecked(false);
}
break;
case Qt::AlignRight:
if (!f_align_right->isChecked()) {
f_align_left->setChecked(false);
f_align_center->setChecked(false);
f_align_right->setChecked(true);
f_align_justify->setChecked(false);
}
break;
case Qt::AlignJustify:
if (!f_align_justify->isChecked()) {
f_align_left->setChecked(false);
f_align_center->setChecked(false);
f_align_right->setChecked(false);
f_align_justify->setChecked(true);
}
break;
default:
break;
}
}
void MRichTextEdit::fontChanged(const QFont &f) {
f_fontsize->setCurrentIndex(f_fontsize->findText(QString::number(f.pointSize())));
f_bold->setChecked(f.bold());
f_italic->setChecked(f.italic());
f_underline->setChecked(f.underline());
f_strikeout->setChecked(f.strikeOut());
if (f.pointSize() == m_fontsize_h1) {
f_paragraph->setCurrentIndex(ParagraphHeading1);
} else if (f.pointSize() == m_fontsize_h2) {
f_paragraph->setCurrentIndex(ParagraphHeading2);
} else if (f.pointSize() == m_fontsize_h3) {
f_paragraph->setCurrentIndex(ParagraphHeading3);
} else if (f.pointSize() == m_fontsize_h4) {
f_paragraph->setCurrentIndex(ParagraphHeading4);
} else {
if (f.fixedPitch() && f.family() == "Monospace") {
f_paragraph->setCurrentIndex(ParagraphMonospace);
} else {
f_paragraph->setCurrentIndex(ParagraphStandard);
}
}
if (f_textedit->textCursor().currentList()) {
QTextListFormat lfmt = f_textedit->textCursor().currentList()->format();
if (lfmt.style() == QTextListFormat::ListDisc) {
f_list_bullet->setChecked(true);
f_list_ordered->setChecked(false);
} else if (lfmt.style() == QTextListFormat::ListDecimal) {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(true);
} else {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(false);
}
} else {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(false);
}
}
void MRichTextEdit::bgColorChanged(const QColor &c) {
QPixmap pix(16, 16);
if (c.isValid()) {
pix.fill(c);
} else {
pix.fill(QApplication::palette().background().color());
}
f_bgcolor->setIcon(pix);
}
void MRichTextEdit::fgColorChanged(const QColor &c)
{
QPixmap pix(16, 16);
if (c.isValid()) {
pix.fill(c);
} else {
pix.fill(QApplication::palette().foreground().color());
}
f_fgcolor->setIcon(pix);
}
void MRichTextEdit::slotCurrentCharFormatChanged(const QTextCharFormat &format) {
fontChanged(format.font());
bgColorChanged((format.background().isOpaque()) ? format.background().color() : QColor());
fgColorChanged((format.foreground().isOpaque()) ? format.foreground().color() : QColor());
f_link->setChecked(format.isAnchor());
}
void MRichTextEdit::slotClipboardDataChanged() {
#ifndef QT_NO_CLIPBOARD
if (const QMimeData *md = QApplication::clipboard()->mimeData())
f_paste->setEnabled(md->hasText());
#endif
}
QString MRichTextEdit::toHtml() const {
QString s = f_textedit->toHtml();
// convert emails to links
s = s.replace(QRegExp("(<[^a][^>]+>(?:<span[^>]+>)?|\\s)([a-zA-Z\\d]+@[a-zA-Z\\d]+\\.[a-zA-Z]+)"), "\\1<a href=\"mailto:\\2\">\\2</a>");
// convert links
s = s.replace(QRegExp("(<[^a][^>]+>(?:<span[^>]+>)?|\\s)((?:https?|ftp|file)://[^\\s'\"<>]+)"), "\\1<a href=\"\\2\">\\2</a>");
return s;
}
QString MRichTextEdit::toHtmlNoFontSize() const
{
QString s = f_textedit->toHtml();
s.replace(QRegularExpression("font-size:[0-9]+pt\\s?"), "");
return s;
}
void MRichTextEdit::increaseIndentation() {
indent(+1);
}
void MRichTextEdit::decreaseIndentation() {
indent(-1);
}
void MRichTextEdit::indent(int delta) {
QTextCursor cursor = f_textedit->textCursor();
cursor.beginEditBlock();
QTextBlockFormat bfmt = cursor.blockFormat();
int ind = bfmt.indent();
if (ind + delta >= 0) {
bfmt.setIndent(ind + delta);
}
cursor.setBlockFormat(bfmt);
cursor.endEditBlock();
}
void MRichTextEdit::setText(const QString& text) {
if (text.isEmpty()) {
setPlainText(text);
return;
}
if (text[0] == '<') {
setHtml(text);
} else {
setPlainText(text);
}
}
void MRichTextEdit::insertImage() {
QSettings s;
QString attdir = s.value("general/filedialog-path").toString();
QString file = QFileDialog::getOpenFileName(this,
tr("Select an image"),
attdir,
tr("Images (*.png *.xpm *.jpg *.gif *.bmp);; All (*)"));
if (!file.isEmpty()) {
QImage image = QImageReader(file).read();
f_textedit->dropImage(image, QFileInfo(file).suffix().toUpper().toLocal8Bit().data());
}
}

102
widgets/mrichtextedit.h Normal file
View File

@@ -0,0 +1,102 @@
/*
** Copyright (C) 2013 Jiří Procházka (Hobrasoft)
** Contact: http://www.hobrasoft.cz/
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file is under the terms of the GNU Lesser General Public License
** version 2.1 as published by the Free Software Foundation and appearing
** in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the
** GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
*/
#ifndef _MRICHTEXTEDIT_H_
#define _MRICHTEXTEDIT_H_
#include <QPointer>
#include "ui_mrichtextedit.h"
/**
* @Brief A simple rich-text editor
*/
class MRichTextEdit : public QWidget, protected Ui::MRichTextEdit {
Q_OBJECT
public:
MRichTextEdit(QWidget *parent = 0);
QString toPlainText() const { return f_textedit->toPlainText(); }
QString toHtml() const;
QString toHtmlNoFontSize() const;
QTextDocument *document() { return f_textedit->document(); }
QTextCursor textCursor() const { return f_textedit->textCursor(); }
void setTextCursor(const QTextCursor& cursor) { f_textedit->setTextCursor(cursor); }
public slots:
void setText(const QString &text);
protected slots:
void setPlainText(const QString &text) { f_textedit->setPlainText(text); }
void setHtml(const QString &text) { f_textedit->setHtml(text); }
void textRemoveFormat();
void textRemoveAllFormat();
void textBold();
void textUnderline();
void textStrikeout();
void textItalic();
void textSize(const QString &p);
void textLink(bool checked);
void textStyle(int index);
void textBgColor();
void textFgColor();
void textAlignLeft();
void textAlignCenter();
void textAlignRight();
void textAlignJustify();
void listBullet(bool checked);
void listOrdered(bool checked);
void slotCurrentCharFormatChanged(const QTextCharFormat &format);
void slotCursorPositionChanged();
void slotClipboardDataChanged();
void increaseIndentation();
void decreaseIndentation();
void insertImage();
void textSource();
protected:
void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
void mergeBlockFormatOnWordOrSelection(const QTextBlockFormat &format);
void fontChanged(const QFont &f);
void bgColorChanged(const QColor &c);
void fgColorChanged(const QColor &c);
void list(bool checked, QTextListFormat::Style style);
void indent(int delta);
void focusInEvent(QFocusEvent *event);
QStringList m_paragraphItems;
int m_fontsize_h1;
int m_fontsize_h2;
int m_fontsize_h3;
int m_fontsize_h4;
enum ParagraphItems { ParagraphStandard = 0,
ParagraphHeading1,
ParagraphHeading2,
ParagraphHeading3,
ParagraphHeading4,
ParagraphMonospace };
QPointer<QTextList> m_lastBlockList;
};
#endif

704
widgets/mrichtextedit.ui Normal file
View File

@@ -0,0 +1,704 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MRichTextEdit</class>
<widget class="QWidget" name="MRichTextEdit">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>576</width>
<height>290</height>
</rect>
</property>
<property name="font">
<font>
<family>MS Shell Dlg 2</family>
</font>
</property>
<property name="windowTitle">
<string notr="true"/>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>1</number>
</property>
<property name="leftMargin">
<number>1</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>1</number>
</property>
<property name="bottomMargin">
<number>1</number>
</property>
<item>
<widget class="QWidget" name="f_toolbar" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QComboBox" name="f_paragraph">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Paragraph formatting</string>
</property>
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_undo">
<property name="enabled">
<bool>false</bool>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Undo (CTRL+Z)</string>
</property>
<property name="text">
<string>Undo</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Undo-96.png</normaloff>:/res/icons_textedit/Undo-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_redo">
<property name="enabled">
<bool>false</bool>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Redo</string>
</property>
<property name="text">
<string>Redo</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Redo-96.png</normaloff>:/res/icons_textedit/Redo-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_cut">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Cut (CTRL+X)</string>
</property>
<property name="text">
<string>Cut</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Cut-96.png</normaloff>:/res/icons_textedit/Cut-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_copy">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Copy (CTRL+C)</string>
</property>
<property name="text">
<string>Copy</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Copy-96.png</normaloff>:/res/icons_textedit/Copy-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_paste">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Paste (CTRL+V)</string>
</property>
<property name="text">
<string>Paste</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Paste-96.png</normaloff>:/res/icons_textedit/Paste-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_link">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Link (CTRL+L)</string>
</property>
<property name="text">
<string>Link</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Link-96.png</normaloff>:/res/icons_textedit/Link-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="f_fontsize">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Font size</string>
</property>
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_6">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_image">
<property name="toolTip">
<string>Insert image...</string>
</property>
<property name="text">
<string notr="true">Insert Image</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Add Image-96.png</normaloff>:/res/icons_textedit/Add Image-96.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="f_menu">
<property name="toolTip">
<string>Menu</string>
</property>
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Menu 2-96.png</normaloff>:/res/icons_textedit/Menu 2-96.png</iconset>
</property>
</widget>
</item>
</layout>
<zorder>f_paragraph</zorder>
<zorder>line_4</zorder>
<zorder>f_undo</zorder>
<zorder>f_redo</zorder>
<zorder>f_cut</zorder>
<zorder>f_copy</zorder>
<zorder>f_paste</zorder>
<zorder>line</zorder>
<zorder>f_link</zorder>
<zorder>line_3</zorder>
<zorder>f_fontsize</zorder>
<zorder>f_image</zorder>
<zorder>line_6</zorder>
<zorder>f_menu</zorder>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QToolButton" name="f_bold">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string notr="true">Bold (CTRL+B)</string>
</property>
<property name="text">
<string>Bold</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Bold-96.png</normaloff>:/res/icons_textedit/Bold-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_italic">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Italic (CTRL+I)</string>
</property>
<property name="text">
<string>Italic</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Italic-96.png</normaloff>:/res/icons_textedit/Italic-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_underline">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Underline (CTRL+U)</string>
</property>
<property name="text">
<string>Underline</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Text Color-96.png</normaloff>:/res/icons_textedit/Text Color-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_strikeout">
<property name="text">
<string>Strike Out</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Strikethrough-96.png</normaloff>:/res/icons_textedit/Strikethrough-96.png</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_list_bullet">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Bullet list (CTRL+-)</string>
</property>
<property name="text">
<string>Bullet list</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Bulleted List-96.png</normaloff>:/res/icons_textedit/Bulleted List-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_list_ordered">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Ordered list (CTRL+=)</string>
</property>
<property name="text">
<string>Ordered list</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Numbered List-96.png</normaloff>:/res/icons_textedit/Numbered List-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_indent_dec">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Decrease indentation (CTRL+,)</string>
</property>
<property name="text">
<string>Decrease indentation</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Indent-96.png</normaloff>:/res/icons_textedit/Indent-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_indent_inc">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Increase indentation (CTRL+.)</string>
</property>
<property name="text">
<string>Increase indentation</string>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Outdent-96.png</normaloff>:/res/icons_textedit/Outdent-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_bgcolor">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="toolTip">
<string>Text background color</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Fill Color-96.png</normaloff>:/res/icons_textedit/Fill Color-96.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_fgcolor">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Text color</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Fill Color-96.png</normaloff>:/res/icons_textedit/Fill Color-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_7">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_align_left">
<property name="toolTip">
<string>Align Left</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Align Left-96.png</normaloff>:/res/icons_textedit/Align Left-96.png</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_align_center">
<property name="toolTip">
<string>Align Center</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Align Center-96.png</normaloff>:/res/icons_textedit/Align Center-96.png</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_align_right">
<property name="toolTip">
<string>Align Right</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Align Right-96.png</normaloff>:/res/icons_textedit/Align Right-96.png</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="f_align_justify">
<property name="toolTip">
<string>Align Justify</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons_textedit/Align Justify-96.png</normaloff>:/res/icons_textedit/Align Justify-96.png</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>192</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="MTextEdit" name="f_textedit">
<property name="autoFormatting">
<set>QTextEdit::AutoNone</set>
</property>
<property name="tabChangesFocus">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>MTextEdit</class>
<extends>QTextEdit</extends>
<header>widgets/mtextedit.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>f_textedit</tabstop>
<tabstop>f_image</tabstop>
<tabstop>f_menu</tabstop>
</tabstops>
<resources>
<include location="../res.qrc"/>
</resources>
<connections/>
</ui>

101
widgets/mtextedit.cpp Normal file
View File

@@ -0,0 +1,101 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "mtextedit.h"
#include <QTextDocument>
#include <QTextCursor>
#include <QImage>
#include <QByteArray>
#include <QBuffer>
#include <stdlib.h>
#include <QInputDialog>
MTextEdit::MTextEdit(QWidget *parent) : QTextEdit(parent) {
}
bool MTextEdit::canInsertFromMimeData(const QMimeData *source) const {
return source->hasImage() || QTextEdit::canInsertFromMimeData(source);
}
void MTextEdit::insertFromMimeData(const QMimeData *source) {
if (source->hasImage()) {
QStringList formats = source->formats();
QString format;
for (int i=0; i<formats.size(); i++) {
if (formats[i] == "image/bmp") { format = "BMP"; break; }
if (formats[i] == "image/jpeg") { format = "JPG"; break; }
if (formats[i] == "image/jpg") { format = "JPG"; break; }
if (formats[i] == "image/gif") { format = "GIF"; break; }
if (formats[i] == "image/png") { format = "PNG"; break; }
if (formats[i] == "image/pbm") { format = "PBM"; break; }
if (formats[i] == "image/pgm") { format = "PGM"; break; }
if (formats[i] == "image/ppm") { format = "PPM"; break; }
if (formats[i] == "image/tiff") { format = "TIFF"; break; }
if (formats[i] == "image/xbm") { format = "XBM"; break; }
if (formats[i] == "image/xpm") { format = "XPM"; break; }
}
if (!format.isEmpty()) {
// dropImage(qvariant_cast<QImage>(source->imageData()), format);
dropImage(qvariant_cast<QImage>(source->imageData()), "JPG");
return;
}
}
QTextEdit::insertFromMimeData(source);
}
QMimeData *MTextEdit::createMimeDataFromSelection() const {
return QTextEdit::createMimeDataFromSelection();
}
void MTextEdit::dropImage(const QImage& image, const QString& format) {
bool ok;
int w = QInputDialog::getInt(this, tr("Insert Image"),
tr("Enter image width to use (source width: %1)").arg(image.width()),
400, 5, 1920, 20, &ok);
if (ok) {
int h = (w * image.height()) / image.width();
QImage img1 = image.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
img1.save(&buffer, format.toLocal8Bit().data());
buffer.close();
QByteArray base64 = bytes.toBase64();
QByteArray base64l;
for (int i=0; i<base64.size(); i++) {
base64l.append(base64[i]);
if (i%80 == 0) {
base64l.append("\n");
}
}
QTextCursor cursor = textCursor();
QTextImageFormat imageFormat;
imageFormat.setWidth ( img1.width() );
imageFormat.setHeight ( img1.height() );
imageFormat.setName ( QString("data:image/%1;base64,%2")
.arg(QString("%1.%2").arg(rand()).arg(format))
.arg(base64l.data())
);
cursor.insertImage ( imageFormat );
}
}

40
widgets/mtextedit.h Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef _MTEXTEDIT_H_
#define _MTEXTEDIT_H_
#include <QTextEdit>
#include <QMimeData>
#include <QImage>
class MTextEdit : public QTextEdit {
Q_OBJECT
public:
MTextEdit(QWidget *parent);
void dropImage(const QImage& image, const QString& format);
protected:
bool canInsertFromMimeData(const QMimeData *source) const;
void insertFromMimeData(const QMimeData *source);
QMimeData *createMimeDataFromSelection() const;
};
#endif

103
widgets/pagelistitem.cpp Normal file
View File

@@ -0,0 +1,103 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "pagelistitem.h"
#include <QHBoxLayout>
PageListItem::PageListItem(QString name,
QString icon,
QString groupIcon,
QWidget *parent) : QWidget(parent)
{
mIconLabel = new QLabel;
mNameLabel = new QLabel;
mGroupLabel = new QLabel;
mSpaceStart = new QSpacerItem(2, 0);
mIconLabel->setScaledContents(true);
mGroupLabel->setScaledContents(true);
setName(name);
setIcon(icon);
setGroupIcon(groupIcon);
QHBoxLayout *layout = new QHBoxLayout;
layout->setMargin(0);
layout->addSpacerItem(mSpaceStart);
layout->addWidget(mIconLabel);
layout->addWidget(mNameLabel);
layout->addStretch();
layout->addWidget(mGroupLabel);
layout->addSpacing(2);
this->setLayout(layout);
}
void PageListItem::setName(const QString &name)
{
mNameLabel->setText(name);
}
void PageListItem::setIcon(const QString &path)
{
if (!path.isEmpty()) {
mIconLabel->setPixmap(QPixmap(path));
QFontMetrics fm(this->font());
int height = fm.height() * 0.9;
mIconLabel->setFixedSize(height, height);
} else {
mIconLabel->setPixmap(QPixmap());
}
}
void PageListItem::setGroupIcon(const QString &path)
{
if (!path.isEmpty()) {
QPixmap pix(path);
mGroupLabel->setPixmap(pix);
QFontMetrics fm(this->font());
int height = fm.height() * 0.9;
mGroupLabel->setFixedSize((height * pix.width()) / pix.height(), height);
} else {
mSpaceStart->changeSize(2, 0);
mGroupLabel->setPixmap(QPixmap());
}
}
QString PageListItem::name()
{
return mNameLabel->text();
}
void PageListItem::setBold(bool bold)
{
QFont f = mNameLabel->font();
f.setBold(bold);
mNameLabel->setFont(f);
}
void PageListItem::setIndented(bool indented)
{
mSpaceStart->changeSize(indented ? 15 : 2, 0);
}

57
widgets/pagelistitem.h Normal file
View File

@@ -0,0 +1,57 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PAGELISTITEM_H
#define PAGELISTITEM_H
#include <QWidget>
#include <QPixmap>
#include <QLabel>
#include <QSpacerItem>
#include <QResizeEvent>
#include "datatypes.h"
class PageListItem : public QWidget
{
Q_OBJECT
public:
explicit PageListItem(QString name = "",
QString icon = "",
QString groupIcon = "",
QWidget *parent = 0);
void setName(const QString &name);
void setIcon(const QString &path);
void setGroupIcon(const QString &path);
QString name();
void setBold(bool bold);
void setIndented(bool indented);
signals:
public slots:
private:
QLabel *mIconLabel;
QLabel *mNameLabel;
QLabel *mGroupLabel;
QSpacerItem *mSpaceStart;
};
#endif // PAGELISTITEM_H

64
widgets/paramdialog.cpp Normal file
View File

@@ -0,0 +1,64 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "paramdialog.h"
#include "ui_paramdialog.h"
ParamDialog::ParamDialog(QString title,
QString text,
ConfigParams *params,
QStringList names,
QWidget *parent) :
QDialog(parent),
ui(new Ui::ParamDialog)
{
ui->setupUi(this);
setWindowTitle(title);
ui->textLabel->setText(text);
mConfig = *params;
for (QString s: names) {
ConfigParam *p = mConfig.getParam(s);
if (p) {
p->transmittable = false; // To hide the read buttons.
ui->paramTable->addParamRow(&mConfig, s);
}
}
}
ParamDialog::~ParamDialog()
{
delete ui;
}
void ParamDialog::showParams(QString title,
QString text,
ConfigParams *params,
QStringList names,
QWidget *parent)
{
ParamDialog *p = new ParamDialog(title, text, params, names, parent);
p->exec();
}
void ParamDialog::on_closeButton_clicked()
{
close();
}

57
widgets/paramdialog.h Normal file
View File

@@ -0,0 +1,57 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARAMDIALOG_H
#define PARAMDIALOG_H
#include <QDialog>
#include "configparams.h"
namespace Ui {
class ParamDialog;
}
class ParamDialog : public QDialog
{
Q_OBJECT
public:
explicit ParamDialog(QString title,
QString text,
ConfigParams *params,
QStringList names,
QWidget *parent = 0);
~ParamDialog();
static void showParams(QString title,
QString text,
ConfigParams *params,
QStringList names,
QWidget *parent = 0);
private slots:
void on_closeButton_clicked();
private:
Ui::ParamDialog *ui;
ConfigParams mConfig;
};
#endif // PARAMDIALOG_H

65
widgets/paramdialog.ui Normal file
View File

@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ParamDialog</class>
<widget class="QDialog" name="ParamDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>557</width>
<height>363</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="textLabel">
<property name="text">
<string/>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="ParamTable" name="paramTable"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="closeButton">
<property name="text">
<string>Close</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>ParamTable</class>
<extends>QTableWidget</extends>
<header>widgets/paramtable.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

101
widgets/parameditbool.cpp Normal file
View File

@@ -0,0 +1,101 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "parameditbool.h"
#include "ui_parameditbool.h"
#include <QMessageBox>
#include "helpdialog.h"
ParamEditBool::ParamEditBool(QWidget *parent) :
QWidget(parent),
ui(new Ui::ParamEditBool)
{
ui->setupUi(this);
}
ParamEditBool::~ParamEditBool()
{
delete ui;
}
void ParamEditBool::setConfig(ConfigParams *config)
{
mConfig = config;
ConfigParam *param = mConfig->getParam(mName);
if (param) {
ui->readButton->setVisible(param->transmittable);
ui->readDefaultButton->setVisible(param->transmittable);
ui->valueBox->setCurrentIndex(param->valInt ? 1 : 0);
}
connect(mConfig, SIGNAL(paramChangedBool(QObject*,QString,bool)),
this, SLOT(paramChangedBool(QObject*,QString,bool)));
}
QString ParamEditBool::name() const
{
return mName;
}
void ParamEditBool::setName(const QString &name)
{
mName = name;
}
void ParamEditBool::paramChangedBool(QObject *src, QString name, bool newParam)
{
if (src != this && name == mName) {
ui->valueBox->setCurrentIndex(newParam ? 1 : 0);
}
}
void ParamEditBool::on_readButton_clicked()
{
if (mConfig) {
mConfig->setUpdateOnly(mName);
mConfig->requestUpdate();
}
}
void ParamEditBool::on_readDefaultButton_clicked()
{
if (mConfig) {
mConfig->setUpdateOnly(mName);
mConfig->requestUpdateDefault();
}
}
void ParamEditBool::on_helpButton_clicked()
{
if (mConfig) {
HelpDialog::showHelp(this, mConfig, mName);
}
}
void ParamEditBool::on_valueBox_currentIndexChanged(int index)
{
if (mConfig) {
if (mConfig->getUpdateOnly() != mName) {
mConfig->setUpdateOnly("");
}
mConfig->updateParamBool(mName, index, this);
}
}

55
widgets/parameditbool.h Normal file
View File

@@ -0,0 +1,55 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARAMEDITBOOL_H
#define PARAMEDITBOOL_H
#include <QWidget>
#include "configparams.h"
namespace Ui {
class ParamEditBool;
}
class ParamEditBool : public QWidget
{
Q_OBJECT
public:
explicit ParamEditBool(QWidget *parent = 0);
~ParamEditBool();
void setConfig(ConfigParams *config);
QString name() const;
void setName(const QString &name);
private slots:
void paramChangedBool(QObject *src, QString name, bool newParam);
void on_readButton_clicked();
void on_readDefaultButton_clicked();
void on_helpButton_clicked();
void on_valueBox_currentIndexChanged(int index);
private:
Ui::ParamEditBool *ui;
ConfigParams *mConfig;
QString mName;
};
#endif // PARAMEDITBOOL_H

112
widgets/parameditbool.ui Normal file
View File

@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ParamEditBool</class>
<widget class="QWidget" name="ParamEditBool">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>285</width>
<height>29</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QComboBox" name="valueBox">
<item>
<property name="text">
<string>False</string>
</property>
</item>
<item>
<property name="text">
<string>True</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QPushButton" name="readButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Read current setting</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Upload-96.png</normaloff>:/res/icons/Upload-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="readDefaultButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Read default setting</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Data Backup-96.png</normaloff>:/res/icons/Data Backup-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="helpButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Show help</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Help-96.png</normaloff>:/res/icons/Help-96.png</iconset>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../res.qrc"/>
</resources>
<connections/>
</ui>

200
widgets/parameditdouble.cpp Normal file
View File

@@ -0,0 +1,200 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "parameditdouble.h"
#include "ui_parameditdouble.h"
#include <QMessageBox>
#include "helpdialog.h"
#include <cmath>
ParamEditDouble::ParamEditDouble(QWidget *parent) :
QWidget(parent),
ui(new Ui::ParamEditDouble)
{
ui->setupUi(this);
mConfig = 0;
mMaxVal = 1.0;
mDisplay = new DisplayPercentage(this);
mDoubleBox = new QDoubleSpinBox(this);
mPercentageBox = new QSpinBox(this);
mPercentageBox->setSuffix(" %");
ui->mainLayout->insertWidget(0, mDisplay);
ui->mainLayout->insertWidget(0, mDoubleBox);
ui->mainLayout->insertWidget(0, mPercentageBox);
ui->mainLayout->setStretchFactor(mDoubleBox, 1);
ui->mainLayout->setStretchFactor(mPercentageBox, 1);
ui->mainLayout->setStretchFactor(mDisplay, 10);
mPercentageBox->setVisible(false);
mDisplay->setVisible(false);
connect(mPercentageBox, SIGNAL(valueChanged(int)),
this, SLOT(percentageChanged(int)));
connect(mDoubleBox, SIGNAL(valueChanged(double)),
this, SLOT(doubleChanged(double)));
}
ParamEditDouble::~ParamEditDouble()
{
delete ui;
}
void ParamEditDouble::setConfig(ConfigParams *config)
{
mConfig = config;
ConfigParam *param = mConfig->getParam(mName);
if (param) {
ui->readButton->setVisible(param->transmittable);
ui->readDefaultButton->setVisible(param->transmittable);
mParam = *param;
mMaxVal = fabs(mParam.maxDouble) > fabs(mParam.minDouble) ?
fabs(mParam.maxDouble) : fabs(mParam.minDouble);
mDoubleBox->setMaximum(mParam.maxDouble * mParam.editorScale);
mDoubleBox->setMinimum(mParam.minDouble * mParam.editorScale);
mDoubleBox->setSingleStep(mParam.stepDouble);
mDoubleBox->setValue(mParam.valDouble * mParam.editorScale);
int p = (mParam.valDouble * 100.0) / mMaxVal;
mPercentageBox->setMaximum((100.0 * mParam.maxDouble) / mMaxVal);
mPercentageBox->setMinimum((100.0 * mParam.minDouble) / mMaxVal);
mPercentageBox->setValue(p);
mDisplay->setDual(mParam.minDouble < 0.0 && mParam.maxDouble > 0.0);
// Rounding...
if (!mParam.editAsPercentage) {
updateDisplay(mParam.valDouble);
}
}
connect(mConfig, SIGNAL(paramChangedDouble(QObject*,QString,double)),
this, SLOT(paramChangedDouble(QObject*,QString,double)));
}
QString ParamEditDouble::name() const
{
return mName;
}
void ParamEditDouble::setName(const QString &name)
{
mName = name;
}
void ParamEditDouble::setSuffix(const QString &suffix)
{
mDoubleBox->setSuffix(suffix);
}
void ParamEditDouble::setDecimals(int decimals)
{
mDoubleBox->setDecimals(decimals);
}
void ParamEditDouble::setShowAsPercentage(bool showAsPercentage)
{
mDoubleBox->setVisible(!showAsPercentage);
mPercentageBox->setVisible(showAsPercentage);
}
void ParamEditDouble::showDisplay(bool show)
{
mDisplay->setVisible(show);
}
void ParamEditDouble::paramChangedDouble(QObject *src, QString name, double newParam)
{
if (src != this && name == mName) {
mPercentageBox->setValue(round((100.0 * newParam) / mMaxVal));
mDoubleBox->setValue(newParam * mParam.editorScale);
updateDisplay(newParam);
}
}
void ParamEditDouble::percentageChanged(int p)
{
if (mParam.editAsPercentage) {
double val = ((double)p / 100.0) * mMaxVal;
if (mConfig) {
if (mConfig->getUpdateOnly() != mName) {
mConfig->setUpdateOnly("");
}
mConfig->updateParamDouble(mName, val, this);
}
updateDisplay(val);
}
}
void ParamEditDouble::doubleChanged(double d)
{
if (!mParam.editAsPercentage) {
double val = d / mParam.editorScale;
if (mConfig) {
if (mConfig->getUpdateOnly() != mName) {
mConfig->setUpdateOnly("");
}
mConfig->updateParamDouble(mName, val, this);
}
updateDisplay(val);
}
}
void ParamEditDouble::on_readButton_clicked()
{
if (mConfig) {
mConfig->setUpdateOnly(mName);
mConfig->requestUpdate();
}
}
void ParamEditDouble::on_readDefaultButton_clicked()
{
if (mConfig) {
mConfig->setUpdateOnly(mName);
mConfig->requestUpdateDefault();
}
}
void ParamEditDouble::on_helpButton_clicked()
{
if (mConfig) {
HelpDialog::showHelp(this, mConfig, mName);
}
}
void ParamEditDouble::updateDisplay(double val)
{
double p = (100.0 * val) / mMaxVal;
mDisplay->setValue(p);
mDisplay->setText(tr("%1%2").
arg(val * mParam.editorScale, 0, 'f', mParam.editorDecimalsDouble).
arg(mParam.suffix));
}

73
widgets/parameditdouble.h Normal file
View File

@@ -0,0 +1,73 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARAMEDITDOUBLE_H
#define PARAMEDITDOUBLE_H
#include <QWidget>
#include <QHBoxLayout>
#include <QDoubleSpinBox>
#include <QSpinBox>
#include "configparams.h"
#include "displaypercentage.h"
namespace Ui {
class ParamEditDouble;
}
class ParamEditDouble : public QWidget
{
Q_OBJECT
public:
explicit ParamEditDouble(QWidget *parent = 0);
~ParamEditDouble();
void setConfig(ConfigParams *config);
QString name() const;
void setName(const QString &name);
void setSuffix(const QString &suffix);
void setDecimals(int decimals);
void setShowAsPercentage(bool showAsPercentage);
void showDisplay(bool show);
private slots:
void paramChangedDouble(QObject *src, QString name, double newParam);
void percentageChanged(int p);
void doubleChanged(double d);
void on_readButton_clicked();
void on_readDefaultButton_clicked();
void on_helpButton_clicked();
private:
Ui::ParamEditDouble *ui;
ConfigParams *mConfig;
ConfigParam mParam;
QString mName;
double mMaxVal;
DisplayPercentage *mDisplay;
QDoubleSpinBox *mDoubleBox;
QSpinBox *mPercentageBox;
void updateDisplay(double val);
};
#endif // PARAMEDITDOUBLE_H

102
widgets/parameditdouble.ui Normal file
View File

@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ParamEditDouble</class>
<widget class="QWidget" name="ParamEditDouble">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>86</width>
<height>26</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="mainLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="readButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Read current setting</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Upload-96.png</normaloff>:/res/icons/Upload-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="readDefaultButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Read default setting</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Data Backup-96.png</normaloff>:/res/icons/Data Backup-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="helpButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Show help</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Help-96.png</normaloff>:/res/icons/Help-96.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources>
<include location="../res.qrc"/>
</resources>
<connections/>
</ui>

107
widgets/parameditenum.cpp Normal file
View File

@@ -0,0 +1,107 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "parameditenum.h"
#include "ui_parameditenum.h"
#include <QDebug>
#include "helpdialog.h"
ParamEditEnum::ParamEditEnum(QWidget *parent) :
QWidget(parent),
ui(new Ui::ParamEditEnum)
{
ui->setupUi(this);
mConfig = 0;
}
ParamEditEnum::~ParamEditEnum()
{
delete ui;
}
void ParamEditEnum::setConfig(ConfigParams *config)
{
mConfig = config;
ConfigParam *param = mConfig->getParam(mName);
if (param) {
ui->readButton->setVisible(param->transmittable);
ui->readDefaultButton->setVisible(param->transmittable);
int val = param->valInt;
ui->valueBox->insertItems(0, param->enumNames);
if (val >= 0 && val < param->enumNames.size()) {
ui->valueBox->setCurrentIndex(val);
}
}
connect(mConfig, SIGNAL(paramChangedEnum(QObject*,QString,int)),
this, SLOT(paramChangedEnum(QObject*,QString,int)));
}
QString ParamEditEnum::name() const
{
return mName;
}
void ParamEditEnum::setName(const QString &name)
{
mName = name;
}
void ParamEditEnum::paramChangedEnum(QObject *src, QString name, int newParam)
{
if (src != this && name == mName) {
ui->valueBox->setCurrentIndex(newParam);
}
}
void ParamEditEnum::on_readButton_clicked()
{
if (mConfig) {
mConfig->setUpdateOnly(mName);
mConfig->requestUpdate();
}
}
void ParamEditEnum::on_readDefaultButton_clicked()
{
if (mConfig) {
mConfig->setUpdateOnly(mName);
mConfig->requestUpdateDefault();
}
}
void ParamEditEnum::on_helpButton_clicked()
{
if (mConfig) {
HelpDialog::showHelp(this, mConfig, mName);
}
}
void ParamEditEnum::on_valueBox_currentIndexChanged(int index)
{
if (mConfig) {
if (mConfig->getUpdateOnly() != mName) {
mConfig->setUpdateOnly("");
}
mConfig->updateParamEnum(mName, index, this);
}
}

56
widgets/parameditenum.h Normal file
View File

@@ -0,0 +1,56 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARAMEDITENUM_H
#define PARAMEDITENUM_H
#include <QWidget>
#include "configparams.h"
namespace Ui {
class ParamEditEnum;
}
class ParamEditEnum : public QWidget
{
Q_OBJECT
public:
explicit ParamEditEnum(QWidget *parent = 0);
~ParamEditEnum();
void setConfig(ConfigParams *config);
QString name() const;
void setName(const QString &name);
private slots:
void paramChangedEnum(QObject *src, QString name, int newParam);
void on_readButton_clicked();
void on_readDefaultButton_clicked();
void on_helpButton_clicked();
void on_valueBox_currentIndexChanged(int index);
private:
Ui::ParamEditEnum *ui;
ConfigParams *mConfig;
QString mName;
};
#endif // PARAMEDITENUM_H

101
widgets/parameditenum.ui Normal file
View File

@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ParamEditEnum</class>
<widget class="QWidget" name="ParamEditEnum">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>302</width>
<height>29</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QComboBox" name="valueBox"/>
</item>
<item>
<widget class="QPushButton" name="readButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Read current setting</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Upload-96.png</normaloff>:/res/icons/Upload-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="readDefaultButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Read default setting</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Data Backup-96.png</normaloff>:/res/icons/Data Backup-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="helpButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Show help</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Help-96.png</normaloff>:/res/icons/Help-96.png</iconset>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../res.qrc"/>
</resources>
<connections/>
</ui>

210
widgets/parameditint.cpp Normal file
View File

@@ -0,0 +1,210 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "parameditint.h"
#include "ui_parameditint.h"
#include <QMessageBox>
#include <cstdlib>
#include "helpdialog.h"
ParamEditInt::ParamEditInt(QWidget *parent) :
QWidget(parent),
ui(new Ui::ParamEditInt)
{
ui->setupUi(this);
mConfig = 0;
mMaxVal = 1;
mDisplay = new DisplayPercentage(this);
mIntBox = new QSpinBox(this);
mPercentageBox = new QSpinBox(this);
mPercentageBox->setSuffix(" %");
ui->mainLayout->insertWidget(0, mDisplay);
ui->mainLayout->insertWidget(0, mIntBox);
ui->mainLayout->insertWidget(0, mPercentageBox);
ui->mainLayout->setStretchFactor(mIntBox, 1);
ui->mainLayout->setStretchFactor(mPercentageBox, 1);
ui->mainLayout->setStretchFactor(mDisplay, 10);
mPercentageBox->setVisible(false);
mDisplay->setVisible(false);
connect(mIntBox, SIGNAL(valueChanged(int)),
this, SLOT(intChanged(int)));
connect(mPercentageBox, SIGNAL(valueChanged(int)),
this, SLOT(percentageChanged(int)));
}
ParamEditInt::~ParamEditInt()
{
delete ui;
}
void ParamEditInt::setConfig(ConfigParams *config)
{
mConfig = config;
ConfigParam *param = mConfig->getParam(mName);
if (param) {
ui->readButton->setVisible(param->transmittable);
ui->readDefaultButton->setVisible(param->transmittable);
mParam = *param;
mMaxVal = abs(mParam.maxInt) > abs(mParam.minInt) ?
abs(mParam.maxInt) : abs(mParam.minInt);
mIntBox->setMaximum(multScale(mParam.maxInt));
mIntBox->setMinimum(multScale(mParam.minInt));
mIntBox->setSingleStep(mParam.stepInt);
mIntBox->setValue(multScale(mParam.valInt));
int p = (mParam.valInt * 100) / mMaxVal;
mPercentageBox->setMaximum((100 * mParam.maxInt) / mMaxVal);
mPercentageBox->setMinimum((100 * mParam.minInt) / mMaxVal);
mPercentageBox->setValue(p);
mDisplay->setDual(mParam.minInt < 0 && mParam.maxInt > 0);
// Rounding...
if (!mParam.editAsPercentage) {
updateDisplay(mParam.valInt);
}
}
connect(mConfig, SIGNAL(paramChangedInt(QObject*,QString,int)),
this, SLOT(paramChangedInt(QObject*,QString,int)));
}
QString ParamEditInt::name() const
{
return mName;
}
void ParamEditInt::setName(const QString &name)
{
mName = name;
}
void ParamEditInt::setSuffix(const QString &suffix)
{
mIntBox->setSuffix(suffix);
}
void ParamEditInt::setShowAsPercentage(bool showAsPercentage)
{
mIntBox->setVisible(!showAsPercentage);
mPercentageBox->setVisible(showAsPercentage);
}
void ParamEditInt::showDisplay(bool show)
{
mDisplay->setVisible(show);
}
void ParamEditInt::paramChangedInt(QObject *src, QString name, int newParam)
{
if (src != this && name == mName) {
mIntBox->setValue(multScale(newParam));
}
}
void ParamEditInt::percentageChanged(int p)
{
if (mParam.editAsPercentage) {
int val = (p * mMaxVal) / 100;
if (mConfig) {
if (mConfig->getUpdateOnly() != mName) {
mConfig->setUpdateOnly("");
}
mConfig->updateParamInt(mName, val, this);
}
updateDisplay(val);
}
}
void ParamEditInt::intChanged(int i)
{
if (!mParam.editAsPercentage) {
int val = divScale(i);
if (mConfig) {
if (mConfig->getUpdateOnly() != mName) {
mConfig->setUpdateOnly("");
}
mConfig->updateParamInt(mName, val, this);
}
updateDisplay(val);
}
}
void ParamEditInt::on_readButton_clicked()
{
if (mConfig) {
mConfig->setUpdateOnly(mName);
mConfig->requestUpdate();
}
}
void ParamEditInt::on_readDefaultButton_clicked()
{
if (mConfig) {
mConfig->setUpdateOnly(mName);
mConfig->requestUpdateDefault();
}
}
void ParamEditInt::on_helpButton_clicked()
{
if (mConfig) {
HelpDialog::showHelp(this, mConfig, mName);
}
}
void ParamEditInt::updateDisplay(int val)
{
int p = (100 * val) / mMaxVal;
mDisplay->setValue(p);
if (mParam.editAsPercentage) {
mDisplay->setText(tr("%1%2").
arg((double)val * mParam.editorScale, 0, 'f', 2).
arg(mParam.suffix));
} else {
mDisplay->setText(tr("%1%2").
arg(multScale(val)).
arg(mParam.suffix));
}
}
int ParamEditInt::multScale(int val)
{
return (int)((double)val * mParam.editorScale);
}
int ParamEditInt::divScale(int val)
{
return (int)((double)val / mParam.editorScale);
}

73
widgets/parameditint.h Normal file
View File

@@ -0,0 +1,73 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARAMEDITINT_H
#define PARAMEDITINT_H
#include <QWidget>
#include <QSpinBox>
#include "configparams.h"
#include "displaypercentage.h"
namespace Ui {
class ParamEditInt;
}
class ParamEditInt : public QWidget
{
Q_OBJECT
public:
explicit ParamEditInt(QWidget *parent = 0);
~ParamEditInt();
void setConfig(ConfigParams *config);
QString name() const;
void setName(const QString &name);
void setSuffix(const QString &suffix);
void setShowAsPercentage(bool showAsPercentage);
void showDisplay(bool show);
private slots:
void paramChangedInt(QObject *src, QString name, int newParam);
void percentageChanged(int p);
void intChanged(int i);
void on_readButton_clicked();
void on_readDefaultButton_clicked();
void on_helpButton_clicked();
private:
Ui::ParamEditInt *ui;
ConfigParams *mConfig;
ConfigParam mParam;
QString mName;
int mMaxVal;
DisplayPercentage *mDisplay;
QSpinBox *mIntBox;
QSpinBox *mPercentageBox;
void updateDisplay(int val);
int multScale(int val);
int divScale(int val);
};
#endif // PARAMEDITINT_H

102
widgets/parameditint.ui Normal file
View File

@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ParamEditInt</class>
<widget class="QWidget" name="ParamEditInt">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>86</width>
<height>26</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="mainLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="readButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Read current setting</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Upload-96.png</normaloff>:/res/icons/Upload-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="readDefaultButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Read default setting</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Data Backup-96.png</normaloff>:/res/icons/Data Backup-96.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="helpButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Show help</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Help-96.png</normaloff>:/res/icons/Help-96.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources>
<include location="../res.qrc"/>
</resources>
<connections/>
</ui>

View File

@@ -0,0 +1,82 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "parameditstring.h"
#include "ui_parameditstring.h"
#include "helpdialog.h"
ParamEditString::ParamEditString(QWidget *parent) :
QWidget(parent),
ui(new Ui::ParamEditString)
{
ui->setupUi(this);
}
ParamEditString::~ParamEditString()
{
delete ui;
}
void ParamEditString::setConfig(ConfigParams *config)
{
mConfig = config;
ConfigParam *param = mConfig->getParam(mName);
if (param) {
ui->valueEdit->setText(param->valString);
}
connect(mConfig, SIGNAL(paramChangedQString(QObject*,QString,QString)),
this, SLOT(paramChangedQString(QObject*,QString,QString)));
}
QString ParamEditString::name() const
{
return mName;
}
void ParamEditString::setName(const QString &name)
{
mName = name;
}
void ParamEditString::paramChangedQString(QObject *src, QString name, QString newParam)
{
if (src != this && name == mName) {
ui->valueEdit->setText(newParam);
}
}
void ParamEditString::on_helpButton_clicked()
{
if (mConfig) {
HelpDialog::showHelp(this, mConfig, mName);
}
}
void ParamEditString::on_valueEdit_textChanged(const QString &arg1)
{
if (mConfig) {
if (mConfig->getUpdateOnly() != mName) {
mConfig->setUpdateOnly("");
}
mConfig->updateParamString(mName, arg1, this);
}
}

54
widgets/parameditstring.h Normal file
View File

@@ -0,0 +1,54 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARAMEDITSTRING_H
#define PARAMEDITSTRING_H
#include <QWidget>
#include "configparams.h"
namespace Ui {
class ParamEditString;
}
class ParamEditString : public QWidget
{
Q_OBJECT
public:
explicit ParamEditString(QWidget *parent = 0);
~ParamEditString();
void setConfig(ConfigParams *config);
QString name() const;
void setName(const QString &name);
private slots:
void paramChangedQString(QObject *src, QString name, QString newParam);
void on_helpButton_clicked();
void on_valueEdit_textChanged(const QString &arg1);
private:
Ui::ParamEditString *ui;
ConfigParams *mConfig;
QString mName;
};
#endif // PARAMEDITSTRING_H

View File

@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ParamEditString</class>
<widget class="QWidget" name="ParamEditString">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>258</width>
<height>32</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="valueEdit"/>
</item>
<item>
<widget class="QPushButton" name="helpButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Show help</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../res.qrc">
<normaloff>:/res/icons/Help-96.png</normaloff>:/res/icons/Help-96.png</iconset>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../res.qrc"/>
</resources>
<connections/>
</ui>

80
widgets/paramtable.cpp Normal file
View File

@@ -0,0 +1,80 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "paramtable.h"
#include <QDebug>
#include <QHeaderView>
#include <QLabel>
ParamTable::ParamTable(QWidget *parent) : QTableWidget(parent)
{
setColumnCount(2);
QStringList headers;
headers.append("Name");
headers.append("Edit");
setHorizontalHeaderLabels(headers);
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::NoSelection);
horizontalHeader()->setStretchLastSection(true);
horizontalHeader()->setVisible(false);
verticalHeader()->setVisible(false);
}
bool ParamTable::addParamRow(ConfigParams *params, QString paramName)
{
bool res = false;
QWidget *editor = params->getEditor(paramName);
QString name = params->getLongName(paramName);
if (editor) {
int row = rowCount();
setRowCount(row + 1);
QTableWidgetItem *item = new QTableWidgetItem(name);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
setItem(row, 0, item);
setCellWidget(row, 1, editor);
res = true;
resizeColumnToContents(0);
resizeRowsToContents();
}
return res;
}
void ParamTable::addRowSeparator(QString text)
{
int row = rowCount();
setRowCount(row + 1);
QLabel *label = new QLabel(text);
label->setAlignment(Qt::AlignHCenter);
QFont font;
font.setBold(true);
label->setFont(font);
label->setStyleSheet("QLabel { background-color : lightblue; color : black; }");
setCellWidget(row, 0, label);
setSpan(row, 0, 1, 2);
resizeColumnToContents(0);
resizeRowsToContents();
}

36
widgets/paramtable.h Normal file
View File

@@ -0,0 +1,36 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARAMTABLE_H
#define PARAMTABLE_H
#include <QWidget>
#include <QTableWidget>
#include "configparams.h"
class ParamTable : public QTableWidget
{
public:
ParamTable(QWidget *parent = 0);
bool addParamRow(ConfigParams *params, QString paramName);
void addRowSeparator(QString text);
};
#endif // PARAMTABLE_H

30121
widgets/qcustomplot.cpp Normal file

File diff suppressed because it is too large Load Diff

6659
widgets/qcustomplot.h Normal file

File diff suppressed because it is too large Load Diff

304
widgets/rtdatatext.cpp Normal file
View File

@@ -0,0 +1,304 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "rtdatatext.h"
#include <QFont>
#include <QPainter>
#include <QPaintEvent>
#include <QDebug>
RtDataText::RtDataText(QWidget *parent) : QWidget(parent)
{
mBoxH = 10;
mBoxW = 10;
mTxtOfs = 2;
mValues.packVoltage = 0.0;
mValues.packCurrent = 0.0;
mValues.soC = 0;
mValues.cVHigh = 0.0;
mValues.cVAverage = 0.0;
mValues.cVLow = 0.0;
mValues.cVMisMatch = 0.0;
mValues.loadLCVoltage = 0.0;
mValues.loadLCCurrent = 0.0;
mValues.loadHCVoltage = 0.0;
mValues.loadHCCurrent = 0.0;
mValues.chargerVoltage = 0.0;
mValues.auxVoltage = 0.0;
mValues.auxCurrent = 0.0;
mValues.tempBattHigh = 0.0;
mValues.tempBattAverage = 0.0;
mValues.tempBattLow = 0.0;
mValues.tempBMSHigh = 0.0;
mValues.tempBMSAverage = 0.0;
mValues.tempBMSLow = 0.0;
mValues.humidity = 0.0;
mValues.opState = "Unknown.";
mValues.faultState = "Unknown.";
mMode = false;
}
void RtDataText::setValues(const BMS_VALUES &values)
{
mValues = values;
//mValues.opState.remove(0, 11);
update();
}
void RtDataText::setCells(const QVector<double> &values)
{
mCells = values;
update();
}
void RtDataText::setMode(bool newMode)
{
mMode = newMode;
}
QSize RtDataText::sizeHint() const
{
QSize size;
size.setWidth(mBoxW + 2 * mTxtOfs);
size.setHeight(mBoxH + 2 * mTxtOfs);
return size;
}
void RtDataText::paintEvent(QPaintEvent *event)
{
if ( mMode )
paintCellsEvent(event);
else
paintBMSEvent(event);
}
void RtDataText::paintBMSEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// Paint begins here
painter.fillRect(event->rect(), QBrush(Qt::transparent));
QFont font;
font.setFamily("DejaVu Sans Mono");
font.setPointSize(11);
painter.setFont(font);
QRectF br = painter.boundingRect(QRectF(0, 0, 4000, 4000),
"Fault : 00000000000000000"
"T\n"
"T\n"
"T\n"
"T\n"
"T\n"
"T\n");
int boxh_new = br.height();
int boxw_new = br.width();
int txtofs_new = 5;
if (mBoxH != boxh_new || mBoxW != boxw_new || mTxtOfs != txtofs_new) {
mBoxH = boxh_new;
mBoxW = boxw_new;
mTxtOfs = txtofs_new;
updateGeometry();
}
QString str;
const double bbox_w = mBoxW + 2 * mTxtOfs;
const double bbow_h = mBoxH + 2 * mTxtOfs;
const double vidw = event->rect().width();
// Left info box
str.sprintf("V Pack : %.2f V\n"
"I Pack : %.2f A\n"
"P Pack : %.1f W\n"
"CVHigh : %.3f V\n"
"CVAverage : %.3f V\n"
"CVLow : %.3f V\n"
"CMismatch : %.3f V\n",
mValues.packVoltage,
mValues.packCurrent,
mValues.packCurrent * mValues.packVoltage,
mValues.cVHigh,
mValues.cVAverage,
mValues.cVLow,
mValues.cVMisMatch);
painter.setOpacity(0.7);
painter.fillRect(0, 0, bbox_w, bbow_h, Qt::black);
painter.setOpacity(1.0);
painter.setPen(Qt::white);
painter.drawText(QRectF(mTxtOfs, mTxtOfs, mBoxW, mBoxH),
Qt::AlignLeft, str);
// Middle info box
str.sprintf("T Batt High : %.1f \u00B0C\n"
"T Batt Avrg : %.1f \u00B0C\n"
"T Batt Low : %.1f \u00B0C\n"
"T BMS High : %.1f \u00B0C\n"
"T BMS Avrg : %.1f \u00B0C\n"
"T BMS Low : %.1f \u00B0C\n"
"Humidity : %.1f %%\n",
mValues.tempBattHigh,
mValues.tempBattAverage,
mValues.tempBattLow,
mValues.tempBMSHigh,
mValues.tempBMSAverage,
mValues.tempBMSLow,
mValues.humidity);
painter.setOpacity(0.7);
painter.fillRect(vidw / 2.0 - bbox_w / 2.0, 0, bbox_w, bbow_h, Qt::black);
painter.setOpacity(1.0);
painter.setPen(Qt::white);
painter.drawText(QRectF(vidw / 2.0 - bbox_w / 2.0 + mTxtOfs, mTxtOfs, mBoxW, mBoxH),
Qt::AlignLeft, str);
// Right info box
str.sprintf("V Load : %.1f V\n"
"P Load : %.1f W\n"
"V Charger : %.1f V\n"
"P Charger : %.1f W\n"
"SoC : %i %%\n"
"OpState : %s\n"
"FaultState : %s\n",
mValues.loadLCVoltage,
mValues.packCurrent * mValues.loadLCVoltage,
mValues.chargerVoltage,
mValues.packCurrent * mValues.chargerVoltage,
mValues.soC,
mValues.opState.toLocal8Bit().data(),
mValues.faultState.toLocal8Bit().data());
painter.setOpacity(0.7);
painter.fillRect(vidw - bbox_w, 0, bbox_w,mBoxH + 2 * mTxtOfs, Qt::black);
painter.setOpacity(1.0);
painter.setPen(Qt::white);
painter.drawText(QRectF(vidw - bbox_w + mTxtOfs, mTxtOfs, mBoxW, mBoxH),Qt::AlignLeft, str);
}
void RtDataText::paintCellsEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// Paint begins here
painter.fillRect(event->rect(), QBrush(Qt::transparent));
QFont font;
font.setFamily("DejaVu Sans Mono");
font.setPointSize(11);
painter.setFont(font);
QRectF br = painter.boundingRect(QRectF(0, 0, 4000, 4000),
"Cell 2 : 0.123 V\n"
"T\n"
"T\n"
"T\n");
int boxh_new = br.height();
int boxw_new = br.width();
int txtofs_new = 5;
if (mBoxH != boxh_new || mBoxW != boxw_new || mTxtOfs != txtofs_new) {
mBoxH = boxh_new;
mBoxW = boxw_new;
mTxtOfs = txtofs_new;
updateGeometry();
}
// convert voltages to text
QVector<QString> voltages;
for(auto v: mCells)
{
QString str;
str.sprintf("%.3f V", v);
voltages.push_back(str);
}
// guarantee at least 12 items in 'voltages' vector
for(int i=voltages.length(); i<12; ++i)
voltages.push_back("N/A");
QString str;
const double bbox_w = mBoxW + 2 * mTxtOfs;
const double bbow_h = mBoxH + 2 * mTxtOfs;
const double vidw = event->rect().width();
// Left info box
str = QString::asprintf("Cell 1 : %ls\n"
"Cell 2 : %ls\n"
"Cell 3 : %ls\n"
"Cell 4 : %ls\n",
voltages[0].utf16(),
voltages[1].utf16(),
voltages[2].utf16(),
voltages[3].utf16());
painter.setOpacity(0.7);
painter.fillRect(0, 0, bbox_w, bbow_h, Qt::black);
painter.setOpacity(1.0);
painter.setPen(Qt::white);
painter.drawText(QRectF(mTxtOfs, mTxtOfs, mBoxW, mBoxH),
Qt::AlignLeft, str);
// Middle info box
str = QString::asprintf("Cell 5 : %ls\n"
"Cell 6 : %ls\n"
"Cell 7 : %ls\n"
"Cell 8 : %ls\n",
voltages[4].utf16(),
voltages[5].utf16(),
voltages[6].utf16(),
voltages[7].utf16());
painter.setOpacity(0.7);
painter.fillRect(vidw / 2.0 - bbox_w / 2.0, 0, bbox_w, bbow_h, Qt::black);
painter.setOpacity(1.0);
painter.setPen(Qt::white);
painter.drawText(QRectF(vidw / 2.0 - bbox_w / 2.0 + mTxtOfs, mTxtOfs, mBoxW, mBoxH),
Qt::AlignLeft, str);
// Right info box
str = QString::asprintf("Cell 9 : %ls\n"
"Cell 10 : %ls\n"
"Cell 11 : %ls\n"
"Cell 12 : %ls\n",
voltages[8].utf16(),
voltages[9].utf16(),
voltages[10].utf16(),
voltages[11].utf16());
painter.setOpacity(0.7);
painter.fillRect(vidw - bbox_w, 0, bbox_w,mBoxH + 2 * mTxtOfs, Qt::black);
painter.setOpacity(1.0);
painter.setPen(Qt::white);
painter.drawText(QRectF(vidw - bbox_w + mTxtOfs, mTxtOfs, mBoxW, mBoxH),Qt::AlignLeft, str);
}

55
widgets/rtdatatext.h Normal file
View File

@@ -0,0 +1,55 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef RTDATATEXT_H
#define RTDATATEXT_H
#include <QWidget>
#include "datatypes.h"
class RtDataText : public QWidget
{
Q_OBJECT
public:
explicit RtDataText(QWidget *parent = 0);
void setMode(bool newMode);
void setValues(const BMS_VALUES &values);
void setCells(const QVector<double> &values);
QSize sizeHint() const;
signals:
public slots:
protected:
void paintEvent(QPaintEvent *event);
void paintBMSEvent(QPaintEvent *event);
void paintCellsEvent(QPaintEvent *event);
private:
bool mMode;
BMS_VALUES mValues;
QVector<double> mCells;
int mBoxH;
int mBoxW;
int mTxtOfs;
};
#endif // RTDATATEXT_H

61
widgets/vtextbrowser.cpp Normal file
View File

@@ -0,0 +1,61 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "vtextbrowser.h"
#include <QDesktopServices>
#include <QMouseEvent>
VTextBrowser::VTextBrowser(QWidget *parent)
: QTextEdit(parent)
{
setReadOnly(true);
setTextInteractionFlags(Qt::TextSelectableByMouse |
Qt::LinksAccessibleByMouse |
Qt::LinksAccessibleByKeyboard);
viewport()->setMouseTracking(true);
}
void VTextBrowser::mousePressEvent(QMouseEvent *e)
{
mLastAnchor = (e->button() & Qt::LeftButton) ? anchorAt(e->pos()) : QString();
QTextEdit::mousePressEvent(e);
}
void VTextBrowser::mouseReleaseEvent(QMouseEvent *e)
{
if ((e->button() & Qt::LeftButton) &&
!mLastAnchor.isEmpty() &&
anchorAt(e->pos()) == mLastAnchor) {
QDesktopServices::openUrl(mLastAnchor);
}
QTextEdit::mouseReleaseEvent(e);
}
void VTextBrowser::mouseMoveEvent(QMouseEvent *e)
{
if (!anchorAt(e->pos()).isEmpty()) {
viewport()->setCursor(Qt::PointingHandCursor);
} else {
viewport()->setCursor(Qt::ArrowCursor);
}
QTextEdit::mouseMoveEvent(e);
}

41
widgets/vtextbrowser.h Normal file
View File

@@ -0,0 +1,41 @@
/*
Copyright 2016 - 2017 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#ifndef VTEXTBROWSER_H
#define VTEXTBROWSER_H
#include <QTextEdit>
class VTextBrowser : public QTextEdit
{
Q_OBJECT
public:
explicit VTextBrowser(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
private:
QString mLastAnchor;
};
#endif // VTEXTBROWSER_H

52
widgets/widgets.pri Normal file
View File

@@ -0,0 +1,52 @@
FORMS += \
$$PWD/parameditbool.ui \
$$PWD/parameditdouble.ui \
$$PWD/parameditenum.ui \
$$PWD/parameditint.ui \
$$PWD/mrichtextedit.ui \
$$PWD/helpdialog.ui \
$$PWD/parameditstring.ui \
$$PWD/paramdialog.ui
HEADERS += \
$$PWD/parameditbool.h \
$$PWD/parameditdouble.h \
$$PWD/parameditenum.h \
$$PWD/parameditint.h \
$$PWD/displaybar.h \
$$PWD/displaypercentage.h \
$$PWD/helpdialog.h \
$$PWD/mrichtextedit.h \
$$PWD/mtextedit.h \
$$PWD/pagelistitem.h \
$$PWD/paramtable.h \
$$PWD/qcustomplot.h \
$$PWD/rtdatatext.h \
$$PWD/vtextbrowser.h \
$$PWD/imagewidget.h \
$$PWD/parameditstring.h \
$$PWD/paramdialog.h \
$$PWD/aspectimglabel.h \
$$PWD/historylineedit.h
SOURCES += \
$$PWD/parameditbool.cpp \
$$PWD/parameditdouble.cpp \
$$PWD/parameditenum.cpp \
$$PWD/parameditint.cpp \
$$PWD/displaybar.cpp \
$$PWD/displaypercentage.cpp \
$$PWD/helpdialog.cpp \
$$PWD/mrichtextedit.cpp \
$$PWD/mtextedit.cpp \
$$PWD/pagelistitem.cpp \
$$PWD/paramtable.cpp \
$$PWD/qcustomplot.cpp \
$$PWD/rtdatatext.cpp \
$$PWD/vtextbrowser.cpp \
$$PWD/imagewidget.cpp \
$$PWD/parameditstring.cpp \
$$PWD/paramdialog.cpp \
$$PWD/aspectimglabel.cpp \
$$PWD/historylineedit.cpp