StyleSheetDemo Example

#include <QtGui>
#include <QMessageBox>
#include <QFileDialog>
#include "QtnGridFilter.h"

#include "window.h"

/* Window */
Window::Window()
: DemoMainWindow(QStringLiteral("QtitanDataGrid"), QStringLiteral(QTN_VERSION_DATAGRID_STR), tr("Style Sheet Example"))
{
    Grid::loadTranslation();
    m_grid = new Qtitan::Grid();
#if 1
    // Configure grid view
    m_grid->setViewType(Qtitan::Grid::TableView);
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
    view->beginUpdate();
    view->options().setFooterSummaryVisible(true);
    view->options().setGroupSummaryPlace(GridViewOptions::SummaryRowFooter);
    view->tableOptions().setRowsQuickSelection(true);
    view->tableOptions().setFrozenPlaceQuickSelection(true);
    view->tableOptions().setRowFrozenButtonVisible(true);
    view->options().setSelectionPolicy(GridViewOptions::MultiRowSelection);

    //view->options().setGridLineWidth(0);
    //view->tableOptions().setColumnAutoWidth(true);
    view->options().setShowFocusDecoration(true);
    view->options().setScrollRowStyle(Qtitan::ScrollItemStyle::ScrollByPixel);

    //Connect to Grid's signals.
    connect(view, SIGNAL(contextMenu(ContextMenuEventArgs*)), this, SLOT(contextMenu(ContextMenuEventArgs* )));

    CustomFilterModel* model = new CustomFilterModel(m_grid);
    view->setModel(model);

    Qtitan::GridTableColumn* column = (Qtitan::GridTableColumn *)view->getColumn(0);
    column->setEditorType(GridEditor::Numeric);
    ((Qtitan::GridNumericEditorRepository *)column->editorRepository())->setMinimum(-10000);
    ((Qtitan::GridNumericEditorRepository *)column->editorRepository())->setMaximum(10000);
    column->editorRepository()->setEditorActivationPolicy(
        GridEditor::ActivationPolicy(GridEditor::ActivateByDblClick | GridEditor::ActivateByEditPress));

    column = (Qtitan::GridTableColumn*)view->getColumn(1);
    column->setEditorType(GridEditor::String);
    //column->editorRepository()->setEditable(false);
    column->editorRepository()->setAlignment(Qt::AlignCenter);

    column = (Qtitan::GridTableColumn *)view->getColumn(2);
    column->setEditorType(GridEditor::String);
    column->editorRepository()->setValidateOnEnter(false);
    column->editorRepository()->setEditorActivationPolicy(
        GridEditor::ActivationPolicy(GridEditor::ActivateByDblClick | GridEditor::ActivateByEditPress));
    column->setAutoWidth(false);
    column->setCheckable(true);
    column->setMenuButtonVisible(true);

    column = (Qtitan::GridTableColumn *)view->getColumn(3);
    column->setEditorType(GridEditor::Date);
    column->editorRepository()->setAutoSelect(true);
    column->editorRepository()->setEditorActivationPolicy(
        GridEditor::ActivationPolicy(GridEditor::ActivateByDblClick | GridEditor::ActivateByEditPress));
    column->setGroupIndex(0);
    column->setGroupSummary(column, GridSummary::Max);
    column->setFooterSummary(GridSummary::Count);

    column = (Qtitan::GridTableColumn *)view->getColumn(4);
    column->setEditorType(GridEditor::Time);
    column->editorRepository()->setAutoSelect(true);
    column->editorRepository()->setEditorActivationPolicy(
        GridEditor::ActivationPolicy(GridEditor::ActivateByDblClick | GridEditor::ActivateByEditPress));
    view->endUpdate();

#endif
    //Create settings widget
    QWidget* settings = new QWidget(this);
    QVBoxLayout* l = new QVBoxLayout(settings);

    QLabel* text = new QLabel(settings);
    text->setText(tr("Buttons below allows you to customize the grid via Qt Style Sheet. \
        Press 'Add QSS' button to apply Style Sheet and 'Remove QSS' to remove Style Sheet setting \
        and restore the default view."));
    text->setWordWrap(true);
    l->addWidget(text);

    QVBoxLayout* layout = new QVBoxLayout();
    QPushButton* addButton = new QPushButton(settings);
    addButton->setText(tr("Add QSS"));
    connect(addButton, SIGNAL(clicked()), this, SLOT(addStyleSheet()));
    layout->addWidget(addButton);

    QPushButton* addDesignButton = new QPushButton(settings);
    addDesignButton->setText(tr("Add QSS Design (new)"));
    connect(addDesignButton, SIGNAL(clicked()), this, SLOT(addStyleSheetDesign()));
    layout->addWidget(addDesignButton);

    QPushButton* removeButton = new QPushButton(settings);
    removeButton->setText(tr("Remove QSS"));
    connect(removeButton, SIGNAL(clicked()), this, SLOT(removeStyleSheet()));
    layout->addWidget(removeButton);
    l->addLayout(layout);

    setDemoWidget(m_grid, settings);

    setMinimumHeight(10);
}

void Window::contextMenu(ContextMenuEventArgs* args)
{
    args->contextMenu()->addAction(tr("Print Preview"), this, SLOT(printPreview()));
    args->contextMenu()->addSeparator();
    args->contextMenu()->addAction(tr("Developer Machines on the Web"), this, SLOT(showCompanyWebSite()));
}

void Window::addClicked()
{
    QMessageBox::information(this, tr("Add New Record"), tr("Add New Record"));
}

