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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
#include "fkcombobox.h"
#include "common/unused.h"
#include "datagrid/sqlqueryitem.h"
#include "schemaresolver.h"
#include "services/notifymanager.h"
#include "sqlquerymodel.h"
#include "sqlqueryview.h"
#include "uiconfig.h"
#include <QGuiApplication>
#include <QScreen>
#include <QLineEdit>
#include <QScrollBar>
#include <QCompleter>
FkComboBox::FkComboBox(QWidget* parent, int dropDownViewMinWidth)
: QComboBox(parent), dropDownViewMinWidth(dropDownViewMinWidth)
{
init();
}
QString FkComboBox::getSqlForFkEditor(Db* db, SqlQueryModelColumn* columnModel, const QVariant& currentValue)
{
static_qstring(sql, "SELECT %4, %1 FROM %2%3");
static_qstring(currValueTpl, "(%1 == %2) AS %3");
static_qstring(currNullValueTpl, "(%1 IS NULL) AS %2");
static_qstring(dbColTpl, "%1 AS %2");
static_qstring(conditionTpl, "%1.%2 = %3.%4");
static_qstring(conditionPrefixTpl, " WHERE %1");
QStringList selectedCols;
QStringList fkConditionTables;
QStringList fkConditionCols;
QStringList srcCols;
SchemaResolver resolver(db);
QList<SqlQueryModelColumn::ConstraintFk*> fkList = columnModel->getFkConstraints();
int i = 0;
QString src;
QString fullSrcCol;
QString col;
QString firstSrcCol;
QStringList usedNames;
QHash<QString, int> implicitFkColumnIdxByTable;
QHash<QString, QStringList> implicitFkColumnsByTable;
for (SqlQueryModelColumn::ConstraintFk*& fk : fkList)
{
src = wrapObjIfNeeded(fk->foreignTable);
if (!fk->foreignColumn.isNull())
{
col = wrapObjIfNeeded(fk->foreignColumn);
}
else
{
// FK is using shorthand syntax (i.e. "REFERENCES parent" without explicit columns)
col = resolveImplicitColumn(db, columnModel, fk, implicitFkColumnIdxByTable, implicitFkColumnsByTable);
if (col.isNull())
continue;
}
if (i == 0)
{
fullSrcCol = src + "." + col;
firstSrcCol = fullSrcCol;
selectedCols << dbColTpl.arg(fullSrcCol, wrapObjIfNeeded(columnModel->column));
}
if (fkConditionTables.contains(src, Qt::CaseInsensitive))
continue;
srcCols = resolver.getTableColumns(src);
for (QString& srcCol : srcCols)
{
if (fk->foreignColumn.compare(srcCol, Qt::CaseInsensitive) == 0)
continue; // Exclude matching column. We don't want the same column several times.
fullSrcCol = src + "." + wrapObjIfNeeded(srcCol);
selectedCols << fullSrcCol;
usedNames << srcCol;
}
fkConditionCols << col;
fkConditionTables << src;
i++;
}
QStringList conditions;
QString firstSrc = fkConditionTables.first();
QString firstCol = fkConditionCols.first();
for (i = 1; i < fkConditionTables.size(); i++)
{
src = fkConditionTables[i];
col = fkConditionCols[i];
conditions << conditionTpl.arg(firstSrc, firstCol, src, col);
}
QString conditionsStr;
if (!conditions.isEmpty()) {
conditionsStr = conditionPrefixTpl.arg(conditions.join(", "));
}
// Current value column (will be 1 for row which matches current cell value)
QString currValueColName = generateUniqueName("curr", usedNames);
QString currValueExpr = currentValue.isNull() ?
currNullValueTpl.arg(firstSrcCol, currValueColName) :
currValueTpl.arg(firstSrcCol, valueToSqlLiteral(currentValue), currValueColName);
return sql.arg(
selectedCols.join(", "),
fkConditionTables.join(", "),
conditionsStr,
currValueExpr
);
}
QString FkComboBox::resolveImplicitColumn(Db* db,
SqlQueryModelColumn *columnModel,
SqlQueryModelColumn::ConstraintFk*& fk,
QHash<QString, int> &implicitFkColumnIdxByTable,
QHash<QString, QStringList> &implicitFkColumnsByTable)
{
QString lowerTabName = fk->foreignTable.toLower();
QStringList implicitCols = implicitFkColumnsByTable[lowerTabName];
if (implicitCols.isEmpty())
{
SchemaResolver resolver(db);
implicitCols = resolver.getTablePrimaryKeyColumns(fk->foreignTable);
if (implicitCols.isEmpty())
{
qCritical() << "Unable to resolve implicit FK columns for FK on:" << columnModel->table
<< "." << columnModel->column
<< "and FK to table" << fk->foreignTable;
return QString();
}
else
{
implicitFkColumnsByTable[lowerTabName] = implicitCols;
}
}
int implicitIdx = implicitFkColumnIdxByTable[lowerTabName];
if (implicitIdx >= implicitCols.size())
{
qCritical() << "Unable to resolve implicit FK columns for FK on:" << columnModel->table
<< "." << columnModel->column
<< "and FK to table" << fk->foreignTable
<< "because implicitIdx =" << implicitIdx
<< "which is greater or equal to total implicit columns:" << implicitCols;
return QString();
}
implicitFkColumnIdxByTable[lowerTabName] = implicitIdx + 1;
return implicitCols[implicitIdx];
}
void FkComboBox::init(Db* db, SqlQueryModelColumn* columnModel)
{
this->columnModel = columnModel;
comboModel->setDb(db);
}
void FkComboBox::setValue(const QVariant& value)
{
bool doExecQuery = (sourceValue != value || comboModel->getQuery().isNull());
sourceValue = value;
setCurrentText(value.toString());
if (doExecQuery)
{
comboModel->setQuery(getSql());
if (!comboModel->getQuery().isNull())
comboModel->executeQuery();
}
}
QVariant FkComboBox::getValue(bool* manualValueUsed, bool* ok) const
{
manualValueUsed && (*manualValueUsed = false);
ok && (*ok = true);
SqlQueryModel* cbModel = dynamic_cast<SqlQueryModel*>(model());
if (cbModel->isExecutionInProgress() || !cbModel->isAllDataLoaded())
{
ok && (*ok = false);
return QVariant();
}
int idx = currentIndex();
QModelIndex cbCol0Index = cbModel->index(idx, 0);
QString cbText = currentText();
bool customValue = false;
if (currentIndex() > -1 && !cbModel->itemFromIndex(cbCol0Index))
{
// Inserted QStandardItem by QComboBox, meaning custom value (out of dropdown model)
// With Qt 5.15 (maybe earlier) QComboBox started inserting QStandardItems and setting them as currentIndex.
// Here we're extracting this inserted value and remembering this is the custom value.
cbText = cbModel->data(cbCol0Index).toString();
customValue = true;
}
// Regardless if its preselected value or custom value, we need to honor empty=null setting
if (CFG_UI.General.KeepNullWhenEmptyValue.get() && sourceValue.isNull() && cbText.isEmpty())
{
ok && (*ok = false);
return QVariant();
}
// Out of index? So it's custom value. Set it and it's done.
// If we deal with custom value inserted as item, we also just set it and that's it.
if (idx < 0 || idx >= cbModel->rowCount() || customValue)
{
manualValueUsed && (*manualValueUsed = true);
return cbText;
}
// Otherwise we will have at least 2 columns. 1st column is hidden and is meta-column holding 1/0 (1 for value matching current cell value)
QList<SqlQueryItem *> row = cbModel->getRow(idx);
if (row.size() < 2 || !row[1])
{
// This happens when inexisting value is confirmed with "Enter" key,
// and rowCount() is apparently incremented, but items not yet.
// Very likely this was addressed in recent Qt versions (5.15 or a bit earlier)
// which resulted in value insertion and the "customValue" flag above in this method.
qCritical() << "Confirmed FK edition, but there is no combo item in the row for index" << idx << ", CB row count is" << cbModel->rowCount();
manualValueUsed && (*manualValueUsed = true);
return cbText;
}
return row[1]->getValue();
}
void FkComboBox::init()
{
setEditable(true);
// Prepare combo dropdown view.
comboView = new SqlQueryView();
comboView->setSimpleBrowserMode(true);
comboView->setMaximumWidth(QGuiApplication::primaryScreen()->size().width());
connect(comboView->horizontalHeader(), &QHeaderView::sectionResized, this, [this/*, comboView, model*/](int, int, int)
{
// This line is supposed to check if the source SqlQueryModel has already finished loading,
// before updating combo geometry, but I'm not sure at the moment, why & how would that ever need to be checked.
// Leaving it here for now, in case some bug appears related to this.
// if (!model->isAllDataLoaded())
// return;
updateComboViewGeometry(false);
});
comboModel = new SqlQueryModel(comboView);
comboModel->setView(comboView);
// When execution is done, update combo.
connect(comboModel, SIGNAL(aboutToLoadResults()), this, SLOT(fkDataAboutToLoad()));
connect(comboModel, SIGNAL(executionSuccessful()), this, SLOT(fkDataReady()));
connect(comboModel, SIGNAL(executionFailed(QString)), this, SLOT(fkDataFailed(QString)));
connect(this, SIGNAL(currentTextChanged(QString)), this, SLOT(notifyValueModified()));
// Setup combo, model, etc.
setModel(comboModel);
setView(comboView);
setModelColumn(1);
view()->viewport()->installEventFilter(new FkComboShowFilter(this));
view()->verticalScrollBar()->installEventFilter(new FkComboShowFilter(this));
comboModel->setHardRowLimit(MAX_ROWS_FOR_FK);
comboModel->setCellDataLengthLimit(FK_CELL_LENGTH_LIMIT);
comboModel->setAsyncMode(true);
comboView->verticalHeader()->setVisible(false);
comboView->horizontalHeader()->setVisible(true);
comboView->setSelectionMode(QAbstractItemView::SingleSelection);
comboView->setSelectionBehavior(QAbstractItemView::SelectRows);
connect(completer(), QOverload<const QString &>::of(&QCompleter::highlighted),
[=](const QString &value)
{
// #5083 In case of case-sensitive mismatch, we need to sync case, so that next/prev navigation with keybord works correctly.
setCurrentText(value.left(currentText().length()));
});
}
void FkComboBox::updateComboViewGeometry(bool initial) const
{
int wd = getFkViewHeaderWidth(true);
comboView->setMinimumWidth(qMin(qMax(dropDownViewMinWidth, wd), comboView->maximumWidth()));
if (initial && wd < comboView->minimumWidth())
{
// First time, upon showing up
int currentSize = comboView->horizontalHeader()->sectionSize(1);
int gap = comboView->minimumWidth() - wd;
comboView->horizontalHeader()->resizeSection(1, currentSize + gap);
}
QWidget* container = comboView->parentWidget();
if (container->width() > comboView->minimumWidth())
{
container->setMaximumWidth(comboView->minimumWidth());
container->resize(comboView->minimumWidth(), container->height());
}
}
void FkComboBox::updateCurrentItemIndex(const QString& value)
{
QModelIndex startIdx = comboModel->index(0, modelColumn());
QModelIndex endIdx = comboModel->index(comboModel->rowCount() - 1, modelColumn());
QModelIndexList idxList = comboModel->findIndexes(startIdx, endIdx, SqlQueryItem::DataRole::VALUE, value.isNull() ? currentText() : value, 1, true);
if (idxList.size() > 0)
{
setCurrentIndex(idxList.first().row());
view()->selectionModel()->setCurrentIndex(idxList.first(), QItemSelectionModel::SelectCurrent);
}
}
int FkComboBox::getFkViewHeaderWidth(bool includeScrollBar) const
{
int wd = comboView->horizontalHeader()->length();
if (includeScrollBar && comboView->verticalScrollBar()->isVisible())
wd += comboView->verticalScrollBar()->width();
return wd;
}
void FkComboBox::fkDataAboutToLoad()
{
beforeLoadValue = currentText();
// Not editable combo needs to keep track of initially pre-loading value by using source value
if (!isEditable() && beforeLoadValue.isNull() && !sourceValue.isNull())
beforeLoadValue = sourceValue.toString();
}
void FkComboBox::fkDataReady()
{
disableValueChangeNotifications = true;
comboView->horizontalHeader()->setSectionHidden(0, true);
comboView->resizeColumnsToContents();
comboView->resizeRowsToContents();
updateComboViewGeometry(true);
if (comboModel->rowCount() > 0)
{
QModelIndex startIdx = comboModel->index(0, modelColumn());
QModelIndex endIdx = comboModel->index(comboModel->rowCount() - 1, modelColumn());
QModelIndexList idxList = comboModel->findIndexes(startIdx, endIdx, SqlQueryItem::DataRole::VALUE, beforeLoadValue, 1, true);
if (idxList.size() > 0)
{
setCurrentIndex(idxList.first().row());
}
else
{
setCurrentIndex(-1);
setEditText(beforeLoadValue);
}
}
else
{
setEditText(beforeLoadValue);
}
disableValueChangeNotifications = false;
}
void FkComboBox::fkDataFailed(const QString& errorText)
{
notifyWarn(tr("Cannot edit this cell. Details: %1").arg(errorText));
}
void FkComboBox::notifyValueModified()
{
if (disableValueChangeNotifications || !comboModel->isAllDataLoaded())
return;
oldValue = currentText();
disableValueChangeNotifications = true;
updateCurrentItemIndex();
disableValueChangeNotifications = false;
emit valueModified();
}
FkComboBox::FkComboShowFilter::FkComboShowFilter(FkComboBox* parentCombo)
: QObject(parentCombo)
{
}
bool FkComboBox::FkComboShowFilter::eventFilter(QObject* obj, QEvent* event)
{
UNUSED(obj);
if (event->type() == QEvent::Show)
dynamic_cast<FkComboBox*>(parent())->updateComboViewGeometry(true);
return false;
}
QString FkComboBox::getSql() const
{
if (columnModel == nullptr) {
qWarning() << "Called FkComboBox::getSqlForFkEditor() without column model defined. Tried to setValue() before init()?";
return QString();
}
return getSqlForFkEditor(comboModel->getDb(), columnModel, sourceValue);
}
qlonglong FkComboBox::getRowCountForFkEditor(Db* db, const QString& query, bool* isError)
{
static_qstring(tpl, "SELECT count(*) FROM (%1)");
QString sql = tpl.arg(query);
SqlQueryPtr result = db->exec(sql);
if (isError)
*isError = result->isError();
return result->getSingleCell().toLongLong();
}
|