0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-23 16:22:58 +03:00

Add: added a function to get an arbitrary role of a model item

Example:
$D{appdata.Column_1}
$S{
var vRow = line('DataBand1') - 1;
// 8 - Qt::BackgroundRole
var vColor = getFieldByRowIndexEx('appdata.Column_1', vRow, 8);

THIS.backgroundColor = LimeReport.color('lightgray');
if(vColor > '')
{
	THIS.backgroundColor = vColor;
}
''
}

Added several functions to get extended information from the model

- getFieldByRowIndexEx2(fieldName, rowIndex, roleName), default:
Qt::DisplayRole

- getHeaderData(fieldName, roleName), default: Qt::DisplayRole

- getHeaderColumnNameByIndex(datasourceName, columnIndex), default:
Qt::UserRole or Qt::DisplayRole

- getColumnCount(datasourceName), default: -1
This commit is contained in:
Dmitry Zagorodnev 2024-03-26 11:43:58 +03:00
parent 2b388b02fe
commit 4c1c0c49e6
7 changed files with 261 additions and 0 deletions

View File

@ -272,6 +272,24 @@ QVariant ModelToDataSource::dataByRowIndex(const QString &columnName, int rowInd
return QVariant();
}
QVariant ModelToDataSource::dataByRowIndex(const QString &columnName, int rowIndex, int roleName)
{
if(m_model->rowCount() > rowIndex)
return m_model->data(m_model->index(rowIndex, columnIndexByName(columnName)));
return QVariant();
}
QVariant ModelToDataSource::dataByRowIndex(const QString &columnName, int rowIndex, const QString &roleName)
{
if(m_model->rowCount() > rowIndex) {
int roleCode{roleName.isEmpty() ? Qt::DisplayRole
: m_model->roleNames().key(roleName.toUtf8(), Qt::DisplayRole)};
return m_model->data(m_model->index(rowIndex, columnIndexByName(columnName)), roleCode);
}
return QVariant();
}
QVariant ModelToDataSource::dataByKeyField(const QString& columnName, const QString& keyColumnName, QVariant keyData)
{
for( int i=0; i < m_model->rowCount(); ++i ){
@ -310,6 +328,13 @@ int ModelToDataSource::columnIndexByName(QString name)
return -1;
}
QVariant ModelToDataSource::headerData(const QString &columnName, const QString &roleName)
{
int roleCode{roleName.isEmpty() ? Qt::DisplayRole
: m_model->roleNames().key(roleName.toUtf8(), Qt::DisplayRole)};
return m_model->headerData(columnIndexByName(columnName), Qt::Horizontal, roleCode);
}
QString ModelToDataSource::lastError()
{
return m_lastError;
@ -722,6 +747,18 @@ QVariant CallbackDatasource::dataByRowIndex(const QString &columnName, int rowIn
return result;
}
QVariant CallbackDatasource::dataByRowIndex(const QString &columnName, int rowIndex, int roleName)
{
Q_UNUSED(roleName)
return dataByRowIndex(columnName, rowIndex);
}
QVariant CallbackDatasource::dataByRowIndex(const QString &columnName, int rowIndex, const QString &roleName)
{
Q_UNUSED(roleName)
return dataByRowIndex(columnName, rowIndex);
}
QVariant CallbackDatasource::dataByKeyField(const QString& columnName, const QString& keyColumnName, QVariant keyData)
{
int backupCurrentRow = m_currentRow;
@ -816,6 +853,12 @@ int CallbackDatasource::columnIndexByName(QString name)
return -1;
}
QVariant CallbackDatasource::headerData(const QString &columnName, const QString &roleName)
{
Q_UNUSED(roleName)
return columnName; // STUB
}
bool CallbackDatasource::checkNextRecord(int recordNum){
if (bof()) checkIfEmpty();
if (m_rowCount > 0) {

View File

@ -388,10 +388,13 @@ public:
bool bof();
QVariant data(const QString& columnName);
QVariant dataByRowIndex(const QString &columnName, int rowIndex);
QVariant dataByRowIndex(const QString &columnName, int rowIndex, int roleName);
QVariant dataByRowIndex(const QString &columnName, int rowIndex, const QString &roleName);
QVariant dataByKeyField(const QString& columnName, const QString& keyColumnName, QVariant keyData);
int columnCount();
QString columnNameByIndex(int columnIndex);
int columnIndexByName(QString name);
QVariant headerData(const QString &columnName, const QString &roleName);
QString lastError();
virtual QAbstractItemModel* model();
int currentRow();
@ -421,6 +424,8 @@ public:
bool eof(){return m_eof;}
QVariant data(const QString &columnName);
QVariant dataByRowIndex(const QString& columnName, int rowIndex);
QVariant dataByRowIndex(const QString &columnName, int rowIndex, int roleName);
QVariant dataByRowIndex(const QString &columnName, int rowIndex, const QString &roleName);
QVariant dataByKeyField(const QString& columnName, const QString& keyColumnName, QVariant keyData);
int columnCount();
QString columnNameByIndex(int columnIndex);
@ -428,6 +433,7 @@ public:
bool isInvalid() const{ return false;}
QString lastError(){ return "";}
QAbstractItemModel *model(){return 0;}
QVariant headerData(const QString &columnName, const QString &roleName);
private:
bool checkNextRecord(int recordNum);
bool checkIfEmpty();
@ -441,6 +447,7 @@ private:
QHash<QString, QVariant> m_valuesCache;
bool m_getDataFromCache;
int m_lastKeyRow;
};
class CallbackDatasourceHolder :public QObject, public IDataSourceHolder{

View File

@ -18,9 +18,12 @@ public:
virtual bool eof() = 0;
virtual QVariant data(const QString& columnName) = 0;
virtual QVariant dataByRowIndex(const QString& columnName, int rowIndex) = 0;
virtual QVariant dataByRowIndex(const QString &columnName, int rowIndex, int roleName) = 0;
virtual QVariant dataByRowIndex(const QString &columnName, int rowIndex, const QString &roleName) = 0;
virtual QVariant dataByKeyField(const QString& columnName, const QString& keyColumnName, QVariant keyData) = 0;
virtual int columnCount() = 0;
virtual QString columnNameByIndex(int columnIndex) = 0;
virtual QVariant headerData(const QString &columnName, const QString &roleName) = 0;
virtual int columnIndexByName(QString name) = 0;
virtual bool isInvalid() const = 0;
virtual QString lastError() = 0;

View File

@ -1689,6 +1689,28 @@ QVariant DataSourceManager::fieldDataByRowIndex(const QString &fieldName, int ro
return QVariant();
}
QVariant DataSourceManager::fieldDataByRowIndex(const QString &fieldName, int rowIndex, int role)
{
if(containsField(fieldName)) {
IDataSource *ds = dataSource(extractDataSource(fieldName));
if(ds) {
return ds->dataByRowIndex(extractFieldName(fieldName), rowIndex, role);
}
}
return QVariant();
}
QVariant DataSourceManager::fieldDataByRowIndex(const QString &fieldName, int rowIndex, const QString &roleName)
{
if(containsField(fieldName)) {
IDataSource *ds = dataSource(extractDataSource(fieldName));
if(ds) {
return ds->dataByRowIndex(extractFieldName(fieldName), rowIndex, roleName);
}
}
return QVariant();
}
QVariant DataSourceManager::fieldDataByKey(const QString& datasourceName, const QString& valueFieldName, const QString& keyFieldName, QVariant keyValue)
{
IDataSource* ds = dataSource(datasourceName);
@ -1698,6 +1720,36 @@ QVariant DataSourceManager::fieldDataByKey(const QString& datasourceName, const
return QVariant();
}
QVariant DataSourceManager::headerData(const QString &fieldName, const QString &roleName)
{
if(containsField(fieldName)) {
IDataSource *ds = dataSource(extractDataSource(fieldName));
if(ds) {
return ds->headerData(extractFieldName(fieldName), roleName);
}
}
return QVariant();
}
QString DataSourceManager::columnName(const QString &datasourceName, int index)
{
IDataSource *ds = dataSource(datasourceName);
if(ds && !ds->isInvalid() && ds->columnCount() > index) {
return ds->columnNameByIndex(index);
}
return QString("unknown");
}
int DataSourceManager::columnCount(const QString &datasourceName)
{
IDataSource *ds = dataSource(datasourceName);
if(ds && !ds->isInvalid()) {
return ds->columnCount();
}
return -1;
}
void DataSourceManager::reopenDatasource(const QString& datasourceName)
{
QueryHolder* qh = dynamic_cast<QueryHolder*>(dataSourceHolder(datasourceName));

View File

@ -173,12 +173,17 @@ public:
bool containsField(const QString& fieldName);
QVariant fieldData(const QString& fieldName);
QVariant fieldDataByRowIndex(const QString& fieldName, int rowIndex);
QVariant fieldDataByRowIndex(const QString &fieldName, int rowIndex, int role);
QVariant fieldDataByRowIndex(const QString &fieldName, int rowIndex, const QString &roleName);
QVariant fieldDataByKey(
const QString& datasourceName,
const QString& valueFieldName,
const QString& keyFieldName,
QVariant keyValue
);
QVariant headerData(const QString &fieldName, const QString &roleName);
QString columnName(const QString &datasourceName, int index);
int columnCount(const QString &datasourceName);
void reopenDatasource(const QString& datasourceName);
QString extractDataSource(const QString& fieldName);

View File

@ -1073,6 +1073,79 @@ bool ScriptEngineManager::createReopenDatasourceFunction()
return addFunction(fd);
}
bool ScriptEngineManager::createGetFieldByRowIndexEx()
{
JSFunctionDesc fd;
fd.setManager(m_functionManager);
fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME);
fd.setCategory(tr("GENERAL"));
fd.setName("getFieldByRowIndexEx");
fd.setDescription("getFieldByRowIndexEx(\"" + tr("FieldName") + "\", \"" + tr("RowIndex")
+ "\", \"" + tr("RoleIndex") + "\")");
fd.setScriptWrapper(QString("function getFieldByRowIndexEx(fieldName, rowIndex, role){"
"return %1.getFieldByRowIndexEx(fieldName, rowIndex, role);}")
.arg(LimeReport::Const::FUNCTION_MANAGER_NAME));
return addFunction(fd);
}
bool ScriptEngineManager::createGetFieldByRowIndexEx2()
{
JSFunctionDesc fd;
fd.setManager(m_functionManager);
fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME);
fd.setCategory(tr("GENERAL"));
fd.setName("getFieldByRowIndexEx2");
fd.setDescription("getFieldByRowIndexEx2(\"" + tr("FieldName") + "\", \"" + tr("RowIndex")
+ "\", \"" + tr("RoleName") + "\")");
fd.setScriptWrapper(QString("function getFieldByRowIndexEx2(fieldName, rowIndex, role){"
"return %1.getFieldByRowIndexEx2(fieldName, rowIndex, role);}")
.arg(LimeReport::Const::FUNCTION_MANAGER_NAME));
return addFunction(fd);
}
bool ScriptEngineManager::createHeaderData()
{
JSFunctionDesc fd;
fd.setManager(m_functionManager);
fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME);
fd.setCategory(tr("GENERAL"));
fd.setName("getHeaderData");
fd.setDescription("getHeaderData(\"" + tr("FieldName") + "\", \"" + tr("RoleName") + "\")");
fd.setScriptWrapper(QString("function getHeaderData(fieldName, role){"
"return %1.getHeaderData(fieldName, role);}")
.arg(LimeReport::Const::FUNCTION_MANAGER_NAME));
return addFunction(fd);
}
bool ScriptEngineManager::createHeaderColumnNameByIndex()
{
JSFunctionDesc fd;
fd.setManager(m_functionManager);
fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME);
fd.setCategory(tr("GENERAL"));
fd.setName("getHeaderColumnNameByIndex");
fd.setDescription("getHeaderColumnNameByIndex(\"" + tr("datasourceName") + "\", \""
+ tr("columnIndex") + "\")");
fd.setScriptWrapper(QString("function getHeaderColumnNameByIndex(datasourceName, columnIndex){"
"return %1.getHeaderColumnNameByIndex(datasourceName, columnIndex);}")
.arg(LimeReport::Const::FUNCTION_MANAGER_NAME));
return addFunction(fd);
}
bool ScriptEngineManager::createColumnCount()
{
JSFunctionDesc fd;
fd.setManager(m_functionManager);
fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME);
fd.setCategory(tr("GENERAL"));
fd.setName("getColumnCount");
fd.setDescription("getColumnCount(\"" + tr("datasourceName") + "\")");
fd.setScriptWrapper(QString("function getColumnCount(datasourceName){"
"return %1.getColumnCount(datasourceName);}")
.arg(LimeReport::Const::FUNCTION_MANAGER_NAME));
return addFunction(fd);
}
ScriptEngineManager::ScriptEngineManager()
:m_model(0), m_context(0), m_dataManager(0)
{
@ -1114,6 +1187,11 @@ ScriptEngineManager::ScriptEngineManager()
createAddTableOfContentsItemFunction();
createClearTableOfContentsFunction();
createReopenDatasourceFunction();
createGetFieldByRowIndexEx();
createGetFieldByRowIndexEx2();
createHeaderData();
createHeaderColumnNameByIndex();
createColumnCount();
m_model = new ScriptEngineModel(this);
}
@ -1866,6 +1944,36 @@ QFont ScriptFunctionsManager::font(const QString &family, int pointSize, bool it
return result;
}
QVariant ScriptFunctionsManager::getFieldByRowIndexEx(const QString &fieldName, int rowIndex, const int role)
{
DataSourceManager *dm = scriptEngineManager()->dataManager();
return dm->fieldDataByRowIndex(fieldName, rowIndex, role);
}
QVariant ScriptFunctionsManager::getFieldByRowIndexEx2(const QString &fieldName, int rowIndex, const QString &roleName)
{
DataSourceManager *dm = scriptEngineManager()->dataManager();
return dm->fieldDataByRowIndex(fieldName, rowIndex, roleName);
}
QVariant ScriptFunctionsManager::getHeaderData(const QString &fieldName, const QString &roleName)
{
DataSourceManager *dm = scriptEngineManager()->dataManager();
return dm->headerData(fieldName, roleName);
}
QVariant ScriptFunctionsManager::getHeaderColumnNameByIndex(const QString &datasourceName, const int columnIndex)
{
DataSourceManager *dm = scriptEngineManager()->dataManager();
return dm->columnName(datasourceName, columnIndex);
}
int ScriptFunctionsManager::getColumnCount(const QString &datasourceName)
{
DataSourceManager *dm = scriptEngineManager()->dataManager();
return dm->columnCount(datasourceName);
}
#ifdef USE_QJSENGINE
void ScriptFunctionsManager::addItemsToComboBox(QJSValue object, const QStringList &values)

