CardViewNewCardFeature Example

#include <QtGui>
#include <QDesktopServices>
#include <QMessageBox>

#include "window.h"

static void add_model_item(QAbstractItemModel* model, const QPixmap& photo, const QString& name, int titleIndex,
    int countryIndex, const QString& city, const QString& code, const QString& geoLocation)
{
    int row = model->rowCount();
    model->insertRow(row);

    model->setData(model->index(row, 0), photo);
    model->setData(model->index(row, 1), name);
    model->setData(model->index(row, 2), titleIndex);
    model->setData(model->index(row, 3), countryIndex);
    model->setData(model->index(row, 4), city);
    model->setData(model->index(row, 5), code);
    QString link = QStringLiteral("<a href='http://www.geonames.org/maps/google_%1.html'>%2</a>").arg(geoLocation).arg(geoLocation);
    model->setData(model->index(row, 6), link);

    const QString previewText = QObject::tr("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sodales, \
                                libero at hendrerit commodo, urna velit vestibulum purus, cursus dignissim tellus ante non leo. \
                                Donec sed neque sit amet mauris iaculis varius ac eu ipsum. <b>Proin vel hendrerit magna</b>. \
                                Curabitur posuere ligula eu turpis porttitor faucibus rhoncus neque pretium. Duis quam lorem, bibendum id luctus eu, consequat at purus. \
                                Aenean ut urna felis.");
    //model->setData(model->index(0, 0), previewText, Qt::WhatsThisRole);
}

static QAbstractItemModel* create_model(QObject *parent)
{
    QStandardItemModel *model = new QStandardItemModel(0, 7, parent);

    model->setHeaderData(0, Qt::Horizontal, QObject::tr("Photo"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Title"));
    model->setHeaderData(3, Qt::Horizontal, QObject::tr("Country"));
    model->setHeaderData(4, Qt::Horizontal, QObject::tr("City"));
    model->setHeaderData(5, Qt::Horizontal, QObject::tr("Postal Code"));
    model->setHeaderData(6, Qt::Horizontal, QObject::tr("Location"));

    add_model_item(model, QPixmap(QStringLiteral(":res/photo2.png")),  QObject::tr("Dietrich Wolf"), 0/*Founder*/, 0/*Germany*/, QObject::tr("Berlin"), QStringLiteral("10245"), QStringLiteral("52.517_13.4"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo1.png")),  QObject::tr("Albert Schneider"), 1/*President*/, 0/*Germany*/, QObject::tr("Berlin"), QStringLiteral("10178"), QStringLiteral("52.523_13.414"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo3.png")),  QObject::tr("Adelina Adams"), 3/*Sales Manager*/, 0/*Germany*/, QObject::tr("Berlin"), QStringLiteral("10707"), QStringLiteral("52.517_13.4"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo4.png")),  QObject::tr("Bertrand Maison"), 4/*Software Engineer*/, 1/*France*/, QObject::tr("Paris"), QStringLiteral("75003"), QStringLiteral("48.864_2.361"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo5.png")),  QObject::tr("Anna Laroche"), 3/*Sales Manager*/, 1/*France*/, QObject::tr("Saint-Bernard"), QStringLiteral("68720"), QStringLiteral("47.667_7.2"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo6.png")),  QObject::tr("Dominik Malet"), 4/*Software Engineer*/, 1/*France*/, QObject::tr("Pulversheim"), QStringLiteral("68840"), QStringLiteral("47.838_7.301"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo7.png")),  QObject::tr("Berta Padovano"), 2/*Vice President*/, 2/*Italy*/, QObject::tr("Milano"), QStringLiteral("20121"), QStringLiteral("45.464_9.19"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo8.png")),  QObject::tr("Andrew Armstrong"), 5/*Software Developer*/, 2/*Italy*/, QObject::tr("Modena"), QStringLiteral("41100"), QStringLiteral("44.648_10.925"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo9.png")),  QObject::tr("Caterina Venezia"), 6/*Head of Sales*/, 2/*Italy*/, QObject::tr("Roma"), QStringLiteral("00136"), QStringLiteral("41.892_12.511"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo10.png")), QObject::tr("Sophia Smith"), 7/*Technical Support*/, 3/*United States*/, QObject::tr("Boston"), QStringLiteral("02115"), QStringLiteral("42.343_-71.092"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo11.png")), QObject::tr("Jacob Morris"), 3/*Software Engineer*/, 3/*United States*/, QObject::tr("New York"), QStringLiteral("10001"), QStringLiteral("40.748_-73.997"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo12.png")), QObject::tr("Andrew Fisher"), 5/*Software Developer*/, 3/*United States*/, QObject::tr("Miami Beach"), QStringLiteral("33140"), QStringLiteral("25.82_-80.134"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo13.png")), QObject::tr("Isabella Henderson"), 7/*Technical Support*/, 4/*United Kingdom*/, QObject::tr("Manchester"), QStringLiteral("M60"), QStringLiteral("53.481_-2.237"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo14.png")), QObject::tr("Olivia Hewitt"), 7/*Technical Support*/, 4/*United Kingdom*/, QObject::tr("Richmond"), QStringLiteral("TW9"), QStringLiteral("51.46_-0.301"));
    add_model_item(model, QPixmap(QStringLiteral(":res/photo15.png")), QObject::tr("Daniel Fuller"), 8/*Administration Manager*/, 4/*United Kingdom*/, QObject::tr("Notting Hill"), QStringLiteral("W11"), QStringLiteral("51.511_-0.206"));

    return model;
}

Window::Window()
: DemoMainWindow(QStringLiteral("QtitanDataGrid"), QStringLiteral(QTN_VERSION_DATAGRID_STR), tr("Add new row for CardView"))
{
    Grid::loadTranslation();

    m_grid = new Qtitan::CardGrid();
    QAbstractItemModel* model = create_model(m_grid);

    // Configure grid view
    m_grid->setViewType(Qtitan::CardGrid::CardView);
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    view->beginUpdate();

    view->options().setNewRowPlace(Qtitan::AtBeginning);
    view->options().setModelItemsDragEnabled(true);

    //Connect Grid's context menu handler.
    connect(view, SIGNAL(contextMenu(ContextMenuEventArgs*)), this, SLOT(contextMenu(ContextMenuEventArgs* )));
    connect(view, SIGNAL(editorLinkActivated(EditorLinkEventArgs*)), this, SLOT(editorLinkActivated(EditorLinkEventArgs*)));

    view->setModel(model);

    GridCardColumn* column = static_cast<GridCardColumn *>(view->getColumnByModelColumnName(tr("Photo")));
    column->setEditorType(GridEditor::Picture);
    column->setRowSpan(4);
    QVariant defValue = GridPictureEditorRepository::convertToVariant(QVariant::ByteArray, QPixmap(QStringLiteral(":res/new_user.png")));
    column->editorRepository()->setDefaultValue(defValue, Qt::DisplayRole);
    column->editorRepository()->setDefaultValue(defValue, Qt::EditRole);

    column = static_cast<GridCardColumn *>(view->getColumnByModelColumnName(tr("Title")));
    column->setEditorType(GridEditor::ComboBox);
    QStringListModel* titleModel = new QStringListModel(column);
    QStringList options;
    options << tr("Founder") << tr("President") << tr("Vice President") << tr("Sales Manager") << tr("Software Engineer")
            << tr("Software Developer") << tr("Head of Sales") << tr("Technical Support") << tr("Administration Manager");
    titleModel->setStringList(options);
    column->dataBinding()->setRelationModel(titleModel);

    column = static_cast<GridCardColumn *>(view->getColumnByModelColumnName(tr("Country")));
    column->setEditorType(GridEditor::ComboBox);
    QStringListModel* countryModel = new QStringListModel(column);
    options.clear();
    options << tr("Germany") << tr("France") << tr("Italy") << tr("United States") << tr("United Kingdom") << tr("Russia") << tr("China") << tr("Canada");
    countryModel->setStringList(options);
    column->dataBinding()->setRelationModel(countryModel);

    column = static_cast<GridCardColumn *>(view->getColumnByModelColumnName(tr("Location")));
    column->setEditorType(GridEditor::Memo);
    static_cast<GridMemoEditorRepository *>(column->editorRepository())->setHTML(true);

    //Show button menu for all column headers.
    for (int i = 0; i < view->getColumnCount(); ++i)
        static_cast<GridTableColumn *>(view->getColumn(i))->setMenuButtonVisible(true);

    setDemoWidget(m_grid, createSettingsWidget());
    view->endUpdate();

    view->bestFit();
#if 0
    addPreviewRow();
#endif
}

