KissCount/src/view/grid/TabDelegate.cpp

82 lines
2.5 KiB
C++
Raw Normal View History

/*
2012-02-01 11:02:54 +01:00
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 <http://www.gnu.org/licenses/>.
*/
#include <QtGui>
#include <QString>
2017-10-15 11:36:27 +02:00
#include <QtWidgets>
#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<QLineEdit *>(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<QLineEdit *>(editor);
2017-10-15 11:36:27 +02:00
QString s = index.data().toString();
line->setText(s.trimmed());
}
2012-03-11 20:40:07 +01:00
/*
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<QLineEdit *>(_grid->cellWidget(index.row(), index.column()));
QKeyEvent* kevent = dynamic_cast<QKeyEvent *> (event);
line->setText(kevent->text());
_completer->setCompletionPrefix(kevent->text());
_completer->complete();
return true;
}
}
return false;
}