Merge pull request #452 from DmitryZagorodnev/feature/dataRoles

Feature/data roles
This commit is contained in:
Alexander Arin 2024-03-30 14:16:02 +03:00 committed by GitHub
commit 5139ac0ab0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 286 additions and 27 deletions

View File

@ -77,10 +77,6 @@ TextItem::TextItem(QObject *owner, QGraphicsItem *parent)
TextItem::~TextItem(){}
int TextItem::fakeMarginSize() const{
return marginSize()+5;
}
void TextItem::preparePopUpMenu(QMenu &menu)
{
QAction* editAction = menu.addAction(QIcon(":/report/images/edit_pecil2.png"),tr("Edit"));
@ -180,7 +176,7 @@ void TextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* style, Q
qreal hOffset = 0, vOffset = 0;
switch (m_angle){
case Angle0:
hOffset = fakeMarginSize();
hOffset = marginSize();
if ((tmpSize.height() > 0) && (m_alignment & Qt::AlignVCenter)){
vOffset = tmpSize.height() / 2;
}
@ -189,8 +185,8 @@ void TextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* style, Q
painter->translate(hOffset,vOffset);
break;
case Angle90:
hOffset = width() - fakeMarginSize();
vOffset = fakeMarginSize();
hOffset = width() - marginSize();
vOffset = marginSize();
if (m_alignment & Qt::AlignVCenter){
hOffset = (width() - text->size().height()) / 2 + text->size().height();
}
@ -202,8 +198,8 @@ void TextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* style, Q
painter->rotate(90);
break;
case Angle180:
hOffset = width() - fakeMarginSize();
vOffset = height() - fakeMarginSize();
hOffset = width() - marginSize();
vOffset = height() - marginSize();
if ((tmpSize.width()>0) && (m_alignment & Qt::AlignVCenter)){
vOffset = tmpSize.height() / 2+ text->size().height();
}
@ -214,8 +210,8 @@ void TextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* style, Q
painter->rotate(180);
break;
case Angle270:
hOffset = fakeMarginSize();
vOffset = height()-fakeMarginSize();
hOffset = marginSize();
vOffset = height()-marginSize();
if (m_alignment & Qt::AlignVCenter){
hOffset = (width() - text->size().height())/2;
}
@ -321,7 +317,7 @@ void TextItem::updateItemSize(DataSourceManager* dataManager, RenderPass pass, i
initTextSizes();
if (m_textSize.width()>width() && ((m_autoWidth==MaxWordLength)||(m_autoWidth==MaxStringLength))){
setWidth(m_textSize.width() + fakeMarginSize()*2);
setWidth(m_textSize.width() + marginSize()*2);
}
if (m_textSize.height()>height()) {
@ -380,9 +376,9 @@ QString TextItem::replaceReturns(QString text) const
void TextItem::setTextFont(TextPtr text, const QFont& value) const {
text->setDefaultFont(value);
if ((m_angle==Angle0)||(m_angle==Angle180)){
text->setTextWidth(rect().width()-fakeMarginSize()*2);
text->setTextWidth(rect().width()-marginSize()*2);
} else {
text->setTextWidth(rect().height()-fakeMarginSize()*2);
text->setTextWidth(rect().height()-marginSize()*2);
}
}
@ -394,7 +390,7 @@ void TextItem::adaptFontSize(TextPtr text) const{
if (_font.pixelSize()>2)
_font.setPixelSize(_font.pixelSize()-1);
else break;
} while(text->size().height()>this->height() || text->size().width()>(this->width()) - fakeMarginSize() * 2);
} while(text->size().height()>this->height() || text->size().width()>(this->width()) - marginSize() * 2);
}
int TextItem::underlineLineSize() const

View File

@ -192,7 +192,6 @@ protected:
bool isNeedExpandContent() const;
QString replaceBR(QString text) const;
QString replaceReturns(QString text) const;
int fakeMarginSize() const;
QString getTextPart(int height, int skipHeight);
void restoreLinksEvent();
void preparePopUpMenu(QMenu &menu);

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;

View File

@ -22,15 +22,16 @@ QModelIndex CompleterModel::index(int row, int column, const QModelIndex &parent
return createIndex(row, column, parentItem->child(row));
}
QModelIndex CompleterModel::parent(const QModelIndex &child) const
{
QModelIndex CompleterModel::parent(const QModelIndex &child) const {
if(child.isValid()) {
CompleterItem *childItem = static_cast<CompleterItem*>(child.internalPointer());
if(CompleterItem *childItem = static_cast<CompleterItem *>(child.internalPointer());
childItem) {
CompleterItem *parentItem = childItem->parent();
if(parentItem != &m_root) {
return indexFromItem(parentItem);
}
}
}
return QModelIndex();
}

View File

@ -12,7 +12,9 @@ public:
CompleterItem(QString text, QIcon icon):m_parent(0), m_text(text), m_icon(icon), m_model(0){}
~CompleterItem();
int rowCount() const {return m_children.count();}
CompleterItem* child(int row) const {return m_children.at(row).data();}
CompleterItem *child(int row) const {
return m_children.count() > row ? m_children.at(row).data() : nullptr;
}
CompleterItem* parent() const {return m_parent;}
int row() const{
if (m_parent){
@ -33,7 +35,7 @@ public:
void appendRow(CompleterItem* child);
void appendRows(const QList<CompleterItem *> &children);
private:
CompleterItem* m_parent;
CompleterItem* m_parent{nullptr};
QVector<QSharedPointer<CompleterItem> > m_children;
QString m_text;
QIcon m_icon;