0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-24 08:34:38 +03:00

Callback interface has been simplified

From now on if slot getCallbackData(const LimeReport::CallbackInfo& info, QVariant& data) returns row count then there is no more need to implement second slot changePos(const LimeReport::CallbackInfo::ChangePosType& type, bool& result);
This commit is contained in:
Arin Alexander 2016-03-27 10:40:13 +03:00
parent 517bf8357e
commit 9d9f1f4d98
5 changed files with 92 additions and 36 deletions

View File

@ -91,6 +91,10 @@ MainWindow::MainWindow(QWidget *parent) :
stringListModel->setStringList(simpleData); stringListModel->setStringList(simpleData);
report->dataManager()->addModel("string_list",stringListModel,true); report->dataManager()->addModel("string_list",stringListModel,true);
QStringList strList;
strList<<"value1"<<"value2";
QScriptValue value = qScriptValueFromSequence(report->scriptManager()->scriptEngine(),strList);
report->scriptManager()->scriptEngine()->globalObject().setProperty("test_list",value);
} }
@ -152,6 +156,9 @@ void MainWindow::renderFinished()
void MainWindow::prepareData(QSqlQuery* ds, LimeReport::CallbackInfo info, QVariant &data) void MainWindow::prepareData(QSqlQuery* ds, LimeReport::CallbackInfo info, QVariant &data)
{ {
switch (info.dataType) { switch (info.dataType) {
case LimeReport::CallbackInfo::ColumnCount:
data = ds->record().count();
break;
case LimeReport::CallbackInfo::IsEmpty: case LimeReport::CallbackInfo::IsEmpty:
data = !ds->first(); data = !ds->first();
break; break;

View File

@ -4,7 +4,7 @@
namespace LimeReport { namespace LimeReport {
struct CallbackInfo{ struct CallbackInfo{
enum DataType{IsEmpty, HasNext, ColumnHeaderData, ColumnData}; enum DataType{IsEmpty, HasNext, ColumnHeaderData, ColumnData, ColumnCount, RowCount};
enum ChangePosType{First, Next}; enum ChangePosType{First, Next};
DataType dataType; DataType dataType;
int index; int index;

View File

@ -4,7 +4,7 @@
namespace LimeReport { namespace LimeReport {
struct CallbackInfo{ struct CallbackInfo{
enum DataType{IsEmpty, HasNext, ColumnHeaderData, ColumnData}; enum DataType{IsEmpty, HasNext, ColumnHeaderData, ColumnData, ColumnCount, RowCount};
enum ChangePosType{First, Next}; enum ChangePosType{First, Next};
DataType dataType; DataType dataType;
int index; int index;

View File

@ -594,8 +594,17 @@ bool CallbackDatasource::next(){
m_currentRow++; m_currentRow++;
bool result = false; bool result = false;
emit changePos(CallbackInfo::Next,result); emit changePos(CallbackInfo::Next,result);
m_eof = !result; // !checkNextRecord(m_currentRow); if (m_rowCount != -1){
return result; if (m_rowCount>0 && m_currentRow<m_rowCount){
m_eof = false;
} else {
m_eof = true;
}
return !m_eof;
} else {
m_eof = !result;
return result;
}
} else return false; } else return false;
} }
@ -603,8 +612,16 @@ void CallbackDatasource::first(){
m_currentRow = 0; m_currentRow = 0;
m_eof=checkIfEmpty(); m_eof=checkIfEmpty();
bool result=false; bool result=false;
if (m_rowCount == -1){
QVariant rowCount;
CallbackInfo info;
info.dataType = CallbackInfo::RowCount;
emit getCallbackData(info,rowCount);
if (rowCount.isValid()) m_rowCount = rowCount.toInt();
}
emit changePos(CallbackInfo::First,result); emit changePos(CallbackInfo::First,result);
m_eof = !result; if (m_rowCount>0) m_eof = false;
else m_eof = !result;
} }
QVariant CallbackDatasource::data(const QString& columnName) QVariant CallbackDatasource::data(const QString& columnName)
@ -619,21 +636,40 @@ QVariant CallbackDatasource::data(const QString& columnName)
} }
int CallbackDatasource::columnCount(){ int CallbackDatasource::columnCount(){
if (m_headers.size()==0){ CallbackInfo info;
int currIndex = 0; if (m_columnCount == -1){
do { QVariant columnCount;
QVariant columnName; info.dataType = CallbackInfo::ColumnCount;
CallbackInfo info; emit getCallbackData(info,columnCount);
info.dataType = CallbackInfo::ColumnHeaderData; if (columnCount.isValid()){
info.index = currIndex; m_columnCount = columnCount.toInt();
emit getCallbackData(info,columnName); }
if (columnName.isValid()){ if (m_columnCount != -1){
m_headers.append(columnName.toString()); for(int i=0;i<m_columnCount;++i) {
currIndex++; QVariant columnName;
} else break; info.dataType = CallbackInfo::ColumnHeaderData;
} while (true); info.index = i;
emit getCallbackData(info,columnName);
if (columnName.isValid())
m_headers.append(columnName.toString());
}
} else {
int currIndex = 0;
do {
QVariant columnName;
CallbackInfo info;
info.dataType = CallbackInfo::ColumnHeaderData;
info.index = currIndex;
emit getCallbackData(info,columnName);
if (columnName.isValid()){
m_headers.append(columnName.toString());
currIndex++;
} else break;
} while (true);
}
} }
return m_headers.size(); if (m_headers.size()>0) m_columnCount = m_headers.size();
return m_columnCount;
} }
QString CallbackDatasource::columnNameByIndex(int columnIndex) QString CallbackDatasource::columnNameByIndex(int columnIndex)
@ -658,4 +694,29 @@ int CallbackDatasource::columnIndexByName(QString name)
return -1; return -1;
} }
bool CallbackDatasource::checkNextRecord(int recordNum){
if (m_rowCount>0 && m_currentRow<m_rowCount){
return true;
} else {
QVariant result = false;
CallbackInfo info;
info.dataType = CallbackInfo::HasNext;
info.index = recordNum;
emit getCallbackData(info,result);
return result.toBool();
}
}
bool CallbackDatasource::checkIfEmpty(){
if (m_rowCount == 0) {
return true;
} else {
QVariant result = true;
CallbackInfo info;
info.dataType = CallbackInfo::IsEmpty;
emit getCallbackData(info,result);
return result.toBool();
}
}
} //namespace LimeReport } //namespace LimeReport

View File

@ -366,7 +366,7 @@ private:
class CallbackDatasource :public ICallbackDatasource, public IDataSource { class CallbackDatasource :public ICallbackDatasource, public IDataSource {
Q_OBJECT Q_OBJECT
public: public:
CallbackDatasource(): m_currentRow(-1), m_eof(false){} CallbackDatasource(): m_currentRow(-1), m_eof(false), m_columnCount(-1), m_rowCount(-1){}
bool next(); bool next();
bool hasNext(){ if (!m_eof) return checkNextRecord(m_currentRow); else return false;} bool hasNext(){ if (!m_eof) return checkNextRecord(m_currentRow); else return false;}
bool prior(){ if (m_currentRow !=-1) {m_currentRow--; return true;} else return false;} bool prior(){ if (m_currentRow !=-1) {m_currentRow--; return true;} else return false;}
@ -382,26 +382,14 @@ public:
QString lastError(){ return "";} QString lastError(){ return "";}
QAbstractItemModel *model(){return 0;} QAbstractItemModel *model(){return 0;}
private: private:
bool checkNextRecord(int recordNum){ bool checkNextRecord(int recordNum);
QVariant result = false; bool checkIfEmpty();
CallbackInfo info;
info.dataType = CallbackInfo::HasNext;
info.index = recordNum;
emit getCallbackData(info,result);
return result.toBool();
}
bool checkIfEmpty(){
QVariant result = true;
CallbackInfo info;
info.dataType = CallbackInfo::IsEmpty;
emit getCallbackData(info,result);
//emit getIsEmpty(result);
return result.toBool();
}
private: private:
QVector<QString> m_headers; QVector<QString> m_headers;
int m_currentRow; int m_currentRow;
bool m_eof; bool m_eof;
int m_columnCount;
int m_rowCount;
}; };
class CallbackDatasourceHolder :public QObject, public IDataSourceHolder{ class CallbackDatasourceHolder :public QObject, public IDataSourceHolder{