void Window::setShadeColor(const QColor& color)
{
    m_grid->themeManager()->setShadeColor(color);
}

void Window::addStyleSheet()
{
    QFile file(QStringLiteral(":/res/StyleSheet.qss"));
    file.open(QFile::ReadOnly);
    QString styleSheet = QLatin1String(file.readAll());
    qApp->setStyleSheet(styleSheet);
}

void Window::addStyleSheetDesign()
{
    Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();

    view->beginUpdate();

    view->options().setRowStyle(GridViewOptions::RowStyleOffice);

    view->options().setGridLines(Qtitan::LinesStyle::LinesHorizontal);
    view->options().setAlternatingRowColors(false);
    view->options().setScrollBarsTransparent(true);

    view->tableOptions().setRowsQuickSelection(true);
    view->tableOptions().setFrozenPlaceQuickSelection(true);
    view->tableOptions().setRowFrozenButtonVisible(true);
    view->tableOptions().setColumnsQuickCustomization(false);
    view->clearGrouping();

    GridGroupPanelToolButton* widget = new GridGroupPanelToolButton();
    widget->setIcon(SegoeMDL2Font::icon(SegoeMDL2Font::Add));
    connect(widget, SIGNAL(clicked()), this, SLOT(addClicked()));
    view->addToolWidget(widget);
    view->endUpdate();

    QFile file(QStringLiteral(":/res/StyleSheetDesign.qss"));
    file.open(QFile::ReadOnly);
    QString styleSheet = QLatin1String(file.readAll());
    qApp->setStyleSheet(styleSheet);

    view->bestFit();
}

void Window::removeStyleSheet()
{
    qApp->setStyleSheet(QString());
}

/* CustomFilterModel */
CustomFilterModel::CustomFilterModel(QObject *parent)
: QAbstractItemModel(parent)
{
    m_values.resize(100);
    int y = 2012;
    int m = 1;
    int d = 1;
    for (int i = 0; i < m_values.size(); ++i)
    {
        m_values[i].id = i;
        m_values[i].name = QStringLiteral("Brad Pitt");
        m_values[i].url = QStringLiteral(":res/login_face_icon.png");
        m_values[i].v1 = QStringLiteral("String = %1").arg(i);
        m_values[i].v3 = QTime(12, 0, 0);
        if (d > 28)
        {
            d = 1;
            m++;
            if (m > 12)
            {
                m = 1;
                y++;
            }
        }
        m_values[i].v2 = QDate(y, m, d);
        if (!((i + 1) % 10))
            d++;
    }
}

QVariant CustomFilterModel::headerData(int section, Qt::Orientation orientation,
                                      int role) const
{
    Q_UNUSED(orientation);
    Q_UNUSED(role);
    if (role != Qt::DisplayRole)
        return QVariant();
    switch (section)
    {
    case 0:
        return QStringLiteral("ID");
    case 1:
        return QStringLiteral("Photo");
    case 2:
        return QStringLiteral("String");
    case 3:
        return QStringLiteral("Date");
    case 4:
        return QStringLiteral("Time");
    default:
        break;
    }
    return QVariant();
}

QModelIndex CustomFilterModel::parent(const QModelIndex & /*child*/) const
{
    return QModelIndex();
}

bool CustomFilterModel::hasChildren(const QModelIndex &parent) const
{
    if (parent.model() == this || !parent.isValid())
    {
        return rowCount(parent) > 0 && columnCount(parent) > 0;
    }
    return false;
}

int CustomFilterModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_values.size();
}

int CustomFilterModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return 5;
}

QModelIndex CustomFilterModel::index(int row, int column, const QModelIndex &parent) const
{
    if (parent.isValid())
        return QModelIndex();

    if (row < 0 || row >= rowCount(parent))
        return QModelIndex();

    if (column < 0 || column >= columnCount(parent))
        return QModelIndex();

    return createIndex(row, column, Q_NULL);
}

QVariant CustomFilterModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() < 0 || index.row() >= rowCount(index.parent()))
        return QVariant();

    if (index.column() < 0 || index.column() >= columnCount(index.parent()))
        return QVariant();

    if (role == Qt::DecorationRole)
    {
        switch (index.column())
        {
        case 1:
            return Qtitan::RoundIcon(QPixmap(m_values[index.row()].url), 4);
            break;
        }
    }
    else if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
        switch (index.column())
        {
        case 0:
            return m_values[index.row()].id;
        case 1:
            return m_values[index.row()].name;
        case 2:
            return m_values[index.row()].v1;
        case 3:
            return m_values[index.row()].v2;
        case 4:
            return m_values[index.row()].v3;
        }
    }
    else if (role == Qt::CheckStateRole)
    {
        switch (index.column())
        {
        case 0:
            return m_values[index.row()].id;
        }
    }
    return QVariant();
}

bool CustomFilterModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    if (index.row() < 0 || index.row() >= rowCount(index.parent()))
        return false;

    if (index.column() < 0 || index.column() >= columnCount(index.parent()))
        return false;

    if (role != Qt::EditRole)
        return false;

    switch (index.column())
    {
    case 0:
        m_values[index.row()].id = value.toInt();
       break;
        case 1:
        break;
    case 2:
        m_values[index.row()].v1 = value.toString();
        break;
    case 3:
        m_values[index.row()].v2 = value.toDate();
        break;
    case 4:
        m_values[index.row()].v3 = value.toTime();
        break;
    default:
        break;
    }

    emit dataChanged(index, index);
    return true;
}

Qt::ItemFlags CustomFilterModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemFlags();
    return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
}