wfview/tablewidget.cpp

158 wiersze
4.8 KiB
C++
Czysty Zwykły widok Historia

2023-04-27 18:56:25 +00:00
#include <QDebug>
#include "logcategories.h"
2023-04-19 13:51:23 +00:00
#include "tablewidget.h"
tableWidget::tableWidget(QWidget *parent): QTableWidget(parent)
{
}
void tableWidget::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::RightButton && editingEnabled)
2023-04-19 13:51:23 +00:00
{
2023-04-23 20:29:44 +00:00
QMenu menu;
QAction *add= menu.addAction("Add Item");
2023-04-29 14:27:25 +00:00
QAction *insert= menu.addAction("Insert Item");
QAction *clone= menu.addAction("Clone Item");
2023-04-23 20:29:44 +00:00
QAction *del = menu.addAction("Delete Item");
2023-04-27 18:56:25 +00:00
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
QAction *selectedAction = menu.exec(event->globalPos());
#else
2023-04-19 13:51:23 +00:00
QAction *selectedAction = menu.exec(event->globalPosition().toPoint());
2023-04-27 18:56:25 +00:00
#endif
2023-04-19 13:51:23 +00:00
2023-04-29 14:27:25 +00:00
if(selectedAction == add)
{
2023-05-02 20:18:41 +00:00
int row=this->rowCount();
2023-04-29 14:27:25 +00:00
this->insertRow(this->rowCount());
2023-05-02 20:18:41 +00:00
emit rowAdded(row);
2023-04-29 14:27:25 +00:00
}
else if(selectedAction == insert)
2023-04-23 20:29:44 +00:00
{
2023-05-02 20:18:41 +00:00
int row=this->currentRow();
2023-04-23 20:29:44 +00:00
this->insertRow(this->currentRow());
2023-05-02 20:18:41 +00:00
emit rowAdded(row);
2023-04-23 20:29:44 +00:00
}
2023-04-29 14:27:25 +00:00
else if( selectedAction == clone )
2023-04-19 13:51:23 +00:00
{
2023-05-25 22:17:54 +00:00
int row=this->currentRow(); // This will be the new row with the old one as row+1
this->insertRow(this->currentRow());
2023-04-29 14:27:25 +00:00
for (int i=0;i<this->columnCount();i++)
{
2023-05-25 22:17:54 +00:00
if (this->item(row+1,i) != NULL && dynamic_cast<QComboBox*>(this->item(row+1,i)) == nullptr) // Don't try to copy checkbox
this->model()->setData(this->model()->index(row,i),this->item(row+1,i)->text());
2023-04-29 14:27:25 +00:00
}
2023-05-25 22:17:54 +00:00
emit rowAdded(row);
2023-04-19 13:51:23 +00:00
}
else if( selectedAction == del )
{
2023-05-01 20:56:52 +00:00
emit rowDeleted((this->item(this->currentRow(),1) == NULL) ? 0 : this->item(this->currentRow(),1)->text().toUInt());
2023-04-19 13:51:23 +00:00
this->removeRow(this->currentRow());
}
}
}
2023-05-09 11:11:38 +00:00
tableEditor::tableEditor(QString validExp, QObject *parent)
2023-05-02 20:18:41 +00:00
: QItemDelegate(parent), validExp(validExp)
{
}
QWidget* tableEditor::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
Q_UNUSED(index)
Q_UNUSED(option)
edit = new QLineEdit(parent);
2023-05-09 11:11:38 +00:00
if (!validExp.isEmpty())
2023-05-02 20:18:41 +00:00
{
2023-05-09 11:11:38 +00:00
edit->setInputMask(validExp);
2023-05-02 20:18:41 +00:00
}
edit->setFrame(false);
return edit ;
}
2023-04-23 20:29:44 +00:00
tableCombobox::tableCombobox(QAbstractItemModel* model, bool sort, QObject *parent)
2023-04-19 13:51:23 +00:00
: QItemDelegate(parent), modelData(model)
{
2023-04-23 20:29:44 +00:00
if (sort)
modelData->sort(0);
2023-04-19 13:51:23 +00:00
}
QWidget* tableCombobox::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
2023-04-23 20:29:44 +00:00
Q_UNUSED(index)
Q_UNUSED(option)
2023-05-01 20:56:52 +00:00
combo = new QComboBox(parent);
QObject::connect(combo,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));
combo->blockSignals(true);
combo->setModel(modelData);
combo->blockSignals(false);
return combo;
2023-04-19 13:51:23 +00:00
}
void tableCombobox::setEditorData(QWidget *editor, const QModelIndex &index) const {
// update model widget
2023-05-01 20:56:52 +00:00
Q_UNUSED(editor)
combo->blockSignals(true);
QString text = index.model()->data( index, Qt::DisplayRole ).toString();
combo->setCurrentIndex(combo->findText(text,Qt::MatchFixedString));
2023-05-01 20:56:52 +00:00
combo->blockSignals(false);
2023-04-19 13:51:23 +00:00
}
void tableCombobox::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
2023-05-03 12:52:15 +00:00
Q_UNUSED(editor)
2023-05-01 20:56:52 +00:00
model->setData( index, combo->currentText() );
2023-04-19 13:51:23 +00:00
}
void tableCombobox::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
2023-04-23 20:29:44 +00:00
Q_UNUSED(index)
2023-04-19 13:51:23 +00:00
editor->setGeometry(option.rect);
}
2023-05-01 20:56:52 +00:00
void tableCombobox::setData(int val)
{
Q_UNUSED(val)
if (combo != Q_NULLPTR) {
emit commitData(combo);
}
}
2023-04-19 13:51:23 +00:00
tableCheckbox::tableCheckbox(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget* tableCheckbox::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
2023-04-23 20:29:44 +00:00
Q_UNUSED(index)
Q_UNUSED(option)
2023-04-19 13:51:23 +00:00
QCheckBox* checkBox = new QCheckBox(parent);
return checkBox;
}
void tableCheckbox::setEditorData(QWidget *editor, const QModelIndex &index) const {
// update model widget
bool value = index.model()->data(index, Qt::EditRole).toBool();
qDebug() << "Value:" << value;
QCheckBox* checkBox = static_cast<QCheckBox*>(editor);
checkBox->setEnabled(value);
}
void tableCheckbox::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
// store edited model data to model
QCheckBox* checkBox = static_cast<QCheckBox*>(editor);
bool value = checkBox->isChecked();
2023-05-01 20:56:52 +00:00
model->setData(index, value, Qt::EditRole);
2023-04-19 13:51:23 +00:00
}
void tableCheckbox::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
2023-04-23 20:29:44 +00:00
Q_UNUSED(index)
2023-04-19 13:51:23 +00:00
editor->setGeometry(option.rect);
}