summaryrefslogtreecommitdiffstats
path: root/tdewallet/kwmapeditor.cpp
blob: 32ff65e5caf28a0c915eeaaf51530e5c5c2a6c1f (plain)
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
/*
   Copyright (C) 2003,2004 George Staikos <staikos@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "kwmapeditor.h"

#include <tdeaction.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <kstdaction.h>
#include <twin.h>

#include <tqapplication.h>
#include <tqclipboard.h>
#include <tqpushbutton.h>
#include <tqtextedit.h>

KWMapEditor::KWMapEditor(TQMap<TQString,TQString>& map, TQWidget *parent, const char *name)
: TQTable(0, 3, parent, name), _map(map) {
	_ac = new TDEActionCollection(this);
	_copyAct = KStdAction::copy(TQT_TQOBJECT(this), TQT_SLOT(copy()), _ac);
	connect(this, TQT_SIGNAL(valueChanged(int,int)), this, TQT_SIGNAL(dirty()));
	connect(this, TQT_SIGNAL(contextMenuRequested(int,int,const TQPoint&)),
		this, TQT_SLOT(contextMenu(int,int,const TQPoint&)));
	setSelectionMode(TQTable::NoSelection);
	horizontalHeader()->setLabel(0, TQString());
	horizontalHeader()->setLabel(1, i18n("Key"));
	horizontalHeader()->setLabel(2, i18n("Value"));
	setColumnWidth(0, 20); // FIXME: this is arbitrary
	reload();
}

void KWMapEditor::reload() {
	unsigned row = 0;

	while ((row = numRows()) > _map.count()) {
		removeRow(row - 1);
	}

	if ((row = numRows()) < _map.count()) {
		insertRows(row, _map.count() - row);
		for (int x = row; x < numRows(); ++x) {
			TQPushButton *b = new TQPushButton("X", this);
			connect(b, TQT_SIGNAL(clicked()), this, TQT_SLOT(erase()));
			setCellWidget(x, 0, b);
		}
	}

	row = 0;
	for (TQMap<TQString,TQString>::Iterator it = _map.begin(); it != _map.end(); ++it) {
		setText(row, 1, it.key());
		setText(row, 2, it.data());
		row++;
	}
}


KWMapEditor::~KWMapEditor() {
}


void KWMapEditor::erase() {
	const TQObject *o = sender();
	for (int i = 0; i < numRows(); i++) {
		if (cellWidget(i, 0) == o) {
			removeRow(i);
			break;
		}
	}

	emit dirty();
}


void KWMapEditor::saveMap() {
	_map.clear();

	for (int i = 0; i < numRows(); i++) {
		_map[text(i, 1)] = text(i, 2);
	}
}


void KWMapEditor::addEntry() {
	int x = numRows();
	insertRows(x, 1);
	TQPushButton *b = new TQPushButton("X", this);
	connect(b, TQT_SIGNAL(clicked()), this, TQT_SLOT(erase()));
	setCellWidget(x, 0, b);
	ensureCellVisible(x, 1);
	setCurrentCell(x, 1);
	emit dirty();
}


void KWMapEditor::emitDirty() {
	emit dirty();
}


void KWMapEditor::contextMenu(int row, int col, const TQPoint& pos) {
	_contextRow = row;
	_contextCol = col;
	TDEPopupMenu *m = new TDEPopupMenu(this);
	m->insertItem(i18n("&New Entry"), this, TQT_SLOT(addEntry()));
	_copyAct->plug(m);
	m->popup(pos);
}


void KWMapEditor::copy() {
	TQApplication::clipboard()->setText(text(_contextRow, 2));
}


class InlineEditor : public TQTextEdit {
	public:
		InlineEditor(KWMapEditor *p, int row, int col) 
		  : TQTextEdit(), _p(p), row(row), col(col) { 
			setWFlags(WStyle_NoBorder | WDestructiveClose); 
			KWin::setType(winId(), NET::Override);
			connect(p, TQT_SIGNAL(destroyed()), TQT_SLOT(close()));
 		}
		virtual ~InlineEditor() { if (!_p) return; _p->setText(row, col, text()); _p->emitDirty(); }

	protected:
		virtual void focusOutEvent(TQFocusEvent* fe) { 
			if (fe->reason() == TQFocusEvent::Popup) {
				TQWidget *focusW = tqApp->focusWidget();
				if (focusW && focusW == popup) {
					return;
				}
			}
			close(); 
		}
		virtual void keyPressEvent(TQKeyEvent *e) {
			if (e->key() == TQt::Key_Escape) {
				e->accept();
				close();
			} else {
				e->ignore();
				TQTextEdit::keyPressEvent(e);
			}
		}
		virtual TQPopupMenu *createPopupMenu(const TQPoint &p) {
			popup = TQTextEdit::createPopupMenu(p);
			return popup;
		}
		TQGuardedPtr<KWMapEditor> _p;
		int row, col;
		TQGuardedPtr<TQPopupMenu> popup;
};


TQWidget *KWMapEditor::beginEdit(int row, int col, bool replace) {
	//kdDebug(2300) << "EDIT COLUMN " << col << endl;
	if (col != 2) {
		return TQTable::beginEdit(row, col, replace);
	}

	TQRect geo = cellGeometry(row, col);
	TQTextEdit *e = new InlineEditor(this, row, col);
	e->setText(text(row, col));
	e->move(mapToGlobal(geo.topLeft()));
	e->resize(geo.width() * 2, geo.height() * 3);
	e->show();
	return e;
}


#include "kwmapeditor.moc"