1 /* vi: set sw=4 ts=4: 2 * 3 * Copyright (C) 2010 Christian Hohnstaedt. 4 * 5 * All rights reserved. 6 */ 7 8 #ifndef __KVVIEW_H 9 #define __KVVIEW_H 10 11 #include <QAbstractItemModel> 12 #include <QTableView> 13 #include <QComboBox> 14 #include <QItemDelegate> 15 #include <QLabel> 16 17 #include "lib/base.h" 18 19 class kvView; 20 21 class kvDelegate : public QItemDelegate 22 { 23 public: kvDelegate(QObject * parent)24 kvDelegate(QObject *parent) 25 :QItemDelegate(parent) 26 { 27 } addKey(QString &)28 virtual void addKey(QString &) {}; 29 }; 30 31 class comboDelegate : public kvDelegate 32 { 33 QStringList keys; 34 35 public: 36 comboDelegate(QStringList k, QObject *parent = 0) kvDelegate(parent)37 :kvDelegate(parent) 38 { 39 keys = k; 40 } addKey(QString & key)41 void addKey(QString &key) 42 { 43 if (!key.isEmpty() && (keys.count() == 0 || !keys.contains(key))) 44 keys << key; 45 } 46 QWidget *createEditor(QWidget *parent, 47 const QStyleOptionViewItem &option, 48 const QModelIndex &index) const; 49 void setEditorData(QWidget *editor, const QModelIndex &index) const; 50 void setModelData(QWidget *editor, QAbstractItemModel *model, 51 const QModelIndex &index) const; updateEditorGeometry(QWidget * editor,const QStyleOptionViewItem & option,const QModelIndex & index)52 void updateEditorGeometry(QWidget *editor, 53 const QStyleOptionViewItem &option, 54 const QModelIndex &index) const 55 { 56 (void)index; 57 editor->setGeometry(option.rect); 58 } 59 }; 60 61 class lineDelegate : public kvDelegate 62 { 63 Q_OBJECT 64 65 QLabel *infoLabel; 66 public: 67 lineDelegate(QLabel *lbl = 0, QObject *parent = 0) kvDelegate(parent)68 :kvDelegate(parent) 69 { 70 infoLabel = lbl; 71 } 72 QWidget *createEditor(QWidget *parent, 73 const QStyleOptionViewItem &option, 74 const QModelIndex &index) const; 75 void setEditorData(QWidget *editor, const QModelIndex &index) const; 76 void setModelData(QWidget *editor, QAbstractItemModel *model, 77 const QModelIndex &index) const; updateEditorGeometry(QWidget * editor,const QStyleOptionViewItem & option,const QModelIndex & index)78 void updateEditorGeometry(QWidget *editor, 79 const QStyleOptionViewItem &option, 80 const QModelIndex &index) const 81 { 82 (void)index; 83 editor->setGeometry(option.rect); 84 } 85 signals: 86 void setupLineEdit(const QString &s, QLineEdit *l) const; 87 }; 88 89 90 class kvmodel: public QAbstractTableModel 91 { 92 QStringList items; 93 QStringList header; 94 int myCols; 95 96 public: 97 kvmodel(QStringList &heads); 98 QStringList getRow(int i); 99 void addRow(const QStringList &newrow); flags(const QModelIndex & index)100 Qt::ItemFlags flags(const QModelIndex &index) const 101 { 102 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; 103 } 104 QModelIndex index(int row, int column, 105 const QModelIndex &parent = QModelIndex()) const 106 { 107 (void)parent; 108 return createIndex(row, column, row*myCols +column); 109 } 110 QVariant data(const QModelIndex &index, int role) const; 111 QVariant headerData(int section, Qt::Orientation orientation, 112 int role) const; 113 bool insertRows(int row, int count, 114 const QModelIndex &parent = QModelIndex()); 115 bool removeRows(int row, int count, 116 const QModelIndex & parent = QModelIndex()); rowCount(const QModelIndex & parent)117 int rowCount(const QModelIndex &parent) const 118 { 119 (void)parent; 120 return items.count()/myCols; 121 } columnCount(const QModelIndex & parent)122 int columnCount(const QModelIndex &parent) const 123 { 124 (void)parent; 125 return myCols; 126 } 127 bool setData(const QModelIndex &index, const QVariant &value, int role); 128 void moveRow(int oldi, int newi); 129 }; 130 131 class kvView: public QTableView 132 { 133 Q_OBJECT 134 135 QStringList keys0; 136 QLabel *infoLabel; 137 138 public: 139 kvView(QWidget *parent = 0); 140 ~kvView(); rowCount()141 int rowCount() 142 { 143 return model()->rowCount(QModelIndex()); 144 } getRow(int i)145 QStringList getRow(int i) 146 { 147 return static_cast<kvmodel*>(model())->getRow(i); 148 } 149 void addRow(const QStringList &newrow); deleteAllRows()150 void deleteAllRows() 151 { 152 model()->removeRows(0, rowCount(), QModelIndex()); 153 } 154 void setInfoLabel(QLabel *lbl, int col = 1) 155 { 156 infoLabel = lbl; 157 initLineDelegate(col); 158 } 159 void initLineDelegate(int col = 1); 160 void setKeys(const QStringList &k, int col = 0); 161 void initCols(QStringList &heads); 162 private slots: 163 void moveRow(int logical, int oldi, int newi); 164 void editorExited(); 165 public slots: 166 void addKvRow(); 167 void deleteCurrentRow(); 168 }; 169 170 #endif 171