Fixes the Combobox behavior on the inline-edit

This patch fixes the combobox behavior on the inline edit
to what it was ( well, similar to ) the GTK version,
up and arrow keys will walk you to the list of choices,
and it will update the other data as soon as you walks over it

one drawback is that you cant ( for now, since I do have a
very big headache at the moment ) cancel, since the cancel
will just forgets the item and do not call 'setData' on the
model, but we already called it while walking on the list.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-07-16 19:13:58 -03:00
parent c5a0c4e0d7
commit 1a0a4b7e08
3 changed files with 43 additions and 23 deletions

View file

@ -67,7 +67,13 @@ void ComboBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index)
c->setEditText(data); c->setEditText(data);
} }
QComboBox *comboEditor; struct CurrSelected{
QComboBox *comboEditor;
int currRow;
QString activeText;
QAbstractItemModel *model;
} currCombo;
QWidget* ComboBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const QWidget* ComboBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{ {
QComboBox *comboDelegate = new QComboBox(parent); QComboBox *comboDelegate = new QComboBox(parent);
@ -77,19 +83,26 @@ QWidget* ComboBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewI
comboDelegate->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive); comboDelegate->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive);
comboDelegate->completer()->setCompletionMode(QCompleter::PopupCompletion); comboDelegate->completer()->setCompletionMode(QCompleter::PopupCompletion);
comboDelegate->lineEdit()->installEventFilter( const_cast<QObject*>(qobject_cast<const QObject*>(this))); comboDelegate->lineEdit()->installEventFilter( const_cast<QObject*>(qobject_cast<const QObject*>(this)));
comboEditor = comboDelegate; connect(comboDelegate, SIGNAL(highlighted(QString)), this, SLOT(testActivation(QString)));
currCombo.comboEditor = comboDelegate;
currCombo.currRow = index.row();
currCombo.model = const_cast<QAbstractItemModel*>(index.model());
return comboDelegate; return comboDelegate;
} }
void ComboBoxDelegate::testActivation(const QString& s)
{
currCombo.activeText = s;
setModelData(currCombo.comboEditor, currCombo.model, QModelIndex());
}
bool ComboBoxDelegate::eventFilter(QObject* object, QEvent* event) bool ComboBoxDelegate::eventFilter(QObject* object, QEvent* event)
{ {
// Reacts on Key_UP and Key_DOWN to show the QComboBox - list of choices. // Reacts on Key_UP and Key_DOWN to show the QComboBox - list of choices.
if (event->type() == QEvent::KeyPress){ if (event->type() == QEvent::KeyPress){
QKeyEvent *ev = static_cast<QKeyEvent*>(event); QKeyEvent *ev = static_cast<QKeyEvent*>(event);
if(ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down){ if(ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down){
QString curr = comboEditor->currentText(); currCombo.comboEditor->showPopup();
comboEditor->showPopup();
return true;
} }
} }
@ -106,18 +119,16 @@ void ComboBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionV
editor->setGeometry(defaultRect); editor->setGeometry(defaultRect);
} }
void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const
{ {
QComboBox *c = qobject_cast<QComboBox*>(editor); CylindersModel *mymodel = qobject_cast<CylindersModel *>(currCombo.model);
CylindersModel *mymodel = qobject_cast<CylindersModel *>(model);
TankInfoModel *tanks = TankInfoModel::instance(); TankInfoModel *tanks = TankInfoModel::instance();
QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, c->currentText()); QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, currCombo.activeText);
int row; int row;
if (matches.isEmpty()) { if (matches.isEmpty()) {
// we need to add this // we need to add this
tanks->insertRows(tanks->rowCount(), 1); tanks->insertRows(tanks->rowCount(), 1);
tanks->setData(tanks->index(tanks->rowCount() -1, 0), c->currentText()); tanks->setData(tanks->index(tanks->rowCount() -1, 0), currCombo.activeText);
row = tanks->rowCount() - 1; row = tanks->rowCount() - 1;
} else { } else {
row = matches.first().row(); row = matches.first().row();
@ -125,9 +136,9 @@ void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
int tankSize = tanks->data(tanks->index(row, TankInfoModel::ML)).toInt(); int tankSize = tanks->data(tanks->index(row, TankInfoModel::ML)).toInt();
int tankPressure = tanks->data(tanks->index(row, TankInfoModel::BAR)).toInt(); int tankPressure = tanks->data(tanks->index(row, TankInfoModel::BAR)).toInt();
mymodel->setData(model->index(thisindex.row(), CylindersModel::TYPE), c->currentText(), Qt::EditRole); mymodel->setData(model->index(currCombo.currRow, CylindersModel::TYPE), currCombo.activeText, Qt::EditRole);
mymodel->passInData(model->index(thisindex.row(), CylindersModel::WORKINGPRESS), tankPressure); mymodel->passInData(model->index(currCombo.currRow, CylindersModel::WORKINGPRESS), tankPressure);
mymodel->passInData(model->index(thisindex.row(), CylindersModel::SIZE), tankSize); mymodel->passInData(model->index(currCombo.currRow, CylindersModel::SIZE), tankSize);
} }
TankInfoDelegate::TankInfoDelegate(QObject* parent): ComboBoxDelegate(TankInfoModel::instance(), parent) TankInfoDelegate::TankInfoDelegate(QObject* parent): ComboBoxDelegate(TankInfoModel::instance(), parent)
@ -136,23 +147,23 @@ TankInfoDelegate::TankInfoDelegate(QObject* parent): ComboBoxDelegate(TankInfoMo
void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const
{ {
QComboBox *c = qobject_cast<QComboBox*>(editor); WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
WeightModel *mymodel = qobject_cast<WeightModel *>(model);
WSInfoModel *wsim = WSInfoModel::instance(); WSInfoModel *wsim = WSInfoModel::instance();
QModelIndexList matches = wsim->match(wsim->index(0,0), Qt::DisplayRole, c->currentText()); QModelIndexList matches = wsim->match(wsim->index(0,0), Qt::DisplayRole, currCombo.activeText);
int row; int row;
if (matches.isEmpty()) { if (matches.isEmpty()) {
// we need to add this puppy // we need to add this puppy
wsim->insertRows(wsim->rowCount(), 1); wsim->insertRows(wsim->rowCount(), 1);
wsim->setData(wsim->index(wsim->rowCount() - 1, 0), c->currentText()); wsim->setData(wsim->index(wsim->rowCount() - 1, 0), currCombo.activeText);
row = wsim->rowCount() - 1; row = wsim->rowCount() - 1;
} else { } else {
row = matches.first().row(); row = matches.first().row();
} }
int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt(); int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt();
QVariant v = QString(c->currentText()); QVariant v = QString(currCombo.activeText);
mymodel->setData(model->index(thisindex.row(), WeightModel::TYPE), v, Qt::EditRole); mymodel->setData(model->index(currCombo.currRow, WeightModel::TYPE), v, Qt::EditRole);
mymodel->passInData(model->index(thisindex.row(), WeightModel::WEIGHT), grams); mymodel->passInData(model->index(currCombo.currRow, WeightModel::WEIGHT), grams);
qDebug() << "Fixme, every weigth is 0.0 grams. see:" << grams;
} }
WSInfoDelegate::WSInfoDelegate(QObject* parent): ComboBoxDelegate(WSInfoModel::instance(), parent) WSInfoDelegate::WSInfoDelegate(QObject* parent): ComboBoxDelegate(WSInfoModel::instance(), parent)

View file

@ -22,9 +22,10 @@ public:
virtual void setEditorData(QWidget* editor, const QModelIndex& index) const; virtual void setEditorData(QWidget* editor, const QModelIndex& index) const;
virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const; virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const;
virtual bool eventFilter(QObject* object, QEvent* event); virtual bool eventFilter(QObject* object, QEvent* event);
public slots:
void testActivation(const QString& s);
protected: protected:
QAbstractItemModel *model; QAbstractItemModel *model;
QComboBox *comboBox; // warning - it's null everytime a edit's finished.
}; };
class TankInfoDelegate : public ComboBoxDelegate{ class TankInfoDelegate : public ComboBoxDelegate{

View file

@ -161,12 +161,14 @@ void CylindersModel::passInData(const QModelIndex& index, const QVariant& value)
if (cyl->type.size.mliter != value.toInt()) { if (cyl->type.size.mliter != value.toInt()) {
cyl->type.size.mliter = value.toInt(); cyl->type.size.mliter = value.toInt();
mark_divelist_changed(TRUE); mark_divelist_changed(TRUE);
dataChanged(index, index);
} }
break; break;
case WORKINGPRESS: case WORKINGPRESS:
if (cyl->type.workingpressure.mbar != value.toInt()) { if (cyl->type.workingpressure.mbar != value.toInt()) {
cyl->type.workingpressure.mbar = value.toInt(); cyl->type.workingpressure.mbar = value.toInt();
mark_divelist_changed(TRUE); mark_divelist_changed(TRUE);
dataChanged(index, index);
} }
break; break;
} }
@ -262,7 +264,8 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in
} }
break; break;
} }
return QAbstractItemModel::setData(index, value, role); dataChanged(index, index);
return true;
} }
int CylindersModel::rowCount(const QModelIndex& parent) const int CylindersModel::rowCount(const QModelIndex& parent) const
@ -408,6 +411,7 @@ void WeightModel::passInData(const QModelIndex& index, const QVariant& value)
if (ws->weight.grams != value.toInt()) { if (ws->weight.grams != value.toInt()) {
ws->weight.grams = value.toInt(); ws->weight.grams = value.toInt();
mark_divelist_changed(TRUE); mark_divelist_changed(TRUE);
dataChanged(index, index);
} }
} }
} }
@ -440,7 +444,8 @@ bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int r
} }
break; break;
} }
return QAbstractItemModel::setData(index, value, role); dataChanged(index, index);
return true;
} }
Qt::ItemFlags WeightModel::flags(const QModelIndex& index) const Qt::ItemFlags WeightModel::flags(const QModelIndex& index) const
@ -540,6 +545,7 @@ bool WSInfoModel::setData(const QModelIndex& index, const QVariant& value, int r
info->grams = value.toInt(); info->grams = value.toInt();
break; break;
} }
emit dataChanged(index, index);
return TRUE; return TRUE;
} }
@ -680,6 +686,7 @@ bool TankInfoModel::setData(const QModelIndex& index, const QVariant& value, int
info->bar = value.toInt(); info->bar = value.toInt();
break; break;
} }
emit dataChanged(index, index);
return TRUE; return TRUE;
} }
@ -1275,6 +1282,7 @@ bool DiveComputerModel::setData(const QModelIndex& index, const QVariant& value,
dcWorkingMap.remove(node.model, node); dcWorkingMap.remove(node.model, node);
node.nickName = value.toString(); node.nickName = value.toString();
dcWorkingMap.insert(node.model, node); dcWorkingMap.insert(node.model, node);
emit dataChanged(index, index);
return true; return true;
} }