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( goPrevYear() ) );
00120   connect( mPrevMonth, SIGNAL( clicked() ), SIGNAL( goPrevMonth() ) );
00121   connect( mNextMonth, SIGNAL( clicked() ), SIGNAL( goNextMonth() ) );
00122   connect( mNextYear, SIGNAL( clicked() ), SIGNAL( goNextYear() ) );
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 ) maxwidth = w;
00167     }
00168     mMonth->setMinimumWidth( maxwidth );
00169 
00170     mHasMinWidth = true;
00171 
00172     // set the label text at the top of the navigator
00173     mMonth->setText( i18n( "monthname", "%1" ).arg( calSys->monthName( mDate ) ) );
00174     mYear->setText( i18n( "4 digit year", "%1" ).arg( calSys->yearString( mDate, false ) ) );
00175   }
00176 }
00177 
00178 void NavigatorBar::selectMonthFromMenu()
00179 {
00180   // every year can have different month names (in some calendar systems)
00181   const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00182 
00183   int i, month, months = calSys->monthsInYear( mDate );
00184 
00185   QPopupMenu *popup = new QPopupMenu( mMonth );
00186 
00187   for ( i = 1; i <= months; i++ )
00188     popup->insertItem( calSys->monthName( i, calSys->year( mDate ) ), i );
00189 
00190   popup->setActiveItem( calSys->month( mDate ) - 1 );
00191   popup->setMinimumWidth( mMonth->width() );
00192 
00193   if ( ( month = popup->exec( mMonth->mapToGlobal( QPoint( 0, 0 ) ),
00194                               calSys->month( mDate ) - 1 ) ) == -1 ) {
00195     delete popup;
00196     return;  // canceled
00197   }
00198 
00199   emit goMonth( month );
00200 
00201   delete popup;
00202 }
00203 
00204 void NavigatorBar::selectYearFromMenu()
00205 {
00206   const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00207 
00208   int year = calSys->year( mDate );
00209   int years = 11;  // odd number (show a few years ago -> a few years from now)
00210   int minYear = year - ( years / 3 );
00211 
00212   QPopupMenu *popup = new QPopupMenu( mYear );
00213 
00214   QString yearStr;
00215   int y = minYear;
00216   for ( int i=0; i < years; i++ ) {
00217     popup->insertItem( yearStr.setNum( y ), i );
00218     y++;
00219   }
00220   popup->setActiveItem( year - minYear );
00221 
00222   if ( ( year = popup->exec( mYear->mapToGlobal( QPoint( 0, 0 ) ),
00223                              year - minYear ) ) == -1 ) {
00224     delete popup;
00225     return;  // canceled
00226   }
00227 
00228   emit goYear( year + minYear );
00229 
00230   delete popup;
00231 }
00232 
00233 #include "navigatorbar.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys