summaryrefslogtreecommitdiff
path: root/noncore/unsupported/libopie/ofontselector.cpp
Unidiff
Diffstat (limited to 'noncore/unsupported/libopie/ofontselector.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/unsupported/libopie/ofontselector.cpp411
1 files changed, 411 insertions, 0 deletions
diff --git a/noncore/unsupported/libopie/ofontselector.cpp b/noncore/unsupported/libopie/ofontselector.cpp
new file mode 100644
index 0000000..87b7869
--- a/dev/null
+++ b/noncore/unsupported/libopie/ofontselector.cpp
@@ -0,0 +1,411 @@
1/*
2               =. This file is part of the OPIE Project
3             .=l. Copyright (c) 2002 Robert Griebl <sandman@handhelds.org>
4           .>+-=
5 _;:,     .>    :=|. This library is free software; you can
6.> <`_,   >  .   <= redistribute it and/or modify it under
7:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
8.="- .-=="i,     .._ License as published by the Free Software
9 - .   .-<_>     .<> Foundation; either version 2 of the License,
10     ._= =}       : or (at your option) any later version.
11    .%`+i>       _;_.
12    .i_,=:_.      -<s. This library is distributed in the hope that
13     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
14    : ..    .:,     . . . without even the implied warranty of
15    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
16  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
17..}^=.=       =       ; Library General Public License for more
18++=   -.     .`     .: details.
19 :     =  ...= . :.=-
20 -.   .:....=;==+<; You should have received a copy of the GNU
21  -_. . .   )=.  = Library General Public License along with
22    --        :-=` this library; see the file COPYING.LIB.
23 If not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA.
26
27*/
28
29#include <qlayout.h>
30#include <qlistbox.h>
31#include <qcombobox.h>
32#include <qlabel.h>
33#include <qmultilineedit.h>
34
35#include <qpe/fontdatabase.h>
36
37#include "ofontselector.h"
38
39class OFontSelectorPrivate {
40public:
41 QListBox * m_font_family_list;
42 QComboBox * m_font_style_list;
43 QComboBox * m_font_size_list;
44 QMultiLineEdit *m_preview;
45
46 bool m_pointbug : 1;
47
48 FontDatabase m_fdb;
49};
50
51namespace {
52
53class FontListItem : public QListBoxText {
54public:
55 FontListItem ( const QString &t, const QStringList &styles, const QValueList<int> &sizes ) : QListBoxText ( )
56 {
57 m_name = t;
58 m_styles = styles;
59 m_sizes = sizes;
60
61 QString str = t;
62 str [0] = str [0]. upper ( );
63 setText ( str );
64 }
65
66 QString family ( ) const
67 {
68 return m_name;
69 }
70
71 const QStringList &styles ( ) const
72 {
73 return m_styles;
74 }
75
76 const QValueList<int> &sizes ( ) const
77 {
78 return m_sizes;
79 }
80
81private:
82 QStringList m_styles;
83 QValueList<int> m_sizes;
84 QString m_name;
85};
86
87
88static int findItemCB ( QComboBox *box, const QString &str )
89{
90 for ( int i = 0; i < box-> count ( ); i++ ) {
91 if ( box-> text ( i ) == str )
92 return i;
93 }
94 return -1;
95}
96
97}
98/* static same as anon. namespace */
99static int qt_version ( )
100{
101 const char *qver = qVersion ( );
102
103 return ( qver [0] - '0' ) * 100 + ( qver [2] - '0' ) * 10 + ( qver [4] - '0' );
104}
105
106/**
107 * Constructs the Selector object
108 * @param withpreview If a font preview should be given
109 * @param parent The parent of the Font Selector
110 * @param name The name of the object
111 * @param fl WidgetFlags
112 */
113OFontSelector::OFontSelector ( bool withpreview, QWidget *parent, const char *name, WFlags fl ) : QWidget ( parent, name, fl )
114{
115 d = new OFontSelectorPrivate ( );
116
117 QGridLayout *gridLayout = new QGridLayout ( this, 0, 0, 4, 4 );
118 gridLayout->setRowStretch ( 4, 10 );
119
120 d-> m_font_family_list = new QListBox( this, "FontListBox" );
121 gridLayout->addMultiCellWidget( d-> m_font_family_list, 0, 4, 0, 0 );
122 connect( d-> m_font_family_list, SIGNAL( highlighted(int) ), this, SLOT( fontFamilyClicked(int) ) );
123
124 QLabel *label = new QLabel( tr( "Style" ), this );
125 gridLayout->addWidget( label, 0, 1 );
126
127 d-> m_font_style_list = new QComboBox( this, "StyleListBox" );
128 connect( d-> m_font_style_list, SIGNAL( activated(int) ), this, SLOT( fontStyleClicked(int) ) );
129 gridLayout->addWidget( d-> m_font_style_list, 1, 1 );
130
131 label = new QLabel( tr( "Size" ), this );
132 gridLayout->addWidget( label, 2, 1 );
133
134 d-> m_font_size_list = new QComboBox( this, "SizeListBox" );
135 connect( d-> m_font_size_list, SIGNAL( activated(int) ),
136 this, SLOT( fontSizeClicked(int) ) );
137 gridLayout->addWidget( d-> m_font_size_list, 3, 1 );
138
139 d-> m_pointbug = ( qt_version ( ) <= 233 );
140
141 if ( withpreview ) {
142 d-> m_preview = new QMultiLineEdit ( this, "Preview" );
143 d-> m_preview-> setAlignment ( AlignCenter );
144 d-> m_preview-> setWordWrap ( QMultiLineEdit::WidgetWidth );
145 d-> m_preview-> setMargin ( 3 );
146 d-> m_preview-> setText ( tr( "The Quick Brown Fox Jumps Over The Lazy Dog" ));
147 gridLayout-> addRowSpacing ( 5, 4 );
148 gridLayout-> addMultiCellWidget ( d-> m_preview, 6, 6, 0, 1 );
149 gridLayout-> setRowStretch ( 6, 5 );
150 }
151 else
152 d-> m_preview = 0;
153
154 loadFonts ( d-> m_font_family_list );
155}
156
157OFontSelector::~OFontSelector ( )
158{
159 delete d;
160}
161
162/**
163 * This methods tries to set the font
164 * @param f The wishes font
165 * @return success or failure
166 */
167bool OFontSelector::setSelectedFont ( const QFont &f )
168{
169 return setSelectedFont ( f. family ( ), d-> m_fdb. styleString ( f ), f. pointSize ( ), QFont::encodingName ( f. charSet ( )));
170}
171
172
173/**
174 * This is an overloaded method @see setSelectedFont
175 * @param familyStr The family of the font
176 * @param styleStr The style of the font
177 * @param sizeVal The size of font
178 * @param charset The charset to be used. Will be deprecated by QT3
179 */
180bool OFontSelector::setSelectedFont ( const QString &familyStr, const QString &styleStr, int sizeVal, const QString & charset )
181{
182 QString sizeStr = QString::number ( sizeVal );
183
184 QListBoxItem *family = d-> m_font_family_list-> findItem ( familyStr );
185 if ( !family )
186 family = d-> m_font_family_list-> findItem ( "Helvetica" );
187 if ( !family )
188 family = d-> m_font_family_list-> firstItem ( );
189 d-> m_font_family_list-> setCurrentItem ( family );
190 fontFamilyClicked ( d-> m_font_family_list-> index ( family ));
191
192 int style = findItemCB ( d-> m_font_style_list, styleStr );
193 if ( style < 0 )
194 style = findItemCB ( d-> m_font_style_list, "Regular" );
195 if ( style < 0 && d-> m_font_style_list-> count ( ) > 0 )
196 style = 0;
197 d-> m_font_style_list-> setCurrentItem ( style );
198 fontStyleClicked ( style );
199
200 int size = findItemCB ( d-> m_font_size_list, sizeStr );
201 if ( size < 0 )
202 size = findItemCB ( d-> m_font_size_list, "10" );
203 if ( size < 0 && d-> m_font_size_list-> count ( ) > 0 )
204 size = 0;
205 d-> m_font_size_list-> setCurrentItem ( size );
206 fontSizeClicked ( size );
207
208 return (( family ) && ( style >= 0 ) && ( size >= 0 ));
209}
210
211/**
212 * This method returns the name, style and size of the currently selected
213 * font or false if no font is selected
214 * @param family The font family will be written there
215 * @param style The style will be written there
216 * @param size The size will be written there
217 * @return success or failure
218 */
219bool OFontSelector::selectedFont ( QString &family, QString &style, int &size )
220{
221 QString dummy;
222 return selectedFont ( family, style, size, dummy );
223}
224
225
226/**
227 * This method does return the font family or QString::null if there is
228 * no font item selected
229 * @return the font family
230 */
231QString OFontSelector::fontFamily ( ) const
232{
233 FontListItem *fli = (FontListItem *) d-> m_font_family_list-> item ( d-> m_font_family_list-> currentItem ( ));
234
235 return fli ? fli-> family ( ) : QString::null;
236}
237
238/**
239 * This method will return the style of the font or QString::null
240 * @return the style of the font
241 */
242QString OFontSelector::fontStyle ( ) const
243{
244 FontListItem *fli = (FontListItem *) d-> m_font_family_list-> item ( d-> m_font_family_list-> currentItem ( ));
245 int fst = d-> m_font_style_list-> currentItem ( );
246
247 return ( fli && fst >= 0 ) ? fli-> styles ( ) [fst] : QString::null;
248}
249
250/**
251 * This method will return the font size or 10 if no font size is available
252 */
253int OFontSelector::fontSize ( ) const
254{
255 FontListItem *fli = (FontListItem *) d-> m_font_family_list-> item ( d-> m_font_family_list-> currentItem ( ));
256 int fsi = d-> m_font_size_list-> currentItem ( );
257
258 return ( fli && fsi >= 0 ) ? fli-> sizes ( ) [fsi] : 10;
259}
260
261/**
262 * returns the charset of the font or QString::null
263 */
264QString OFontSelector::fontCharSet ( ) const
265{
266 FontListItem *fli = (FontListItem *) d-> m_font_family_list-> item ( d-> m_font_family_list-> currentItem ( ));
267
268 return fli ? d-> m_fdb. charSets ( fli-> family ( )) [0] : QString::null;
269}
270
271/**
272 * Overloaded member function see above
273 * @see selectedFont
274 */
275bool OFontSelector::selectedFont ( QString &family, QString &style, int &size, QString &charset )
276{
277 int ffa = d-> m_font_family_list-> currentItem ( );
278 int fst = d-> m_font_style_list-> currentItem ( );
279 int fsi = d-> m_font_size_list-> currentItem ( );
280
281 FontListItem *fli = (FontListItem *) d-> m_font_family_list-> item ( ffa );
282
283 if ( fli ) {
284 family = fli-> family ( );
285 style = fst >= 0 ? fli-> styles ( ) [fst] : QString::null;
286 size = fsi >= 0 ? fli-> sizes ( ) [fsi] : 10;
287 charset = d-> m_fdb. charSets ( fli-> family ( )) [0];
288
289 return true;
290 }
291 else
292 return false;
293}
294
295
296
297
298void OFontSelector::loadFonts ( QListBox *list )
299{
300 QStringList f = d-> m_fdb. families ( );
301
302 for ( QStringList::ConstIterator it = f. begin ( ); it != f. end ( ); ++it ) {
303 QValueList <int> ps = d-> m_fdb. pointSizes ( *it );
304
305 if ( d-> m_pointbug ) {
306 for ( QValueList <int>::Iterator it = ps. begin ( ); it != ps. end ( ); it++ )
307 *it /= 10;
308 }
309
310 list-> insertItem ( new FontListItem ( *it, d-> m_fdb. styles ( *it ), ps ));
311 }
312}
313
314void OFontSelector::fontFamilyClicked ( int index )
315{
316 QString oldstyle = d-> m_font_style_list-> currentText ( );
317 QString oldsize = d-> m_font_size_list-> currentText ( );
318
319 FontListItem *fli = (FontListItem *) d-> m_font_family_list-> item ( index );
320
321 d-> m_font_style_list-> clear ( );
322 d-> m_font_style_list-> insertStringList ( fli-> styles ( ));
323 d-> m_font_style_list-> setEnabled ( !fli-> styles ( ). isEmpty ( ));
324
325 int i;
326
327 i = findItemCB ( d-> m_font_style_list, oldstyle );
328 if ( i < 0 )
329 i = findItemCB ( d-> m_font_style_list, "Regular" );
330 if (( i < 0 ) && ( d-> m_font_style_list-> count ( ) > 0 ))
331 i = 0;
332
333 if ( i >= 0 ) {
334 d-> m_font_style_list-> setCurrentItem ( i );
335 fontStyleClicked ( i );
336 }
337
338 d-> m_font_size_list-> clear ( );
339 QValueList<int> sl = fli-> sizes ( );
340
341 for ( QValueList<int>::Iterator it = sl. begin ( ); it != sl. end ( ); ++it )
342 d-> m_font_size_list-> insertItem ( QString::number ( *it ));
343
344 i = findItemCB ( d-> m_font_size_list, oldsize );
345 if ( i < 0 )
346 i = findItemCB ( d-> m_font_size_list, "10" );
347 if (( i < 0 ) && ( d-> m_font_size_list-> count ( ) > 0 ))
348 i = 0;
349
350 if ( i >= 0 ) {
351 d-> m_font_size_list-> setCurrentItem ( i );
352 fontSizeClicked ( i );
353 }
354 changeFont ( );
355}
356
357void OFontSelector::fontStyleClicked ( int /*index*/ )
358{
359 changeFont ( );
360}
361
362void OFontSelector::fontSizeClicked ( int /*index*/ )
363{
364 changeFont ( );
365}
366
367void OFontSelector::changeFont ( )
368{
369 QFont f = selectedFont ( );
370
371 if ( d-> m_preview )
372 d-> m_preview-> setFont ( f );
373
374 emit fontSelected ( f );
375}
376
377/**
378 * Return the selected font
379 */
380QFont OFontSelector::selectedFont ( )
381{
382 int ffa = d-> m_font_family_list-> currentItem ( );
383 int fst = d-> m_font_style_list-> currentItem ( );
384 int fsi = d-> m_font_size_list-> currentItem ( );
385
386 FontListItem *fli = (FontListItem *) d-> m_font_family_list-> item ( ffa );
387
388 if ( fli ) {
389 return d-> m_fdb. font ( fli-> family ( ), \
390 fst >= 0 ? fli-> styles ( ) [fst] : QString::null, \
391 fsi >= 0 ? fli-> sizes ( ) [fsi] : 10, \
392 d-> m_fdb. charSets ( fli-> family ( )) [0] );
393 }
394 else
395 return QFont ( );
396}
397
398
399void OFontSelector::resizeEvent ( QResizeEvent *re )
400{
401 if ( d-> m_preview ) {
402 d-> m_preview-> setMinimumHeight ( 1 );
403 d-> m_preview-> setMaximumHeight ( 32767 );
404 }
405
406 QWidget::resizeEvent ( re );
407
408 if ( d-> m_preview )
409 d-> m_preview-> setFixedHeight ( d-> m_preview-> height ( ));
410
411}