Correct code and coding conventions prior merging numericalboxes.

parent 97dea18d
......@@ -27,8 +27,7 @@ target_link_libraries(kritabasicflakes
PUBLIC
kritawidgets
kritaflake
kritapigment
kritaui
kritapigment
)
set_target_properties(kritabasicflakes PROPERTIES
......
......@@ -30,7 +30,7 @@
#include "KoShapeStroke.h"
#include "KoStrokeConfigWidget.h"
#include "KoCanvasBase.h"
#include "kis_intparsespinbox.h"
#include "kis_int_parse_spin_box.h"
#include <KoColor.h>
#include <klocalizedstring.h>
......
......@@ -50,7 +50,7 @@
#include <math.h>
#include "KoCreatePathTool_p.h"
#include "kis_doubleparsespinbox.h"
#include "kis_double_parse_spin_box.h"
KoPencilTool::KoPencilTool(KoCanvasBase *canvas)
: KoToolBase(canvas)
......
......@@ -361,9 +361,7 @@ set(kritaui_LIB_SRCS
KisPaletteModel.cpp
KisColorsetChooser.cpp
KisSaveGroupVisitor.cpp
utils/kis_numparser.cpp
KisSaveGroupVisitor.cpp
)
if(WIN32)
......
......@@ -42,7 +42,7 @@
#include <QLineEdit>
#include <kis_canvas_resource_provider.h>
#include "kis_intparsespinbox.h"
#include "kis_int_parse_spin_box.h"
class ColorSetDelegate : public QAbstractItemDelegate
{
......
......@@ -342,7 +342,7 @@
<customwidget>
<class>KisDoubleParseUnitSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>kis_doubleparseunitspinbox.h</header>
<header>kis_double_parse_unit_spin_box.h</header>
</customwidget>
</customwidgets>
<tabstops>
......
......@@ -109,7 +109,7 @@
<customwidget>
<class>KisDoubleParseUnitSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>kis_doubleparseunitspinbox.h</header>
<header>kis_double_parse_unit_spin_box.h</header>
</customwidget>
</customwidgets>
<resources/>
......
......@@ -118,7 +118,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
</customwidgets>
<tabstops>
......
......@@ -371,7 +371,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KColorButton</class>
......
......@@ -346,7 +346,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KColorButton</class>
......
......@@ -701,7 +701,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KColorButton</class>
......
......@@ -121,7 +121,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
</customwidgets>
<resources/>
......
......@@ -653,7 +653,7 @@
<customwidget>
<class>KisDoubleParseSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>kis_doubleparsespinbox.h</header>
<header>kis_double_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KColorButton</class>
......@@ -670,7 +670,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
</customwidgets>
<tabstops>
......
......@@ -182,7 +182,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KisPresetChooser</class>
......
......@@ -291,7 +291,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KisDoubleSliderSpinBox</class>
......
......@@ -185,12 +185,12 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KisDoubleParseSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>kis_doubleparsespinbox.h</header>
<header>kis_double_parse_spin_box.h</header>
</customwidget>
</customwidgets>
<resources/>
......
......@@ -262,7 +262,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KisSliderSpinBox</class>
......
......@@ -249,7 +249,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KisSliderSpinBox</class>
......
......@@ -433,7 +433,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
</customwidgets>
<resources/>
......
......@@ -457,7 +457,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KisSliderSpinBox</class>
......
......@@ -289,7 +289,7 @@
<customwidget>
<class>KisIntParseSpinBox</class>
<extends>QSpinBox</extends>
<header>kis_intparsespinbox.h</header>
<header>kis_int_parse_spin_box.h</header>
</customwidget>
<customwidget>
<class>KisSliderSpinBox</class>
......
......@@ -26,7 +26,7 @@
#include <QHBoxLayout>
#include <QDoubleSpinBox>
#include "kis_doubleparsespinbox.h"
#include "kis_double_parse_spin_box.h"
KisDoubleWidget::KisDoubleWidget(QWidget* parent, const char* name)
: QWidget(parent)
......
/*
* Copyright (c) 2016 Laurent Valentin Jospin <laurent.valentin@famillejospin.ch>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kis_doubleparsespinbox.h"
#include "kis_numparser.h"
#include <QLabel>
#include <QPixmap>
#include <QIcon>
#include <QFile>
#include <QLineEdit>
KisDoubleParseSpinBox::KisDoubleParseSpinBox(QWidget *parent) :
QDoubleSpinBox(parent),
_isLastValid(true)
{
_lastExprParsed = new QString("0.0");
connect(this, SIGNAL(noMoreParsingError()),
this, SLOT(clearErrorStyle()));
//hack to let the clearError be called, even if the value changed method is the one from QDoubleSpinBox.
connect(this, SIGNAL(valueChanged(double)),
this, SLOT(clearError()));
connect(this, SIGNAL(errorWhileParsing(QString)),
this, SLOT(setErrorStyle()));
_oldValue = value();
_warningIcon = new QLabel(this);
if (QFile(":/./16_light_warning.svg").exists()) {
_warningIcon->setPixmap(QIcon(":/./16_light_warning.svg").pixmap(16, 16));
} else {
_warningIcon->setText("!");
}
_warningIcon->setStyleSheet("background:transparent;");
_warningIcon->move(1, 1);
_warningIcon->setVisible(false);
_isOldPaletteSaved = false;
_areOldMarginsSaved = false;
}
KisDoubleParseSpinBox::~KisDoubleParseSpinBox()
{
//needed to avoid a segfault during destruction.
delete _lastExprParsed;
}
double KisDoubleParseSpinBox::valueFromText(const QString & text) const
{
*_lastExprParsed = text;
bool ok;
double ret;
if ( (suffix().isEmpty() || !text.endsWith(suffix())) &&
(prefix().isEmpty() || !text.startsWith(prefix())) ) {
ret = KisNumericParser::parseSimpleMathExpr(text, &ok);
} else {
QString expr = text;
if (text.endsWith(suffix())) {
expr.remove(text.size()-suffix().size(), suffix().size());
}
if(text.startsWith(prefix())){
expr.remove(0, prefix().size());
}
*_lastExprParsed = expr;
ret = KisNumericParser::parseSimpleMathExpr(expr, &ok);
}
if(isnan(ret) || isinf(ret)){
ok = false;
}
if (!ok) {
if (_isLastValid) {
_oldValue = value();
}
_isLastValid = false;
ret = _oldValue; //in case of error set to minimum.
} else {
if (!_isLastValid) {
_oldValue = ret;
}
_isLastValid = true;
}
return ret;
}
QString KisDoubleParseSpinBox::textFromValue(double val) const
{
if (!_isLastValid) {
emit errorWhileParsing(*_lastExprParsed);
return *_lastExprParsed;
}
emit noMoreParsingError();
double v = KisNumericParser::parseSimpleMathExpr(cleanText());
v = QString("%1").arg(v, 0, 'f', decimals()).toDouble();
if (hasFocus() && (v == value() || (v >= maximum() && value() == maximum()) || (v <= minimum() && value() == minimum())) ) { //solve a very annoying bug where the formula can collapse while editing. With this trick the formula is not lost until focus is lost.
return cleanText();
}
return QDoubleSpinBox::textFromValue(val);
}
QValidator::State KisDoubleParseSpinBox::validate ( QString & input, int & pos ) const
{
Q_UNUSED(input);
Q_UNUSED(pos);
return QValidator::Acceptable;
}
void KisDoubleParseSpinBox::stepBy(int steps)
{
_isLastValid = true; //reset to valid state so we can use the up and down buttons.
emit noMoreParsingError();
QDoubleSpinBox::stepBy(steps);
}
void KisDoubleParseSpinBox::setValue(double value)
{
if(value == _oldValue && hasFocus()){ //avoid to reset the button when it set the value of something that will recall this slot.
return;
}
if (!hasFocus()) {
clearError();
}
QDoubleSpinBox::setValue(value);
}
void KisDoubleParseSpinBox::setErrorStyle()
{
if (!_isLastValid) {
//setStyleSheet(_oldStyleSheet + "Background: red; color: white; padding-left: 18px;");
if (!_isOldPaletteSaved) {
_oldPalette = palette();
}
_isOldPaletteSaved = true;
QPalette nP = _oldPalette;
nP.setColor(QPalette::Background, Qt::red);
nP.setColor(QPalette::Base, Qt::red);
nP.setColor(QPalette::Text, Qt::white);
setPalette(nP);
if (!_areOldMarginsSaved) {
_oldMargins = lineEdit()->textMargins();
}
_areOldMarginsSaved = true;
if (width() - height() >= 3*height()) { //if we have twice as much place as needed by the warning icon then display it.
QMargins newMargins = _oldMargins;
newMargins.setLeft( newMargins.left() + height() - 4 );
lineEdit()->setTextMargins(newMargins);
int h = _warningIcon->height();
int hp = height()-2;
if (h != hp) {
_warningIcon->resize(hp, hp);
if (QFile(":/./16_light_warning.svg").exists()) {
_warningIcon->setPixmap(QIcon(":/./16_light_warning.svg").pixmap(hp-7, hp-7));
}
}
_warningIcon->move(_oldMargins.left()+4, 1);
_warningIcon->setVisible(true);
}
}
}
void KisDoubleParseSpinBox::clearErrorStyle()
{
if (_isLastValid) {
_warningIcon->setVisible(false);
//setStyleSheet("");
setPalette(_oldPalette);
_isOldPaletteSaved = false;
lineEdit()->setTextMargins(_oldMargins);
_areOldMarginsSaved = false;
}
}
void KisDoubleParseSpinBox::clearError()
{
_isLastValid = true;
emit noMoreParsingError();
_oldValue = value();
clearErrorStyle();
}
......@@ -26,7 +26,7 @@
#include <kis_config_widget.h>
#include "kritaui_export.h"
#include "kis_doubleparsespinbox.h"
#include "kis_double_parse_spin_box.h"
class KisDelayedActionDoubleInput : public KisDoubleParseSpinBox
{
......
......@@ -25,7 +25,7 @@
#include <QVector>
#include <QSpinBox>
#include "kis_intparsespinbox.h"
#include "kis_int_parse_spin_box.h"
#include <vector>
......
......@@ -36,7 +36,7 @@
#include "KisPart.h"
#include "input/kis_input_manager.h"
#include "kis_numparser.h"
#include "kis_num_parser.h"
class KisAbstractSliderSpinBoxPrivate {
public:
......
......@@ -64,6 +64,15 @@ set(kritawidgets_LIB_SRCS
KoToolBoxFactory.cpp
KoToolDocker.cpp
KoPageLayoutWidget.cpp
KoPageLayoutDialog.cpp
KoShadowConfigWidget.cpp
KoStrokeConfigWidget.cpp
KoMarkerSelector.cpp
KoMarkerModel.cpp
KoMarkerItemDelegate.cpp
KoDocumentInfoDlg.cpp
KoGlobal.cpp
KoTableView.cpp
......@@ -71,6 +80,10 @@ set(kritawidgets_LIB_SRCS
WidgetsDebug.cpp
kis_file_name_requester.cpp
kis_double_parse_spin_box.cpp
kis_double_parse_unit_spin_box.cpp
kis_int_parse_spin_box.cpp
)
ki18n_wrap_ui( kritawidgets_LIB_SRCS
......
......@@ -67,7 +67,7 @@
#include <KoShapeStrokeModel.h>
// Krita
#include "kis_doubleparseunitspinbox.h"
#include "kis_double_parse_unit_spin_box.h"
class CapNJoinMenu : public QMenu
{
......
/*
* Copyright (c) 2016 Laurent Valentin Jospin <laurent.valentin@famillejospin.ch>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kis_double_parse_spin_box.h"
#include "kis_num_parser.h"
#include <QLabel>
#include <QPixmap>
#include <QIcon>
#include <QFile>
#include <QLineEdit>
KisDoubleParseSpinBox::KisDoubleParseSpinBox(QWidget *parent) :
QDoubleSpinBox(parent),
boolLastValid(true)
{
lastExprParsed = new QString("0.0");
connect(this, SIGNAL(noMoreParsingError()),
this, SLOT(clearErrorStyle()));
//hack to let the clearError be called, even if the value changed method is the one from QDoubleSpinBox.
connect(this, SIGNAL(valueChanged(double)),
this, SLOT(clearError()));