Commit 98e8a7ad authored by Nicolas Fella's avatar Nicolas Fella
Browse files

Add and make use of ECM clang-format

parent 16bdae3b
......@@ -22,6 +22,8 @@ include(ECMInstallIcons)
include(ECMSetupVersion)
include(KDECMakeSettings)
include(KDECompilerSettings NO_POLICY_SCOPE)
include(KDEClangFormat)
include(KDEGitCommitHooks)
find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS Core Gui Svg Widgets PrintSupport)
......@@ -53,4 +55,10 @@ if (KF5DocTools_FOUND)
kdoctools_install(po)
endif()
install(FILES org.kde.kmplot.appdata.xml DESTINATION ${KDE_INSTALL_METAINFODIR})
file(GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES *.cpp *.h)
kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
kde_configure_git_pre_commit_hook(CHECKS CLANG_FORMAT)
feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES)
......@@ -15,71 +15,68 @@
#include "equationeditorwidget.h"
#include "xparser.h"
#include <KTextEdit>
#include <KConfigGroup>
#include <KTextEdit>
#include <QDialogButtonBox>
#include <QScrollBar>
#include <QVBoxLayout>
//BEGIN class Calculator
Calculator::Calculator( QWidget * parent )
: QDialog( parent )
// BEGIN class Calculator
Calculator::Calculator(QWidget *parent)
: QDialog(parent)
{
setModal( false );
QWidget * widget = new QWidget( this );
setWindowTitle( i18nc("@title:window", "Calculator") );
QVBoxLayout *layout = new QVBoxLayout( this );
setModal(false);
QWidget *widget = new QWidget(this);
setWindowTitle(i18nc("@title:window", "Calculator"));
m_display = new KTextEdit( widget );
QSizePolicy displaySizePolicy = m_display->sizePolicy();
displaySizePolicy.setVerticalStretch( 10 );
displaySizePolicy.setVerticalPolicy( QSizePolicy::MinimumExpanding );
m_display->setSizePolicy( displaySizePolicy );
layout->addWidget( m_display );
QVBoxLayout *layout = new QVBoxLayout(this);
m_input = new EquationEditorWidget( this );
layout->addWidget( m_input );
m_display = new KTextEdit(widget);
QSizePolicy displaySizePolicy = m_display->sizePolicy();
displaySizePolicy.setVerticalStretch(10);
displaySizePolicy.setVerticalPolicy(QSizePolicy::MinimumExpanding);
m_display->setSizePolicy(displaySizePolicy);
layout->addWidget(m_display);
m_display->setReadOnly( true );
m_input = new EquationEditorWidget(this);
layout->addWidget(m_input);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
connect(buttonBox, &QDialogButtonBox::accepted, this, &Calculator::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &Calculator::reject);
layout->addWidget( buttonBox );
m_display->setReadOnly(true);
connect(m_input->edit, &EquationEdit::returnPressed, this, &Calculator::calculate);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
connect(buttonBox, &QDialogButtonBox::accepted, this, &Calculator::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &Calculator::reject);
layout->addWidget(buttonBox);
// Set minimum size and margin to avoid cutting the right side
resize( 1.05*layout->minimumSize() );
m_input->edit->setFocus();
connect(m_input->edit, &EquationEdit::returnPressed, this, &Calculator::calculate);
// Set minimum size and margin to avoid cutting the right side
resize(1.05 * layout->minimumSize());
m_input->edit->setFocus();
}
Calculator::~Calculator()
{
m_input->deleteLater();
m_input->deleteLater();
}
void Calculator::calculate()
{
Parser::Error error;
double value = XParser::self()->eval( m_input->edit->text(), &error );
m_displayText += m_input->edit->text().replace( '<', "&lt;" );
if ( error == Parser::ParseSuccess )
m_displayText += " = <b>" + Parser::number( value ) + "</b>";
else
m_displayText += " = ? <font color=\"blue\">(" + Parser::errorString( error ) + ")</font>";
m_displayText += "<br>";
m_display->document()->setHtml( m_displayText );
m_display->verticalScrollBar()->setValue( m_display->verticalScrollBar()->maximum() );
m_input->edit->selectAll();
Parser::Error error;
double value = XParser::self()->eval(m_input->edit->text(), &error);
m_displayText += m_input->edit->text().replace('<', "&lt;");
if (error == Parser::ParseSuccess)
m_displayText += " = <b>" + Parser::number(value) + "</b>";
else
m_displayText += " = ? <font color=\"blue\">(" + Parser::errorString(error) + ")</font>";
m_displayText += "<br>";
m_display->document()->setHtml(m_displayText);
m_display->verticalScrollBar()->setValue(m_display->verticalScrollBar()->maximum());
m_input->edit->selectAll();
}
//END class Calculator
// END class Calculator
......@@ -24,21 +24,21 @@ class KTextEdit;
*/
class Calculator : public QDialog
{
Q_OBJECT
public:
explicit Calculator( QWidget *parent = 0 );
~Calculator();
protected Q_SLOTS:
/**
* The user hit enter from the input box.
*/
void calculate();
protected:
EquationEditorWidget* m_input;
KTextEdit* m_display;
QString m_displayText;
Q_OBJECT
public:
explicit Calculator(QWidget *parent = 0);
~Calculator();
protected Q_SLOTS:
/**
* The user hit enter from the input box.
*/
void calculate();
protected:
EquationEditorWidget *m_input;
KTextEdit *m_display;
QString m_displayText;
};
#endif
......@@ -11,180 +11,159 @@
*/
#include "constants.h"
#include "xparser.h"
#include <KConfig>
//BEGIN class Constant
Constant::Constant( )
// BEGIN class Constant
Constant::Constant()
{
// By default, have both types
// This minimizes loss of information
type = Document | Global;
// By default, have both types
// This minimizes loss of information
type = Document | Global;
}
//END class Constant
// END class Constant
//BEGIN class Constants
// BEGIN class Constants
Constants::Constants()
{
}
Constants::~Constants()
{
}
Value Constants::value( const QString & name ) const
Value Constants::value(const QString &name) const
{
return m_constants[ name ].value;
return m_constants[name].value;
}
bool Constants::have( const QString & name ) const
bool Constants::have(const QString &name) const
{
return m_constants.contains( name );
return m_constants.contains(name);
}
void Constants::remove( const QString & name )
void Constants::remove(const QString &name)
{
if ( m_constants.remove( name ) > 0 )
emit constantsChanged();
if (m_constants.remove(name) > 0)
emit constantsChanged();
}
void Constants::add( const QString & name, const Constant & constant )
void Constants::add(const QString &name, const Constant &constant)
{
m_constants[name] = constant;
emit constantsChanged();
m_constants[name] = constant;
emit constantsChanged();
}
ConstantList Constants::list( int type ) const
ConstantList Constants::list(int type) const
{
ConstantList list;
for ( ConstantList::const_iterator it = m_constants.begin(); it != m_constants.end(); ++it )
{
if ( type & it.value().type )
list.insert( it.key(), it.value() );
}
return list;
}
ConstantList list;
for (ConstantList::const_iterator it = m_constants.begin(); it != m_constants.end(); ++it) {
if (type & it.value().type)
list.insert(it.key(), it.value());
}
bool Constants::isValidName( const QString & name ) const
{
// Don't allow empty names
if ( name.isEmpty() )
return false;
// Don't allow constants names that are already used by a function
if ( XParser::self()->predefinedFunctions( true ).contains( name ) ||
XParser::self()->userFunctions().contains( name ) )
return false;
// special cases: don't allow predefined constants either
if ( name == QLatin1String("pi") || name == PiSymbol || name == QLatin1String("e") || name == InfinitySymbol )
return false;
// Now make sure that the constant name contains only letters
for ( int i = 0; i < name.length(); ++i )
{
if ( !name.at(i).isLetter() )
return false;
}
// All ok!
return true;
return list;
}
bool Constants::isValidName(const QString &name) const
{
// Don't allow empty names
if (name.isEmpty())
return false;
// Don't allow constants names that are already used by a function
if (XParser::self()->predefinedFunctions(true).contains(name) || XParser::self()->userFunctions().contains(name))
return false;
// special cases: don't allow predefined constants either
if (name == QLatin1String("pi") || name == PiSymbol || name == QLatin1String("e") || name == InfinitySymbol)
return false;
// Now make sure that the constant name contains only letters
for (int i = 0; i < name.length(); ++i) {
if (!name.at(i).isLetter())
return false;
}
// All ok!
return true;
}
QString Constants::generateUniqueName() const
{
QString name;
int at = 0;
while (true)
{
at++;
name.resize( at );
for ( char c = 'A'; c <= 'Z'; ++c )
{
name[at-1] = c;
if ( isValidName(name) && !have(name) )
return name;
}
}
QString name;
int at = 0;
while (true) {
at++;
name.resize(at);
for (char c = 'A'; c <= 'Z'; ++c) {
name[at - 1] = c;
if (isValidName(name) && !have(name))
return name;
}
}
}
void Constants::load()
{
/// \todo Need more robust way of exchanging constants with kcalc
KConfig conf (QStringLiteral("kcalcrc"), KConfig::SimpleConfig);
KConfigGroup group = conf.group("UserConstants");
QString tmp;
for( int i=0; ;i++)
{
tmp.setNum(i);
QString name = group.readEntry("nameConstant"+tmp, QStringLiteral(" "));
QString expression = group.readEntry("expressionConstant"+tmp, QStringLiteral(" "));
QString value = group.readEntry("valueConstant"+tmp, QStringLiteral(" ") );
if ( name == QLatin1String(" ") )
return;
if ( name.isEmpty() )
continue;
if ( expression == QLatin1String(" ") )
{
// Old config file
expression = value;
}
if ( !isValidName( name ) || have( name ) )
name = generateUniqueName();
Constant constant;
constant.value = expression;
constant.type = Constant::Global;
add( name, constant );
}
/// \todo Need more robust way of exchanging constants with kcalc
KConfig conf(QStringLiteral("kcalcrc"), KConfig::SimpleConfig);
KConfigGroup group = conf.group("UserConstants");
QString tmp;
for (int i = 0;; i++) {
tmp.setNum(i);
QString name = group.readEntry("nameConstant" + tmp, QStringLiteral(" "));
QString expression = group.readEntry("expressionConstant" + tmp, QStringLiteral(" "));
QString value = group.readEntry("valueConstant" + tmp, QStringLiteral(" "));
if (name == QLatin1String(" "))
return;
if (name.isEmpty())
continue;
if (expression == QLatin1String(" ")) {
// Old config file
expression = value;
}
if (!isValidName(name) || have(name))
name = generateUniqueName();
Constant constant;
constant.value = expression;
constant.type = Constant::Global;
add(name, constant);
}
}
void Constants::save()
{
KConfig conf (QStringLiteral("kcalcrc"), KConfig::SimpleConfig);
conf.deleteGroup("Constants");
// remove any previously saved constants
conf.deleteGroup( "UserConstants" );
KConfigGroup group = conf.group("UserConstants");
QString tmp;
ConstantList global = list( Constant::Global );
int i = 0;
for ( ConstantList::iterator it = global.begin(); it != global.end(); ++it )
{
tmp.setNum(i);
group.writeEntry( "nameConstant"+tmp, it.key() ) ;
group.writeEntry( "expressionConstant"+tmp, it.value().value.expression() );
group.writeEntry( "valueConstant"+tmp, it.value().value.value() );
i++;
}
KConfig conf(QStringLiteral("kcalcrc"), KConfig::SimpleConfig);
conf.deleteGroup("Constants");
// remove any previously saved constants
conf.deleteGroup("UserConstants");
KConfigGroup group = conf.group("UserConstants");
QString tmp;
ConstantList global = list(Constant::Global);
int i = 0;
for (ConstantList::iterator it = global.begin(); it != global.end(); ++it) {
tmp.setNum(i);
group.writeEntry("nameConstant" + tmp, it.key());
group.writeEntry("expressionConstant" + tmp, it.value().value.expression());
group.writeEntry("valueConstant" + tmp, it.value().value.value());
i++;
}
}
//END class Constants
// END class Constants
......@@ -11,7 +11,6 @@
*/
#ifndef CONSTANT_H
#define CONSTANT_H
......@@ -19,106 +18,104 @@
#include <QMap>
/**
* Stores the details of a constant other than its name.
* \author David Saxton
*/
class Constant
{
public:
Constant();
/**
* The scope of the constant.
*/
enum Type
{
Document = 0x1, ///< The constant is part of the document
Global = 0x2, ///< The constant is to be saved globally in the application settings
All = 0x4-1
};
/**
* The actual value of the constant.
*/
Value value;
/**
* The OR'ed types.
*/
int type;
public:
Constant();
/**
* The scope of the constant.
*/
enum Type {
Document = 0x1, ///< The constant is part of the document
Global = 0x2, ///< The constant is to be saved globally in the application settings
All = 0x4 - 1
};
/**
* The actual value of the constant.
*/
Value value;
/**
* The OR'ed types.
*/
int type;
};
typedef QMap<QString, Constant> ConstantList;
/**
* @short Manages a list of constants.
*/
class Constants : public QObject
{
Q_OBJECT
public:
Constants();
virtual ~Constants();
/**
* Load the constants at the start.
*/
void load();
/**
* Save the constants when closing the program.
*/
void save();
/**
* \return if the constant name is valid.
*/
bool isValidName( const QString & name ) const;
/**
* \return the value of the constant with the given name. This will
* return a default Value if the constant does not exist; use
* Constants::have to check for existence.
*/
Value value( const QString & name ) const;
/**
* Removes the constant with the given name from the constants list.
*/
void remove( const QString & name );
/**
* Adds the constant to the internal list (overwriting any previous
* constant with the same name).
*/
void add( const QString & name, const Constant & constant );
/**
* \return whether the constant with the given name exists.
*/
bool have( const QString & name ) const;
/**
* \return a unique (i.e. unused) constant name.
*/
QString generateUniqueName() const;
/**
* \param type OR'ed list of Constant::Type
* \return a copy of the list of constants.
*/
ConstantList list( int type ) const;
/**
* \return a list of the constant names.
*/
QStringList names() const { return m_constants.keys(); }
signals:
/**
* Emitted when a constant is added or removed, or the value of an
* existing constant has changed.
*/
void constantsChanged();
protected:
ConstantList m_constants;
Q_OBJECT
public:
Constants();
virtual ~Constants();
/**
* Load the constants at the start.
*/
void load();
/**
* Save the constants when closing the program.
*/
void save();
/**
* \return if the constant name is valid.
*/
bool isValidName(const QString &name) const;
/**
* \return the value of the constant with the given name. This will
* return a default Value if the constant does not exist; use
* Constants::have to check for existence.
*/
Value value(const QString &name) const;
/**
* Removes the constant with the given name from the constants list.
*/