View File

@ -378,6 +378,43 @@ public:
Q_INVOKABLE void addTableOfContentsItem(const QString& uniqKey, const QString& content, int indent = 0);
Q_INVOKABLE void clearTableOfContents();
Q_INVOKABLE QFont font(const QString& family, int pointSize = -1, bool bold = false, bool italic = false, bool underLine = false);
/*!
* \brief getFieldByRowIndexEx Выдает для поля значение заданной роли
* \param fieldName имя источника данных + имя поля
* \param rowIndex индекс строки
* \param role код роли
* \return
*/
Q_INVOKABLE QVariant getFieldByRowIndexEx(const QString &fieldName, int rowIndex, const int role);
/*!
* \brief getFieldByRowIndexEx2 Выдает для поля значение заданной роли
* \param fieldName имя источника данных + имя поля
* \param rowIndex индекс строки
* \param roleName имя роли из roleNames()
* \return
*/
Q_INVOKABLE QVariant getFieldByRowIndexEx2(const QString &fieldName, int rowIndex, const QString &roleName);
/*!
* \brief getHeaderData Выдает для поля заголовка значение заданной роли
* \param fieldName имя источника данных + имя поля
* \param role имя роли из roleNames()
* \return
*/
Q_INVOKABLE QVariant getHeaderData(const QString &fieldName, const QString &roleName);
/*!
* \brief getHeaderColumnNameByIndex Выдает имя колонки по ее индексу (имя используемое LR)
* \param datasourceName имя источника данных
* \param columnIndex индекс колонки
* \return
*/
Q_INVOKABLE QVariant getHeaderColumnNameByIndex(const QString &datasourceName, const int columnIndex);
/*!
* \brief getColumnCount Выдает число столбцов в источнике данных
* \param datasourceName имя источника данных
* \return возможно -1 при ошибке
*/
Q_INVOKABLE int getColumnCount(const QString &datasourceName);
#ifdef USE_QJSENGINE
Q_INVOKABLE void addItemsToComboBox(QJSValue object, const QStringList& values);
Q_INVOKABLE void addItemToComboBox(QJSValue object, const QString& value);
@ -506,6 +543,12 @@ private:
bool createAddTableOfContentsItemFunction();
bool createClearTableOfContentsFunction();
bool createReopenDatasourceFunction();
bool createGetFieldByRowIndexEx();
bool createGetFieldByRowIndexEx2();
bool createHeaderData();
bool createHeaderColumnNameByIndex();
bool createColumnCount();
private:
ScriptEngineManager();
ScriptEngineType* m_scriptEngine;