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
|
/* vi: set sw=4 ts=4:
*
* Copyright (C) 2010 Christian Hohnstaedt.
*
* All rights reserved.
*/
#ifndef __KVVIEW_H
#define __KVVIEW_H
#include <QAbstractItemModel>
#include <QTableView>
#include <QComboBox>
#include <QItemDelegate>
#include <QLabel>
#include "lib/base.h"
class kvView;
class kvDelegate : public QItemDelegate
{
Q_OBJECT
public:
kvDelegate(QObject *parent)
:QItemDelegate(parent)
{
}
virtual void addKey(QString &) {};
};
class comboDelegate : public kvDelegate
{
Q_OBJECT
QStringList keys{};
public:
comboDelegate(QStringList k, QObject *parent = nullptr)
:kvDelegate(parent), keys(k)
{
}
void addKey(QString &key)
{
if (!key.isEmpty() && (keys.count() == 0 || !keys.contains(key)))
keys << key;
}
QWidget *createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
(void)index;
editor->setGeometry(option.rect);
}
};
class lineDelegate : public kvDelegate
{
Q_OBJECT
QLabel *infoLabel{};
public:
lineDelegate(QLabel *lbl = nullptr, QObject *parent = nullptr)
: kvDelegate(parent), infoLabel(lbl)
{ }
QWidget *createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &) const
{
editor->setGeometry(option.rect);
}
signals:
void setupLineEdit(const QString &s, QLineEdit *l) const;
};
class kvmodel: public QAbstractTableModel
{
QStringList items{};
QStringList header{};
int myCols{};
public:
kvmodel(const QStringList &heads);
QStringList getRow(int i);
void addRow(const QStringList &newrow);
Qt::ItemFlags flags(const QModelIndex &index) const
{
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const
{
(void)parent;
return createIndex(row, column, row*myCols +column);
}
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role) const;
bool insertRows(int row, int count,
const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count,
const QModelIndex & parent = QModelIndex());
int rowCount(const QModelIndex &parent) const
{
(void)parent;
return items.count()/myCols;
}
int columnCount(const QModelIndex &parent) const
{
(void)parent;
return myCols;
}
bool setData(const QModelIndex &index, const QVariant &value, int role);
void moveRow(int oldi, int newi);
};
class kvView: public QTableView
{
Q_OBJECT
QStringList keys0{};
QLabel *infoLabel{};
public:
kvView(QWidget *parent = nullptr);
~kvView();
int rowCount()
{
return model()->rowCount(QModelIndex());
}
QStringList getRow(int i)
{
return static_cast<kvmodel*>(model())->getRow(i);
}
void addRow(const QStringList &newrow);
void deleteAllRows()
{
if (model()->rowCount() > 0)
model()->removeRows(0, rowCount(), QModelIndex());
}
void setInfoLabel(QLabel *lbl, int col = 1)
{
infoLabel = lbl;
initLineDelegate(col);
}
void initLineDelegate(int col = 1);
void setKeys(const QStringList &k, int col = 0);
void initCols(QStringList &heads);
private slots:
void moveRow(int logical, int oldi, int newi);
void editorExited();
public slots:
void addKvRow();
void deleteCurrentRow();
};
#endif
|