File: tablewidget.cpp

package info (click to toggle)
wfview 2.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,256 kB
  • sloc: cpp: 43,386; ansic: 3,196; sh: 32; xml: 29; makefile: 11
file content (189 lines) | stat: -rw-r--r-- 6,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <QDebug>
#include "logcategories.h"

#include "tablewidget.h"



tableWidget::tableWidget(QWidget *parent): QTableWidget(parent)
{
    menuActions.append(new QAction("Add Item"));
    menuActions.append(new QAction("Insert Item"));
    menuActions.append(new QAction("Clone Item"));
    menuActions.append(new QAction("Delete Item"));
}

void tableWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::RightButton && editingEnabled)
    {
        QMenu menu;

#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
        QAction *selectedAction = menu.exec(menuActions,event->globalPos());
#else
        QAction *selectedAction = menu.exec(menuActions,event->globalPosition().toPoint());
#endif


        if(selectedAction == menuActions[0])
        {
            int row=this->rowCount();
            this->insertRow(this->rowCount());
            emit rowAdded(row);
        }
        else if(selectedAction == menuActions[1])
        {
            int row=this->currentRow();
            this->insertRow(this->currentRow());
            emit rowAdded(row);
        }
        else if( selectedAction == menuActions[2] )
        {
            this->setSortingEnabled(false);
            int row=this->currentRow(); // This will be the new row with the old one as row+1
            this->insertRow(this->currentRow());
            for (int i=0;i<this->columnCount();i++)
            {
                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());
            }
            emit rowAdded(row);
            this->setSortingEnabled(true);
        }
        else if( selectedAction == menuActions[3] )
        {
            emit rowDeleted(this->currentRow());
            emit rowValDeleted((this->item(this->currentRow(),1) == NULL) ? 0 : this->item(this->currentRow(),1)->text().toUInt());
            this->removeRow(this->currentRow());
        } else
        {
            emit menuAction(selectedAction,this->currentRow());
        }
    }
}


tableEditor::tableEditor(QString validExp, QObject *parent)
    : 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);
    if (!validExp.isEmpty())
    {
        edit->setInputMask(validExp);
    }
    edit->setFrame(false);
    return edit ;
}


tableCombobox::tableCombobox(QAbstractItemModel* model, bool sort, QObject *parent)
    : QItemDelegate(parent), modelData(model)
{
    if (sort)
        modelData->sort(0);
}

QWidget* tableCombobox::createEditor(QWidget *parent, const   QStyleOptionViewItem &option, const QModelIndex &index) const {
    Q_UNUSED(index)
    Q_UNUSED(option)
    combo = new QComboBox(parent);

    QObject::connect(combo,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));
    combo->blockSignals(true);
    combo->setModel(modelData);

    combo->setFocusPolicy(Qt::StrongFocus);
    combo->setEditable(true);
    combo->setInsertPolicy(QComboBox::NoInsert);

    QSortFilterProxyModel* filter = new QSortFilterProxyModel(combo);
    filter->setFilterCaseSensitivity(Qt::CaseInsensitive);
    filter->setSourceModel(combo->model());

    QCompleter* completer = new QCompleter(filter,combo);
    completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
    combo->setCompleter(completer);
    QObject::connect(combo->lineEdit(),SIGNAL(textEdited(QString)),filter,SLOT(setFilterFixedString(QString)));

    comboValidator* valid = new comboValidator(combo);
    combo->lineEdit()->setValidator(valid);

    connect(combo->lineEdit(), &QLineEdit::returnPressed, combo->lineEdit(), [=]() {
        if (combo->findText(combo->lineEdit()->text(), Qt::MatchExactly) < 0)
        {
            combo->lineEdit()->setText(combo->itemText(0)); // Set to first entry in combobox.
        }
    });


    combo->blockSignals(false);
    return combo;
}

void tableCombobox::setEditorData(QWidget *editor, const QModelIndex &index) const {
    // update model widget
    Q_UNUSED(editor)
    combo->blockSignals(true);
    QString text = index.model()->data( index, Qt::DisplayRole ).toString();
    combo->setCurrentIndex(combo->findText(text,Qt::MatchFixedString));
    combo->blockSignals(false);
}

void tableCombobox::setModelData(QWidget *editor, QAbstractItemModel *model,   const QModelIndex &index) const {
    Q_UNUSED(editor)
    model->setData( index, combo->currentText() );
}

void tableCombobox::updateEditorGeometry(QWidget *editor, const     QStyleOptionViewItem &option, const QModelIndex &index) const {
    Q_UNUSED(index)
    editor->setGeometry(option.rect);
}

void tableCombobox::setData(int val)
{
    Q_UNUSED(val)

    if (combo != Q_NULLPTR) {
        emit commitData(combo);
    }
}


tableCheckbox::tableCheckbox(QObject *parent)
    : QItemDelegate(parent)
{

}

QWidget* tableCheckbox::createEditor(QWidget *parent, const   QStyleOptionViewItem &option, const QModelIndex &index) const {
    Q_UNUSED(index)
    Q_UNUSED(option)
    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();
    model->setData(index, value, Qt::EditRole);    
}

void tableCheckbox::updateEditorGeometry(QWidget *editor, const     QStyleOptionViewItem &option, const QModelIndex &index) const {
    Q_UNUSED(index)
    editor->setGeometry(option.rect);
}