korganizer

navigatorbar.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <qstring.h>
00026 #include <qtooltip.h>
00027 #include <qpushbutton.h>
00028 #include <qlayout.h>
00029 #include <qframe.h>
00030 #include <qpopupmenu.h>
00031 #include <qlabel.h>
00032 
00033 #include <kdebug.h>
00034 #include <klocale.h>
00035 #include <kglobal.h>
00036 #include <kiconloader.h>
00037 
00038 #include "koglobals.h"
00039 #include "koprefs.h"
00040 
00041 #include <kcalendarsystem.h>
00042 
00043 #include "navigatorbar.h"
00044 
00045 ActiveLabel::ActiveLabel( QWidget *parent, const char *name )
00046   : QLabel( parent, name )
00047 {
00048 }
00049 
00050 void ActiveLabel::mouseReleaseEvent( QMouseEvent * )
00051 {
00052   emit clicked();
00053 }
00054 
00055 
00056 NavigatorBar::NavigatorBar( QWidget *parent, const char *name )
00057   : QWidget( parent, name ), mHasMinWidth( false )
00058 {
00059   QFont tfont = font();
00060   tfont.setPointSize( 10 );
00061   tfont.setBold( false );
00062 
00063   bool isRTL = KOGlobals::self()->reverseLayout();
00064 
00065   QPixmap pix;
00066   // Create backward navigation buttons
00067   pix = KOGlobals::self()->smallIcon( isRTL ? "2rightarrow" : "2leftarrow" );
00068   mPrevYear = new QPushButton( this );
00069   mPrevYear->setPixmap( pix );
00070   mPrevYear->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00071   QToolTip::add( mPrevYear, i18n( "Previous year" ) );
00072 
00073   pix = KOGlobals::self()->smallIcon( isRTL ? "1rightarrow" : "1leftarrow");
00074   mPrevMonth = new QPushButton( this );
00075   mPrevMonth->setPixmap( pix );
00076   mPrevMonth->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00077   QToolTip::add( mPrevMonth, i18n( "Previous month" ) );
00078 
00079   // Create forward navigation buttons
00080   pix = KOGlobals::self()->smallIcon( isRTL ? "1leftarrow" : "1rightarrow");
00081   mNextMonth = new QPushButton( this );
00082   mNextMonth->setPixmap( pix );
00083   mNextMonth->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00084   QToolTip::add( mNextMonth, i18n( "Next month" ) );
00085 
00086   pix = KOGlobals::self()->smallIcon( isRTL ? "2leftarrow" : "2rightarrow");
00087   mNextYear = new QPushButton( this );
00088   mNextYear->setPixmap( pix );
00089   mNextYear->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00090   QToolTip::add( mNextYear, i18n( "Next year" ) );
00091 
00092   // Create month name button
00093   mMonth = new ActiveLabel( this );
00094   mMonth->setFont( tfont );
00095   mMonth->setAlignment( AlignCenter );
00096   mMonth->setMinimumHeight( mPrevYear->sizeHint().height() );
00097   QToolTip::add( mMonth, i18n( "Select a month" ) );
00098 
00099   // Create year button
00100   mYear = new ActiveLabel( this );
00101   mYear->setFont( tfont );
00102   mYear->setAlignment( AlignCenter );
00103   mYear->setMinimumHeight( mPrevYear->sizeHint().height() );
00104   QToolTip::add( mYear, i18n( "Select a year" ) );
00105 
00106   // set up control frame layout
00107   QBoxLayout *ctrlLayout = new QHBoxLayout( this, 0, 4 );
00108   ctrlLayout->addWidget( mPrevYear );
00109   ctrlLayout->addWidget( mPrevMonth );
00110   QBoxLayout *dLayout = new QHBoxLayout( this );
00111   dLayout->insertStretch( 0, 50 );
00112   dLayout->addWidget( mMonth );
00113   dLayout->addWidget( mYear );
00114   dLayout->addStretch( 50 );
00115   ctrlLayout->addLayout( dLayout );
00116   ctrlLayout->addWidget( mNextMonth );
00117   ctrlLayout->addWidget( mNextYear );
00118 
00119   connect( mPrevYear, SIGNAL( clicked() ), SIGNAL( prevYearClicked() ) );
00120   connect( mPrevMonth, SIGNAL( clicked() ), SIGNAL( prevMonthClicked() ) );
00121   connect( mNextMonth, SIGNAL( clicked() ), SIGNAL( nextMonthClicked() ) );
00122   connect( mNextYear, SIGNAL( clicked() ), SIGNAL( nextYearClicked() ) );
00123   connect( mMonth, SIGNAL( clicked() ), SLOT( selectMonthFromMenu() ) );
00124   connect( mYear, SIGNAL( clicked() ), SLOT( selectYearFromMenu() ) );
00125 }
00126 
00127 NavigatorBar::~NavigatorBar()
00128 {
00129 }
00130 
00131 void NavigatorBar::showButtons( bool left, bool right )
00132 {
00133   if ( left ) {
00134     mPrevYear->show();
00135     mPrevMonth->show();
00136   } else {
00137     mPrevYear->hide();
00138     mPrevMonth->hide();
00139   }
00140 
00141   if ( right ) {
00142     mNextYear->show();
00143     mNextMonth->show();
00144   } else {
00145     mNextYear->hide();
00146     mNextMonth->hide();
00147   }
00148 
00149 }
00150 
00151 void NavigatorBar::selectDates( const KCal::DateList &dateList )
00152 {
00153   if ( dateList.count() > 0 ) {
00154     mDate = dateList.first();
00155 
00156     const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00157 
00158     // Set minimum width to width of widest month name label
00159     int i;
00160     int maxwidth = 0;
00161 
00162     for( i = 1; i <= calSys->monthsInYear( mDate ); ++i ) {
00163       int w = QFontMetrics( mMonth->font() ).
00164               width( QString( "%1" ).
00165                      arg( calSys->monthName( i, calSys->year( mDate ) ) ) );
00166       if ( w > maxwidth ) {
00167         maxwidth = w;
00168       }
00169     }
00170     mMonth->setMinimumWidth( maxwidth );
00171 
00172     mHasMinWidth = true;
00173 
00174     // set the label text at the top of the navigator
00175     mMonth->setText( i18n( "monthname", "%1" ).arg( calSys->monthName( mDate ) ) );
00176     mYear->setText( i18n( "4 digit year", "%1" ).arg( calSys->yearString( mDate, false ) ) );
00177   }
00178 }
00179 
00180 void NavigatorBar::selectMonthFromMenu()
00181 {
00182   // every year can have different month names (in some calendar systems)
00183   const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00184 
00185   int i, month, months = calSys->monthsInYear( mDate );
00186 
00187   QPopupMenu *popup = new QPopupMenu( mMonth );
00188 
00189   for ( i = 1; i <= months; i++ )
00190     popup->insertItem( calSys->monthName( i, calSys->year( mDate ) ), i );
00191 
00192   popup->setActiveItem( calSys->month( mDate ) - 1 );
00193   popup->setMinimumWidth( mMonth->width() );
00194 
00195   if ( ( month = popup->exec( mMonth->mapToGlobal( QPoint( 0, 0 ) ),
00196                               calSys->month( mDate ) - 1 ) ) == -1 ) {
00197     delete popup;
00198     return;  // canceled
00199   }
00200 
00201   emit monthSelected( month );
00202 
00203   delete popup;
00204 }
00205 
00206 void NavigatorBar::selectYearFromMenu()
00207 {
00208   const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00209 
00210   int year = calSys->year( mDate );
00211   int years = 11;  // odd number (show a few years ago -> a few years from now)
00212   int minYear = year - ( years / 3 );
00213 
00214   QPopupMenu *popup = new QPopupMenu( mYear );
00215 
00216   QString yearStr;
00217   int y = minYear;
00218   for ( int i=0; i < years; i++ ) {
00219     popup->insertItem( yearStr.setNum( y ), i );
00220     y++;
00221   }
00222   popup->setActiveItem( year - minYear );
00223 
00224   if ( ( year = popup->exec( mYear->mapToGlobal( QPoint( 0, 0 ) ),
00225                              year - minYear ) ) == -1 ) {
00226     delete popup;
00227     return;  // canceled
00228   }
00229 
00230   emit yearSelected( year + minYear );
00231 
00232   delete popup;
00233 }
00234 
00235 #include "navigatorbar.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys