1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
#include "formview.h"
#include "common/unused.h"
#include "datagrid/fkcombobox.h"
#include "datagrid/sqlquerymodel.h"
#include "datagrid/sqlqueryview.h"
#include "widgetresizer.h"
#include "datagrid/sqlqueryitem.h"
#include "uiconfig.h"
#include "common/datawidgetmapper.h"
#include "iconmanager.h"
#include <QGroupBox>
#include <QVBoxLayout>
#include <QResizeEvent>
#include <QDebug>
CFG_KEYS_DEFINE(FormView)
FormView::FormView(QWidget *parent) :
QScrollArea(parent)
{
init();
}
void FormView::init()
{
setWidgetResizable(true);
initActions();
dataMapper = new DataWidgetMapper(this);
dataMapper->setSubmitFilter([](QWidget* w) -> bool
{
return dynamic_cast<MultiEditor*>(w)->isModified();
});
connect(dataMapper, SIGNAL(currentIndexChanged(int)), this, SLOT(currentIndexChanged(int)));
contents = new QWidget();
QVBoxLayout *contentsLayout = new QVBoxLayout();
contentsLayout->setSpacing(spacing);
contentsLayout->setMargin(margins);
contents->setLayout(contentsLayout);
connect(CFG_UI.General.DataEditorsOrder, SIGNAL(changed(QVariant)), this, SLOT(reload()));
setWidget(contents);
}
SqlQueryModel* FormView::getModel() const
{
return model.data();
}
void FormView::setModel(SqlQueryModel* value)
{
if (!model.isNull())
{
disconnect(model.data(), SIGNAL(loadingEnded(bool)), this, SLOT(dataLoaded(bool)));
disconnect(value, SIGNAL(commitStatusChanged(bool)), this, SLOT(gridCommitRollbackStatusChanged()));
}
model = value;
connect(value, SIGNAL(loadingEnded(bool)), this, SLOT(dataLoaded(bool)));
connect(value, SIGNAL(commitStatusChanged(bool)), this, SLOT(gridCommitRollbackStatusChanged()));
}
void FormView::load()
{
shouldReload = true;
indexForReload = 0;
}
void FormView::reload()
{
shouldReload = true;
indexForReload = dataMapper->getCurrentIndex();
reloadInternal();
}
void FormView::focusFirstEditor()
{
if (editors.size() == 0)
return;
editors.first()->focusThisEditor();
}
void FormView::reloadInternal()
{
if (!shouldReload)
return;
shouldReload = false;
// Cleanup
dataMapper->clearMapping();
for (QWidget*& widget : widgets)
{
contents->layout()->removeWidget(widget);
delete widget;
}
widgets.clear();
editors.clear();
readOnly.clear();
// Recreate
dataMapper->setModel(model.data());
int i = 0;
for (SqlQueryModelColumnPtr& column : model->getColumns())
addColumn(i++, column.data());
}
bool FormView::isModified() const
{
return valueModified;
}
MultiEditor* FormView::addColumn(int colIdx, SqlQueryModelColumn* column)
{
bool readOnly = (column->editionForbiddenReason.size() > 0);
// Group with label
QString groupLabel = column->displayName;
if (!column->dataType.toString().isEmpty())
groupLabel += " (" + column->dataType.toString() + ")";
// MultiEditor
MultiEditor* multiEditor = new MultiEditor();
multiEditor->setReadOnly(readOnly);
multiEditor->setCornerLabel(groupLabel);
dataMapper->addMapping(multiEditor, colIdx, "value");
widgets << multiEditor;
editors << multiEditor;
contents->layout()->addWidget(multiEditor);
this->readOnly << readOnly;
connect(multiEditor, SIGNAL(modified()), this, SLOT(editorValueModified()));
// MultiEditor editors
if (!column->getFkConstraints().isEmpty())
{
Db* db = model->getDb();
QString sql = FkComboBox::getSqlForFkEditor(db, column, QVariant());
bool countingError = false;
qlonglong rowCount = FkComboBox::getRowCountForFkEditor(db, sql, &countingError);
if (!countingError && rowCount <= FkComboBox::MAX_ROWS_FOR_FK)
multiEditor->enableFk(db, column);
else
{
qDebug() << "FkCombo excluded from FormView for column" << column->column << "due to"
<< (countingError ?
"error with row counting query" :
"too many rows in the FK table: " + QString::number(rowCount));
}
}
multiEditor->setDataType(column->dataType);
// Resizer
WidgetResizer* resizer = new WidgetResizer(Qt::Vertical);
resizer->setWidget(multiEditor);
resizer->setWidgetMinimumSize(0, minimumFieldHeight);
widgets << resizer;
contents->layout()->addWidget(resizer);
return multiEditor;
}
bool FormView::isCurrentRowModifiedInGrid()
{
if (!model)
return false;
QModelIndex startIdx = model->index(gridView->getCurrentIndex().row(), 0);
QModelIndex endIdx = model->index(gridView->getCurrentIndex().row(), model->columnCount() - 1);
return model->findIndexes(startIdx, endIdx, SqlQueryItem::DataRole::UNCOMMITTED, true, 1).size() > 0;
}
void FormView::updateDeletedState()
{
SqlQueryItem* item = model->itemFromIndex(dataMapper->getCurrentIndex(), 0);
if (!item)
{
for (MultiEditor* editor : editors)
editor->setEnabled(false);
return;
}
bool deleted = item->isDeletedRow();
int i = 0;
for (MultiEditor* editor : editors)
{
editor->setEnabled(true);
editor->setDeletedRow(deleted);
editor->setReadOnly(readOnly[i++] || deleted);
}
}
void FormView::dataLoaded(bool successful)
{
if (successful)
load();
}
void FormView::currentIndexChanged(int index)
{
valueModified = false;
emit commitStatusChanged();
if (gridView.isNull())
return;
if (currentIndexUpdating)
return;
currentIndexUpdating = true;
gridView->setCurrentRow(index);
currentIndexUpdating = false;
// If row was deleted, we need to make fields readonly
updateDeletedState();
emit currentRowChanged();
}
void FormView::editorValueModified()
{
valueModified = true;
emit commitStatusChanged();
}
void FormView::gridCommitRollbackStatusChanged()
{
valueModified = isCurrentRowModifiedInGrid();
emit commitStatusChanged();
}
void FormView::copyDataToGrid()
{
dataMapper->submit();
}
void FormView::updateFromGrid()
{
currentIndexUpdating = true;
dataMapper->setCurrentIndex(gridView->getCurrentIndex().row());
// Already modified in grid?
valueModified = isCurrentRowModifiedInGrid();
currentIndexUpdating = false;
updateDeletedState();
emit currentRowChanged();
}
SqlQueryView* FormView::getGridView() const
{
return gridView.data();
}
void FormView::setGridView(SqlQueryView* value)
{
gridView = value;
}
int FormView::getCurrentRow()
{
return dataMapper->getCurrentIndex();
}
void FormView::createActions()
{
createAction(COMMIT, ICONS.COMMIT, tr("Commit row", "form view"), this, SIGNAL(requestForCommit()), this);
createAction(ROLLBACK, ICONS.ROLLBACK, tr("Rollback row", "form view"), this, SIGNAL(requestForRollback()), this);
createAction(FIRST_ROW, ICONS.PAGE_FIRST, tr("First row", "form view"), this, SIGNAL(requestForFirstRow()), this);
createAction(PREV_ROW, ICONS.PAGE_PREV, tr("Previous row", "form view"), this, SIGNAL(requestForPrevRow()), this);
createAction(NEXT_ROW, ICONS.PAGE_NEXT, tr("Next row", "form view"), this, SIGNAL(requestForNextRow()), this);
createAction(LAST_ROW, ICONS.PAGE_LAST, tr("Last row", "form view"), this, SIGNAL(requestForLastRow()), this);
createAction(INSERT_ROW, ICONS.INSERT_ROW, tr("Insert new row", "form view"), this, SIGNAL(requestForRowInsert()), this);
createAction(DELETE_ROW, ICONS.DELETE_ROW, tr("Delete current row", "form view"), this, SIGNAL(requestForRowDelete()), this);
}
void FormView::setupDefShortcuts()
{
setShortcutContext({ROLLBACK, COMMIT, NEXT_ROW, PREV_ROW, FIRST_ROW, LAST_ROW, INSERT_ROW, DELETE_ROW}, Qt::ApplicationShortcut);
BIND_SHORTCUTS(FormView, Action);
}
QToolBar* FormView::getToolBar(int toolbar) const
{
UNUSED(toolbar);
return nullptr;
}
void FormView::showEvent(QShowEvent* event)
{
UNUSED(event);
reloadInternal();
}
|