Skip to content

Instantly share code, notes, and snippets.

@Riateche
Created July 24, 2013 10:13
Show Gist options
  • Save Riateche/6069399 to your computer and use it in GitHub Desktop.
Save Riateche/6069399 to your computer and use it in GitHub Desktop.
#include "CalendarFilterModel.h"
#include <QTableView>
#include <QDebug>
#include <QStyledItemDelegate>
QObject* find_child_by_class(QObject* parent, const QString& class_name) {
foreach(QObject* object, parent->findChildren<QObject*>()) {
if (object->metaObject()->className() == class_name) {
return object;
}
}
return 0;
}
CalendarFilterModel::CalendarFilterModel(QCalendarWidget* p_calendar) :
QSortFilterProxyModel(p_calendar), calendar(p_calendar)
{
QAbstractItemModel* source_model = static_cast<QAbstractItemModel*>(find_child_by_class(calendar, "QCalendarModel"));
setSourceModel(source_model);
if (!source_model) {
qFatal("failed to find source model");
return;
}
QTableView* view = static_cast<QTableView*>(find_child_by_class(calendar, "QCalendarView"));
if (!view) {
qFatal("failed to find view");
return;
}
view->setModel(this);
view->setItemDelegate(new QStyledItemDelegate());
}
Qt::ItemFlags CalendarFilterModel::flags(const QModelIndex &index) const {
Qt::ItemFlags v = QSortFilterProxyModel::flags(index);
qDebug() << "flags" << v;
return v;
}
QVariant CalendarFilterModel::data(const QModelIndex &index, int role) const {
qDebug() << "data" << role;
if (role == Qt::BackgroundColorRole) {
return Qt::green;
}
qDebug() << QSortFilterProxyModel::data(index, role);
return QSortFilterProxyModel::data(index, role);
}
void CalendarFilterModel::set_disabled_dates(const QList<QDate> &dates) {
emit layoutAboutToBeChanged();
disabled_dates = dates;
emit layoutChanged();
}
#ifndef CALENDARFILTERMODEL_H
#define CALENDARFILTERMODEL_H
#include <QSortFilterProxyModel>
#include <QCalendarWidget>
#include <qitemselectionmodel.h>
class CalendarFilterModel : public QSortFilterProxyModel {
Q_OBJECT
public:
explicit CalendarFilterModel(QCalendarWidget* p_calendar);
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role) const;
void set_disabled_dates(const QList<QDate>& dates);
QDate referenceDate() const;
QDate dateForCell(int row, int column) const;
private:
QCalendarWidget* calendar;
QList<QDate> disabled_dates;
};
#endif // CALENDARFILTERMODEL_H
// in the main window constructor
CalendarFilterModel* m = new CalendarFilterModel(ui->calendarWidget);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment