Skip to content

Commit

Permalink
More QWAM
Browse files Browse the repository at this point in the history
  • Loading branch information
dangelog committed Feb 15, 2022
1 parent 62ff327 commit 468b3e3
Show file tree
Hide file tree
Showing 11 changed files with 741 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Qt-Widgets-and-more/CheckIndex/CheckIndex.h
Original file line number Diff line number Diff line change
@@ -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<const QAbstractTableModel *>(this) \
|| qobject_cast<const QAbstractListModel *>(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)
13 changes: 13 additions & 0 deletions Qt-Widgets-and-more/CheckIndex/CheckIndex.pro
Original file line number Diff line number Diff line change
@@ -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
234 changes: 234 additions & 0 deletions Qt-Widgets-and-more/CheckIndex/DepecheModel.cpp
Original file line number Diff line number Diff line change
@@ -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 <QColor>

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<Column>(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<Column>(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<Column>(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 {};
}
Loading

0 comments on commit 468b3e3

Please sign in to comment.