summaryrefslogtreecommitdiff
path: root/noncore/apps/odict/dingwidget.cpp
blob: 2913836f727c0ebadf1c5130b11527bd5d7247d8 (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
/***************************************************************************
   application:             : ODict

   begin                    : December 2002
   copyright                : ( C ) 2002, 2003 by Carsten Niehaus
   email                    : cniehaus@handhelds.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 "dingwidget.h"

#include <qfile.h>
#include <qpe/config.h>
#include <qhbox.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <qmainwindow.h>
#include <qstring.h>
#include <qtextstream.h>
#include <qstringlist.h>
#include <qregexp.h>
#include <qtextbrowser.h>
#include <stdlib.h> // for getenv

DingWidget::DingWidget(QString word, QTextBrowser *browser_top, QTextBrowser *browser_bottom, QString activated_name)
{
	methodname = activated_name;
	queryword = word;
	trenner = QString::null;
	lines = 0L;
	loadValues();
  	QString opie_dir = getenv("OPIEDIR");
	QFile file( opie_dir+"/noncore/apps/odict/eng_ita.dic" );
	
	if(  file.open(  IO_ReadOnly ) )
	{
		QTextStream stream(  &file );
		while ( !stream.eof() )
		{
			lines.append(  stream.readLine() );
		}
		file.close();
	}

	lines = lines.grep( queryword );

	topbrowser = browser_top;
	bottombrowser = browser_bottom;
}

void DingWidget::setText()
{
	QString top, bottom;
	QStringList test = lines;
	parseInfo( test, top , bottom );
	
	topbrowser->setText( top );
	bottombrowser->setText( bottom );
}

void DingWidget::loadValues()
{
	Config cfg(  "odict" );
	cfg.setGroup( "Method_"+methodname );
	trenner = cfg.readEntry( "Seperator" );
}

void DingWidget::parseInfo( QStringList &lines, QString &top, QString &bottom )
{

 	QString current;
 	QString left;
 	QString right;
	QString html_header      = "<html><table>";
	QString html_footer      = "</table></html>";
	QString html_table_left  = "<tr><td width='50'>";
	QString html_table_sep   = "</td><td>";
	QString html_table_right = "</td></tr>";
	QRegExp reg_div( trenner );
	QRegExp reg_word( queryword );
	QString substitute = "<a href=''>"+queryword+"</a>";
	QStringList toplist, bottomlist;
	for( QStringList::Iterator it = lines.begin() ; it != lines.end() ; ++it )
	{
		current = *it;
		left = current.left( current.find(reg_div) );
 		right = current.right( current.length() - current.find(reg_div) - 1 );
 		if ( left.contains( reg_word ) )
		{
		  left.replace( queryword, substitute );
 		  toplist.append( html_table_left + left + html_table_sep + right + html_table_right );
		}
		else
		{
		  right.replace( reg_word, substitute );
 		  bottomlist.append( html_table_left + right + html_table_sep + left + html_table_right );
		}
	}
		
	//thats it, the lists are rendered. Lets put them in one string
	bottom = html_header + bottomlist.join( "<br>" ) + html_footer;
	top    = html_header + toplist.join( "<br>" )    + html_footer;
}