2424#include < qf/core/collator.h>
2525#include < qf/core/assert.h>
2626#include < qf/core/exception.h>
27+ #include < qf/core/sql/query.h>
2728#include < qf/gui/model/sqltablemodel.h>
2829#include < qf/gui/model/tablemodel.h>
2930#include < qf/core/utils/treetable.h>
@@ -413,7 +414,7 @@ void TableView::cloneRow()
413414 cloneRowInline ();
414415 }
415416 else {
416- QVariant id = selectedRow ().value (idColumnName ());
417+ QVariant id = selectedRow ().value (tableModel ()-> idColumnName ());
417418 qfDebug () << " \t emit editRowInExternalEditor(ModeCopy)" ;
418419 emit editRowInExternalEditor (id, ModeCopy);
419420 emit editSelectedRowsInExternalEditor (ModeCopy);
@@ -431,7 +432,7 @@ void TableView::removeSelectedRows()
431432 else {
432433 QList<int > sel_rows = selectedRowsIndexes ();
433434 if (sel_rows.count () == 1 ) {
434- QVariant id = tableRow (sel_rows.value (0 )).value (idColumnName ());
435+ QVariant id = tableRow (sel_rows.value (0 )).value (tableModel ()-> idColumnName ());
435436 if (id.isValid ())
436437 emit editRowInExternalEditor (id, ModeDelete);
437438 }
@@ -584,14 +585,15 @@ void TableView::paste()
584585 int origin_view_row = origin_ix.row ();
585586 bool insert_rows = w->isInsert ();
586587 int dest_row = toTableModelRowNo (origin_view_row);
587- for (int src_row= 0 ; src_row< src_tm->rowCount (); src_row++) {
588+ for (int src_row = 0 ; src_row < src_tm->rowCount (); src_row++) {
588589 if (insert_rows) {
589590 qfDebug () << " insert row:" << dest_row << " model row count:" << dest_tm->rowCount ();
590591 dest_tm->insertRow (++dest_row);
591592 }
592593 else {
593- if ((origin_view_row + src_row) >= dest_tm->rowCount ())
594+ if ((origin_view_row + src_row) >= dest_tm->rowCount ()) {
594595 break ;
596+ }
595597 dest_row = toTableModelRowNo (origin_view_row + src_row);
596598 }
597599 int dest_col = origin_ix.column ();
@@ -1118,122 +1120,78 @@ QList<int> TableView::selectedColumnsIndexes() const
11181120 return ret;
11191121}
11201122
1121- void TableView::rowExternallySaved (const QVariant &id, int mode)
1122- {
1123- qfLogFuncFrame () << " id:" << id.toString () << " mode:" << mode;
1124- qfm::TableModel *tmd = tableModel ();
1125- if (tmd) {
1126- if (mode == ModeInsert || mode == ModeCopy) {
1127- // / ModeInsert or ModeCopy
1128- qfDebug () << " \t ModeInsert or ModeCopy" ;
1129- // qfDebug() << "\tri:" << ri;
1130- // qfDebug() << "\tmodel->rowCount():" << ri;
1131- QModelIndex curr_ix = currentIndex ();
1132- int ri = curr_ix.row () + 1 ;
1133- if (ri >= 0 && ri < model ()->rowCount ())
1134- ri = toTableModelRowNo (ri);
1135- else
1136- ri = tmd->rowCount ();
1137- ri = std::min (ri, tmd->rowCount ());
1138- qfDebug () << " \t ri:" << ri;
1139- if (ri < 0 ) {
1140- qfWarning () << " Invalid row number:" << ri;
1141- ri = 0 ;
1142- }
1143- tmd->insertRow (ri);
1144- tmd->setValue (ri, idColumnName (), id);
1145- // qfu::TableRow &row_ref = tmd->table().rowRef(ri);
1146- // row_ref.setValue(idColumnName(), id);
1147- // row_ref.setInsert(false);
1148- int reloaded_row_cnt = tmd->reloadRow (ri);
1149- if (reloaded_row_cnt == 0 ) {
1150- // inserted row cannot be reloaded, it can happen if it doesn't meet WHERE contition of query
1151- // remove just inserted row from table
1152- qfWarning () << " Inserted/Copied row id:" << id.toString () << " cannot be reloaded, it will be deleted in table." ;
1153- tmd->qfm ::TableModel::removeRowNoOverload (ri, !qf::core::Exception::Throw);
1154- return ;
1155- }
1156- if (reloaded_row_cnt != 1 ) {
1157- qfWarning () << " Inserted/Copied row id:" << id.toString () << " reloaded in" << reloaded_row_cnt << " instances." ;
1158- return ;
1159- }
1160- if (curr_ix.isValid ()) {
1161- updateRow (curr_ix.row ());
1162- setCurrentIndex (curr_ix.sibling (ri, curr_ix.column ()));
1123+ void TableView::rowExternallySaved (const QVariant &id)
1124+ {
1125+ qfLogFuncFrame () << " id:" << id.toString ();
1126+ auto table_model = qobject_cast<qf::gui::model::SqlTableModel*>(tableModel ());
1127+ if (!table_model) {
1128+ qfWarning () << " Not SqlTableModel." ;
1129+ return ;
1130+ }
1131+ if (table_model) {
1132+ std::optional<int > row_with_id_index;
1133+ for (int ri = 0 ; ri < table_model->rowCount (); ri++) {
1134+ auto v = table_model->value (ri, tableModel ()->idColumnName ());
1135+ // qfDebug() << "\t row" << ri << "id:" << v.toString();
1136+ if (v == id) {
1137+ row_with_id_index = ri;
1138+ break ;
11631139 }
1164- else {
1165- setCurrentIndex (model ()->index (ri, 0 , QModelIndex ()));
1140+ }
1141+ auto query_str = table_model->reloadRowQuery (id);
1142+ if (query_str.isEmpty ()) {
1143+ return ;
1144+ }
1145+ qf::core::sql::Query q;
1146+ bool ok = q.exec (query_str);
1147+ if (!ok) {
1148+ qfInfo () << " Query:" << query_str;
1149+ qfWarning () << " SQL error:" << q.lastErrorText ();
1150+ return ;
1151+ }
1152+ int row_cnt = 0 ;
1153+ while (q.next ()) {
1154+ row_cnt++;
1155+ }
1156+ if (row_cnt > 1 ) {
1157+ qfWarning () << " More rows returned for id:" << id;
1158+ return ;
1159+ }
1160+ auto row_exists_in_db = row_cnt == 1 ;
1161+
1162+ auto curr_col = currentIndex ().column ();
1163+ auto curr_row = std::max (0 , toTableModelRowNo (currentIndex ().row ()));
1164+ QModelIndex new_ix;
1165+ if (row_with_id_index && row_exists_in_db) {
1166+ // edit
1167+ table_model->reloadRow (row_with_id_index.value ());
1168+ }
1169+ else if (!row_with_id_index && row_exists_in_db) {
1170+ // insert
1171+ table_model->insertRow (curr_row);
1172+ table_model->setValue (curr_row, tableModel ()->idColumnName (), id);
1173+ table_model->reloadRow (curr_row);
1174+ new_ix = table_model->index (curr_row, curr_col >= 0 ? curr_col: 0 );
1175+ setCurrentIndex (m_proxyModel->mapFromSource (new_ix));
1176+ }
1177+ else if (row_with_id_index && !row_exists_in_db) {
1178+ // delete
1179+ table_model->qfm ::TableModel::removeRowNoOverload (row_with_id_index.value (), !qf::core::Exception::Throw);
1180+ auto row = currentIndex ().row ();
1181+ if (row >= model ()->rowCount ()) {
1182+ row = model ()->rowCount () - 1 ;
11661183 }
1167- updateRow ( currentIndex (). row ( ));
1184+ setCurrentIndex ( model ()-> index (row, curr_col ));
11681185 }
11691186 else {
1170- // / find row with id
1171- // / start with currentRow, because id value is most probabbly here
1172- int ri = currentIndex ().row ();
1173- if (ri >= 0 ) {
1174- QVariant v = tmd->value (ri, idColumnName ());
1175- // qfDebug() << "\t found id:" << v.toString();
1176- if (v != id)
1177- ri = -1 ;
1178- }
1179- if (ri < 0 ) for (ri=0 ; ri<tmd->rowCount (); ri++) {
1180- QVariant v = tmd->value (ri, idColumnName ());
1181- // qfDebug() << "\t row" << ri << "id:" << v.toString();
1182- if (v == id) {
1183- break ;
1184- }
1185- }
1186- if ((ri < 0 || ri >= tmd->rowCount ()) && currentIndex ().row () >= 0 ) {
1187- // this can happen if ID column value is changed
1188- // reload current row to do the best
1189- ri = currentIndex ().row ();
1190- // set ID value to the new one
1191- tmd->setValue (ri, idColumnName (), id);
1192- tmd->setDirty (ri, idColumnName (), false );
1193- }
1194- if (ri >= 0 && ri < tmd->rowCount ()) {
1195- if (mode == ModeEdit || mode == ModeView) {
1196- int reloaded_row_cnt = tmd->reloadRow (ri);
1197- if (reloaded_row_cnt != 1 ) {
1198- qfWarning () << " Edited row index:" << ri << " id:" << id.toString () << " reloaded in" << reloaded_row_cnt << " instances." ;
1199- }
1200- updateRow (currentIndex ().row ());
1201- }
1202- else if (mode == ModeDelete) {
1203- int reloaded_row_cnt = tmd->reloadRow (ri);
1204- if (reloaded_row_cnt > 0 ) {
1205- qfWarning () << " Deleted row id:" << id.toString () << " still exists." ;
1206- }
1207- else {
1208- tmd->qfm ::TableModel::removeRowNoOverload (ri, !qf::core::Exception::Throw);
1209- if (ri >= tmd->rowCount ())
1210- ri = tmd->rowCount () - 1 ;
1211- QModelIndex ix = currentIndex ();
1212- if (ri >= 0 ) {
1213- ix = tmd->index (ri, (ix.column () >= 0 )? ix.column (): 0 );
1214- // qfInfo() << "ix row:" << ix.row() << "col:" << ix.column();
1215- setCurrentIndex (ix);
1216- }
1217- }
1218- }
1219- }
1187+ // nor in table neither in database
12201188 }
12211189 }
12221190 else {
12231191 qfError () << " Feature not defined for this model type:" << model ();
12241192 }
12251193}
1226- /*
1227- qf::core::utils::Table::SortDef TableView::seekSortDefinition() const
1228- {
1229- qfLogFuncFrame();
1230- qf::core::utils::Table::SortDef ret;
1231- if(tableModel()) {
1232- ret = tableModel()->table().tableProperties().sortDefinition().value(0);
1233- }
1234- return ret;
1235- }
1236- */
1194+
12371195int TableView::seekColumn () const
12381196{
12391197 int ret = -1 ;
@@ -2249,7 +2207,7 @@ bool TableView::edit(const QModelIndex& index, EditTrigger trigger, QEvent* even
22492207 if (trigger == QTableView::DoubleClicked || trigger == QTableView::EditKeyPressed) {
22502208 if (!read_only) {
22512209 emit editCellRequest (index);
2252- QVariant id = selectedRow ().value (idColumnName ());
2210+ QVariant id = selectedRow ().value (tableModel ()-> idColumnName ());
22532211 if (id.isValid ()) {
22542212 emit editRowInExternalEditor (id, ModeEdit);
22552213 }
0 commit comments