QWidget* Window::createSettingsWidget()
{
    //Create settings widget
    QWidget* settings = new QWidget(this);
    QVBoxLayout* l = new QVBoxLayout(settings);

    l->addLayout(createStyleSetting());

    QLabel* placeLabel = new QLabel(settings);
    placeLabel->setText(tr("New Card pane place:"));
    QComboBox* newRowPosition = new QComboBox(settings);
    newRowPosition->addItem(tr("Top"));
    newRowPosition->addItem(tr("Bottom"));
    newRowPosition->addItem(tr("Hide"));
    connect(newRowPosition, SIGNAL(activated(int)), this, SLOT(newRowPositionActivated(int)));
    placeLabel->setBuddy(newRowPosition);
    QHBoxLayout* hl = new QHBoxLayout(0);
    hl->addWidget(placeLabel);
    hl->addWidget(newRowPosition);
    l->addLayout(hl);

    QLabel* effectLabel = new QLabel(settings);
    effectLabel->setText(tr("Highlight effect:"));
    QComboBox* newRowEffect = new QComboBox(settings);
    newRowEffect->addItem(tr("Flash"));
    newRowEffect->addItem(tr("Alpha"));
    connect(newRowEffect, SIGNAL(activated(int)), this, SLOT(newRowEffectActivated(int)));
    effectLabel->setBuddy(newRowEffect);
    hl = new QHBoxLayout(0);
    hl->addWidget(effectLabel);
    hl->addWidget(newRowEffect);
    l->addLayout(hl);

    QCheckBox* fastScrollCheck = new QCheckBox(settings);
    fastScrollCheck->setText(tr("Fast scroll effect"));
    connect(fastScrollCheck, SIGNAL(stateChanged(int)), this, SLOT(fastScrollChanged(int)));
    l->addWidget(fastScrollCheck);
    fastScrollCheck->setChecked(true);

    QCheckBox* dottedLineCheck = new QCheckBox(settings);
    dottedLineCheck->setText(tr("Dotted grid line"));
    connect(dottedLineCheck, SIGNAL(stateChanged(int)), this, SLOT(dottedLineChanged(int)));
    l->addWidget(dottedLineCheck);
    dottedLineCheck->setChecked(false);

    QLabel* label = new QLabel(this);
    hl = new QHBoxLayout(0);
    label->setText(tr("Grid line style:"));
    QComboBox* lineStylesSelect = new QComboBox(settings);

    lineStylesSelect->addItem(tr("None"));
    lineStylesSelect->addItem(tr("Both"));
    lineStylesSelect->addItem(tr("Both2D"));
    lineStylesSelect->addItem(tr("Horizontal"));
    lineStylesSelect->addItem(tr("Horizontal2D"));
    lineStylesSelect->addItem(tr("Vertical"));
    lineStylesSelect->addItem(tr("Vertical2D"));
    connect(lineStylesSelect, SIGNAL(currentIndexChanged(int)), this, SLOT(selectGridLineStyles(int)));
    hl->addWidget(label);
    hl->addWidget(lineStylesSelect);
    l->addLayout(hl);
    lineStylesSelect->setCurrentIndex(3);

    QCheckBox* zoomEnable = new QCheckBox(settings);
    zoomEnable->setText(tr("Zoom enabled"));
    zoomEnable->setChecked(true);
    connect(zoomEnable, SIGNAL(stateChanged(int)), this, SLOT(zoomEnabledChanged(int)));
    l->addWidget(zoomEnable);

    QCheckBox* zoomIndicator = new QCheckBox(settings);
    zoomIndicator->setText(tr("Show zoom indicator"));
    zoomIndicator->setChecked(true);
    connect(zoomIndicator, SIGNAL(stateChanged(int)), this, SLOT(zoomIndicatorChanged(int)));
    l->addWidget(zoomIndicator);

    QSlider* zoomSlider = new QSlider(settings);
    zoomSlider->setOrientation(Qt::Horizontal);
    zoomSlider->setTickPosition(QSlider::TicksBothSides);
    zoomSlider->setMinimum(25);
    zoomSlider->setMaximum(300);
    zoomSlider->setTickInterval(25);
    zoomSlider->setSingleStep(25);
    zoomSlider->setValue(100);
    connect(zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(zoomValueChanged(int)));
    connect(m_grid->view<Qtitan::GridCardView>(), SIGNAL(zoomChanged(int)), zoomSlider, SLOT(setValue(int)));
    l->addWidget(zoomSlider);

    QCheckBox* cellAutoRaise = new QCheckBox(settings);
    cellAutoRaise->setText(tr("Auto raise cell button"));
    connect(cellAutoRaise, SIGNAL(stateChanged(int)), this, SLOT(cellButtonAutoRaiseEnabled(int)));
    cellAutoRaise->setChecked(true);
    l->addWidget(cellAutoRaise);
#if 0
    QCheckBox* previewExpandButtonBox = new QCheckBox(settings);
    previewExpandButtonBox->setText(tr("Preview Expand Button (new)"));
    connect(previewExpandButtonBox, SIGNAL(stateChanged(int)), this, SLOT(previewExpandButtonEnabled(int)));
    previewExpandButtonBox->setChecked(true);
    l->addWidget(previewExpandButtonBox);
#endif
    QCheckBox* transparentBox = new QCheckBox(settings);
    transparentBox->setText(tr("Transparent Background"));
    connect(transparentBox, SIGNAL(stateChanged(int)), this, SLOT(transparentBackgroundEnabled(int)));
    transparentBox->setChecked(false);
    l->addWidget(transparentBox);

    QPushButton* printButton = new QPushButton(settings);
    printButton->setText(tr("Print Preview"));
    connect(printButton, SIGNAL(clicked()), this, SLOT(printPreview()));
    l->addWidget(printButton);
    return settings;
}

