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 #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
00064 QSpacerItem *frontSpacer = new QSpacerItem( 50, 1, QSizePolicy::Expanding );
00065 QSpacerItem *endSpacer = new QSpacerItem( 50, 1, QSizePolicy::Expanding );
00066
00067 bool isRTL = KOGlobals::self()->reverseLayout();
00068
00069 QPixmap pix;
00070
00071 pix = KOGlobals::self()->smallIcon( isRTL ? "2rightarrow" : "2leftarrow" );
00072 mPrevYear = new QPushButton( this );
00073 mPrevYear->setPixmap( pix );
00074 mPrevYear->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00075 QToolTip::add( mPrevYear, i18n( "Previous year" ) );
00076
00077 pix = KOGlobals::self()->smallIcon( isRTL ? "1rightarrow" : "1leftarrow");
00078 mPrevMonth = new QPushButton( this );
00079 mPrevMonth->setPixmap( pix );
00080 mPrevMonth->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00081 QToolTip::add( mPrevMonth, i18n( "Previous month" ) );
00082
00083
00084 pix = KOGlobals::self()->smallIcon( isRTL ? "1leftarrow" : "1rightarrow");
00085 mNextMonth = new QPushButton( this );
00086 mNextMonth->setPixmap( pix );
00087 mNextMonth->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00088 QToolTip::add( mNextMonth, i18n( "Next month" ) );
00089
00090 pix = KOGlobals::self()->smallIcon( isRTL ? "2leftarrow" : "2rightarrow");
00091 mNextYear = new QPushButton( this );
00092 mNextYear->setPixmap( pix );
00093 mNextYear->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00094 QToolTip::add( mNextYear, i18n( "Next year" ) );
00095
00096
00097 mMonth = new ActiveLabel( this );
00098 mMonth->setFont( tfont );
00099 mMonth->setAlignment( AlignCenter );
00100 mMonth->setMinimumHeight( mPrevYear->sizeHint().height() );
00101 QToolTip::add( mMonth, i18n( "Select a month" ) );
00102
00103
00104 mYear = new ActiveLabel( this );
00105 mYear->setFont( tfont );
00106 mYear->setAlignment( AlignCenter );
00107 mYear->setMinimumHeight( mPrevYear->sizeHint().height() );
00108 QToolTip::add( mYear, i18n( "Select a year" ) );
00109
00110
00111 QHBoxLayout *ctrlLayout = new QHBoxLayout( 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, SIGNAL( clicked() ), SIGNAL( prevYearClicked() ) );
00122 connect( mPrevMonth, SIGNAL( clicked() ), SIGNAL( prevMonthClicked() ) );
00123 connect( mNextMonth, SIGNAL( clicked() ), SIGNAL( nextMonthClicked() ) );
00124 connect( mNextYear, SIGNAL( clicked() ), SIGNAL( nextYearClicked() ) );
00125 connect( mMonth, SIGNAL( clicked() ), SLOT( selectMonthFromMenu() ) );
00126 connect( mYear, SIGNAL( clicked() ), 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
00161 int i;
00162 int maxwidth = 0;
00163
00164 for( i = 1; i <= calSys->monthsInYear( mDate ); ++i ) {
00165 int w = QFontMetrics( mMonth->font() ).
00166 width( QString( "%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
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
00185 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00186
00187 int i, month, months = calSys->monthsInYear( mDate );
00188
00189 QPopupMenu *popup = new QPopupMenu( 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( QPoint( 0, 0 ) ),
00198 calSys->month( mDate ) - 1 ) ) == -1 ) {
00199 delete popup;
00200 return;
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;
00214 int minYear = year - ( years / 3 );
00215
00216 QPopupMenu *popup = new QPopupMenu( mYear );
00217
00218 QString 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( QPoint( 0, 0 ) ),
00227 year - minYear ) ) == -1 ) {
00228 delete popup;
00229 return;
00230 }
00231
00232 emit yearSelected( year + minYear );
00233
00234 delete popup;
00235 }
00236
00237 #include "navigatorbar.moc"