MultiSelection Example
#include <QtGui>
#include <QtSql>
#include <QMessageBox>
#include "window.h"
inline QSqlTableModel* createSQLModel(QSqlDatabase& dataBase, const QString& tableName, QObject* parent)
{
if (tableName.isEmpty() || !dataBase.isValid())
return Q_NULL;
if (!dataBase.isOpen() && !dataBase.open())
return Q_NULL;
QSqlTableModel* sqlModel = new QSqlTableModel(parent, dataBase);
sqlModel->setTable(tableName);
sqlModel->select();
if (sqlModel->lastError().type() != QSqlError::NoError)
Q_DELETE_AND_NULL(sqlModel);
return sqlModel;
}
Window::Window()
: DemoMainWindow(QStringLiteral("QtitanDataGrid"), QStringLiteral(QTN_VERSION_DATAGRID_STR), tr("Multi-selection Example"))
{
Grid::loadTranslation();
m_grid = new Qtitan::Grid(this);
#ifndef Q_OS_ANDROID
QString path = QApplication::applicationDirPath();
path += QStringLiteral("/../examples/SQLFiles/assets");
#else
QString path = QDir::homePath();
copyAssetFile(path, "database.sqlite");
#endif
QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), QStringLiteral("database_demo"));
db.setDatabaseName(path + QStringLiteral("/database.sqlite"));
db.setHostName(QString());
db.setPort(-1);
if (!db.open(QString(), QString()))
{
QSqlError err = db.lastError();
QSqlDatabase::removeDatabase(QStringLiteral("database_demo"));
QMessageBox::critical(0, tr("SQL Error"), QStringLiteral("Error: Can't open database - %1, error - %2").arg(db.databaseName()).arg(err.text()));
QApplication::exit(1);
return;
}
QSqlTableModel* model = createSQLModel(db, QStringLiteral("data"), m_grid);
if (model == Q_NULL)
{
QMessageBox::critical(0, tr("SQL Error"), tr("Error: SQL data base is not valid."));
QApplication::exit(1);
return;
}
model->setEditStrategy(QSqlTableModel::OnFieldChange);
m_grid->setViewType(Qtitan::Grid::BandedTableView);
Qtitan::GridBandedTableView* view = m_grid->view<Qtitan::GridBandedTableView>();
view->beginUpdate();
view->options().setShowFocusDecoration(true);
view->tableOptions().setColumnAutoWidth(true);
view->options().setSupportedDropActions(Qt::CopyAction);
view->options().setModelItemsDragEnabled(true);
connect(view, SIGNAL(contextMenu(ContextMenuEventArgs*)), this, SLOT(contextMenu(ContextMenuEventArgs* )));
Qtitan::GridTableBand* characteristicsBand = view->addBand(tr("Characteristics"));
Qtitan::GridTableBand* engineeringBand = view->addBand(tr("Engineering"));
view->setModel(model);
Qtitan::GridBandedTableColumn* column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("id"));
column->setBandIndex(characteristicsBand->index());
column->setRowSpan(2);
column->addButton(GridColumn::ClearButtonIcon, Qtitan::AtBeginning, GridColumn::FluentFixedPolicy);
column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Airline"));
column->setBandIndex(characteristicsBand->index());
column->setRowSpan(2);
column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Registration"));
column->setBandIndex(characteristicsBand->index());
column->setRowSpan(2);
column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Aircraft"));
column->setBandIndex(characteristicsBand->index());
column->setRowSpan(2);
column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Location"));
column->setBandIndex(characteristicsBand->index());
column->setRowSpan(2);
column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Date"));
column->setBandIndex(characteristicsBand->index());
column->setRowSpan(2);
column->addButton(GridColumn::ClearButtonIcon, Qtitan::AtEnd, GridColumn::MouseOverPolicy);
connect(column, SIGNAL(buttonClicked(CellButtonClickEventArgs*)), this, SLOT(cellButtonClicked(CellButtonClickEventArgs*)));
column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Photo"));
column->setEditorType(GridEditor::ComboBoxPicture);
column->setBandIndex(engineeringBand->index());
column->setRowSpan(2);
column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("History"));
column->setEditorType(GridEditor::Memo);
column->setBandIndex(engineeringBand->index());
column->setRowSpan(2);
column = (Qtitan::GridBandedTableColumn *)view->getColumnByModelColumnName(tr("Info"));
column->setEditorType(GridEditor::Memo);
column->setBandIndex(engineeringBand->index());
column->setRowSpan(2);
for (int i = 0; i < view->getColumnCount(); ++i)
static_cast<GridTableColumn *>(view->getColumn(i))->setMenuButtonVisible(true);
view->endUpdate();
setDemoWidget(m_grid, createSettingsWidget());
}
QWidget* Window::createSettingsWidget()
{
QWidget* settings = new QWidget(this);
QVBoxLayout* l = new QVBoxLayout(settings);
l->addLayout(createStyleSetting());
QHBoxLayout* hl = new QHBoxLayout(0);
QLabel* label = new QLabel(this);
label->setText(tr("Multi-select mode:"));
QComboBox* policyBox = new QComboBox(this);
policyBox->addItem(tr("Single Row"));
policyBox->addItem(tr("Single Cell"));
policyBox->addItem(tr("Multi Rows"));
policyBox->addItem(tr("Multi Rows (Using RubberBand)"));
policyBox->addItem(tr("Multi Cells"));
policyBox->addItem(tr("Multi Cells (Using RubberBand)"));
connect(policyBox, SIGNAL(currentIndexChanged(int)), this, SLOT(newPolicyActivated(int)));
hl->addWidget(label);
hl->addWidget(policyBox);
l->addLayout(hl);
QPushButton* showSelectedButton = new QPushButton(settings);
showSelectedButton->setText(tr("Show Selected"));
connect(showSelectedButton, SIGNAL(clicked()), this, SLOT(showSelected()));
l->addWidget(showSelectedButton);
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(true);
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(5);
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::GridTableView>(), 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);
QCheckBox* frozenRowsBox = new QCheckBox(settings);
frozenRowsBox->setText(tr("Frozen Rows"));
connect(frozenRowsBox, SIGNAL(stateChanged(int)), this, SLOT(frozenRowsEnabled(int)));
frozenRowsBox->setChecked(false);
l->addWidget(frozenRowsBox);
QCheckBox* rowsQuickSelectBox = new QCheckBox(settings);
rowsQuickSelectBox->setText(tr("Rows Quick Selection"));
connect(rowsQuickSelectBox, SIGNAL(stateChanged(int)), this, SLOT(rowsQuickSelectEnabled(int)));
rowsQuickSelectBox->setChecked(true);
l->addWidget(rowsQuickSelectBox);
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);
QCheckBox* rowSizingBox = new QCheckBox(settings);
rowSizingBox->setText(tr("Resizing row (new)"));
connect(rowSizingBox, SIGNAL(stateChanged(int)), this, SLOT(rowSizingEnabled(int)));
rowSizingBox->setChecked(true);
l->addWidget(rowSizingBox);
QPushButton* printButton = new QPushButton(settings);
printButton->setText(tr("Print Preview"));
connect(printButton, SIGNAL(clicked()), this, SLOT(printPreview()));
l->addWidget(printButton);
policyBox->setCurrentIndex(3);
return settings;
}
void Window::newPolicyActivated(int index)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
switch (index)
{
case 0:
view->options().setSelectionPolicy(GridViewOptions::RowSelection);
view->options().setRubberBandSelection(true);
view->tableOptions().setRowsQuickSelection(true);
break;
case 1:
view->options().setSelectionPolicy(GridViewOptions::CellSelection);
view->options().setRubberBandSelection(false);
view->tableOptions().setRowsQuickSelection(true);
break;
case 2:
view->options().setSelectionPolicy(GridViewOptions::MultiRowSelection);
view->options().setRubberBandSelection(false);
view->tableOptions().setRowsQuickSelection(true);
break;
case 3:
view->options().setSelectionPolicy(GridViewOptions::MultiRowSelection);
view->options().setRubberBandSelection(true);
view->tableOptions().setRowsQuickSelection(true);
break;
case 4:
view->options().setSelectionPolicy(GridViewOptions::MultiCellSelection);
view->options().setRubberBandSelection(false);
view->tableOptions().setRowsQuickSelection(true);
break;
case 5:
view->options().setSelectionPolicy(GridViewOptions::MultiCellSelection);
view->options().setRubberBandSelection(true);
view->tableOptions().setRowsQuickSelection(true);
break;
default:
view->options().setSelectionPolicy(GridViewOptions::RowSelection);
view->options().setRubberBandSelection(false);
view->tableOptions().setRowsQuickSelection(false);
}
}
void Window::showSelected()
{
QString text;
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
GridSelection* selection = view->selection();
while (!selection->end())
{
GridRow row = selection->row();
if (row.type() == GridRow::GroupRow)
{
QVariant v;
v = row.groupValue();
text += QStringLiteral("[Group %1]\r\n").arg( v.toString());
selection->next();
}
else
{
QString cellstext;
GridRow current = row;
while (!selection->end() && current.rowIndex() == selection->row().rowIndex())
{
if (!cellstext.isEmpty())
cellstext += QStringLiteral(", ");
cellstext += QStringLiteral("%1").arg(selection->cell().columnIndex());
selection->next();
}
text += QStringLiteral("[Row %1] cells(%2)\r\n").arg( current.rowIndex()).arg(cellstext);
}
}
QTextEdit* edit = new QTextEdit();
edit->setText(text);
QDialog* dlg = new QDialog(this);
QVBoxLayout* vbl = new QVBoxLayout(dlg);
vbl->addWidget(edit);
dlg->exec();
}
void Window::fastScrollChanged(int state)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
view->options().setFastScrollEffect(state == Qt::Checked);
}
void Window::dottedLineChanged(int state)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
QPen pen = view->options().gridLinePen();
pen.setStyle(state == Qt::Checked ? Qt::DotLine : Qt::SolidLine);
view->options().setGridLinePen(pen);
}
void Window::selectGridLineStyles(int index)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
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::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
view->options().setZoomEnabled(state == Qt::Checked);
}
void Window::zoomIndicatorChanged(int state)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
view->options().setZoomIndicatorActive(state == Qt::Checked);
}
void Window::zoomValueChanged(int value)
{
double factor = qCeil((double)value / 25) * 25;
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
view->options().setZoomFactor(factor / 100);
}
void Window::cellButtonAutoRaiseEnabled(int state)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
view->options().setCellButtonAutoRaise(state == Qt::Checked);
}
void Window::frozenRowsEnabled(int state)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
view->tableOptions().setRowFrozenButtonVisible(state == Qt::Checked);
view->tableOptions().setFrozenPlaceQuickSelection(state == Qt::Checked);
}
void Window::transparentBackgroundEnabled(int state)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
view->options().setTransparentBackground(state == Qt::Checked);
view->options().setAlternatingRowColors(!view->options().alternatingRowColors());
}
void Window::rowSizingEnabled(int state)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
view->tableOptions().setRowSizingEnabled(state == Qt::Checked);
}
void Window::rowsQuickSelectEnabled(int state)
{
Qtitan::GridTableView* view = m_grid->view<Qtitan::GridTableView>();
view->tableOptions().setRowsQuickSelection(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::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::setShadeColor(const QColor& color)
{
m_grid->themeManager()->setShadeColor(color);
}
void Window::printPreview()
{
m_grid->view<Qtitan::GridTableView>()->printPreview();
}