C++ (Qt)#define UPDATE_STEP 100 bool UpdateProgress( QProgressDialog & progress, int row ){ int k=1% 100; if ((row+1 ) % UPDATE_STEP) return false; // update once per UPDATE_STEP progress.setValue(row); // calls processEvents for modal dialog return progress.wasCanceled();} bool Spreadsheet::sort_data(const QList<QTableWidgetSelectionRange>& range, const SpreadsheetCompare &compare){ if(range.isEmpty()) {return false;} else { QProgressDialog progress(this); progress.setModal(true); progress.setWindowFlags(Qt::Dialog|Qt::CustomizeWindowHint|Qt::WindowTitleHint); progress.setMinimumDuration(1); progress.setLabelText(tr("Checking range 1/5")); progress.setMaximum(range.size()); for(int it=0;it<range.size();it++) { if(range[it].leftColumn()<0||range[it].bottomRow()<0||range[it].rightColumn()<0||range[it].topRow()<0) {return false;} qApp->processEvents(); if (UpdateProgress(progress, it)) return false; }progress.setLabelText(tr("Adding rows & columns 2/5")); progress.setMaximum(range.size()); progress.setValue(0); QList<int> columns; QList<int> rows; for(int i=0;i<range.size();i++) { for(int col=range[i].leftColumn();col<=range[i].rightColumn();col++) {if(columns.indexOf(col) ==-1) columns.push_back(col);qApp->processEvents();} for(int row=range[i].topRow();row<=range[i].bottomRow();row++) {if(rows.indexOf(row) ==-1) rows.push_back(row);qApp->processEvents();} if (UpdateProgress(progress, i)) return false; } progress.setLabelText(tr("Sorting 3/5")); progress.setMaximum(2); progress.setValue(0); qSort(columns.begin(),columns.end()); qSort(rows.begin(),rows.end()); QList<QList<QTableWidgetItem*>> data_for_sort;progress.setLabelText(tr("Adding rows & columns 4/5")); progress.setMaximum(rows.size()); progress.setValue(0); for(int i=0;i<rows.size();i++) { if (UpdateProgress(progress, i)) return false; QList<QTableWidgetItem*> row; for(int j=0;j<columns.size();j++) { if(is_item_in_list_selection_range(range, rows[i], columns[j])==true ) { if(isError_in_cell(rows[i],columns[j])==true) {return false;} else if(item(rows[i],columns[j])) { row.push_back(takeItem(rows[i],columns[j])); } qApp->processEvents(); } } if(row.size()==columns.size()&&row.size()>0) data_for_sort.push_back(row); else { rows.removeOne(rows[i]); i--; } row.clear(); } QList<QList<QTableWidgetItem*>> temp=data_for_sort; qStableSort(data_for_sort.begin(), data_for_sort.end(),compare); if(data_for_sort==temp) {return false;} set_calculate_function(true); progress.setLabelText(tr("Setting Items 5/5")); progress.setMaximum(rows.size()); progress.setValue(0); for(int i=0;i<data_for_sort.size();i++) { for(int j=0;j<data_for_sort[i].size();j++) { if (UpdateProgress(progress, i)) return false; //setFormula(rows[i], columns[j], data_for_sort[i][j]->data(Qt::EditRole).toString()); setItem(rows[i],columns[j],data_for_sort[i][j]); qApp->processEvents(); } // progress.setValue(((i)*10)/rows.size()+85); } set_calculate_function(false); //progress.setValue(95); // clearSelection(); progress.setValue(100); if(! somethingChanged(progress,true)) return false; //progress.setValue(100); progress.cancel(); return true; } } bool Spreadsheet::somethingChanged(QProgressDialog &progress,bool update){ if (autoRecalc&&!bool_calculate_function||update) if(!recalculate(progress)) return false; if(!bool_calculate_function||update) emit modified(); return true; } bool Spreadsheet::recalculate(QProgressDialog &progress) //мое работает{ progress.setRange(0,RowCount); for (int row = 0; row < RowCount; ++row) { for (int column = 0; column < ColumnCount; ++column) { if (UpdateProgress(progress, row)) return false; if( item(row, column)) if(item(row,column)->text()!="") cell(row, column)->setDirty(); qApp->processEvents(); } } viewport()-> update(); return true; } class SpreadsheetCompare{public: bool operator()(const QList<QTableWidgetItem*> &row1, const QList<QTableWidgetItem*> &row2) const { int column = keys; if (column != -1) { if (row1[column] != row2[column]) { parser p; if (ascending) return row1[column]->text().toDouble()< row2[column]->text().toDouble();//*///p.calculation(row1[column].toStdString().c_str()) < p.calculation(row2[column].toStdString().c_str()); else return row1[column]->text().toDouble()> row2[column]->text().toDouble();//*///p.calculation(row1[column].toStdString().c_str()) > p.calculation(row2[column].toStdString().c_str()); } } return false; } int keys; bool ascending;};
if (UpdateProgress(progress, i)) return false;
C++ (Qt)bool Spreadsheet::is_item_in_list_selection_range(const QList<QTableWidgetSelectionRange>& range, int row_, int column){ if(range.isEmpty()) return false; else { for(int it=0;it<range.size();it++) { if(range[it].leftColumn()<0||range[it].bottomRow()<0||range[it].rightColumn()<0||range[it].topRow()<0) return false; qApp->processEvents(); } for(int i=0;i<range.size();i++) { for(int col=range[i].leftColumn();col<=range[i].rightColumn();col++) { for(int row=range[i].topRow();row<=range[i].bottomRow();row++) { if(col==column&&row==row_) return true; qApp->processEvents(); } } } return false; }}bool Spreadsheet::sort_data_with_GUI(const QList<QTableWidgetSelectionRange>& range){ if(range.isEmpty()) return false; else { for(int it=0;it<range.size();it++) { if(range[it].leftColumn()<0||range[it].bottomRow()<0||range[it].rightColumn()<0||range[it].topRow()<0) return false; qApp->processEvents(); } QString error=""; for(int it=0;it<range.size();it++) for(int i=0;i<range[it].rowCount();i++) for(int j=0;j<range[it].columnCount();j++) { if(!item(range[it].topRow()+i ,range[it].leftColumn()+j)||item(range[it].topRow()+i ,range[it].leftColumn()+j)->text()=="") { error+=tr("item: ")+get_header_label(range[it].leftColumn()+j)+QString::number(range[it].topRow()+i+1)+tr(" - not value\n");} } if(error!="") { QMessageBox *errorMessageDialog = new QMessageBox(this); errorMessageDialog->setWindowTitle(tr("")); errorMessageDialog->setText(tr("Invalid item(s) ( ? select Item with valid value).")); errorMessageDialog->setIcon(QMessageBox::Critical); errorMessageDialog->setDetailedText(error); errorMessageDialog->show(); return false; } else { Dialog_properties_datasheet_sort dialog; QStringList interval_names_for_columns; for(int it=0;it<range.size();it++) for(int i=range[it].leftColumn();i<=range[it].rightColumn();i++) { if(interval_names_for_columns.indexOf(list_names_for_columns ()[i])==-1)interval_names_for_columns+=list_names_for_columns ()[i]; qApp->processEvents(); } dialog.setColumnRange(interval_names_for_columns); if (dialog.exec()) { if( !sort_data(range,dialog.get_settings_for_compare_from_dialog())) { QMessageBox::information(this,tr("Graphbuilder"),tr("Sort nothing has changed or cancel")); return false; } } return true; } }}#define UPDATE_STEP 100 bool UpdateProgress( QProgressDialog & progress, int row ){ int k=1% 100; if ((row+1 ) % UPDATE_STEP) return true; // update once per UPDATE_STEP progress.setValue(row); // calls processEvents for modal dialog return !progress.wasCanceled();} bool Spreadsheet::sort_data(const QList<QTableWidgetSelectionRange>& range, const SpreadsheetCompare &compare){ if(range.isEmpty()) return false; else { QProgressDialog progress(this); progress.setModal(true); progress.setWindowFlags(Qt::Dialog|Qt::CustomizeWindowHint|Qt::WindowTitleHint); progress.setMinimumDuration(1); progress.setLabelText(tr("Checking range 1/5")); progress.setMaximum(range.size()); for(int it=0;it<range.size();it++) { if(range[it].leftColumn()<0||range[it].bottomRow()<0||range[it].rightColumn()<0||range[it].topRow()<0) return false; //qApp->processEvents(); if (!UpdateProgress(progress, it)) return false; } progress.setLabelText(tr("Adding rows & columns 2/5")); progress.setMaximum(range.size()); progress.setValue(0); QList<int> columns; QList<int> rows; for(int i=0;i<range.size();i++) { for(int col=range[i].leftColumn();col<=range[i].rightColumn();col++) {if(columns.indexOf(col) ==-1) columns.push_back(col);qApp->processEvents();} for(int row=range[i].topRow();row<=range[i].bottomRow();row++) {if(rows.indexOf(row) ==-1) rows.push_back(row);qApp->processEvents();} if (!UpdateProgress(progress, i)) return false; } progress.setLabelText(tr("Sorting 3/5")); progress.setMaximum(2); progress.setValue(0); qSort(columns.begin(),columns.end()); qSort(rows.begin(),rows.end()); QList<QList<QTableWidgetItem*>> data_for_sort; progress.setLabelText(tr("Adding data to sort 4/5")); progress.setMaximum(rows.size()); progress.setValue(0); for(int i=0;i<rows.size();i++) { if (!UpdateProgress(progress, i)) return false; QList<QTableWidgetItem*> row; for(int j=0;j<columns.size();j++) { if(is_item_in_list_selection_range(range, rows[i], columns[j])==true ) { if(isError_in_cell(rows[i],columns[j])==true) return false; else if(item(rows[i],columns[j])) row.push_back(takeItem(rows[i],columns[j])); // qApp->processEvents(); } } if(row.size()==columns.size()&&row.size()>0) data_for_sort.push_back(row); else { rows.removeOne(rows[i]); i--; } row.clear(); } QList<QList<QTableWidgetItem*>> temp=data_for_sort; qStableSort(data_for_sort.begin(), data_for_sort.end(),compare); if(data_for_sort==temp) return false; set_calculate_function(true); progress.setLabelText(tr("Setting Items 5/5")); progress.setMaximum(rows.size()); progress.setValue(0); for(int i=0;i<data_for_sort.size();i++) { for(int j=0;j<data_for_sort[i].size();j++) { if (!UpdateProgress(progress, i)) return false; //setFormula(rows[i], columns[j], data_for_sort[i][j]->data(Qt::EditRole).toString()); setItem(rows[i],columns[j],data_for_sort[i][j]); //qApp->processEvents(); } } set_calculate_function(false);/////разрешает обновлять ячейки таблицы (true - запрещает). progress.setValue(100); if(! somethingChanged(progress,true)) return false; //progress.setValue(100); return true; }} bool Spreadsheet::somethingChanged(QProgressDialog &progress,bool update){ if (autoRecalc&&!bool_calculate_function||update) if(!recalculate(progress)) return false; if(!bool_calculate_function||update) emit modified(); return true;} bool Spreadsheet::recalculate(QProgressDialog &progress) //мое работает{ progress.setRange(0,RowCount); for (int row = 0; row < RowCount; ++row) { for (int column = 0; column < ColumnCount; ++column) { if (!UpdateProgress(progress, row)) return false; if( item(row, column)) if(item(row,column)->text()!="") cell(row, column)->setDirty(); qApp->processEvents(); } } viewport()-> update(); return true;} class SpreadsheetCompare{public: int keys; bool ascending; bool operator()(const QList<QTableWidgetItem*> &row1, const QList<QTableWidgetItem*> &row2) const { int column = keys; if (column != -1) { if (row1[column] != row2[column]) { if (ascending) return row1[column]->text().toDouble()< row2[column]->text().toDouble(); else return row1[column]->text().toDouble()> row2[column]->text().toDouble(); } } return false; }};
C++ (Qt)bool Spreadsheet::is_item_in_list_selection_range(const QList<QTableWidgetSelectionRange>& range, int row_, int column){ for(int i = 0 ; i < range.size(); i++) { if (row_ < range[i].topRow()) continue; if (row_ > range[i].bottomRow()) continue; if (column < range[i].leftColumn()) continue; if (column > range[i].rightColumn()) continue; return true; } return false;}
C++ (Qt)void Spreadsheet::setRowCount(int count_,QProgressDialog &dialog) //мое работает{ dialog.setLabelText(tr("set Row")); dialog.setRange(0,0); dialog.setValue(0); dialog.setMinimumDuration(1); QTableWidget::setRowCount(count_); RowCount=count_; }