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 TQt, and distribute the resulting executable,
00022     without including the source code for TQt in the source distribution.
00023 */
00024 
00025 #include <tqstring.h>
00026 #include <tqtooltip.h>
00027 #include <tqpushbutton.h>
00028 #include <tqlayout.h>
00029 #include <tqframe.h>
00030 #include <tqpopupmenu.h>
00031 #include <tqlabel.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( TQWidget *parent, const char *name )
00046   : TQLabel( parent, name )
00047 {
00048 }
00049 
00050 void ActiveLabel::mouseReleaseEvent( TQMouseEvent * )
00051 {
00052   emit clicked();
00053 }
00054 
00055 
00056 NavigatorBar::NavigatorBar( TQWidget *parent, const char *name )
00057   : TQWidget( parent, name ), mHasMinWidth( false )
00058 {
00059   TQFont tfont = font();
00060   tfont.setPointSize( 10 );
00061   tfont.setBold( false );
00062 
00063   // Create a horizontal spacers
00064   TQSpacerItem *frontSpacer = new TQSpacerItem( 50, 1, TQSizePolicy::Expanding );
00065   TQSpacerItem *endSpacer = new TQSpacerItem( 50, 1, TQSizePolicy::Expanding );
00066 
00067   bool isRTL = KOGlobals::self()->reverseLayout();
00068 
00069   TQPixmap pix;
00070   // Create backward navigation buttons
00071   pix = KOGlobals::self()->smallIcon( isRTL ? "2rightarrow" : "2leftarrow" );
00072   mPrevYear = new TQPushButton( this );
00073   mPrevYear->setPixmap( pix );
00074   mPrevYear->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
00075   TQToolTip::add( mPrevYear, i18n( "Previous year" ) );
00076 
00077   pix = KOGlobals::self()->smallIcon( isRTL ? "1rightarrow" : "1leftarrow");
00078   mPrevMonth = new TQPushButton( this );
00079   mPrevMonth->setPixmap( pix );
00080   mPrevMonth->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
00081   TQToolTip::add( mPrevMonth, i18n( "Previous month" ) );
00082 
00083   // Create forward navigation buttons
00084   pix = KOGlobals::self()->smallIcon( isRTL ? "1leftarrow" : "1rightarrow");
00085   mNextMonth = new TQPushButton( this );
00086   mNextMonth->setPixmap( pix );
00087   mNextMonth->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
00088   TQToolTip::add( mNextMonth, i18n( "Next month" ) );
00089 
00090   pix = KOGlobals::self()->smallIcon( isRTL ? "2leftarrow" : "2rightarrow");
00091   mNextYear = new TQPushButton( this );
00092   mNextYear->setPixmap( pix );
00093   mNextYear->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
00094   TQToolTip::add( mNextYear, i18n( "Next year" ) );
00095 
00096   // Create month name button
00097   mMonth = new ActiveLabel( this );
00098   mMonth->setFont( tfont );
00099   mMonth->setAlignment( AlignCenter );
00100   mMonth->setMinimumHeight( mPrevYear->sizeHint().height() );
00101   TQToolTip::add( mMonth, i18n( "Select a month" ) );
00102 
00103   // Create year button
00104   mYear = new ActiveLabel( this );
00105   mYear->setFont( tfont );
00106   mYear->setAlignment( AlignCenter );
00107   mYear->setMinimumHeight( mPrevYear->sizeHint().height() );
00108   TQToolTip::add( mYear, i18n( "Select a year" ) );
00109 
00110   // set up control frame layout
00111   TQHBoxLayout *ctrlLayout = new TQHBoxLayout( this );
00112   ctrlLayout->addWidget( mPrevYear );
00113   ctrlLayout->addWidget( mPrevMonth );
00114   ctrlLayout->addItem( frontSpacer );
00115   ctrlLayout->addWidget( mMonth );
00116   ctrlLayout->addWidget( mYear );
00117   ctrlLayout->addItem( endSpacer );
00118   ctrlLayout->addWidget( mNextMonth );
00119   ctrlLayout->addWidget( mNextYear );
00120 
00121   connect( mPrevYear, TQT_SIGNAL( clicked() ), TQT_SIGNAL( prevYearClicked() ) );
00122   connect( mPrevMonth, TQT_SIGNAL( clicked() ), TQT_SIGNAL( prevMonthClicked() ) );
00123   connect( mNextMonth, TQT_SIGNAL( clicked() ), TQT_SIGNAL( nextMonthClicked() ) );
00124   connect( mNextYear, TQT_SIGNAL( clicked() ), TQT_SIGNAL( nextYearClicked() ) );
00125   connect( mMonth, TQT_SIGNAL( clicked() ), TQT_SLOT( selectMonthFromMenu() ) );
00126   connect( mYear, TQT_SIGNAL( clicked() ), TQT_SLOT( selectYearFromMenu() ) );
00127 }
00128 
00129 NavigatorBar::~NavigatorBar()
00130 {
00131 }
00132 
00133 void NavigatorBar::showButtons( bool left, bool right )
00134 {
00135   if ( left ) {
00136     mPrevYear->show();
00137     mPrevMonth->show();
00138   } else {
00139     mPrevYear->hide();
00140     mPrevMonth->hide();
00141   }
00142 
00143   if ( right ) {
00144     mNextYear->show();
00145     mNextMonth->show();
00146   } else {
00147     mNextYear->hide();
00148     mNextMonth->hide();
00149   }
00150 
00151 }
00152 
00153 void NavigatorBar::selectDates( const KCal::DateList &dateList )
00154 {
00155   if ( dateList.count() > 0 ) {
00156     mDate = dateList.first();
00157 
00158     const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00159 
00160     // Set minimum width to width of widest month name label
00161     int i;
00162     int maxwidth = 0;
00163 
00164     for( i = 1; i <= calSys->monthsInYear( mDate ); ++i ) {
00165       int w = TQFontMetrics( mMonth->font() ).
00166               width( TQString( "%1" ).
00167                      arg( calSys->monthName( i, calSys->year( mDate ) ) ) );
00168       if ( w > maxwidth ) {
00169         maxwidth = w;
00170       }
00171     }
00172     mMonth->setMinimumWidth( maxwidth );
00173 
00174     mHasMinWidth = true;
00175 
00176     // set the label text at the top of the navigator
00177     mMonth->setText( i18n( "monthname", "%1" ).arg( calSys->monthName( mDate ) ) );
00178     mYear->setText( i18n( "4 digit year", "%1" ).arg( calSys->yearString( mDate, false ) ) );
00179   }
00180 }
00181 
00182 void NavigatorBar::selectMonthFromMenu()
00183 {
00184   // every year can have different month names (in some calendar systems)
00185   const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00186 
00187   int i, month, months = calSys->monthsInYear( mDate );
00188 
00189   TQPopupMenu *popup = new TQPopupMenu( mMonth );
00190 
00191   for ( i = 1; i <= months; i++ )
00192     popup->insertItem( calSys->monthName( i, calSys->year( mDate ) ), i );
00193 
00194   popup->setActiveItem( calSys->month( mDate ) - 1 );
00195   popup->setMinimumWidth( mMonth->width() );
00196 
00197   if ( ( month = popup->exec( mMonth->mapToGlobal( TQPoint( 0, 0 ) ),
00198                               calSys->month( mDate ) - 1 ) ) == -1 ) {
00199     delete popup;
00200     return;  // canceled
00201   }
00202 
00203   emit monthSelected( month );
00204 
00205   delete popup;
00206 }
00207 
00208 void NavigatorBar::selectYearFromMenu()
00209 {
00210   const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00211 
00212   int year = calSys->year( mDate );
00213   int years = 11;  // odd number (show a few years ago -> a few years from now)
00214   int minYear = year - ( years / 3 );
00215 
00216   TQPopupMenu *popup = new TQPopupMenu( mYear );
00217 
00218   TQString yearStr;
00219   int y = minYear;
00220   for ( int i=0; i < years; i++ ) {
00221     popup->insertItem( yearStr.setNum( y ), i );
00222     y++;
00223   }
00224   popup->setActiveItem( year - minYear );
00225 
00226   if ( ( year = popup->exec( mYear->mapToGlobal( TQPoint( 0, 0 ) ),
00227                              year - minYear ) ) == -1 ) {
00228     delete popup;
00229     return;  // canceled
00230   }
00231 
00232   emit yearSelected( year + minYear );
00233 
00234   delete popup;
00235 }
00236 
00237 #include "navigatorbar.moc"