0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-24 16:44:39 +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);
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)
{
switch (info.dataType) {
case LimeReport::CallbackInfo::ColumnCount:
data = ds->record().count();
break;
case LimeReport::CallbackInfo::IsEmpty:
data = !ds->first();
break;

View File

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

View File

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

View File

@ -594,8 +594,17 @@ bool CallbackDatasource::next(){
m_currentRow++;
bool result = false;
emit changePos(CallbackInfo::Next,result);
m_eof = !result; // !checkNextRecord(m_currentRow);
return result;
if (m_rowCount != -1){
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;
}
@ -603,8 +612,16 @@ void CallbackDatasource::first(){
m_currentRow = 0;
m_eof=checkIfEmpty();
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);
m_eof = !result;
if (m_rowCount>0) m_eof = false;
else m_eof = !result;
}
QVariant CallbackDatasource::data(const QString& columnName)
@ -619,21 +636,40 @@ QVariant CallbackDatasource::data(const QString& columnName)
}
int CallbackDatasource::columnCount(){
if (m_headers.size()==0){
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);
CallbackInfo info;
if (m_columnCount == -1){
QVariant columnCount;
info.dataType = CallbackInfo::ColumnCount;
emit getCallbackData(info,columnCount);
if (columnCount.isValid()){
m_columnCount = columnCount.toInt();
}
if (m_columnCount != -1){
for(int i=0;i<m_columnCount;++i) {
QVariant columnName;
info.dataType = CallbackInfo::ColumnHeaderData;
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)
@ -658,4 +694,29 @@ int CallbackDatasource::columnIndexByName(QString name)
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

View File

@ -366,7 +366,7 @@ private:
class CallbackDatasource :public ICallbackDatasource, public IDataSource {
Q_OBJECT
public:
CallbackDatasource(): m_currentRow(-1), m_eof(false){}
CallbackDatasource(): m_currentRow(-1), m_eof(false), m_columnCount(-1), m_rowCount(-1){}
bool next();
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;}
@ -382,26 +382,14 @@ public:
QString lastError(){ return "";}
QAbstractItemModel *model(){return 0;}
private:
bool checkNextRecord(int recordNum){
QVariant result = false;
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();
}
bool checkNextRecord(int recordNum);
bool checkIfEmpty();
private:
QVector<QString> m_headers;
int m_currentRow;
bool m_eof;
int m_columnCount;
int m_rowCount;
};
class CallbackDatasourceHolder :public QObject, public IDataSourceHolder{