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