/* Copyright 2010-2012 Grégory Soutadé This file is part of KissCount. KissCount is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. KissCount is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with KissCount. If not, see . */ #include #include #include "TabDelegate.hpp" QWidget * TabDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const { QLineEdit* lineEdit = new QLineEdit(parent); if (_completer) lineEdit->setCompleter(_completer); return lineEdit; } void TabDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QLineEdit *line = qobject_cast(editor); QString value = line->text().trimmed(); if (index.row() < (int) _operations->size() && (*_operations)[index.row()].id && (*_operations)[index.row()].parent) model->setData(index, qVariantFromValue(" " + value)); else model->setData(index, qVariantFromValue(value)); } void TabDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QLineEdit* line = qobject_cast(editor); QString s = qVariantValue(index.data()); line->setText(s.trimmed()); } /* Implement editorEvent due to a "feature" in Qt : If we have a QLineEdit with a completer and we press a key on a non edited cell, editor immediatly returns (it's perhaps a focus problem) */ bool TabDelegate::editorEvent ( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index ) { if (event->type() == QEvent::KeyPress && _completer) { QTableWidgetItem* item = _grid->item(index.row(), index.column()); if (item->text() == "") { _grid->editItem(item); QLineEdit* line = qobject_cast(_grid->cellWidget(index.row(), index.column())); QKeyEvent* kevent = dynamic_cast (event); line->setText(kevent->text()); _completer->setCompletionPrefix(kevent->text()); _completer->complete(); return true; } } return false; }