22 #include <kactioncollection.h>
26 #include <kstandardaction.h>
27 #include <kwindowsystem.h>
29 #include <QApplication>
31 #include <QToolButton>
32 #include <ktextedit.h>
33 #include <QFocusEvent>
35 #include <QItemDelegate>
37 class InlineEditor :
public KTextEdit {
40 setAttribute(Qt::WA_DeleteOnClose);
41 setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
42 setCheckSpellingEnabled(
false);
43 connect(p, SIGNAL(destroyed()), SLOT(close()));
47 virtual void focusOutEvent(QFocusEvent *e) {
48 if (e->reason() == Qt::PopupFocusReason) {
56 virtual void keyPressEvent(QKeyEvent *e) {
57 if (e->key() == Qt::Key_Escape) {
62 KTextEdit::keyPressEvent(e);
65 virtual void contextMenuEvent( QContextMenuEvent *event )
67 QMenu *menu = createStandardContextMenu();
69 popup->exec( event->globalPos() );
72 QPointer<KWMapEditor> _p;
73 QPointer<QMenu> popup;
76 class KWMapEditorDelegate :
public QItemDelegate
79 KWMapEditorDelegate(
KWMapEditor *parent) : QItemDelegate(parent)
83 QWidget *createEditor(
QWidget *parentWidget,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const
85 if (index.column() != 2) {
86 return QItemDelegate::createEditor(parentWidget, option, index);
90 return new InlineEditor(mapEditor);
93 void updateEditorGeometry(
QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const
95 if (dynamic_cast<InlineEditor*>(editor))
98 const QRect geo = mapEditor->visualRect(index);
99 editor->move(mapEditor->mapToGlobal(geo.topLeft()));
100 editor->resize(geo.width() * 2, geo.height() * 3);
104 QItemDelegate::updateEditorGeometry(editor, option, index);
108 void setEditorData(
QWidget *editor,
const QModelIndex &index)
const
110 InlineEditor *e =
dynamic_cast<InlineEditor*
>(editor);
114 e->setText(mapEditor->item(index.row(), index.column())->text());
118 QItemDelegate::setEditorData(editor, index);
122 void setModelData(
QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index)
const
124 InlineEditor *e =
dynamic_cast<InlineEditor*
>(editor);
128 mapEditor->item(index.row(), index.column())->setText(e->toPlainText());
132 QItemDelegate::setModelData(editor, model, index);
139 setItemDelegate(
new KWMapEditorDelegate(
this));
140 _ac =
new KActionCollection(
this);
141 _copyAct = KStandardAction::copy(
this, SLOT(copy()), _ac);
142 connect(
this, SIGNAL(itemChanged(QTableWidgetItem*)),
this, SIGNAL(
dirty()));
143 connect(
this, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(
contextMenu(QPoint)));
144 setSelectionMode(NoSelection);
145 setHorizontalHeaderLabels(QStringList() << QString() << i18n(
"Key") << i18n(
"Value"));
146 setContextMenuPolicy(Qt::CustomContextMenu);
153 while ((row = rowCount()) > _map.count()) {
157 if ((row = rowCount()) < _map.count()) {
158 setRowCount(_map.count());
159 for (
int x = row; x < rowCount(); ++x) {
160 QToolButton *b =
new QToolButton(
this);
161 b->setIcon(KIcon(
"edit-delete"));
162 b->setToolTip(i18n(
"Delete Entry"));
163 connect(b, SIGNAL(clicked()),
this, SLOT(
erase()));
164 setCellWidget(x, 0, b);
165 if (columnWidth(0) != b->sizeHint().width()) {
166 setColumnWidth(0, b->sizeHint().width());
168 setItem(x, 1,
new QTableWidgetItem());
169 setItem(x, 2,
new QTableWidgetItem());
174 for (QMap<QString,QString>::Iterator it = _map.begin(); it != _map.end(); ++it) {
175 item(row, 1)->setText(it.key());
176 item(row, 2)->setText(it.value());
187 const QObject *o = sender();
188 for (
int i = 0; i < rowCount(); i++) {
189 if (cellWidget(i, 0) == o) {
202 for (
int i = 0; i < rowCount(); i++) {
203 _map[item(i, 1)->text()] = item(i, 2)->text();
211 QToolButton *b =
new QToolButton(
this);
212 b->setIcon(KIcon(
"edit-delete"));
213 b->setToolTip(i18n(
"Delete Entry"));
214 connect(b, SIGNAL(clicked()),
this, SLOT(
erase()));
215 setCellWidget(x, 0, b);
216 setItem(x, 1,
new QTableWidgetItem());
217 setItem(x, 2,
new QTableWidgetItem());
218 scrollToItem(item(x, 1));
219 setCurrentCell(x, 1);
230 QTableWidgetItem *twi = itemAt(pos);
231 _contextRow = row(twi);
233 m->addAction( i18n(
"&New Entry" ),
this, SLOT(
addEntry()));
234 m->addAction( _copyAct );
235 m->popup(mapToGlobal(pos));
239 void KWMapEditor::copy() {
240 QTableWidgetItem *twi = item(_contextRow, 2);
242 QApplication::clipboard()->setText(twi->text());
245 #include "kwmapeditor.moc"
void contextMenu(const QPoint &pos)
KWMapEditor(QMap< QString, QString > &map, QWidget *parent=0)