C++ (Qt)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) {process=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(); process=0;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")); return false; } } return true; } } } } bool Spreadsheet::sort_data(const QList<QTableWidgetSelectionRange>& range, const SpreadsheetCompare &compare){ 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(); } QList<int> columns; QList<int> rows_n; 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_n.indexOf(row) ==-1) rows_n.push_back(row);qApp->processEvents();} } qSort(columns.begin(),columns.end()); qSort(rows_n.begin(),rows_n.end()); QList<QStringList> rows; for(int i=0;i<rows_n.size();i++) { QStringList row; for(int j=0;j<columns.size();j++) { if(is_item_in_list_selection_range(range, rows_n[i], columns[j])==true ) { if(isError_in_cell(rows_n[i],columns[j])==true) {return false;} else if(item(rows_n[i],columns[j])) { row.push_back(formula(rows_n[i],columns[j])); } qApp->processEvents(); } } if(row.size()==columns.size()&&row.size()>0) rows.push_back(row); else { rows_n.removeOne(rows_n[i]); i--; } row.clear(); } QList<QStringList> temp=rows; qStableSort(rows.begin(), rows.end(),compare); if(rows==temp) {process=0;return false;} for(int i=0;i<rows.size();i++) { for(int j=0;j<rows[i].size();j++) { setFormula(rows_n[i], columns[j], rows[i][j]); qApp->processEvents(); } } somethingChanged(); return true; } }
progress.setProgress(it);.. qApp->processEvents(); if (progress.wasCanceled()) { return false; }
C++ (Qt)bool Spreadsheet::sort_data(const QList<QTableWidgetSelectionRange>& range, const SpreadsheetCompare &compare){ if(range.isEmpty()) {return false;} else { QProgressDialog *p_d=new QProgressDialog(this); connect(p_d, SIGNAL(canceled()), this,SLOT(?????return 0));///////////////////////////////// p_d->setMinimumDuration(1); 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(); } p_d->setValue(17);///////////////////////////////////////////////////////////////////// QList<int> columns; QList<int> rows_n; 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_n.indexOf(row) ==-1) rows_n.push_back(row);qApp->processEvents();} } p_d->setValue(34);/////////////////////////////////////////////////////////////////////////////////////// qSort(columns.begin(),columns.end()); p_d->setValue(51);//////////////////////////////////////////////////////////////////////////////////// qSort(rows_n.begin(),rows_n.end()); QList<QStringList> rows; for(int i=0;i<rows_n.size();i++) { QStringList row; for(int j=0;j<columns.size();j++) { if(is_item_in_list_selection_range(range, rows_n[i], columns[j])==true ) { if(isError_in_cell(rows_n[i],columns[j])==true) {process=0;return false;} else if(item(rows_n[i],columns[j])) { row.push_back(text(rows_n[i],columns[j])); } qApp->processEvents(); } } if(row.size()==columns.size()&&row.size()>0) rows.push_back(row); else { rows_n.removeOne(rows_n[i]); i--; } row.clear(); } p_d->setValue(68);////////////////////////////////////////////////////////////// QList<QStringList> temp=rows; qStableSort(rows.begin(), rows.end(),compare); p_d->setValue(85);///////////////////////////////////////////////////////////// if(rows==temp) {return false;} for(int i=0;i<rows.size();i++) { for(int j=0;j<rows[i].size();j++) { setItem(rows_n[i], columns[j], rows[i][j]); qApp->processEvents(); } p_d->setValue((i*10)/rows.size()+85);////////////////////////////////////// } p_d->setValue(95);///////////////////////////////////////////////// // clearSelection(); somethingChanged(); p_d->setValue(100);///////////////////////////////////////// return true; } }
#define UPDATE_STEP 100bool UpdateProgress( QProgressDialog & progress, int row ){ 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; QProgressDialog progress(this); progess.setModal(true); // should be modal // check range progress.setLabelText(tr("Checking range")); progress.setMaximum(range.size()); for (int it = 0; it < range.size(); it++) { if (range[it].leftColumn() < 0) return false; if (range[it].bottomRow() < 0) return false; if (range[it].rightColumn() < 0) return false; if (range[it].topRow() < 0) return false; if (!UpdateProgress(progress, it)) return false; }// fill columns QList<int> columns; QList<int> rows_n; progress.setLabelText(tr("Adding rows & columns")); progress.setMaximum(range.size()); progress.setValue(0); 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); for(int row = range[i].topRow(); row <= range[i].bottomRow(); row++) if (rows_n.indexOf(row) == -1) rows_n.push_back(row); if (!UpdateProgress(progress, i)) return false; } // sort progress.setLabelText(tr("Sorting")); progress.setMaximum(2); progress.setValue(0); qSort(columns.begin(), columns.end()); qSort(rows_n.begin(), rows_n.end());// add progress.setLabelText(tr("Adding rows & columns")); progress.setMaximum(rows_n.size()); progress.setValue(0); QList<QStringList> rows; for(int i = 0; i < rows_n.size(); i++) { if (!UpdateProgress(progress, i)) return false; QStringList row; for (int j = 0; j < columns.size(); j++) { if (is_item_in_list_selection_range(range, rows_n[i], columns[j])) { if (isError_in_cell(rows_n[i], columns[j])) return false; if (item(rows_n[i], columns[j])) row.push_back(text(rows_n[i], columns[j])); } } if (row.size() == columns.size() && row.size() > 0) rows.push_back(row); else { rows_n.removeOne(rows_n[i]); i--; } row.clear(); } QList<QStringList> temp = rows; qStableSort(rows.begin(), rows.end(), compare); if (rows == temp) return false;// set items progress.setLabelText(tr("Setting Items")); progress.setMaximum(rows.size()); progress.setValue(0); for (int i = 0; i < rows.size(); i++) { if (!UpdateProgress(progress, i)) return false; for(int j = 0; j < rows[i].size(); j++) setItem(rows_n[i], columns[j], rows[i][j]); } return true;}
C++ (Qt)if ((row+1 ) % UPDATE_STEP) return false; if (UpdateProgress(progress, it)) return false; каждую проверку заменить
bool somethingChanged( QProgressDialog & progress );// вызовif (!somethingChanged(progress)) return false;