예제 #1
0
QVariant KexiComboBoxBase::value()
{
    KexiTableViewData *relData = column() ? column()->relatedData() : 0;
    KexiDB::LookupFieldSchema *lookupFieldSchema = 0;
    if (relData) {
        if (m_internalEditorValueChanged) {
            //we've user-entered text: look for id
//TODO: make error if matching text not found?
            int rowToHighlight;
            return valueForString(m_userEnteredValue.toString(), &rowToHighlight, 1, 0, true/*allowNulls*/);
        } else {
            //use 'related table data' model
            KexiDB::RecordData *record = popup() ? popup()->tableView()->selectedItem() : 0;
            return record ? record->at(0) : origValue();
        }
    } else if ((lookupFieldSchema = this->lookupFieldSchema())) {
        if (lookupFieldSchema->boundColumn() == -1)
            return origValue();
        KexiDB::RecordData *record = popup() ? popup()->tableView()->selectedItem() : 0;
        if (/*!record &&*/ m_internalEditorValueChanged && !m_userEnteredValue.toString().isEmpty()) { //
            //try to select a row using the user-entered text
            if (!popup()) {
                QVariant prevUserEnteredValue = m_userEnteredValue;
                createPopup(false);
                m_userEnteredValue = prevUserEnteredValue;
            }
            record = selectItemForEnteredValueInLookupTable(m_userEnteredValue);
        }
        return record ? record->at(lookupFieldSchema->boundColumn()) : QVariant();
    } else if (popup()) {
        //use 'enum hints' model
        const int row = popup()->tableView()->currentRow();
        if (row >= 0)
            return QVariant(row);
    }

    if (valueFromInternalEditor().toString().isEmpty())
        return QVariant();
    /*! \todo don't return just 1st row, but use autocompletion feature
          and: show message box if entered text does not match! */
    return origValue(); //unchanged
}
bool KexiComboBoxTableEdit::handleKeyPress(QKeyEvent *ke, bool editorActive)
{
    const int k = ke->key();
    if ((ke->modifiers() == Qt::NoButton && k == Qt::Key_F4)
            || (ke->modifiers() == Qt::AltButton && k == Qt::Key_Down)) {
        //show popup
        slotButtonClicked();
        return true;
    } else if (editorActive) {
        const bool enterPressed = k == Qt::Key_Enter || k == Qt::Key_Return;
        if (enterPressed && m_internalEditorValueChanged) {
            createPopup(false);
            selectItemForEnteredValueInLookupTable(m_userEnteredValue);
            return false;
        }

        return handleKeyPressForPopup(ke);
    }

    return false;
}