|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** This file is part of KDAlgorithms |
| 4 | +** |
| 5 | +** SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> |
| 6 | +** |
| 7 | +** SPDX-License-Identifier: MIT |
| 8 | +** |
| 9 | +****************************************************************************/ |
| 10 | + |
| 11 | +#include "../src/kdalgorithms.h" |
| 12 | +#include <QApplication> |
| 13 | +#include <QDebug> |
| 14 | +#include <QLabel> |
| 15 | +#include <QTableWidget> |
| 16 | +#include <QVBoxLayout> |
| 17 | + |
| 18 | +/* |
| 19 | + For details see https://github.com/KDAB/KDAlgorithms/blob/main/Documentation/inspiration.md |
| 20 | +*/ |
| 21 | +class Test : public QWidget |
| 22 | +{ |
| 23 | +public: |
| 24 | + Test() |
| 25 | + { |
| 26 | + auto layout = new QVBoxLayout(this); |
| 27 | + |
| 28 | + m_table = new QTableWidget(5, 5); |
| 29 | + layout->addWidget(m_table); |
| 30 | + |
| 31 | + m_label1 = new QLabel("Select Something"); |
| 32 | + layout->addWidget(m_label1); |
| 33 | + connect(m_table, &QTableWidget::currentCellChanged, this, &Test::updateLabelV1); |
| 34 | + |
| 35 | + m_label2 = new QLabel("Select Something"); |
| 36 | + layout->addWidget(m_label2); |
| 37 | + connect(m_table, &QTableWidget::currentCellChanged, this, &Test::updateLabelV2); |
| 38 | + |
| 39 | + m_label3 = new QLabel("Select Something"); |
| 40 | + layout->addWidget(m_label3); |
| 41 | + connect(m_table, &QTableWidget::currentCellChanged, this, &Test::updateLabelV3); |
| 42 | + } |
| 43 | + |
| 44 | +private: |
| 45 | + /* The normal implementation using for loops */ |
| 46 | + void updateLabelV1() |
| 47 | + { |
| 48 | + bool allColumnsSelected = true; |
| 49 | + int row = m_table->currentRow(); |
| 50 | + auto selectionModel = m_table->selectionModel(); |
| 51 | + auto model = m_table->model(); |
| 52 | + for (int i = 0; i < m_table->columnCount(); ++i) { |
| 53 | + if (!selectionModel->isSelected(model->index(row, i))) { |
| 54 | + allColumnsSelected = false; |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (allColumnsSelected) |
| 60 | + m_label1->setText("All columns selected"); |
| 61 | + else |
| 62 | + m_label1->setText("Not all columns selected"); |
| 63 | + } |
| 64 | + |
| 65 | + /* The improved version using algorithms */ |
| 66 | + void updateLabelV2() |
| 67 | + { |
| 68 | + auto columns = kdalgorithms::iota(m_table->columnCount()); |
| 69 | + auto isColumnSelected = [row = m_table->currentRow(), |
| 70 | + selectionModel = m_table->selectionModel(), |
| 71 | + model = m_table->model()](int column) { |
| 72 | + return selectionModel->isSelected(model->index(row, column)); |
| 73 | + }; |
| 74 | + |
| 75 | + if (kdalgorithms::all_of(columns, isColumnSelected)) |
| 76 | + m_label2->setText("All columns selected"); |
| 77 | + else |
| 78 | + m_label2->setText("Not all columns selected"); |
| 79 | + } |
| 80 | + |
| 81 | + /* The version I would have written without algorithms */ |
| 82 | + void updateLabelV3() |
| 83 | + { |
| 84 | + bool allColumnsSelected = [this] { |
| 85 | + int row = m_table->currentRow(); |
| 86 | + auto selectionModel = m_table->selectionModel(); |
| 87 | + auto model = m_table->model(); |
| 88 | + for (int i = 0; i < m_table->columnCount(); ++i) { |
| 89 | + if (!selectionModel->isSelected(model->index(row, i))) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | + return true; |
| 94 | + }(); |
| 95 | + |
| 96 | + if (allColumnsSelected) |
| 97 | + m_label3->setText("All columns selected"); |
| 98 | + else |
| 99 | + m_label3->setText("Not all columns selected"); |
| 100 | + } |
| 101 | + |
| 102 | + QTableWidget *m_table; |
| 103 | + QLabel *m_label1; |
| 104 | + QLabel *m_label2; |
| 105 | + QLabel *m_label3; |
| 106 | +}; |
| 107 | + |
| 108 | +int main(int argc, char **argv) |
| 109 | +{ |
| 110 | + QApplication app(argc, argv); |
| 111 | + |
| 112 | + Test test; |
| 113 | + test.resize(600, 300); |
| 114 | + test.show(); |
| 115 | + app.exec(); |
| 116 | +} |
0 commit comments