00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "tabdialog.h"
00029
00030
00031
00032 #include <opie2/ocolorbutton.h>
00033 #include <opie2/odebug.h>
00034 #include <opie2/ofiledialog.h>
00035 #include <opie2/ofontselector.h>
00036 #include <opie2/oresource.h>
00037 #include <opie2/otabwidget.h>
00038
00039
00040 #include <qpe/qpeapplication.h>
00041
00042
00043 #include <qlayout.h>
00044 #include <qvbox.h>
00045 #include <qtabbar.h>
00046 #include <qiconview.h>
00047 #include <qapplication.h>
00048 #include <qlabel.h>
00049 #include <qfileinfo.h>
00050 #include <qradiobutton.h>
00051 #include <qbuttongroup.h>
00052 #include <qwhatsthis.h>
00053 #include <qcheckbox.h>
00054 #include <qspinbox.h>
00055
00056
00057 using namespace Opie::Ui;
00058 class SampleItem : public QIconViewItem {
00059 public:
00060 SampleItem ( QIconView *v, const QString &text, const QPixmap &pix ) : QIconViewItem ( v, text )
00061 {
00062 m_large = pix;
00063 m_small. convertFromImage ( pix. convertToImage ( ). smoothScale ( pix. width ( ) / 2, pix. height ( ) / 2 ));
00064 }
00065
00066 void sizeChange ( )
00067 {
00068 calcRect ( );
00069 repaint ( );
00070 }
00071
00072 QPixmap *pixmap ( ) const
00073 {
00074 if ( iconView ( )-> itemTextPos ( ) == QIconView::Right )
00075 return (QPixmap *) &m_small;
00076 else
00077 return (QPixmap *) &m_large;
00078 }
00079
00080 private:
00081 QPixmap m_large, m_small;
00082 };
00083
00084
00085
00086 class SampleView : public QIconView {
00087 public:
00088 SampleView ( QWidget *parent = 0, const char *name = 0 ) : QIconView ( parent, name )
00089 {
00090 setItemsMovable ( false );
00091 setAutoArrange ( true );
00092 setSorting ( true );
00093 setFrameStyle ( QFrame::NoFrame );
00094 setSpacing ( 4 );
00095 setMargin ( 0 );
00096 setSelectionMode ( QIconView::NoSelection );
00097 setBackgroundMode ( PaletteBase );
00098 setViewMode ( TabConfig::Icon );
00099 calculateGrid ( Bottom );
00100
00101
00102 new SampleItem ( this, QObject::tr( "Sample 1" ),
00103 Opie::Core::OResource::loadPixmap ( "datebook/DateBook", Opie::Core::OResource::BigIcon ));
00104 new SampleItem ( this, QObject::tr( "Sample 2" ),
00105 Opie::Core::OResource::loadPixmap ( "Calibrate", Opie::Core::OResource::BigIcon ));
00106 new SampleItem ( this, QObject::tr( "Sample 3" ),
00107 Opie::Core::OResource::loadPixmap ( "UnknownDocument", Opie::Core::OResource::BigIcon ));
00108
00109 setBackgroundType ( TabConfig::Ruled, QString::null );
00110
00111 setMaximumHeight ( firstItem ( )-> height ( ) + 16 );
00112 }
00113
00114 void setViewMode ( TabConfig::ViewMode m )
00115 {
00116 viewport ( )-> setUpdatesEnabled ( false );
00117
00118 switch ( m ) {
00119 case TabConfig::List:
00120 setItemTextPos( QIconView::Right );
00121 break;
00122 case TabConfig::Icon:
00123 setItemTextPos( QIconView::Bottom );
00124 break;
00125 }
00126
00127
00128 for ( QIconViewItem *it = firstItem ( ); it; it = it-> nextItem ( ))
00129 ((SampleItem *) it )-> sizeChange ( );
00130 arrangeItemsInGrid ( true );
00131 viewport ( )-> setUpdatesEnabled ( true );
00132 update ( );
00133 }
00134
00135
00136 void setBackgroundType( TabConfig::BackgroundType t, const QString &val )
00137 {
00138 switch ( t ) {
00139 case TabConfig::Ruled: {
00140 QPixmap bg ( width ( ), 9 );
00141 QPainter painter ( &bg );
00142 for ( int i = 0; i < 3; i++ ) {
00143 painter. setPen ( white );
00144 painter. drawLine ( 0, i*3, width()-1, i*3 );
00145 painter. drawLine ( 0, i*3+1, width()-1, i*3+1 );
00146 painter. setPen ( colorGroup().background().light(105) );
00147 painter. drawLine ( 0, i*3+2, width()-1, i*3+2 );
00148 }
00149 painter.end ( );
00150 setBackgroundPixmap ( bg );
00151 break;
00152 }
00153
00154 case TabConfig::SolidColor: {
00155 setBackgroundPixmap ( QPixmap ( ));
00156 if ( val. isEmpty ( ))
00157 setBackgroundColor ( colorGroup ( ). base ( ));
00158 else
00159 setBackgroundColor ( val );
00160 break;
00161 }
00162
00163 case TabConfig::Image: {
00164 odebug << "Loading image: " << val << "" << oendl;
00165 QPixmap bg = Opie::Core::OResource::loadPixmap ( val );
00166 if ( bg. isNull () )
00167 bg = QPixmap( val );
00168 setBackgroundPixmap ( bg );
00169 break;
00170 }
00171 }
00172 m_bgtype = t;
00173 viewport ( )-> update ( );
00174 }
00175
00176 void setTextColor ( const QColor &tc )
00177 {
00178 m_textcolor = tc;
00179 QColorGroup cg = colorGroup ( );
00180 cg. setColor ( QColorGroup::Text, tc );
00181 setPalette ( QPalette ( cg, cg, cg ));
00182 viewport ( )-> update ( );
00183 }
00184
00185 void setViewFont ( const QFont &f )
00186 {
00187 setFont ( f );
00188 }
00189
00190 void setItemTextPos ( ItemTextPos pos )
00191 {
00192 calculateGrid ( pos );
00193 QIconView::setItemTextPos( pos );
00194 }
00195
00196
00197 void calculateGrid ( ItemTextPos pos )
00198 {
00199 int dw = QApplication::desktop ( )-> width ( );
00200 int viewerWidth = dw - style ( ).scrollBarExtent ( ). width ( );
00201 if ( pos == Bottom ) {
00202 int cols = 3;
00203 if ( viewerWidth <= 200 )
00204 cols = 2;
00205 else if ( viewerWidth >= 400 )
00206 cols = viewerWidth/96;
00207 setSpacing ( 4 );
00208 setGridX (( viewerWidth - ( cols + 1 ) * spacing ( )) / cols );
00209 setGridY ( fontMetrics ( ). height ( ) * 2 + 24 );
00210 }
00211 else {
00212 int cols = 2;
00213 if ( viewerWidth < 150 )
00214 cols = 1;
00215 else if ( viewerWidth >= 400 )
00216 cols = viewerWidth / 150;
00217 setSpacing ( 2 );
00218 setGridX (( viewerWidth - ( cols + 1 ) * spacing ( )) / cols );
00219 setGridY ( fontMetrics ( ). height ( ) + 2 );
00220 }
00221 }
00222
00223 void paletteChange( const QPalette &p )
00224 {
00225 static bool excllock = false;
00226
00227 if ( excllock )
00228 return;
00229 excllock = true;
00230
00231 unsetPalette ( );
00232 QIconView::paletteChange ( p );
00233 if ( m_bgtype == TabConfig::Ruled )
00234 setBackgroundType ( TabConfig::Ruled, QString::null );
00235 QColorGroup cg = colorGroup ( );
00236 cg.setColor ( QColorGroup::Text, m_textcolor );
00237 setPalette ( QPalette ( cg, cg, cg ));
00238
00239 excllock = false;
00240 }
00241
00242 void setBackgroundPixmap ( const QPixmap &pm )
00243 {
00244 m_bgpix = pm;
00245 }
00246
00247 void setBackgroundColor ( const QColor &c )
00248 {
00249 m_bgcolor = c;
00250 }
00251
00252 void drawBackground ( QPainter *p, const QRect &r )
00253 {
00254 if ( !m_bgpix. isNull ( )) {
00255 p-> drawTiledPixmap ( r, m_bgpix, QPoint (( r. x ( ) + contentsX ( )) % m_bgpix. width ( ),
00256 ( r. y ( ) + contentsY ( )) % m_bgpix. height ( )));
00257 }
00258 else
00259 p-> fillRect ( r, m_bgcolor );
00260 }
00261
00262 private:
00263 QColor m_textcolor;
00264 QColor m_bgcolor;
00265 QPixmap m_bgpix;
00266 TabConfig::BackgroundType m_bgtype;
00267 };
00268
00269
00270
00271 TabDialog::TabDialog ( const QPixmap *tabicon, const QString &tabname, TabConfig &tc, QWidget *parent, const char *dname, bool modal, WFlags fl )
00272 : QDialog ( parent, dname, modal, fl | WStyle_ContextHelp ), m_tc ( tc )
00273 {
00274 setCaption ( tr( "Edit Tab" ));
00275
00276 QVBoxLayout *lay = new QVBoxLayout ( this, 3, 3 );
00277
00278 OTabWidget *tw = new OTabWidget ( this, "tabwidget", OTabWidget::Global, OTabWidget::Bottom );
00279 QWidget *bgtab;
00280
00281 tw-> addTab ( bgtab = createBgTab ( tw ), "appearance/color", tr( "Background" ));
00282 tw-> addTab ( createFontTab ( tw ), "font", tr( "Font" ));
00283 tw-> addTab ( createIconTab ( tw ), "pixmap", tr( "Icons" ) );
00284
00285 tw-> setCurrentTab ( bgtab );
00286
00287 QWidget *sample = new QVBox ( this );
00288 QTabBar *tb = new QTabBar ( sample );
00289 QString name ( tr( "Previewing %1" ). arg ( tabname ));
00290
00291 tb-> addTab ( tabicon ? new QTab ( *tabicon, name ) : new QTab ( name ));
00292
00293 m_sample = new SampleView ( sample );
00294
00295 lay-> addWidget ( tw, 10 );
00296 lay-> addWidget ( sample, 1 );
00297
00298 m_bgimage = tc. m_bg_image;
00299 m_bgtype-> setButton ( tc. m_bg_type );
00300 bgTypeClicked( tc.m_bg_type );
00301
00302 m_fontuse-> setChecked ( tc. m_font_use );
00303 m_fontselect-> setSelectedFont ( QFont ( tc. m_font_family, tc. m_font_size, tc. m_font_weight, tc. m_font_italic ));
00304 m_fontselect-> setEnabled ( m_fontuse-> isChecked ( ));
00305 fontClicked ( m_fontselect-> selectedFont ( ));
00306
00307 m_iconsize-> setButton ( tc. m_view );
00308 iconSizeClicked ( tc. m_view );
00309 iconColorClicked ( m_iconcolor-> color ( ));
00310 m_iconcolumns->setValue( tc. m_iconcolumns );
00311
00312 QWhatsThis::add ( sample, tr( "This is a rough preview of what the currently selected Tab will look like." ));
00313 }
00314
00315
00316 TabDialog::~TabDialog ( )
00317 {
00318 }
00319
00320 QWidget *TabDialog::createFontTab ( QWidget *parent )
00321 {
00322 QWidget *tab = new QWidget ( parent, "FontTab" );
00323 QVBoxLayout *vertLayout = new QVBoxLayout ( tab, 3, 3 );
00324
00325 m_fontuse = new QCheckBox ( tr( "Use a custom font" ), tab );
00326 vertLayout-> addWidget ( m_fontuse );
00327
00328 m_fontselect = new OFontSelector ( false, tab, "fontsel" );
00329 vertLayout-> addWidget ( m_fontselect );
00330
00331 connect ( m_fontuse, SIGNAL( toggled(bool)), m_fontselect, SLOT( setEnabled(bool)));
00332 connect( m_fontselect, SIGNAL( fontSelected(const QFont&)),
00333 this, SLOT( fontClicked(const QFont&)));
00334
00335 return tab;
00336 }
00337
00338 QWidget *TabDialog::createBgTab ( QWidget *parent )
00339 {
00340 QWidget *tab = new QWidget( parent, "BgTab" );
00341 QVBoxLayout *vertLayout = new QVBoxLayout( tab, 3, 3 );
00342
00343 QGridLayout* gridLayout = new QGridLayout ( vertLayout );
00344 gridLayout-> setColStretch ( 1, 10 );
00345
00346 QLabel* label = new QLabel( tr( "Type:" ), tab );
00347 gridLayout-> addWidget ( label, 0, 0 );
00348 m_bgtype = new QButtonGroup( tab, "buttongroup" );
00349 m_bgtype-> hide ( );
00350 m_bgtype-> setExclusive ( true );
00351 connect ( m_bgtype, SIGNAL( clicked(int)), this, SLOT( bgTypeClicked(int)));
00352
00353 QRadioButton *rb;
00354 rb = new QRadioButton( tr( "Ruled" ), tab, "ruled" );
00355 m_bgtype-> insert ( rb, TabConfig::Ruled );
00356 gridLayout-> addWidget( rb, 0, 1 );
00357
00358 QHBoxLayout *hb = new QHBoxLayout ( );
00359 hb-> setSpacing ( 3 );
00360
00361 rb = new QRadioButton( tr( "Solid color" ), tab, "solid" );
00362 m_bgtype-> insert ( rb, TabConfig::SolidColor );
00363 hb-> addWidget ( rb );
00364 hb-> addSpacing ( 10 );
00365
00366 m_solidcolor = new Opie::OColorButton ( tab, QColor ( m_tc. m_bg_color ) );
00367 connect ( m_solidcolor, SIGNAL( colorSelected(const QColor&)), this, SLOT( bgColorClicked(const QColor&)));
00368 hb-> addWidget ( m_solidcolor );
00369 hb-> addStretch ( 10 );
00370
00371 gridLayout-> addLayout ( hb, 1, 1 );
00372
00373 hb = new QHBoxLayout ( );
00374 hb-> setSpacing ( 3 );
00375
00376 rb = new QRadioButton( tr( "Image" ), tab, "image" );
00377 m_bgtype-> insert ( rb, TabConfig::Image );
00378 hb-> addWidget( rb );
00379 hb-> addSpacing ( 10 );
00380
00381 m_imagebrowse = new QPushButton ( tr( "Select..." ), tab );
00382 connect ( m_imagebrowse, SIGNAL( clicked()), this, SLOT( bgImageClicked()));
00383 hb-> addWidget ( m_imagebrowse );
00384 hb-> addStretch ( 10 );
00385
00386 gridLayout-> addLayout ( hb, 2, 1 );
00387
00388 QPushButton *p = new QPushButton ( tr( "Default" ), tab );
00389 connect ( p, SIGNAL( clicked()), this, SLOT( bgDefaultClicked()));
00390 gridLayout-> addWidget ( p, 3, 1 );
00391
00392 vertLayout-> addStretch ( 10 );
00393
00394 return tab;
00395 }
00396
00397 QWidget *TabDialog::createIconTab ( QWidget *parent )
00398 {
00399 QWidget *tab = new QWidget( parent, "IconTab" );
00400 QVBoxLayout *vertLayout = new QVBoxLayout( tab, 3, 3 );
00401
00402 QGridLayout* gridLayout = new QGridLayout ( vertLayout );
00403 gridLayout-> setColStretch ( 1, 10 );
00404
00405 QLabel* label = new QLabel( tr( "Size:" ), tab );
00406 gridLayout-> addWidget ( label, 0, 0 );
00407 m_iconsize = new QButtonGroup( tab, "buttongroup" );
00408 m_iconsize-> hide ( );
00409 m_iconsize-> setExclusive ( true );
00410
00411 QRadioButton *rb;
00412 rb = new QRadioButton( tr( "Small" ), tab, "iconsmall" );
00413 m_iconsize-> insert ( rb, TabConfig::List );
00414 gridLayout-> addWidget( rb, 0, 1 );
00415
00416 rb = new QRadioButton( tr( "Large" ), tab, "iconlarge" );
00417 m_iconsize-> insert ( rb, TabConfig::Icon );
00418 gridLayout-> addWidget( rb, 1, 1 );
00419 connect ( m_iconsize, SIGNAL( clicked(int)), this, SLOT( iconSizeClicked(int)));
00420 gridLayout-> addRowSpacing ( 2, 8 );
00421
00422 label = new QLabel ( tr( "Color:" ), tab );
00423 gridLayout-> addWidget ( label, 3, 0 );
00424 gridLayout-> addRowSpacing ( 3, 8 );
00425
00426 m_iconcolor = new Opie::OColorButton ( tab, QColor ( m_tc. m_text_color ) );
00427 connect ( m_iconcolor, SIGNAL( colorSelected(const QColor&)), this, SLOT( iconColorClicked(const QColor&)));
00428 gridLayout-> addWidget ( m_iconcolor, 3, 1, AlignLeft );
00429
00430 label = new QLabel( tr( "Columns:" ), tab );
00431 gridLayout->addWidget( label, 4, 0 );
00432 m_iconcolumns = new QSpinBox( 0, 10, 1, tab, "iconspinbox" );
00433 m_iconcolumns->setSpecialValueText( tr( "Automatic" ) );
00434 gridLayout->addWidget( m_iconcolumns, 4, 1, AlignLeft );
00435
00436 vertLayout-> addStretch ( 10 );
00437
00438 if ( m_tc.m_last_directory == QString::null ) {
00439 m_tc.m_last_directory = QPEApplication::documentDir();
00440 }
00441
00442 return tab;
00443 }
00444
00445
00446 void TabDialog::iconSizeClicked ( int s )
00447 {
00448 m_sample-> setViewMode ((TabConfig::ViewMode) s );
00449 }
00450
00451 void TabDialog::fontClicked ( const QFont &f )
00452 {
00453 m_sample-> setViewFont ( f );
00454 }
00455
00456 void TabDialog::bgTypeClicked ( int t )
00457 {
00458 QString s;
00459 m_solidcolor-> setEnabled ( t == TabConfig::SolidColor );
00460 m_imagebrowse-> setEnabled ( t == TabConfig::Image );
00461
00462 if ( t == TabConfig::SolidColor )
00463 s = m_solidcolor-> color ( ). name ( );
00464 else if ( t == TabConfig::Image )
00465 s = m_bgimage;
00466
00467 m_sample-> setBackgroundType ((TabConfig::BackgroundType) t, s );
00468 }
00469
00470 void TabDialog::bgColorClicked ( const QColor & )
00471 {
00472 m_sample-> setBackgroundType ( TabConfig::SolidColor, m_solidcolor-> color ( ). name ( ) );
00473 }
00474
00475 void TabDialog::iconColorClicked ( const QColor &col )
00476 {
00477 m_sample-> setTextColor ( col );
00478 }
00479
00480 void TabDialog::bgImageClicked ( )
00481 {
00482
00483
00484
00485 MimeTypes types;
00486 QStringList list;
00487 list << "image/*";
00488 types. insert ( "Images", list );
00489
00490 QString file = OFileDialog::getOpenFileName ( 1, m_tc.m_last_directory, QString::null, types );
00491
00492 if ( !file. isEmpty ( )) {
00493 m_tc.m_last_directory = QFileInfo( file ).dirPath();
00494 m_bgimage = DocLnk ( file ). file ( );
00495 m_sample-> setBackgroundType ( TabConfig::Image, m_bgimage );
00496 }
00497 }
00498
00499 void TabDialog::bgDefaultClicked ( )
00500 {
00501 m_bgtype-> setButton ( TabConfig::Image );
00502 m_bgimage = "launcher/opie-background";
00503 bgTypeClicked( TabConfig::Image );
00504
00505 }
00506
00507 void TabDialog::accept ( )
00508 {
00509 m_tc. m_view = (TabConfig::ViewMode) m_iconsize-> id ( m_iconsize-> selected ( ));
00510 m_tc. m_bg_type = (TabConfig::BackgroundType) m_bgtype-> id ( m_bgtype-> selected ( ));
00511 m_tc. m_bg_color = m_solidcolor-> color ( ). name ( );
00512 m_tc. m_iconcolumns = m_iconcolumns-> value( );
00513 m_tc. m_bg_image = m_bgimage;
00514 m_tc. m_text_color = m_iconcolor-> color ( ). name ( );
00515
00516 m_tc. m_font_use = m_fontuse-> isChecked ( );
00517
00518 if ( m_tc. m_font_use ) {
00519 QFont f = m_fontselect-> selectedFont ( );
00520
00521 m_tc. m_font_family = f. family ( );
00522 m_tc. m_font_size = f. pointSize ( );
00523 m_tc. m_font_weight = f. weight ( );
00524 m_tc. m_font_italic = f. italic ( );
00525 }
00526
00527 QDialog::accept ( );
00528 }