From 468b3e38178d5be35938a4bb69c7a34431c0b389 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 15 Feb 2022 20:58:08 +0100 Subject: [PATCH] More QWAM --- Qt-Widgets-and-more/CheckIndex/CheckIndex.h | 91 +++++++ Qt-Widgets-and-more/CheckIndex/CheckIndex.pro | 13 + .../CheckIndex/DepecheModel.cpp | 234 ++++++++++++++++++ Qt-Widgets-and-more/CheckIndex/DepecheModel.h | 90 +++++++ .../CheckIndex/EnumConverters.h | 56 +++++ .../CheckIndex/FilterCollection.cpp | 34 +++ .../CheckIndex/FilterCollection.h | 35 +++ Qt-Widgets-and-more/CheckIndex/enums.h | 26 ++ Qt-Widgets-and-more/CheckIndex/main.cpp | 46 ++++ .../StretchingTableViews.pro | 6 + .../StretchingTableViews/main.cpp | 110 ++++++++ 11 files changed, 741 insertions(+) create mode 100644 Qt-Widgets-and-more/CheckIndex/CheckIndex.h create mode 100644 Qt-Widgets-and-more/CheckIndex/CheckIndex.pro create mode 100644 Qt-Widgets-and-more/CheckIndex/DepecheModel.cpp create mode 100644 Qt-Widgets-and-more/CheckIndex/DepecheModel.h create mode 100644 Qt-Widgets-and-more/CheckIndex/EnumConverters.h create mode 100644 Qt-Widgets-and-more/CheckIndex/FilterCollection.cpp create mode 100644 Qt-Widgets-and-more/CheckIndex/FilterCollection.h create mode 100644 Qt-Widgets-and-more/CheckIndex/enums.h create mode 100644 Qt-Widgets-and-more/CheckIndex/main.cpp create mode 100644 Qt-Widgets-and-more/StretchingTableViews/StretchingTableViews.pro create mode 100644 Qt-Widgets-and-more/StretchingTableViews/main.cpp diff --git a/Qt-Widgets-and-more/CheckIndex/CheckIndex.h b/Qt-Widgets-and-more/CheckIndex/CheckIndex.h new file mode 100644 index 0000000..74be58b --- /dev/null +++ b/Qt-Widgets-and-more/CheckIndex/CheckIndex.h @@ -0,0 +1,91 @@ +/* MIT License + +Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#pragma once + +#define CHECK_rowCount(index) Q_ASSERT(checkIndex(index)) + +#define CHECK_columnCount(index) Q_ASSERT(checkIndex(index)) + +#define CHECK_data(index) \ + if (qobject_cast(this) \ + || qobject_cast(this)) \ + Q_ASSERT(checkIndex(index, \ + QAbstractItemModel::CheckIndexOption::IndexIsValid \ + | QAbstractItemModel::CheckIndexOption::ParentIsInvalid)); \ + else \ + Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid)) + +#define CHECK_setData(index) CHECK_data(index) + +#define CHECK_headerData(section, orientation) \ + Q_ASSERT(section >= 0); \ + if (orientation == Qt::Horizontal) \ + Q_ASSERT(section < columnCount({})); \ + else \ + Q_ASSERT(section < rowCount({})); + +#define CHECK_setHeaderData(section, orientation) CHECK_headerData(section, orientation) + +#define CHECK_flags(index) Q_ASSERT(checkIndex(index)) + +// If you get an assert for row or column being zero and rowCount() and columnCount() is zero too, +// then there is a chance that it is a subclass of QAbstractProxyModel::headerData making this out +// of bound call. The cure is to implement headerData in the subclass of QAbstractProxyModel, and +// while doing so /not/ use mapToSource for finding the row or column in the source model. +#define CHECK_index(row, column, parent) \ + Q_ASSERT(row >= 0); \ + Q_ASSERT(column >= 0); \ + Q_ASSERT(row < rowCount(parent)); \ + Q_ASSERT(column < columnCount(parent)); \ + Q_ASSERT(checkIndex(parent)); + +#define CHECK_parent(parent) \ + Q_ASSERT(checkIndex(parent, QAbstractItemModel::CheckIndexOption::DoNotUseParent)); + +#define CHECK_insertRows(row, count, parent) \ + Q_ASSERT(checkIndex(parent)); \ + Q_ASSERT(row >= 0); \ + Q_ASSERT(row <= rowCount(parent)); \ + Q_ASSERT(count > 0) + +// Technically it might be OK to call removeRows with count == 0, so you might +// consider taking that out and making it a check in your code instead. +#define CHECK_removeRows(row, count, parent) \ + Q_ASSERT(checkIndex(parent)); \ + Q_ASSERT(row >= 0); \ + Q_ASSERT(count > 0); \ + Q_ASSERT(row <= rowCount(parent) - count) + +#define CHECK_insertColumns(column, count, parent) \ + Q_ASSERT(checkIndex(parent)); \ + Q_ASSERT(column >= 0); \ + Q_ASSERT(column <= columnCount(parent)); \ + Q_ASSERT(count > 0) + +// Same as removeRows above. +#define CHECK_removeColumns(column, count, parent) \ + Q_ASSERT(checkIndex(parent)); \ + Q_ASSERT(column >= 0); \ + Q_ASSERT(count > 0); \ + Q_ASSERT(column <= columnCount(parent) - count) diff --git a/Qt-Widgets-and-more/CheckIndex/CheckIndex.pro b/Qt-Widgets-and-more/CheckIndex/CheckIndex.pro new file mode 100644 index 0000000..e4d4336 --- /dev/null +++ b/Qt-Widgets-and-more/CheckIndex/CheckIndex.pro @@ -0,0 +1,13 @@ +QT += core gui widgets +CONFIG += c++17 +SOURCES += \ + DepecheModel.cpp \ + FilterCollection.cpp \ + main.cpp + +HEADERS += \ + CheckIndex.h \ + DepecheModel.h \ + EnumConverters.h \ + FilterCollection.h \ + enums.h diff --git a/Qt-Widgets-and-more/CheckIndex/DepecheModel.cpp b/Qt-Widgets-and-more/CheckIndex/DepecheModel.cpp new file mode 100644 index 0000000..142bb7b --- /dev/null +++ b/Qt-Widgets-and-more/CheckIndex/DepecheModel.cpp @@ -0,0 +1,234 @@ +/* MIT License + +Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "DepecheModel.h" +#include "CheckIndex.h" +#include "EnumConverters.h" +#include "enums.h" +#include + +static Qt::CheckState boolToState(bool b) +{ + return b ? Qt::Checked : Qt::Unchecked; +}; + +DepecheModel::DepecheModel(QObject *parent) + : QAbstractTableModel(parent) +{ + // source: https://en.wikipedia.org/wiki/Depeche_Mode_discography + m_data = { + {"1980 th", -1, "", true, true, true, true, true, true, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1}, + {"Speak & Spell", 1981, "Mute", true, true, true, false, false, false, 10, 28, -1, -1, 49, + 99, -1, 21, -1, 192}, + {"A Broken Frame", 1982, "Mute", true, true, true, false, false, false, 8, -1, -1, 194, 56, + 88, -1, 22, -1, 177}, + {"Construction Time Again ", 1983, "Mute", true, true, true, false, false, false, 6, -1, -1, + -1, 7, -1, 32, 12, 21, -1}, + {"Some Great Reward", 1984, "Mute", true, true, true, false, false, false, 5, -1, 19, -1, 3, + 32, 34, 7, 5, 51}, + {"Black Celebration", 1986, "Mute", true, true, true, false, false, false, 4, 69, 26, 19, 2, + 17, 35, 5, 1, 90}, + {"Music for the Masses", 1987, "Mute", true, true, true, false, false, false, 10, 60, 16, 7, + 2, 7, 52, 4, 4, 35}, + {"1990th", -1, "", true, true, true, true, true, true, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1}, + {"Violator", 1990, "Mute", true, true, true, true, true, false, 2, 42, 4, 1, 2, 5, 17, 6, 2, + 7}, + {"Songs of Faith and Devotion", 1993, "Mute", true, true, true, true, true, false, 1, 14, 1, + 1, 1, 1, 18, 2, 1, 1}, + {"Ultra", 1997, "Mute", true, true, true, false, false, false, 1, 7, 5, 2, 1, 2, 17, 1, 4, + 5}, + {"2000th", -1, "", true, true, true, true, true, true, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1}, + {"Exciter", 2001, "Mute", true, true, true, false, false, false, 9, 20, 2, 1, 1, 2, 15, 1, + 2, 8}, + {"Playing the Angel", 2005, "Mute,Reprise", true, false, true, false, false, true, 6, 45, 1, + 1, 1, 1, 11, 1, 1, 7}, + {"Sounds of the Universe", 2009, "Mute", true, false, true, false, false, true, 2, 32, 1, 2, + 1, 1, 7, 1, 1, 3}, + {"2010th", -1, "", true, true, true, true, true, true, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1}, + {"Delta Machine", 2013, "Mute, Columbia", true, false, true, false, false, true, 2, 16, 1, + 2, 1, 1, 3, 1, 1, 6}, + {"Spirit", 2017, "Mute, Columbia", true, false, true, false, false, true, 5, 14, 1, 1, 1, 1, + 4, 3, 1, 5}, + }; +} + +int DepecheModel::rowCount(const QModelIndex &parent) const +{ + CHECK_rowCount(parent); + if (parent.isValid()) + return 0; + + return m_data.count(); +} + +QVariant DepecheModel::data(const QModelIndex &index, int role) const +{ + Q_ASSERT(checkIndex(index, CheckIndexOption::IndexIsValid)); + CHECK_data(index); + + auto peekChartPosition = [](int position) { + return (position == -1) ? QString() : QString::number(position); + }; + + auto isTitleRow = [&] { return m_data.at(index.row()).year == -1; }; + + switch (role) { + case +GlobalRole::IsTitleRow: + return isTitleRow(); + + case Qt::DisplayRole: { + const Data &data = m_data.at(index.row()); + switch (enumCast(index.column())) { + case Column::Album: + return data.album; + case Column::Year: + return data.year; + case Column::RecordLabel: + return data.recordLabel; + case Column::UK: + return peekChartPosition(data.UK); + case Column::AUS: + return peekChartPosition(data.AUS); + case Column::AUT: + return peekChartPosition(data.AUT); + case Column::FRA: + return peekChartPosition(data.FRA); + case Column::GER: + return peekChartPosition(data.GER); + case Column::ITA: + return peekChartPosition(data.ITA); + case Column::NLD: + return peekChartPosition(data.NLD); + case Column::SWE: + return peekChartPosition(data.SWE); + case Column::SWI: + return peekChartPosition(data.SWI); + case Column::US: + return peekChartPosition(data.US); + case Column::COLUMNCOUNT: + Q_UNREACHABLE(); + default: + return {}; + } + } + case Qt::CheckStateRole: { + const Data &data = m_data.at(index.row()); + + switch (enumCast(index.column())) { + case Column::CD: + return boolToState(data.CD); + case Column::CS: + return boolToState(data.CS); + case Column::LP: + return boolToState(data.LP); + case Column::DCC: + return boolToState(data.DCC); + case Column::MD: + return boolToState(data.MD); + case Column::download: + return boolToState(data.download); + default: + return {}; + } + } + case Qt::TextAlignmentRole: + if (index.column() == +Column::Album) + return isTitleRow() ? int(Qt::AlignCenter) : int(Qt::AlignLeft | Qt::AlignVCenter); + return (index.column() == +Column::RecordLabel) ? int(Qt::AlignLeft | Qt::AlignVCenter) + : int(Qt::AlignCenter); + + case Qt::ForegroundRole: + return isTitleRow() ? QVariant::fromValue(QColor(Qt::white)) : QVariant(); + + case Qt::BackgroundRole: + return isTitleRow() ? QVariant::fromValue(QColor(Qt::black)) : QVariant(); + } + + return {}; +} + +int DepecheModel::columnCount(const QModelIndex &parent) const +{ + CHECK_columnCount(parent); + return +Column::COLUMNCOUNT; +} + +QVariant DepecheModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + CHECK_headerData(section, orientation); + switch (role) { + case Qt::DisplayRole: { + if (orientation == Qt::Horizontal) { + switch (enumCast(section)) { + case Column::Album: + return "Album"; + case Column::Year: + return "Year"; + case Column::RecordLabel: + return "Record Label"; + case Column::CD: + return "CD"; + case Column::CS: + return "CS"; + case Column::LP: + return "LP"; + case Column::DCC: + return "DCC"; + case Column::MD: + return "MD"; + case Column::download: + return "download"; + case Column::UK: + return "UK"; + case Column::AUS: + return "AUS"; + case Column::AUT: + return "AUT"; + case Column::FRA: + return "FRA"; + case Column::GER: + return "GER"; + case Column::ITA: + return "ITA"; + case Column::NLD: + return "NLD"; + case Column::SWE: + return "SWE"; + case Column::SWI: + return "SWI"; + case Column::US: + return "US"; + case Column::COLUMNCOUNT: + Q_UNREACHABLE(); + } + } + return {}; + } + } + + return {}; +} diff --git a/Qt-Widgets-and-more/CheckIndex/DepecheModel.h b/Qt-Widgets-and-more/CheckIndex/DepecheModel.h new file mode 100644 index 0000000..589450e --- /dev/null +++ b/Qt-Widgets-and-more/CheckIndex/DepecheModel.h @@ -0,0 +1,90 @@ +/* MIT License + +Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#pragma once + +#include +#include + +class DepecheModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + explicit DepecheModel(QObject *parent = nullptr); + + enum class Column { + Album, + Year, + RecordLabel, + CD, + CS, + LP, + DCC, + MD, + download, + COLUMNCOUNT, + // lets skip all these columns, otherwise it wont fit on my screen during recording. + // I'll keep the rest in here, it is after all just an example + UK, + AUS, + AUT, + FRA, + GER, + ITA, + NLD, + SWE, + SWI, + US, + + }; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + +private: + struct Data + { + QString album; + int year; + QString recordLabel; + bool CD; + bool CS; + bool LP; + bool DCC; + bool MD; + bool download; + int UK; + int AUS; + int AUT; + int FRA; + int GER; + int ITA; + int NLD; + int SWE; + int SWI; + int US; + }; + QVector m_data; +}; diff --git a/Qt-Widgets-and-more/CheckIndex/EnumConverters.h b/Qt-Widgets-and-more/CheckIndex/EnumConverters.h new file mode 100644 index 0000000..adbaefa --- /dev/null +++ b/Qt-Widgets-and-more/CheckIndex/EnumConverters.h @@ -0,0 +1,56 @@ +/* MIT License + +Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +#pragma once + +#include + +// Tool to convert class enum value to underlying type for easier use +template +constexpr typename std::underlying_type::type operator+(E e) +{ + return static_cast::type>(e); +} + +namespace { +template +constexpr bool HasColumnCount = false; +template +constexpr bool HasColumnCount> = true; + +template +constexpr bool HasRowCount = false; +template +constexpr bool HasRowCount> = true; +} + +template +TYPE enumCast(int value) +{ + if constexpr (HasColumnCount) + Q_ASSERT(value >= 0 && value < +TYPE::COLUMNCOUNT); + else if constexpr (HasRowCount) + Q_ASSERT(value >= 0 && value < +TYPE::ROWCOUNT); + else + Q_ASSERT(value >= 0 && value < +TYPE::MAXCOUNT); + return static_cast(value); +} diff --git a/Qt-Widgets-and-more/CheckIndex/FilterCollection.cpp b/Qt-Widgets-and-more/CheckIndex/FilterCollection.cpp new file mode 100644 index 0000000..340b56d --- /dev/null +++ b/Qt-Widgets-and-more/CheckIndex/FilterCollection.cpp @@ -0,0 +1,34 @@ +/* MIT License + +Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "FilterCollection.h" +#include "EnumConverters.h" +#include "enums.h" + +bool FilterCollection::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const +{ + return !sourceModel() + ->index(source_row, 0, source_parent) + .data(+GlobalRole::IsTitleRow) + .toBool(); +} diff --git a/Qt-Widgets-and-more/CheckIndex/FilterCollection.h b/Qt-Widgets-and-more/CheckIndex/FilterCollection.h new file mode 100644 index 0000000..6db785b --- /dev/null +++ b/Qt-Widgets-and-more/CheckIndex/FilterCollection.h @@ -0,0 +1,35 @@ +/* MIT License + +Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#pragma once + +#include + +class FilterCollection : public QSortFilterProxyModel +{ + using QSortFilterProxyModel::QSortFilterProxyModel; + + // QSortFilterProxyModel interface +protected: + bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; +}; diff --git a/Qt-Widgets-and-more/CheckIndex/enums.h b/Qt-Widgets-and-more/CheckIndex/enums.h new file mode 100644 index 0000000..0b4e85f --- /dev/null +++ b/Qt-Widgets-and-more/CheckIndex/enums.h @@ -0,0 +1,26 @@ +/* MIT License + +Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#pragma once + +enum class GlobalRole { IsTitleRow = 4215 }; diff --git a/Qt-Widgets-and-more/CheckIndex/main.cpp b/Qt-Widgets-and-more/CheckIndex/main.cpp new file mode 100644 index 0000000..d82e8ea --- /dev/null +++ b/Qt-Widgets-and-more/CheckIndex/main.cpp @@ -0,0 +1,46 @@ +/* MIT License + +Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "DepecheModel.h" +#include "FilterCollection.h" +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + DepecheModel model; + FilterCollection filter; + filter.setSourceModel(&model); + QTreeView view; + view.setModel(&filter); + + QObject::connect(&view, &QAbstractItemView::clicked, [&model](const QModelIndex &index) { + qDebug().noquote() << "♫♫ " << model.data(index.sibling(index.row(), 0)).toString() + << " ♫♫"; + }); + view.show(); + return a.exec(); +} diff --git a/Qt-Widgets-and-more/StretchingTableViews/StretchingTableViews.pro b/Qt-Widgets-and-more/StretchingTableViews/StretchingTableViews.pro new file mode 100644 index 0000000..459cd57 --- /dev/null +++ b/Qt-Widgets-and-more/StretchingTableViews/StretchingTableViews.pro @@ -0,0 +1,6 @@ +QT += core gui widgets +CONFIG += c++17 +SOURCES += \ + main.cpp + +HEADERS += diff --git a/Qt-Widgets-and-more/StretchingTableViews/main.cpp b/Qt-Widgets-and-more/StretchingTableViews/main.cpp new file mode 100644 index 0000000..31a8312 --- /dev/null +++ b/Qt-Widgets-and-more/StretchingTableViews/main.cpp @@ -0,0 +1,110 @@ +/* MIT License + +Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + auto top = new QWidget; + QTableView *view = new QTableView; + QTextEdit *edit = new QTextEdit; + edit->setText("This edit is just to show the table in a layout"); + + auto layout = new QVBoxLayout(top); + layout->addWidget(view); + layout->addWidget(edit); + top->resize(800, 800); + + constexpr int rows = 10; + constexpr int columns = 5; + QStandardItemModel model(rows, columns); + for (int row = 0; row < rows; ++row) { + for (int col = 0; col < columns; ++col) { + model.setItem(row, col, new QStandardItem(QString("(%1,%2)").arg(row).arg(col))); + } + } + view->setModel(&model); + auto verticalHeader = view->verticalHeader(); + auto horizontalHeader = view->horizontalHeader(); + + if (false) { + // stretch last column + horizontalHeader->setStretchLastSection(true); + + // Not sure if this makes sense at all, but at least we got rid of the empty space :-) + verticalHeader->setStretchLastSection(true); + } + + if (false) { + // Stretching other columns that just the last one + horizontalHeader->setSectionResizeMode(1, QHeaderView::Stretch); + horizontalHeader->setSectionResizeMode(2, QHeaderView::Stretch); + horizontalHeader->setSectionResizeMode(3, QHeaderView::ResizeToContents); + } + + if (false) { + // compress columns + horizontalHeader->setSectionResizeMode(QHeaderView::ResizeToContents); + } + + if (true) { + // compress rows. + verticalHeader->setSectionResizeMode(QHeaderView::ResizeToContents); + } + + if (false) { + // Resizing to content initially, but still allowing the columns to be resized later. + horizontalHeader->setResizeContentsPrecision(-1); + for (int col = 0; col < columns; ++col) + view->resizeColumnToContents(col); + horizontalHeader->setStretchLastSection(true); + } + + if (true) { + // Fix the size of the view so it can show without scrollbars + view->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); + view->setSizePolicy({QSizePolicy::Preferred, QSizePolicy::Fixed}); + + auto timer = new QTimer; + timer->setInterval(2000); + QObject::connect(timer, &QTimer::timeout, [&] { + model.setRowCount(model.rowCount() + 1); + model.setItem(model.rowCount() - 1, 0, + new QStandardItem(QString("Row %1").arg(model.rowCount()))); + }); + timer->start(); + } + + top->show(); + return a.exec(); +}