void Window::newRowPositionActivated(int index)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();

    switch (index)
    {
    case 0:
        view->options().setNewRowPlace(Qtitan::AtBeginning);
        break;
    case 1:
        view->options().setNewRowPlace(Qtitan::AtEnd);
        break;
    default:
        view->options().setNewRowPlace(Qtitan::AtNone);
    }
}

void Window::newRowEffectActivated(int index)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();

    switch (index)
    {
    case 0:
        view->options().setNewRowHighlightEffect(Qtitan::FlashEffect);
        break;
    case 1:
        view->options().setNewRowHighlightEffect(Qtitan::AlphaEffect);
        break;
    default:
        view->options().setNewRowHighlightEffect(Qtitan::AlphaEffect);
    }
}

void Window::fastScrollChanged(int state)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    view->options().setFastScrollEffect(state == Qt::Checked);
}

void Window::dottedLineChanged(int state)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    QPen pen = view->options().gridLinePen();
    pen.setStyle(state == Qt::Checked ? Qt::DotLine : Qt::SolidLine);
    view->options().setGridLinePen(pen);
}

void Window::selectGridLineStyles(int index)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    switch (index)
    {
    case 0:
        view->options().setGridLines(Qtitan::LinesNone);
        break;
    case 1:
        view->options().setGridLines(Qtitan::LinesBoth);
        break;
    case 2:
        view->options().setGridLines(Qtitan::LinesBoth2D);
        break;
    case 3:
        view->options().setGridLines(Qtitan::LinesHorizontal);
        break;
    case 4:
        view->options().setGridLines(Qtitan::LinesHorizontal2D);
        break;
    case 5:
        view->options().setGridLines(Qtitan::LinesVertical);
        break;
    case 6:
        view->options().setGridLines(Qtitan::LinesVertical2D);
        break;
    default:
        view->options().setGridLines(Qtitan::LinesBoth);
    }
 }

void Window::zoomEnabledChanged(int state)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    view->options().setZoomEnabled(state == Qt::Checked);
}

void Window::zoomIndicatorChanged(int state)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    view->options().setZoomIndicatorActive(state == Qt::Checked);
}

void Window::zoomValueChanged(int value)
{
    double factor = qCeil((double)value / 25) * 25;
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    view->options().setZoomFactor(factor / 100);
}

void Window::cellButtonAutoRaiseEnabled(int state)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    view->options().setCellButtonAutoRaise(state == Qt::Checked);
}

void Window::transparentBackgroundEnabled(int state)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    view->options().setTransparentBackground(state == Qt::Checked);

    view->options().setAlternatingRowColors(!view->options().alternatingRowColors());
}

void Window::previewExpandButtonEnabled(int state)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    view->options().setPreviewExpandButtonVisible(state == Qt::Checked);
}

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::editorLinkActivated(EditorLinkEventArgs* args)
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    if (!view->isEditing())
        QDesktopServices::openUrl(QUrl(args->anchor()));
}

void Window::cellButtonClicked(CellButtonClickEventArgs* args)
{
    QMessageBox::information(this, tr("Cell button clicked"),
        tr("Clicked: Button - %1, Column Title - %2, RowIndex - %3").arg(args->buttonIndex()).arg(args->column()->caption()).arg(args->row().rowIndex()));
}

void Window::printPreview()
{
    m_grid->view<Qtitan::GridCardView>()->printPreview();
}

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

void Window::addPreviewRow()
{
    Qtitan::GridCardView* view = m_grid->view<Qtitan::GridCardView>();
    view->options().setPreviewRowEnabled(true);
    view->modelController()->previewRowDataBinding()->setDisplayRole(Qt::WhatsThisRole);
    view->options().setPreviewRowHeight(100);
}