summaryrefslogtreecommitdiffstats
path: root/kopete/kopete/config/appearance/tooltipeditdialog.cpp
blob: d23fbe7460745cd992e412247b6776cf811b5b78 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
    tooltipeditdialog.cpp  -  Kopete Tooltip Editor

    Copyright (c) 2004 by Stefan Gehn <metz AT gehn.net>
    Kopete    (c) 2004 by the Kopete developers  <kopete-devel@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.                                   *
    *                                                                       *
    *************************************************************************
*/

#include "tooltipeditdialog.h"
#include "tooltipeditwidget.h"

#include "kopetecontactproperty.h"
#include "kopeteglobal.h"
#include "kopeteprefs.h"

#include <tqapplication.h>
#include <tqtoolbutton.h>
#include <tqheader.h>
#include <tqstringlist.h>

#include <kiconloader.h>
#include <tdelistview.h>
#include <tdelocale.h>

class TooltipItem : public TDEListViewItem
{
	public:
		TooltipItem(TDEListView *parent, const TQString& label, const TQString& propertyName)
			: TDEListViewItem(parent, label),
				mPropName(propertyName)
		{
		}

		TooltipItem(TDEListView *parent, TQListViewItem *item, const TQString& label, const TQString& propertyName)
			: TDEListViewItem(parent, item, label),
				mPropName(propertyName)
		{
		}

		TQString propertyName() const { return mPropName; }
	private:
		TQString mPropName;
};



TooltipEditDialog::TooltipEditDialog(TQWidget *parent, const char* name)
	: KDialogBase(parent, name, true, i18n("Tooltip Editor"), Ok|Cancel, Ok, true)
{
	mMainWidget = new TooltipEditWidget(this, "TooltipEditDialog::mMainWidget");
	setMainWidget(mMainWidget);
	mMainWidget->lstUsedItems->header()->hide();
	mMainWidget->lstUnusedItems->header()->hide();
	mMainWidget->lstUsedItems->setSorting( -1 );
	mMainWidget->lstUnusedItems->setSorting( 0 );

	const Kopete::ContactPropertyTmpl::Map propmap(
		Kopete::Global::Properties::self()->templateMap());
	TQStringList usedKeys = KopetePrefs::prefs()->toolTipContents();

    connect(mMainWidget->lstUnusedItems, TQT_SIGNAL(doubleClicked ( TQListViewItem *, const TQPoint &, int )), this, TQT_SLOT(slotAddButton()));
    connect(mMainWidget->lstUsedItems, TQT_SIGNAL(doubleClicked ( TQListViewItem *, const TQPoint &, int )), this, TQT_SLOT(slotRemoveButton()));

	// first fill the "used" list
	TQStringList::Iterator usedIt=usedKeys.end();
	do
	{
		usedIt--;
		// only add if that property key is really known
		if(propmap.contains(*usedIt) && !propmap[*usedIt].isPrivate())
		{
			new TooltipItem(mMainWidget->lstUsedItems,
				propmap[*usedIt].label(), *usedIt);
		}
	} while(usedIt != usedKeys.begin());

	// then iterate over all known properties and insert the remaining ones
	// into the "unused" list
	Kopete::ContactPropertyTmpl::Map::ConstIterator it;
	for(it = propmap.begin(); it != propmap.end(); ++it)
	{
		if((usedKeys.contains(it.key())==0) && (!it.data().isPrivate()))
			new TooltipItem(mMainWidget->lstUnusedItems, it.data().label(), it.key());
	}

	connect(mMainWidget->lstUnusedItems, TQT_SIGNAL(selectionChanged(TQListViewItem *)),
		this, TQT_SLOT(slotUnusedSelected(TQListViewItem *)));
	connect(mMainWidget->lstUsedItems, TQT_SIGNAL(selectionChanged(TQListViewItem *)),
		this, TQT_SLOT(slotUsedSelected(TQListViewItem *)));

	TQIconSet iconSet;
	iconSet = SmallIconSet("go-up");
	mMainWidget->tbUp->setIconSet(iconSet);
	mMainWidget->tbUp->setEnabled(false);
	mMainWidget->tbUp->setAutoRepeat(true);
	connect(mMainWidget->tbUp, TQT_SIGNAL(clicked()), TQT_SLOT(slotUpButton()));

	iconSet = SmallIconSet("go-down");
	mMainWidget->tbDown->setIconSet(iconSet);
	mMainWidget->tbDown->setEnabled(false);
	mMainWidget->tbDown->setAutoRepeat(true);
	connect(mMainWidget->tbDown, TQT_SIGNAL(clicked()), TQT_SLOT(slotDownButton()));

	iconSet = TQApplication::reverseLayout() ? SmallIconSet("back") : SmallIconSet("forward");
	mMainWidget->tbAdd->setIconSet(iconSet);
	mMainWidget->tbAdd->setEnabled(false);
	connect(mMainWidget->tbAdd, TQT_SIGNAL(clicked()), TQT_SLOT(slotAddButton()));

	iconSet = TQApplication::reverseLayout() ? SmallIconSet("forward") : SmallIconSet("back");
	mMainWidget->tbRemove->setIconSet(iconSet);
	mMainWidget->tbRemove->setEnabled(false);
	connect(mMainWidget->tbRemove, TQT_SIGNAL(clicked()), TQT_SLOT(slotRemoveButton()));

	connect(this, TQT_SIGNAL(okClicked()), this, TQT_SLOT(slotOkClicked()));

	resize(TQSize(450, 450));
}

