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   mPrevYear = new QPushButton( this );
00068   pix = KOGlobals::self()->smallIcon( isRTL ? "2rightarrow" : "2leftarrow" );
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   // set up control frame layout
00100   QBoxLayout *ctrlLayout = new QHBoxLayout( this, 0, 4 );
00101   ctrlLayout->addWidget( mPrevYear, 3 );
00102   ctrlLayout->addWidget( mPrevMonth, 3 );
00103   ctrlLayout->addWidget( mMonth, 3 );
00104   ctrlLayout->addWidget( mNextMonth, 3 );
00105   ctrlLayout->addWidget( mNextYear, 3 );
00106 
00107   connect( mPrevYear, SIGNAL( clicked() ), SIGNAL( goPrevYear() ) );
00108   connect( mPrevMonth, SIGNAL( clicked() ), SIGNAL( goPrevMonth() ) );
00109   connect( mNextMonth, SIGNAL( clicked() ), SIGNAL( goNextMonth() ) );
00110   connect( mNextYear, SIGNAL( clicked() ), SIGNAL( goNextYear() ) );
00111   connect( mMonth, SIGNAL( clicked() ), SLOT( selectMonth() ) );
00112 }
00113 
00114 NavigatorBar::~NavigatorBar()
00115 {
00116 }
00117 
00118 void NavigatorBar::showButtons( bool left, bool right )
00119 {
00120   if ( left ) {
00121     mPrevYear->show();
00122     mPrevMonth->show();
00123   } else {
00124     mPrevYear->hide();
00125     mPrevMonth->hide();
00126   }
00127 
00128   if ( right ) {
00129     mNextYear->show();
00130     mNextMonth->show();
00131   } else {
00132     mNextYear->hide();
00133     mNextMonth->hide();
00134   }
00135 
00136 }
00137 
00138 void NavigatorBar::selectDates( const KCal::DateList &dateList )
00139 {
00140   if ( dateList.count() > 0 ) {
00141     mDate = dateList.first();
00142 
00143     const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00144 
00145     if ( !mHasMinWidth ) {
00146       // Set minimum width to width of widest month name label
00147       int i;
00148       int maxwidth = 0;
00149 
00150       for( i = 1; i <= calSys->monthsInYear( mDate ); ++i ) {
00151         int w = QFontMetrics( mMonth->font() ).width( QString("%1 8888")
00152             .arg( calSys->monthName( i, calSys->year( mDate ) ) ) );
00153         if ( w > maxwidth ) maxwidth = w;
00154       }
00155       mMonth->setMinimumWidth( maxwidth );
00156 
00157       mHasMinWidth = true;
00158     }
00159 
00160     // compute the label at the top of the navigator
00161     mMonth->setText( i18n( "monthname year", "%1 %2" )
00162                      .arg( calSys->monthName( mDate ) )
00163                      .arg( calSys->year( mDate ) ) );
00164   }
00165 }
00166 
00167 void NavigatorBar::selectMonth()
00168 {
00169   // every year can have different month names (in some calendar systems)
00170   const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00171 
00172   int i, month, months = calSys->monthsInYear( mDate );
00173 
00174   QPopupMenu *popup = new QPopupMenu( mMonth );
00175 
00176   for ( i = 1; i <= months; i++ )
00177     popup->insertItem( calSys->monthName( i, calSys->year( mDate ) ), i );
00178 
00179   popup->setActiveItem( calSys->month( mDate ) - 1 );
00180   popup->setMinimumWidth( mMonth->width() );
00181 
00182   if ( ( month = popup->exec( mMonth->mapToGlobal( QPoint( 0, 0 ) ),
00183                               calSys->month( mDate ) - 1 ) ) == -1 ) {
00184     delete popup;
00185     return;  // canceled
00186   }
00187 
00188   emit goMonth( month );
00189 
00190   delete popup;
00191 }
00192 
00193 #include "navigatorbar.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys