summaryrefslogtreecommitdiffstats
path: root/src/kile/symbolview.cpp
blob: 8fdac3752c6a498fbea721f3045ad6a4f209e0b9 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/***************************************************************************
    begin                : Fri Aug 1 2003
    edit		 : Fri April 6 2007
    copyright            : (C) 2003 by Jeroen Wijnhout, 2006 - 2007 by Thomas Braun
    email                : Jeroen.Wijnhout@kdemail.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

/*
dani 2005-11-22
  - add some new symbols 
  - rearranged source

tbraun 2006-07-01
   - added tooltips which show the keys, copied from kfileiconview
   - reorganized the hole thing, more flexible png loading, removing the old big code_array, more groups

tbraun 2007-06-04
    - Send a warning in the logwidget if needed packages are not included for the command
tbraun 2007-06-13
    - Added Most frequently used symbolview, including remembering icons upon restart, removing of least popular item and configurable max item count
*/

#include "symbolview.h"
#include "kileconfig.h"

#include <tqimage.h>
#include <tqstringlist.h>
#include <kimageeffect.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include "kiledebug.h"

#include <kconfig.h>

#include <tqregexp.h>
#include <tqtooltip.h>
#include <tqlabel.h>
#include <tqrect.h>
#include <tqapplication.h>


SymbolView::SymbolView(TQWidget *tqparent, int type, const char *name): KIconView( tqparent, name ),m_toolTip(0L)
{
    setGridX( 36 );
    setGridY( 36);
    setSpacing(5);
    setWordWrapIconText (false);
    setShowToolTips (false);
    setResizeMode( Adjust );
    setHScrollBarMode( AlwaysOff );
    setVScrollBarMode( Auto );
    setAutoArrange(true);
    setSorting(false);
    setItemsMovable(false);
    setArrangement(LeftToRight);
    setAcceptDrops(false);
    initPage(type);
    connect( this, TQT_SIGNAL( onItem( TQIconViewItem * ) ),TQT_SLOT( showToolTip( TQIconViewItem * ) ) );
    connect( this, TQT_SIGNAL( onViewport() ),TQT_SLOT( removeToolTip() ) );
}

SymbolView::~SymbolView()
{
	removeToolTip();
}

void SymbolView::extract(const TQString& key, int& refCnt)
{
	if(!key.isEmpty())
		refCnt = key.section('%',0,0).toInt();
	
	return;
}

void SymbolView::extract(const TQString& key, int& refCnt, TQString &cmd, TQStringList &args, TQStringList &pkgs)
{
	if(key.isEmpty())
		return;
	
	extract(key,refCnt);
	
	TQRegExp rePkgs("(?:\\[(.*)\\])?\\{(.*)\\}");
	
	args.clear();
	pkgs.clear();
	
	cmd = key.section('%',1,1);
	TQString text = key.section('%',2,2);
	
	if( text.find(rePkgs) != -1 )
	{
		args = TQStringList::split(",",rePkgs.cap(1));
		pkgs = TQStringList::split(",",rePkgs.cap(2));
	}
}

void SymbolView::showToolTip( TQIconViewItem *item )
{
	removeToolTip(); 
 
     if ( !item )
     return;
	
	TQString cmd, label;
	TQStringList pkgs, args;
	int refCnt;
	
	extract(item->key(),refCnt,cmd,args,pkgs);
	
	label = i18n("Command: ") + cmd + "\n";
	
	if( pkgs.count() > 0 )
	{
		if(pkgs.count() == 1)
			label += i18n("Package: ");
		else
			label += i18n("Packages: ");
		
		for( uint i = 0; i < pkgs.count() ; i++ )
		{
			if( i < args.count() )
				label = label + "[" + args[i] + "]" + pkgs[i] + "\n";
			else
				label = label + pkgs[i] + "\n";
		}
	}
	
     m_toolTip = new TQLabel(label, 0,"myToolTip",
			  WStyle_StaysOnTop | WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WX11BypassWM );
     m_toolTip->setFrameStyle( TQFrame::Plain | TQFrame::Box );
     m_toolTip->setLineWidth( 1 );
     m_toolTip->tqsetAlignment( AlignLeft | AlignTop );
     m_toolTip->move( TQCursor::pos() + TQPoint( 14, 14 ) );
     m_toolTip->adjustSize();
     TQRect screen = TQApplication::desktop()->screenGeometry(
             TQApplication::desktop()->screenNumber(TQCursor::pos()));
     if (m_toolTip->x()+m_toolTip->width() > screen.right()) {
	     m_toolTip->move(m_toolTip->x()+screen.right()-m_toolTip->x()-m_toolTip->width(), m_toolTip->y());
     }
     if (m_toolTip->y()+m_toolTip->height() > screen.bottom()) {
	     m_toolTip->move(m_toolTip->x(), screen.bottom()-m_toolTip->y()-m_toolTip->height()+m_toolTip->y());
     }
     m_toolTip->setFont( TQToolTip::font() );
     m_toolTip->tqsetPalette( TQToolTip::palette(), true );
     m_toolTip->show();
}

void SymbolView::removeToolTip()
{
    delete m_toolTip;
    m_toolTip = 0;
}

void SymbolView::hideEvent( TQHideEvent *e )
{
    removeToolTip();
    KIconView::hideEvent( e );
}

void SymbolView::initPage(int page)
{
	switch (page)
	{
		case MFUS:
			fillWidget(MFUSprefix);
		break;
			
		case Relation:
			fillWidget("relation");
		break;

		case Operator:
			fillWidget("operators");
		break;
		
		case Arrow:
			fillWidget("arrows");
		break;

		case MiscMath:
			fillWidget("misc-math");
		break;
		
		case MiscText:
 			fillWidget("misc-text");
		break;
		
		case Delimiters:
			fillWidget("delimiters");
		break;
		
		case Greek:
			fillWidget("greek");
		break;
		
		case Special:
			fillWidget("special");
		break;

		case Cyrillic:
			fillWidget("cyrillic");
		break;

		case User:
			fillWidget("user");
		break;

		default:
			kdWarning() << "wrong argument in initPage()" << endl;
		break;
	}
}

void SymbolView::contentsMousePressEvent(TQMouseEvent *e)
{
	KILE_DEBUG() << "===SymbolView::contentsMousePressEvent(TQMouseEvent *e)===" << endl;
	
	TQString code_symbol;
	TQStringList args, pkgs;
	TQIconViewItem *item = NULL;
	int count;
	bool math=false, bracket=false;

	if( (e->button() & Qt::LeftButton) == Qt::LeftButton && ( item = findItem( e->pos() ) ) )
	{
		bracket = (e->state() & TQt::ControlButton) ==  TQt::ControlButton;
		math = (e->state() & TQt::ShiftButton) ==  TQt::ShiftButton;
		
		extract(item->key(),count,code_symbol,args,pkgs);

		if (math == bracket)
			;
		else if(math)
			code_symbol = '$' + code_symbol + '$';
		else if(bracket)
			code_symbol = '{' + code_symbol + '}';
		
		emit(insertText(code_symbol,pkgs));
 		emit(addToList(item));
	}
	
	KILE_DEBUG() << "math is " << math << ", bracket is " << bracket << " and item->key() is " <<  ( item ? item->key() : "" ) << endl;
}

void SymbolView::fillWidget(const TQString& prefix)
{
	KILE_DEBUG() << "===SymbolView::fillWidget(const TQString& " << prefix <<  " )===" << endl;
	TQImage image;
	KIconViewItem* item;
	TQStringList refCnts,paths;
	
	if( prefix == MFUSprefix)
	{
		KConfig *config = KGlobal::config();
		config->setGroup(MFUSGroup);
		TQString configPaths = config->readEntry("paths");
		TQString configrefCnts = config->readEntry("counts");
		paths = TQStringList::split(',',configPaths);
		refCnts = TQStringList::split(',',configrefCnts);
		KILE_DEBUG() << "Read " << paths.count() << " paths and " << refCnts.count() << " refCnts" << endl;
		if( paths.count() != refCnts.count() )
		{
			KILE_DEBUG() << "error in saved LRU list" << endl;
			paths.clear();
			refCnts.clear();
		}
	}
	else
	{
		paths = KGlobal::dirs()->findAllResources("app_symbols", prefix + "/*.png",false,true);
	paths.sort();
		for( uint i = 0 ; i < paths.count() ; i++ )
			refCnts.append("1");
	}
	for ( uint i = 0; i < paths.count(); i++ )
	{
 		if ( image.load(paths[i]) )
		{
//   			KILE_DEBUG() << "path is " << paths[i] << endl;
			item = new KIconViewItem(this);
			item->setPixmap(image);
			item->setKey( refCnts[i] + '%' + image.text("Command") + '%' + image.text("Packages") + '%' + paths[i] );
			image = KImageEffect::blend(tqcolorGroup().text(), image, 1); // destroys our png comments, so we do it after reading the comments
		}
		else
			KILE_DEBUG() << "Loading file " << paths[i] << " failed" << endl;
    	}
}

void SymbolView::writeConfig()
{
	TQIconViewItem *item;
	TQStringList paths,refCnts;
	

	KConfig *config = KGlobal::config();
	config->setGroup(MFUSGroup);

	if( KileConfig::clearMFUS() )
	{
		config->deleteEntry("paths");
		config->deleteEntry("counts");
	}
	else
	{
		for ( item = this->firstItem(); item; item = item->nextItem() )
		{
			refCnts.append(item->key().section('%',0,0));
			paths.append(item->key().section('%',3,3));
			KILE_DEBUG() << "path=" << paths.last() << ", count is " << refCnts.last() << endl;
		}
		config->writeEntry("paths",paths);
		config->writeEntry("counts",refCnts);
	}
}

void SymbolView::slotAddToList(const TQIconViewItem *item)
{
	if( !item || !item->pixmap() )
		return;
		
	TQIconViewItem *tmpItem;
	bool found=false;
	const TQRegExp reCnt("^\\d+");
			
	KILE_DEBUG() << "===void SymbolView::slotAddToList(const TQIconViewItem *" << item << " )===" << endl;
	
	for ( tmpItem = this->firstItem(); tmpItem; tmpItem = tmpItem->nextItem() )
	{
		if( item->key().section('%',1) == tmpItem->key().section('%',1) )
		{
			found=true;
			break;
		}
	}
	
	if( !found && ( this->count() + 1 ) > KileConfig::numSymbolsMFUS() ) // we check before adding the symbol
	{	
		int refCnt, minRefCnt=10000;
		TQIconViewItem *unpopularItem = 0L;

		KILE_DEBUG() << "Removing most unpopular item" << endl;

		for ( tmpItem = this->firstItem(); tmpItem; tmpItem = tmpItem->nextItem() )
		{
			extract(tmpItem->key(),refCnt);

			if( refCnt < minRefCnt )
			{
				refCnt = minRefCnt;
				unpopularItem = tmpItem;
			}
		}
		KILE_DEBUG() << " minRefCnt is " << minRefCnt << endl;
		delete unpopularItem;
	}

	if( found )
	{
		KILE_DEBUG() << "item is already in the iconview" << endl;
		
		int refCnt;
		extract(tmpItem->key(),refCnt);
		
		TQString key = tmpItem->key();
		key.replace(reCnt,TQString::number(refCnt+1));
		tmpItem->setKey(key);
	}
	else
	{
		tmpItem = new KIconViewItem(this,TQString(),*(item->pixmap()));
		tmpItem->setKey(item->key());
    	}
}

#include "symbolview.moc"