void TooltipEditDialog::slotOkClicked()
{
	TQStringList oldList = KopetePrefs::prefs()->toolTipContents();
	TQStringList newList;
	TQListViewItemIterator it(mMainWidget->lstUsedItems);
	TQString keyname;

	while(it.current())
	{
		keyname = static_cast<TooltipItem *>(it.current())->propertyName();
		newList += keyname;
		kdDebug(14000) << k_funcinfo <<
			"Adding key '" << keyname << "' to tooltip list" << endl;
		++it;
	}

	if(oldList != newList)
	{
		KopetePrefs::prefs()->setToolTipContents(newList);
		emit changed(true);
		kdDebug(14000) << k_funcinfo << "tooltip fields changed, emitting changed()" << endl;
	}
}


void TooltipEditDialog::slotUnusedSelected(TQListViewItem *item)
{
	//mMainWidget->tbRemove->setEnabled(false);
	mMainWidget->tbAdd->setEnabled(item!=0);
}

void TooltipEditDialog::slotUsedSelected(TQListViewItem *item)
{
	mMainWidget->tbRemove->setEnabled(item!=0);
	//mMainWidget->tbAdd->setEnabled(false);
	if (item)
	{
		mMainWidget->tbUp->setEnabled(item->itemAbove() != 0);
		mMainWidget->tbDown->setEnabled(item->itemBelow() != 0);
	}
	else
	{
		mMainWidget->tbUp->setEnabled(false);
		mMainWidget->tbDown->setEnabled(false);
	}
}

void TooltipEditDialog::slotUpButton()
{
	TQListViewItem *item = mMainWidget->lstUsedItems->currentItem();
	TQListViewItem *prev = item->itemAbove();
	if(prev == 0) // we are first item already
		return;

	prev->moveItem(item);
	slotUsedSelected(item);
}

void TooltipEditDialog::slotDownButton()
{
	TQListViewItem *item = mMainWidget->lstUsedItems->currentItem();
	TQListViewItem *next = item->itemBelow();
	if(next == 0) // we are last item already
		return;

	item->moveItem(next);
	slotUsedSelected(item);
}

void TooltipEditDialog::slotAddButton()
{
	TooltipItem *item = static_cast<TooltipItem *>(mMainWidget->lstUnusedItems->currentItem());
	if(!item)
		return;
	//kdDebug(14000) << k_funcinfo << endl;

	// build a new one in the "used" list
	new TooltipItem(mMainWidget->lstUsedItems, item->text(0), item->propertyName());

	// remove the old one from "unused" list
	mMainWidget->lstUnusedItems->takeItem(item);
	delete item;
}

void TooltipEditDialog::slotRemoveButton()
{
	TooltipItem *item = static_cast<TooltipItem *>(mMainWidget->lstUsedItems->currentItem());
	if(!item)
		return;
	//kdDebug(14000) << k_funcinfo << endl;

	// build a new one in the "unused" list
	new TooltipItem(mMainWidget->lstUnusedItems, item->text(0), item->propertyName());

	// remove the old one from "used" list
	mMainWidget->lstUsedItems->takeItem(item);
	delete item;
}

#include "tooltipeditdialog.moc"