diff --git a/src/kdefrontend/spreadsheet/DropValuesDialog.cpp b/src/kdefrontend/spreadsheet/DropValuesDialog.cpp index 67d015df4..7f888ae05 100644 --- a/src/kdefrontend/spreadsheet/DropValuesDialog.cpp +++ b/src/kdefrontend/spreadsheet/DropValuesDialog.cpp @@ -1,348 +1,365 @@ /*************************************************************************** File : DropValuesDialog.cpp Project : LabPlot Description : Dialog for droping and masking values in columns -------------------------------------------------------------------- - Copyright : (C) 2015 by Alexander Semke (alexander.semke@web.de) + Copyright : (C) 2015-2020 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 +#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) : QDialog(parent), m_spreadsheet(s), m_mask(mask) { setWindowTitle(i18nc("@title:window", "Drop Values")); 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 than")); ui.cbOperator->addItem(i18n("Greater than or Equal to")); ui.cbOperator->addItem(i18n("Lesser than")); ui.cbOperator->addItem(i18n("Lesser than or Equal to")); ui.leValue1->setValidator( new QDoubleValidator(ui.leValue1) ); ui.leValue2->setValidator( new QDoubleValidator(ui.leValue2) ); QDialogButtonBox* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); ui.horizontalLayout->addWidget(btnBox); m_okButton = btnBox->button(QDialogButtonBox::Ok); connect(btnBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &DropValuesDialog::close); if (m_mask) { m_okButton->setText(i18n("&Mask")); m_okButton->setToolTip(i18n("Mask values in the specified region")); ui.lMode->setText(i18n("Mask values")); setWindowTitle(i18nc("@title:window", "Mask Values")); } else { m_okButton->setText(i18n("&Drop")); m_okButton->setToolTip(i18n("Drop values in the specified region")); } connect(ui.cbOperator, static_cast(&QComboBox::currentIndexChanged), this, &DropValuesDialog::operatorChanged ); connect(m_okButton, &QPushButton::clicked, this, &DropValuesDialog::okClicked); connect(btnBox, &QDialogButtonBox::accepted, this, &DropValuesDialog::accept); connect(btnBox, &QDialogButtonBox::rejected, this, &DropValuesDialog::reject); - resize( QSize(400,0).expandedTo(minimumSize()) ); - operatorChanged(0); + //restore saved settings if available + create(); // ensure there's a window created + KConfigGroup conf(KSharedConfig::openConfig(), QLatin1String("DropValuesDialog")); + if (conf.exists()) { + KWindowConfig::restoreWindowSize(windowHandle(), conf); + resize(windowHandle()->size()); // workaround for QTBUG-40584 + } else + resize(QSize(400, 0).expandedTo(minimumSize())); +} + +DropValuesDialog::~DropValuesDialog() { + //save the current settings + KConfigGroup conf(KSharedConfig::openConfig(), QLatin1String("DropValuesDialog")); + KWindowConfig::saveWindowSize(windowHandle(), conf); } 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() override { m_column->setSuppressDataChangedSignal(true); bool changed = false; auto* data = static_cast* >(m_column->data()); //equal to //TODO: use an enum if (m_operator == 0) { for (int i = 0; i < data->size(); ++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; i < data->size(); ++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; i < data->size(); ++i) { if (data->at(i) > m_value1 && data->at(i) < m_value2) { m_column->setMasked(i, true); changed = true; } } } //greater than else if (m_operator == 3) { for (int i = 0; i < data->size(); ++i) { if (data->at(i) > m_value1) { m_column->setMasked(i, true); changed = true; } } } //greater than or equal to else if (m_operator == 4) { for (int i = 0; i < data->size(); ++i) { if (data->at(i) >= m_value1) { m_column->setMasked(i, true); changed = true; } } } //lesser than else if (m_operator == 5) { for (int i = 0; i < data->size(); ++i) { if (data->at(i) < m_value1) { m_column->setMasked(i, true); changed = true; } } } //lesser than or equal to else if (m_operator == 6) { for (int i = 0; i < data->size(); ++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() override { bool changed = false; auto* data = static_cast* >(m_column->data()); QVector new_data(*data); //equal to if (m_operator == 0) { for (auto& d : new_data) { if (d == m_value1) { d = NAN; changed = true; } } } //between (including end points) else if (m_operator == 1) { for (auto& d : new_data) { if (d >= m_value1 && d <= m_value2) { d = NAN; changed = true; } } } //between (excluding end points) else if (m_operator == 2) { for (auto& d : new_data) { if (d > m_value1 && d < m_value2) { d = NAN; changed = true; } } } //greater than else if (m_operator == 3) { for (auto& d : new_data) { if (d > m_value1) { d = NAN; changed = true; } } } //greater than or equal to else if (m_operator == 4) { for (auto& d : new_data) { if (d >= m_value1) { d = NAN; changed = true; } } } //less than else if (m_operator == 5) { for (auto& d : new_data) { if (d < m_value1) { d = NAN; changed = true; } } } //less than or equal to else if (m_operator == 6) { for (auto& d : new_data) { if (d <= m_value1) { d = NAN; changed = true; } } } if (changed) m_column->replaceValues(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(); for (Column* col: m_columns) { auto* 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); delete 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(); for (Column* col: m_columns) { auto* 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 983eb738c..f899534b5 100644 --- a/src/kdefrontend/spreadsheet/DropValuesDialog.h +++ b/src/kdefrontend/spreadsheet/DropValuesDialog.h @@ -1,60 +1,61 @@ /*************************************************************************** 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 class Column; class Spreadsheet; class QPushButton; class DropValuesDialog : public QDialog { Q_OBJECT public: explicit DropValuesDialog(Spreadsheet* s, bool mask = false, QWidget* parent = nullptr); + ~DropValuesDialog() override; 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 diff --git a/src/kdefrontend/spreadsheet/SortDialog.cpp b/src/kdefrontend/spreadsheet/SortDialog.cpp index 90011c5a1..f212e744f 100644 --- a/src/kdefrontend/spreadsheet/SortDialog.cpp +++ b/src/kdefrontend/spreadsheet/SortDialog.cpp @@ -1,108 +1,112 @@ /*************************************************************************** File : SortDialog.h Project : LabPlot Description : Sorting options dialog -------------------------------------------------------------------- - Copyright : (C) 2011-2018 by Alexander Semke (alexander.semke@web.de) + Copyright : (C) 2011-2020 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 "SortDialog.h" #include "backend/core/column/Column.h" #include +#include #include #include #include /*! \class SortDialog \brief Dialog for sorting the columns in a spreadsheet. \ingroup kdefrontend */ SortDialog::SortDialog(QWidget* parent) : QDialog(parent) { setWindowIcon(QIcon::fromTheme("view-sort-ascending")); setWindowTitle(i18nc("@title:window", "Sort Columns")); setSizeGripEnabled(true); setAttribute(Qt::WA_DeleteOnClose); ui.setupUi(this); ui.buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Sort")); connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &SortDialog::sortColumns); connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &SortDialog::reject); connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &SortDialog::accept); connect(ui.cbSorting, static_cast(&QComboBox::currentIndexChanged), this, &SortDialog::changeType); //restore saved settings if available + create(); // ensure there's a window created KConfigGroup conf(KSharedConfig::openConfig(), QLatin1String("SortDialog")); - if (conf.exists()) + if (conf.exists()) { KWindowConfig::restoreWindowSize(windowHandle(), conf); - else + resize(windowHandle()->size()); // workaround for QTBUG-40584 + } else resize(QSize(300, 0).expandedTo(minimumSize())); ui.cbOrdering->setCurrentIndex(conf.readEntry(QLatin1String("Ordering"), 0)); ui.cbSorting->setCurrentIndex(conf.readEntry(QLatin1String("Sorting"), 0)); } SortDialog::~SortDialog() { //save the current settings KConfigGroup conf(KSharedConfig::openConfig(), QLatin1String("SortDialog")); + KWindowConfig::saveWindowSize(windowHandle(), conf); // general settings conf.writeEntry(QLatin1String("Ordering"), ui.cbOrdering->currentIndex()); conf.writeEntry(QLatin1String("Sorting"), ui.cbSorting->currentIndex()); } void SortDialog::sortColumns() { Column* leading{nullptr}; if (ui.cbSorting->currentIndex() == Together) leading = m_columns.at(ui.cbColumns->currentIndex()); emit sort(leading, m_columns, ui.cbOrdering->currentIndex() == Ascending); } void SortDialog::setColumns(QVector columns) { m_columns = columns; for (auto* col : m_columns) ui.cbColumns->addItem(col->name()); ui.cbColumns->setCurrentIndex(0); if (m_columns.size() == 1) { ui.lSorting->hide(); ui.cbSorting->hide(); ui.lColumns->hide(); ui.cbColumns->hide(); } } void SortDialog::changeType(int Type) { if (Type == Together) ui.cbColumns->setEnabled(true); else ui.cbColumns->setEnabled(false); } diff --git a/src/kdefrontend/worksheet/GridDialog.cpp b/src/kdefrontend/worksheet/GridDialog.cpp index b56910bab..31ab188c3 100644 --- a/src/kdefrontend/worksheet/GridDialog.cpp +++ b/src/kdefrontend/worksheet/GridDialog.cpp @@ -1,118 +1,136 @@ /*************************************************************************** File : GridDialog.cpp Project : LabPlot -------------------------------------------------------------------- - Copyright : (C) 2011-2017 by Alexander Semke + Copyright : (C) 2011-2020 by Alexander Semke Email (use @ for *) : alexander.semke@web.de Description : dialog for editing the grid properties for the worksheet view ***************************************************************************/ /*************************************************************************** * * * 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 "GridDialog.h" + #include #include #include #include #include +#include #include #include +#include +#include +#include //TODO: //1. improve the layout and move the UI-part to a ui-file -//2. restore the dialog size -//3. restore the currently active grid settings +//2. restore the currently active grid settings /** * @brief Provides a dialog for editing the grid properties for the worksheet view * \ingroup kdefrontend */ GridDialog::GridDialog(QWidget* parent) : QDialog(parent) { setWindowTitle(i18nc("@title:window", "Custom Grid")); QWidget* widget = new QWidget; auto* layout = new QGridLayout(widget); QLabel* label = new QLabel(i18n("Style:"), widget); layout->addWidget(label, 0, 0); cbStyle = new QComboBox(this); cbStyle->addItem(i18n("Lines")); cbStyle->addItem(i18n("Dots")); cbStyle->setCurrentIndex(0); layout->addWidget(cbStyle, 0, 1); label = new QLabel(i18n("Horizontal spacing:"), widget); layout->addWidget(label, 1, 0); sbHorizontalSpacing = new QSpinBox(widget); sbHorizontalSpacing->setRange(1,100); sbHorizontalSpacing->setValue(10); layout->addWidget(sbHorizontalSpacing, 1, 1); label = new QLabel(i18n("Vertical spacing:"), widget); layout->addWidget(label, 2, 0); sbVerticalSpacing = new QSpinBox(widget); sbVerticalSpacing->setRange(1,100); sbVerticalSpacing->setValue(10); layout->addWidget(sbVerticalSpacing, 2, 1); label = new QLabel(i18n("Color:"), widget); layout->addWidget(label, 3, 0); kcbColor = new KColorButton(widget); kcbColor->setColor(Qt::gray); layout->addWidget(kcbColor , 3, 1); label = new QLabel(i18n("Opacity:"), widget); layout->addWidget(label, 4, 0); sbOpacity = new QSpinBox(widget); sbOpacity->setRange(1,100); sbOpacity->setValue(100); layout->addWidget(sbOpacity, 4, 1); label = new QLabel(i18n(" %"), widget); layout->addWidget(label, 4, 2); - auto* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); auto* vlayout = new QVBoxLayout(this); vlayout->addWidget(widget); vlayout->addWidget(buttonBox); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); - connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + + //restore saved settings if available + create(); // ensure there's a window created + KConfigGroup conf(KSharedConfig::openConfig(), QLatin1String("GridDialog")); + if (conf.exists()) { + KWindowConfig::restoreWindowSize(windowHandle(), conf); + resize(windowHandle()->size()); // workaround for QTBUG-40584 + } else + resize(QSize(300, 0).expandedTo(minimumSize())); +} + +GridDialog::~GridDialog() { + //save the current settings + KConfigGroup conf(KSharedConfig::openConfig(), QLatin1String("GridDialog")); + KWindowConfig::saveWindowSize(windowHandle(), conf); } void GridDialog::save(WorksheetView::GridSettings& settings) { if (cbStyle->currentIndex() == 0) settings.style = WorksheetView::LineGrid; else settings.style = WorksheetView::DotGrid; settings.horizontalSpacing = sbHorizontalSpacing->value(); settings.verticalSpacing = sbVerticalSpacing->value(); settings.color = kcbColor->color(); settings.opacity = (float)sbOpacity->value()/100; } diff --git a/src/kdefrontend/worksheet/GridDialog.h b/src/kdefrontend/worksheet/GridDialog.h index 59105f7e9..c137c338d 100644 --- a/src/kdefrontend/worksheet/GridDialog.h +++ b/src/kdefrontend/worksheet/GridDialog.h @@ -1,55 +1,56 @@ /*************************************************************************** File : GridDialog.h Project : LabPlot -------------------------------------------------------------------- Copyright : (C) 2011 by Alexander Semke Email (use @ for *) : alexander.semke*web.de Description : dialog for editing the grid properties for the worksheet view ***************************************************************************/ /*************************************************************************** * * * 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 GRIDDIALOG_H #define GRIDDIALOG_H #include #include "commonfrontend/worksheet/WorksheetView.h" class QComboBox; class QSpinBox; class KColorButton; class GridDialog : public QDialog { Q_OBJECT public: explicit GridDialog(QWidget*); + ~GridDialog() override; void save(WorksheetView::GridSettings&); private: QComboBox* cbStyle; QSpinBox* sbHorizontalSpacing; QSpinBox* sbVerticalSpacing; KColorButton* kcbColor; QSpinBox* sbOpacity; }; #endif