diff --git a/src/kdefrontend/spreadsheet/DropValuesDialog.cpp b/src/kdefrontend/spreadsheet/DropValuesDialog.cpp index 5c65b734c..39855fa2a 100644 --- a/src/kdefrontend/spreadsheet/DropValuesDialog.cpp +++ b/src/kdefrontend/spreadsheet/DropValuesDialog.cpp @@ -1,338 +1,345 @@ /*************************************************************************** File : DropValuesDialog.cpp Project : LabPlot Description : Dialog for droping and masking values in columns -------------------------------------------------------------------- Copyright : (C) 2015 by Alexander Semke (alexander.semke@web.de) ***************************************************************************/ /*************************************************************************** * * * 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 "DropValuesDialog.h" #include "backend/core/column/Column.h" #include "backend/lib/macros.h" #include "backend/spreadsheet/Spreadsheet.h" #include +#include +#include #include #include /*! \class DropValuesDialog \brief Dialog for generating values from a mathematical function. \ingroup kdefrontend */ -DropValuesDialog::DropValuesDialog(Spreadsheet* s, bool mask, QWidget* parent, Qt::WFlags fl) : KDialog(parent, fl), +DropValuesDialog::DropValuesDialog(Spreadsheet* s, bool mask, QWidget* parent, Qt::WFlags fl) : QDialog(parent, fl), m_spreadsheet(s), m_mask(mask) { setWindowTitle(i18n("Drop values")); - QFrame* mainWidget = new QFrame(this); - ui.setupUi(mainWidget); - setMainWidget( mainWidget ); + ui.setupUi(this); setAttribute(Qt::WA_DeleteOnClose); ui.cbOperator->addItem(i18n("equal to")); ui.cbOperator->addItem(i18n("between (including end points)")); ui.cbOperator->addItem(i18n("between (excluding end points)")); ui.cbOperator->addItem(i18n("greater then")); ui.cbOperator->addItem(i18n("greater then or equal to")); ui.cbOperator->addItem(i18n("lesser then")); ui.cbOperator->addItem(i18n("lesser then or equal to")); ui.leValue1->setValidator( new QDoubleValidator(ui.leValue1) ); ui.leValue2->setValidator( new QDoubleValidator(ui.leValue2) ); - setButtons( KDialog::Ok | KDialog::Cancel ); + QDialogButtonBox* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + + ui.horizontalLayout->addWidget(btnBox); + m_okButton = btnBox->button(QDialogButtonBox::Ok); + QPushButton* cancelButton = btnBox->button(QDialogButtonBox::Cancel); + + connect(cancelButton, SIGNAL(clicked(bool)), this, SLOT(close())); + if (m_mask) { - setButtonText(KDialog::Ok, i18n("&Mask")); - setButtonToolTip(KDialog::Ok, i18n("Mask values in the specified region")); + m_okButton->setText(i18n("&Mask")); + m_okButton->setToolTip(i18n("Mask values in the specified region")); ui.lMode->setText(i18n("Mask values")); setWindowTitle(i18n("Mask values")); } else { - setButtonText(KDialog::Ok, i18n("&Drop")); - setButtonToolTip(KDialog::Ok, i18n("Drop values in the specified region")); + m_okButton->setText(i18n("&Drop")); + m_okButton->setToolTip(i18n("Drop values in the specified region")); } - connect( ui.cbOperator, SIGNAL(currentIndexChanged(int)), this, SLOT(operatorChanged(int)) ); - connect(this, SIGNAL(okClicked()), this, SLOT(okClicked())); + connect(ui.cbOperator, SIGNAL(currentIndexChanged(int)), this, SLOT(operatorChanged(int)) ); + connect(m_okButton, SIGNAL(clicked(bool)), this, SLOT(okClicked())); resize( QSize(400,0).expandedTo(minimumSize()) ); operatorChanged(0); } void DropValuesDialog::setColumns(QVector columns) { m_columns = columns; } void DropValuesDialog::operatorChanged(int index) const { bool value2 = (index==1) || (index==2); ui.lMin->setVisible(value2); ui.lMax->setVisible(value2); ui.lAnd->setVisible(value2); ui.leValue2->setVisible(value2); } void DropValuesDialog::okClicked() const { if (m_mask) maskValues(); else dropValues(); } //TODO: m_column->setMasked() is slow, we need direct access to the masked-container -> redesign class MaskValuesTask : public QRunnable { public: MaskValuesTask(Column* col, int op, double value1, double value2){ m_column = col; m_operator = op; m_value1 = value1; m_value2 = value2; - }; + } void run() { m_column->setSuppressDataChangedSignal(true); bool changed = false; QVector* data = static_cast* >(m_column->data()); //equal to if (m_operator == 0) { for (int i=0; isize(); ++i) { if (data->at(i) == m_value1) { m_column->setMasked(i, true); changed = true; } } } //between (including end points) else if (m_operator == 1) { for (int i=0; isize(); ++i) { if (data->at(i) >= m_value1 && data->at(i) <= m_value2) { m_column->setMasked(i, true); changed = true; } } } //between (excluding end points) else if (m_operator == 2) { for (int i=0; isize(); ++i) { if (data->at(i) > m_value1 && data->at(i) < m_value2) { m_column->setMasked(i, true); changed = true; } } } - //greater then + //greater than else if (m_operator == 3) { for (int i=0; isize(); ++i) { if (data->at(i) > m_value1) { m_column->setMasked(i, true); changed = true; } } } - //greater then or equal to + //greater than or equal to else if (m_operator == 4) { for (int i=0; isize(); ++i) { if (data->at(i) >= m_value1) { m_column->setMasked(i, true); changed = true; } } } - //lesser then + //lesser than else if (m_operator == 5) { for (int i=0; isize(); ++i) { if (data->at(i) < m_value1) { m_column->setMasked(i, true); changed = true; } } } - //lesser then or equal to + //lesser than or equal to else if (m_operator == 6) { for (int i=0; isize(); ++i) { if (data->at(i) <= m_value1) { m_column->setMasked(i, true); changed = true; } } } m_column->setSuppressDataChangedSignal(false); if (changed) m_column->setChanged(); } private: Column* m_column; int m_operator; double m_value1; double m_value2; }; class DropValuesTask : public QRunnable { public: DropValuesTask(Column* col, int op, double value1, double value2){ m_column = col; m_operator = op; m_value1 = value1; m_value2 = value2; - }; + } void run() { bool changed = false; QVector* data = static_cast* >(m_column->data()); QVector new_data(*data); //equal to if (m_operator == 0) { for (int i=0; i= m_value1 && new_data[i] <= m_value2) { new_data[i] = NAN; changed = true; } } } //between (excluding end points) else if (m_operator == 2) { for (int i=0; i m_value1 && new_data[i] < m_value2) { new_data[i] = NAN; changed = true; } } } - //greater then + //greater than else if (m_operator == 3) { for (int i=0; i m_value1) { new_data[i] = NAN; changed = true; } } } - //greater then or equal to + //greater than or equal to else if (m_operator == 4) { for (int i=0; i= m_value1) { new_data[i] = NAN; changed = true; } } } - //lesser then + //lesser than else if (m_operator == 5) { for (int i=0; ireplaceValues(0, new_data); } private: Column* m_column; int m_operator; double m_value1; double m_value2; }; void DropValuesDialog::maskValues() const { Q_ASSERT(m_spreadsheet); WAIT_CURSOR; m_spreadsheet->beginMacro(i18n("%1: mask values", m_spreadsheet->name())); const int op = ui.cbOperator->currentIndex(); const double value1 = ui.leValue1->text().toDouble(); const double value2 = ui.leValue2->text().toDouble(); foreach(Column* col, m_columns) { MaskValuesTask* task = new MaskValuesTask(col, op, value1, value2); task->run(); //TODO: writing to the undo-stack in Column::setMasked() is not tread-safe -> redesign // QThreadPool::globalInstance()->start(task); } //wait until all columns were processed // QThreadPool::globalInstance()->waitForDone(); m_spreadsheet->endMacro(); RESET_CURSOR; } void DropValuesDialog::dropValues() const { Q_ASSERT(m_spreadsheet); WAIT_CURSOR; m_spreadsheet->beginMacro(i18n("%1: drop values", m_spreadsheet->name())); const int op = ui.cbOperator->currentIndex(); const double value1 = ui.leValue1->text().toDouble(); const double value2 = ui.leValue2->text().toDouble(); foreach(Column* col, m_columns) { DropValuesTask* task = new DropValuesTask(col, op, value1, value2); QThreadPool::globalInstance()->start(task); } //wait until all columns were processed QThreadPool::globalInstance()->waitForDone(); m_spreadsheet->endMacro(); RESET_CURSOR; } diff --git a/src/kdefrontend/spreadsheet/DropValuesDialog.h b/src/kdefrontend/spreadsheet/DropValuesDialog.h index 67129447a..5ce039276 100644 --- a/src/kdefrontend/spreadsheet/DropValuesDialog.h +++ b/src/kdefrontend/spreadsheet/DropValuesDialog.h @@ -1,59 +1,60 @@ /*************************************************************************** File : DropValuesDialog.h Project : LabPlot Description : Dialog for droping and masking values in columns -------------------------------------------------------------------- Copyright : (C) 2015 by Alexander Semke (alexander.semke@web.de) ***************************************************************************/ /*************************************************************************** * * * 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 * * * ***************************************************************************/ #ifndef DROPVALUESDIALOG_H #define DROPVALUESDIALOG_H #include "ui_dropvalueswidget.h" -#include +#include class Column; class Spreadsheet; - -class DropValuesDialog : public KDialog { +class QPushButton; +class DropValuesDialog : public QDialog { Q_OBJECT public: explicit DropValuesDialog(Spreadsheet* s, bool mask = false, QWidget* parent = 0, Qt::WFlags fl = 0); void setColumns(QVector); private: Ui::DropValuesWidget ui; QVector m_columns; Spreadsheet* m_spreadsheet; bool m_mask; void dropValues() const; void maskValues() const; + QPushButton* m_okButton; private slots: void operatorChanged(int) const; void okClicked() const; }; #endif